Skip to content

Commit 41dd042

Browse files
committed
changes SimpleTimer functions to reflect shorthand code
1 parent d02b846 commit 41dd042

File tree

2 files changed

+45
-93
lines changed

2 files changed

+45
-93
lines changed

ESP8266-Power-Monitor.ino

+44-93
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#define BLYNK_PRINT Serial
22
#define BLYNK_MAX_READBYTES 512
33
/****************************************************************************/
4-
4+
#include "settings.h"
55
#include <ESP8266WiFi.h>
66
#include <BlynkSimpleEsp8266.h>
77
#include <Wire.h>
88
#include <Adafruit_INA219.h>
99
#include <SimpleTimer.h>
10-
#include "wifi_credentials.h" // see ReadMe
11-
#include "settings.h"
1210
#ifdef OTA_UPDATES
1311
#include <ArduinoOTA.h>
1412
#endif
@@ -50,23 +48,17 @@ void getINA219values() {
5048
// gather voltage averages
5149
loadvoltage_AVG[loadvoltage_AVG_cycle] = loadvoltage;
5250
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;
5652

5753
// gather current averages
5854
current_AVG[current_AVG_cycle] = current_mA;
5955
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;
6357

6458
// gather power averages
6559
power_AVG[power_AVG_cycle] = power;
6660
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;
7062
}
7163

7264
// this function is for updaing the REAL TIME values and is on a timer
@@ -90,16 +82,12 @@ void sendINA219valuesREAL() {
9082
// this function is for updaing the AVERGE values and is on a timer
9183
void sendINA219valuesAVG() {
9284
// 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];
9686
loadvoltage_AVG_total = loadvoltage_AVG_total / AVG_DEPTH_VOLTAGE;
9787
Blynk.virtualWrite(vPIN_VOLTAGE_AVG, String(loadvoltage_AVG_total, 3) + String(" V"));
9888

9989
// 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];
10391
current_AVG_total = current_AVG_total / AVG_DEPTH_CURRENT;
10492
if (current_AVG_total > 1000 && autoRange == 1) {
10593
Blynk.virtualWrite(vPIN_CURRENT_AVG, String((current_AVG_total / 1000), 2) + String(" A"));
@@ -108,9 +96,7 @@ void sendINA219valuesAVG() {
10896
}
10997

11098
// 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];
114100
power_AVG_total = power_AVG_total / AVG_DEPTH_POWER;
115101
if (power_AVG_total > 1000 && autoRange == 1) {
116102
Blynk.virtualWrite(vPIN_POWER_AVG, String((power_AVG_total / 1000), 2) + String(" W"));
@@ -165,11 +151,6 @@ void sendINA219valuesENERGY() {
165151
}
166152
}
167153

168-
// this is feeding raw data to the graph
169-
void sendINA219_GraphValues() {
170-
Blynk.virtualWrite(vPIN_GRAPH, current_mA);
171-
}
172-
173154
// HOLD BUTTON
174155
BLYNK_WRITE(vPIN_BUTTON_HOLD) {
175156
if (param.asInt()) {
@@ -216,30 +197,23 @@ BLYNK_WRITE(vPIN_BUTTON_RESET_AVG) {
216197
Blynk.virtualWrite(vPIN_VOLTAGE_AVG, "--- V");
217198
Blynk.virtualWrite(vPIN_CURRENT_AVG, "--- mA");
218199
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;
228203
delay(50);
229204
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+
});
231212
} else {
232213
timer.disable(countdownResetCon);
233214
}
234215

235216
}
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-
}
243217

244218
// RESET PEAKS (short) & RESET CONSUMTION (long)
245219
BLYNK_WRITE(vPIN_BUTTON_RESET_MAX) {
@@ -252,75 +226,40 @@ BLYNK_WRITE(vPIN_BUTTON_RESET_MAX) {
252226
powerMax = power;
253227
delay(50);
254228
updateINA219eXtraValues();
255-
countdownResetClock = timer.setTimeout(1000, countdownResetClockCallback);
229+
countdownResetClock = timer.setTimeout(1000, []() {
230+
Blynk.virtualWrite(vPIN_ENERGY_TIME, "--:--:--:--");
231+
stopwatch = 0;
232+
});
256233
} else {
257234
timer.disable(countdownResetClock);
258235
}
259236
}
260-
void countdownResetClockCallback() {
261-
Blynk.virtualWrite(vPIN_ENERGY_TIME, "--:--:--:--");
262-
stopwatch = 0;
263-
}
264237

265238
// the stopwatch counter which is run on a timer
266239
void stopwatchCounter() {
267240
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 = ":";
275243
secs = stopwatch; //convect milliseconds to seconds
276244
mins = secs / 60; //convert seconds to minutes
277245
hours = mins / 60; //convert minutes to hours
278246
days = hours / 24; //convert hours to days
279247
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
280248
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
281249
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";
291253
Blynk.virtualWrite(vPIN_ENERGY_TIME, days + hours_o + hours + mins_o + mins + secs_o + secs);
292254
}
293255

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-
}
303256
#ifdef FIXED_ENERGY_PRICE
304257
BLYNK_WRITE(vPIN_ENERGY_API) {
305258
energyPrice = param.asFloat();
306259
Blynk.virtualWrite(vPIN_ENERGY_PRICE, String(energyPrice, 4) + String('c') );
307260
}
308261
#endif
309262

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-
}
324263
/****************************************************************************/
325264
void setup() {
326265
Serial.begin(115200);
@@ -342,15 +281,25 @@ void setup() {
342281

343282
// TIMERS
344283
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+
});
346287
stopwatchTimer = timer.setInterval(1000, stopwatchCounter);
347288

348289
// setup split-task timers so we dont overload ESP
349290
// 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+
});
354303

355304
// start in auto-range mode & sync widget to hardware state
356305
autoRange = 1;
@@ -364,7 +313,9 @@ void setup() {
364313
#else
365314
// No fixed price set, so pull from local API
366315
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+
});
368319
#endif
369320
}
370321
/****************************************************************************/

settings.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Settings
44
55
**************************************************************/
6+
#include "wifi_credentials.h" // see ReadMe
67
/*
78
Blynk Auth Code
89
*/

0 commit comments

Comments
 (0)