1
1
#define BLYNK_PRINT Serial
2
2
#define BLYNK_MAX_READBYTES 512
3
3
/* ***************************************************************************/
4
-
4
+ # include " settings.h "
5
5
#include < ESP8266WiFi.h>
6
6
#include < BlynkSimpleEsp8266.h>
7
7
#include < Wire.h>
8
8
#include < Adafruit_INA219.h>
9
9
#include < SimpleTimer.h>
10
- #include " wifi_credentials.h" // see ReadMe
11
- #include " settings.h"
12
10
#ifdef OTA_UPDATES
13
11
#include < ArduinoOTA.h>
14
12
#endif
@@ -50,23 +48,17 @@ void getINA219values() {
50
48
// gather voltage averages
51
49
loadvoltage_AVG[loadvoltage_AVG_cycle] = loadvoltage;
52
50
loadvoltage_AVG_cycle++;
53
- if (loadvoltage_AVG_cycle == AVG_DEPTH_VOLTAGE) {
54
- loadvoltage_AVG_cycle = 0 ;
55
- }
51
+ if (loadvoltage_AVG_cycle == AVG_DEPTH_VOLTAGE) loadvoltage_AVG_cycle = 0 ;
56
52
57
53
// gather current averages
58
54
current_AVG[current_AVG_cycle] = current_mA;
59
55
current_AVG_cycle++;
60
- if (current_AVG_cycle == AVG_DEPTH_CURRENT) {
61
- current_AVG_cycle = 0 ;
62
- }
56
+ if (current_AVG_cycle == AVG_DEPTH_CURRENT) current_AVG_cycle = 0 ;
63
57
64
58
// gather power averages
65
59
power_AVG[power_AVG_cycle] = power;
66
60
power_AVG_cycle++;
67
- if (power_AVG_cycle == AVG_DEPTH_POWER) {
68
- power_AVG_cycle = 0 ;
69
- }
61
+ if (power_AVG_cycle == AVG_DEPTH_POWER) power_AVG_cycle = 0 ;
70
62
}
71
63
72
64
// this function is for updaing the REAL TIME values and is on a timer
@@ -90,16 +82,12 @@ void sendINA219valuesREAL() {
90
82
// this function is for updaing the AVERGE values and is on a timer
91
83
void sendINA219valuesAVG () {
92
84
// VOLTAGE
93
- for (int i = 0 ; i < (AVG_DEPTH_VOLTAGE - 1 ); i++) {
94
- loadvoltage_AVG_total += loadvoltage_AVG[i];
95
- }
85
+ for (int i = 0 ; i < (AVG_DEPTH_VOLTAGE - 1 ); i++) loadvoltage_AVG_total += loadvoltage_AVG[i];
96
86
loadvoltage_AVG_total = loadvoltage_AVG_total / AVG_DEPTH_VOLTAGE;
97
87
Blynk.virtualWrite (vPIN_VOLTAGE_AVG, String (loadvoltage_AVG_total, 3 ) + String (" V" ));
98
88
99
89
// CURRENT
100
- for (int i = 0 ; i < (AVG_DEPTH_CURRENT - 1 ); i++) {
101
- current_AVG_total += current_AVG[i];
102
- }
90
+ for (int i = 0 ; i < (AVG_DEPTH_CURRENT - 1 ); i++) current_AVG_total += current_AVG[i];
103
91
current_AVG_total = current_AVG_total / AVG_DEPTH_CURRENT;
104
92
if (current_AVG_total > 1000 && autoRange == 1 ) {
105
93
Blynk.virtualWrite (vPIN_CURRENT_AVG, String ((current_AVG_total / 1000 ), 2 ) + String (" A" ));
@@ -108,9 +96,7 @@ void sendINA219valuesAVG() {
108
96
}
109
97
110
98
// POWER
111
- for (int i = 0 ; i < (AVG_DEPTH_POWER - 1 ); i++) {
112
- power_AVG_total += power_AVG[i];
113
- }
99
+ for (int i = 0 ; i < (AVG_DEPTH_POWER - 1 ); i++) power_AVG_total += power_AVG[i];
114
100
power_AVG_total = power_AVG_total / AVG_DEPTH_POWER;
115
101
if (power_AVG_total > 1000 && autoRange == 1 ) {
116
102
Blynk.virtualWrite (vPIN_POWER_AVG, String ((power_AVG_total / 1000 ), 2 ) + String (" W" ));
@@ -165,11 +151,6 @@ void sendINA219valuesENERGY() {
165
151
}
166
152
}
167
153
168
- // this is feeding raw data to the graph
169
- void sendINA219_GraphValues () {
170
- Blynk.virtualWrite (vPIN_GRAPH, current_mA);
171
- }
172
-
173
154
// HOLD BUTTON
174
155
BLYNK_WRITE (vPIN_BUTTON_HOLD) {
175
156
if (param.asInt ()) {
@@ -216,30 +197,23 @@ BLYNK_WRITE(vPIN_BUTTON_RESET_AVG) {
216
197
Blynk.virtualWrite (vPIN_VOLTAGE_AVG, " --- V" );
217
198
Blynk.virtualWrite (vPIN_CURRENT_AVG, " --- mA" );
218
199
Blynk.virtualWrite (vPIN_POWER_AVG, " --- mW" );
219
- for (int i = 0 ; i < (AVG_DEPTH_VOLTAGE - 1 ); i++) {
220
- loadvoltage_AVG[i] = loadvoltage;
221
- }
222
- for (int i = 0 ; i < (AVG_DEPTH_CURRENT - 1 ); i++) {
223
- current_AVG[i] = current_mA;
224
- }
225
- for (int i = 0 ; i < (AVG_DEPTH_POWER - 1 ); i++) {
226
- power_AVG[i] = power;
227
- }
200
+ for (int i = 0 ; i < (AVG_DEPTH_VOLTAGE - 1 ); i++) loadvoltage_AVG[i] = loadvoltage;
201
+ for (int i = 0 ; i < (AVG_DEPTH_CURRENT - 1 ); i++) current_AVG[i] = current_mA;
202
+ for (int i = 0 ; i < (AVG_DEPTH_POWER - 1 ); i++) power_AVG[i] = power;
228
203
delay (50 );
229
204
updateINA219eXtraValues ();
230
- countdownResetCon = timer.setTimeout (1000 , countdownResetConCallback);
205
+ countdownResetCon = timer.setTimeout (1000 , []() {
206
+ Blynk.virtualWrite (vPIN_ENERGY_USED, " 0.00000 mWh" );
207
+ Blynk.virtualWrite (vPIN_ENERGY_COST, " 0.000000" );
208
+ energy = 0 ;
209
+ energyCost = 0 ;
210
+ energyPrevious = 0 ;
211
+ });
231
212
} else {
232
213
timer.disable (countdownResetCon);
233
214
}
234
215
235
216
}
236
- void countdownResetConCallback () {
237
- Blynk.virtualWrite (vPIN_ENERGY_USED, " 0.00000 mWh" );
238
- Blynk.virtualWrite (vPIN_ENERGY_COST, " 0.00000000" );
239
- energy = 0 ;
240
- energyCost = 0 ;
241
- energyPrevious = 0 ;
242
- }
243
217
244
218
// RESET PEAKS (short) & RESET CONSUMTION (long)
245
219
BLYNK_WRITE (vPIN_BUTTON_RESET_MAX) {
@@ -252,75 +226,40 @@ BLYNK_WRITE(vPIN_BUTTON_RESET_MAX) {
252
226
powerMax = power;
253
227
delay (50 );
254
228
updateINA219eXtraValues ();
255
- countdownResetClock = timer.setTimeout (1000 , countdownResetClockCallback);
229
+ countdownResetClock = timer.setTimeout (1000 , []() {
230
+ Blynk.virtualWrite (vPIN_ENERGY_TIME, " --:--:--:--" );
231
+ stopwatch = 0 ;
232
+ });
256
233
} else {
257
234
timer.disable (countdownResetClock);
258
235
}
259
236
}
260
- void countdownResetClockCallback () {
261
- Blynk.virtualWrite (vPIN_ENERGY_TIME, " --:--:--:--" );
262
- stopwatch = 0 ;
263
- }
264
237
265
238
// the stopwatch counter which is run on a timer
266
239
void stopwatchCounter () {
267
240
stopwatch++;
268
- long days = 0 ;
269
- long hours = 0 ;
270
- long mins = 0 ;
271
- long secs = 0 ;
272
- String secs_o = " :" ;
273
- String mins_o = " :" ;
274
- String hours_o = " :" ;
241
+ long days = 0 , hours = 0 , mins = 0 , secs = 0 ;
242
+ String secs_o = " :" , mins_o = " :" , hours_o = " :" ;
275
243
secs = stopwatch; // convect milliseconds to seconds
276
244
mins = secs / 60 ; // convert seconds to minutes
277
245
hours = mins / 60 ; // convert minutes to hours
278
246
days = hours / 24 ; // convert hours to days
279
247
secs = secs - (mins * 60 ); // subtract the coverted seconds to minutes in order to display 59 secs max
280
248
mins = mins - (hours * 60 ); // subtract the coverted minutes to hours in order to display 59 minutes max
281
249
hours = hours - (days * 24 ); // subtract the coverted hours to days in order to display 23 hours max
282
- if (secs < 10 ) {
283
- secs_o = " :0" ;
284
- }
285
- if (mins < 10 ) {
286
- mins_o = " :0" ;
287
- }
288
- if (hours < 10 ) {
289
- hours_o = " :0" ;
290
- }
250
+ if (secs < 10 ) secs_o = " :0" ;
251
+ if (mins < 10 ) mins_o = " :0" ;
252
+ if (hours < 10 ) hours_o = " :0" ;
291
253
Blynk.virtualWrite (vPIN_ENERGY_TIME, days + hours_o + hours + mins_o + mins + secs_o + secs);
292
254
}
293
255
294
-
295
- // This section is for setting the kWh price from your electric company.
296
- // I use a SPOT rate which means I need to update it all the time.
297
- // If you know your set price per kWh (in cents), then enter the price in settings: FIXED_ENERGY_PRICE
298
- void getPrice () {
299
- #ifdef FIXED_ENERGY_PRICE
300
- Blynk.virtualWrite (vPIN_ENERGY_API, ENERGY_API); // local API Server to get current power price per mWh
301
- #endif
302
- }
303
256
#ifdef FIXED_ENERGY_PRICE
304
257
BLYNK_WRITE (vPIN_ENERGY_API) {
305
258
energyPrice = param.asFloat ();
306
259
Blynk.virtualWrite (vPIN_ENERGY_PRICE, String (energyPrice, 4 ) + String (' c' ) );
307
260
}
308
261
#endif
309
262
310
- // the functions for the split-task timers.
311
- // required to keep the little ESP8266 from disconnections
312
- void splitTask1 () {
313
- sendTimer1 = timer.setInterval (1000 , sendINA219valuesREAL);
314
- }
315
- void splitTask2 () {
316
- sendTimer2 = timer.setInterval (1000 , sendINA219valuesAVG);
317
- }
318
- void splitTask3 () {
319
- sendTimer3 = timer.setInterval (1000 , sendINA219valuesMAX);
320
- }
321
- void splitTask4 () {
322
- sendTimer4 = timer.setInterval (2000 , sendINA219valuesENERGY);
323
- }
324
263
/* ***************************************************************************/
325
264
void setup () {
326
265
Serial.begin (115200 );
@@ -342,15 +281,25 @@ void setup() {
342
281
343
282
// TIMERS
344
283
pollingTimer = timer.setInterval (1000 , getINA219values);
345
- graphTimer = timer.setInterval (2000 , sendINA219_GraphValues);
284
+ graphTimer = timer.setInterval (2000 , []() {
285
+ Blynk.virtualWrite (vPIN_GRAPH, current_mA);
286
+ });
346
287
stopwatchTimer = timer.setInterval (1000 , stopwatchCounter);
347
288
348
289
// setup split-task timers so we dont overload ESP
349
290
// with too many virtualWrites per second
350
- splitTimer1 = timer.setTimeout (200 , splitTask1);
351
- splitTimer2 = timer.setTimeout (400 , splitTask2);
352
- splitTimer3 = timer.setTimeout (600 , splitTask3);
353
- splitTimer4 = timer.setTimeout (800 , splitTask4);
291
+ splitTimer1 = timer.setTimeout (200 , []() {
292
+ sendTimer1 = timer.setInterval (1000 , sendINA219valuesREAL);
293
+ });
294
+ splitTimer2 = timer.setTimeout (400 , []() {
295
+ sendTimer2 = timer.setInterval (1000 , sendINA219valuesAVG);
296
+ });
297
+ splitTimer3 = timer.setTimeout (600 , []() {
298
+ sendTimer3 = timer.setInterval (1000 , sendINA219valuesMAX);
299
+ });
300
+ splitTimer4 = timer.setTimeout (800 , []() {
301
+ sendTimer4 = timer.setInterval (2000 , sendINA219valuesENERGY);
302
+ });
354
303
355
304
// start in auto-range mode & sync widget to hardware state
356
305
autoRange = 1 ;
@@ -364,7 +313,9 @@ void setup() {
364
313
#else
365
314
// No fixed price set, so pull from local API
366
315
Blynk.virtualWrite (vPIN_ENERGY_API, ENERGY_API);
367
- priceTimer = timer.setInterval (20000 , getPrice); // start a 20sec timer for updates
316
+ priceTimer = timer.setInterval (20000 , []() {
317
+ Blynk.virtualWrite (vPIN_ENERGY_API, ENERGY_API);
318
+ });
368
319
#endif
369
320
}
370
321
/* ***************************************************************************/
0 commit comments