-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (65 loc) · 1.57 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Code as of May 2024
//boating with the baileys
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include <SPI.h>
#include "sensesp/signalk/signalk_output.h"
#include "sensesp_app_builder.h"
#define SDA 21
#define SCL 22
using namespace sensesp;
reactesp::ReactESP app;
Adafruit_ADS1115 ads;
unsigned long delayTime = 1000;
SKOutput<float>* ac_amp_output;
const float FACTOR = 20; //20A/1V from teh CT
const float multiplier = 0.00005;
float getcurrent()
{
float voltage;
float current;
float sum = 0;
long time_check = millis();
int counter = 0;
while (millis() - time_check < 1000)
{
voltage = ads.readADC_Differential_0_1() * multiplier;
current = voltage * FACTOR;
//current /= 1000.0;
sum += sq(current);
counter = counter + 1;
}
current = sqrt(sum / counter);
return (current);
}
void printValues()
{
float currentRMS = getcurrent();
ac_amp_output->set_input(currentRMS);
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println(F("AC Monitor test"));
SensESPAppBuilder builder;
sensesp_app = (&builder)
->set_hostname("sensesp-AC_monitor")
->get_app();
Wire.begin(SDA, SCL);
ads.setGain(GAIN_FOUR); // +/- 1.024V 1bit = 0.5mV
ads.begin();
ac_amp_output = new SKOutput<float>(
"ac.current.amps",
"/sensors/ADS1151/ac",
new SKMetadata("A", "AC Amps")
);
sensesp_app->start();
}
void loop() {
static unsigned long last_run = millis();
if (millis() - last_run >= delayTime) {
printValues();
last_run = millis();
}
app.tick();
}