-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPower_Monitor.ino
282 lines (215 loc) · 7.31 KB
/
Power_Monitor.ino
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* This sketch reports on the power usage on the mains and solar lines
*
* By Ben Jackson
*/
#include <WiFi.h>
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "nvs.h"
#include "nvs_flash.h"
/* --------------------------------------------------------------------
* CONSTANTS
* -------------------------------------------------------------------- */
#define ADC_BITS 12
#define ADC_COUNTS (1<<ADC_BITS)
const float ICAL = 90.9; // calibration factor
const int ledPin = 5; // onboard LED for ESP32 Thing
const int mainsPin = 36; // ADC for mains power CT
const int solarPin = 37; // ADC for solar power CT
const static char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
//#define DEBUG
/* --------------------------------------------------------------------
* GLOBALS
* -------------------------------------------------------------------- */
char* ssid;
char* password;
char* sIp;
char* sNetmask;
char* sGateway;
double filteredI = 0;
double last_filtered_value;
double offsetI, sqI, sumI, Irms;
int sample = 0;
struct netconn *conn, *newconn;
err_t err;
//const static char http_index_html[] = "<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page, served by httpserver-netconn.</body></html>";
/* --------------------------------------------------------------------
* FUNCTIONS
* -------------------------------------------------------------------- */
char* readConfigValue(nvs_handle handle, char* key) {
size_t required_size;
nvs_get_str(handle, key, NULL, &required_size); //find the size of the value
char* valueOut = (char*)malloc(required_size); //set the size of the value
nvs_get_str(handle, key, valueOut, &required_size); //get the value
return valueOut;
}
double calcIrms(unsigned int Number_of_Samples, int pin)
{
for (unsigned int n = 0; n < Number_of_Samples; n++)
{
sample = analogRead(pin);
// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centered on 0 counts.
offsetI = (offsetI + (sample-offsetI)/1024);
filteredI = sample - offsetI;
// Root-mean-square method current
// 1) square current values
sqI = filteredI * filteredI;
// 2) sum
sumI += sqI;
#ifdef DEBUG //debug generates the table of output so you can plot a graph
Serial.print(n);
Serial.print("\t");
Serial.print(sample);
Serial.print("\t");
Serial.print(filteredI);
Serial.print("\t");
Serial.println(offsetI);
#endif
}
double I_RATIO = ICAL *((3300/1000.0) / (ADC_COUNTS));
Irms = I_RATIO * sqrt(sumI / Number_of_Samples);
#ifdef DEBUG // summary var output
Serial.println();
Serial.print("I_RATIO ");
Serial.println(I_RATIO);
Serial.print("Irms: ");
Serial.println(Irms);
Serial.print("Power: ");
Serial.println(Irms*240);
#endif
sumI = 0;
return Irms;
}
/*
* Once a connection is found, this reads the connection headers and responds.
*/
static void http_server_netconn_serve(struct netconn *conn)
{
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
digitalWrite(ledPin, HIGH);
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK) {
netbuf_data(inbuf, (void**)&buf, &buflen);
/* Is this an HTTP GET command? (only check the first 5 chars, since
there are other formats for GET, and we're keeping it very simple )*/
if (buflen>=6 &&
buf[0]=='G' &&
buf[1]=='E' &&
buf[2]=='T' &&
buf[3]==' ' &&
buf[4]=='/' &&
buf[5]==' ' ) {
#ifdef DEBUG
Serial.println(" Connection received");
#endif
/* Send the HTML header
* subtract 1 from the size, since we dont send the \0 in the string
* NETCONN_NOCOPY: our data is const static, so no need to copy it
*/
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
/* -------------------------------------------
* This is where the power usage is calculated
------------------------------------------- */
// calculate energy
double Irms = calcIrms(1480, mainsPin); // Calculate current
double dPower = Irms * 240;
// String sHTML = String("<html><head><title>openEnergyMonitor</title></head><body>");
String sHTML = "";
String sPower = String(dPower, 2);
sHTML += sPower;
sHTML += "\tMains Power\r\n";
// sHTML += "</body></html>";
//convert to char array
int ilen = sHTML.length() + 1;
char cHTML[ilen];
sHTML.toCharArray(cHTML, ilen);
Serial.println(cHTML);
/* Send our HTML page */
netconn_write(conn, cHTML, sizeof(cHTML)-1, NETCONN_NOCOPY);
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
digitalWrite(ledPin, LOW);
}
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
offsetI = ADC_COUNTS>>1;
// Read config from NVS
nvs_handle nvs;
err = nvs_flash_init();
if (err != 0) {
Serial.print("Error initalising NVS. ERROR CODE: ");
Serial.println(err, HEX);
}
err = nvs_open("homeSys", NVS_READWRITE, &nvs);
if (err != 0) {
Serial.print("Error opening NVS. ERROR CODE: ");
Serial.println(err, HEX);
}
ssid = readConfigValue(nvs, "wifiSsid");
password = readConfigValue(nvs, "wifiPwd");
sIp = readConfigValue(nvs, "ip");
sNetmask = readConfigValue(nvs, "netmask");
sGateway = readConfigValue(nvs, "gateway");
// Connect to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
//IPAddress local = IPAddress.fromString("192.168.0.152");
IPAddress local, gateway, subnet;
local.fromString(sIp);
gateway.fromString(sGateway);
subnet.fromString(sNetmask);
if (WiFi.config(local, gateway, subnet) == false) {
Serial.println("Error assigning IP Address");
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
delay(5000);
Serial.println("----Ready----");
// Setup the network for the HTTP server
/* Create a new TCP connection handle */
conn = netconn_new(NETCONN_TCP);
/* Bind to port 80 (HTTP) with default IP address */
netconn_bind(conn, NULL, 80);
/* Put the connection into LISTEN state */
netconn_listen(conn);
digitalWrite(ledPin, LOW);
}
void loop()
{
do {
digitalWrite(ledPin, LOW);
err = netconn_accept(conn, &newconn);
if (err == ERR_OK) {
http_server_netconn_serve(newconn);
netconn_delete(newconn);
}
} while(err == ERR_OK);
}