Skip to content

Commit 758edf5

Browse files
committed
changed average data from global vars to array
changed the average data system to arrays and added control over the samples taken in the settings.
1 parent 655861f commit 758edf5

File tree

2 files changed

+61
-91
lines changed

2 files changed

+61
-91
lines changed

ESP8266-Power-Monitor.ino

Lines changed: 53 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ SimpleTimer timer;
1313
Adafruit_INA219 ina219;
1414
float shuntvoltage = 0.00;
1515
float busvoltage = 0.00;
16-
float current_mA = 0.00, current_mA_Max, current_mA_Avg;
17-
float loadvoltage = 0.00, loadvoltageMax, loadvoltageAvg;
16+
float current_mA = 0.00, current_mA_Max;
17+
float loadvoltage = 0.00, loadvoltageMax;
1818
float energy = 0.00, energyPrice = 0.000, energyCost, energyPrevious, energyDifference;
1919
float power = 0.00, powerMax, powerAvg;
2020
int sendTimer, pollingTimer, priceTimer, graphTimer, autoRange, countdownResetCon, countdownResetClock, counter2, secret, stopwatchTimer;
2121
long stopwatch;
2222
int splitTimer1, splitTimer2, splitTimer3, splitTimer4, splitTimer5;
2323
int sendTimer1, sendTimer2, sendTimer3, sendTimer4, sendTimer5;
24-
float current_AVG, current_AVG_cycle, current_AVG_1, current_AVG_2, current_AVG_3, current_AVG_4, current_AVG_5;
25-
float loadvoltage_AVG, loadvoltage_AVG_cycle, loadvoltage_AVG_1, loadvoltage_AVG_2, loadvoltage_AVG_3, loadvoltage_AVG_4, loadvoltage_AVG_5;
26-
float power_AVG, power_AVG_cycle, power_AVG_1, power_AVG_2, power_AVG_3, power_AVG_4, power_AVG_5;
24+
int loadvoltage_AVG_cycle = 0, current_AVG_cycle = 0, power_AVG_cycle = 0;
25+
float loadvoltage_AVG[AVG_DEPTH_VOLTAGE + 1], current_AVG[AVG_DEPTH_CURRENT + 1], power_AVG[AVG_DEPTH_POWER + 1];
26+
float loadvoltage_AVG_total, current_AVG_total, power_AVG_total;
2727
/****************************************************************************/
2828
void getINA219values() {
2929

@@ -43,76 +43,39 @@ void getINA219values() {
4343
energy = 0;
4444
}
4545

46-
// gather voltage average from 5 samples over 5 seconds
46+
// gather voltage averages
47+
loadvoltage_AVG[loadvoltage_AVG_cycle] = loadvoltage;
4748
loadvoltage_AVG_cycle++;
48-
if (loadvoltage_AVG_cycle == 1) {
49-
loadvoltage_AVG_1 = loadvoltage;
50-
}
51-
if (loadvoltage_AVG_cycle == 2) {
52-
loadvoltage_AVG_2 = loadvoltage;
53-
}
54-
if (loadvoltage_AVG_cycle == 3) {
55-
loadvoltage_AVG_3 = loadvoltage;
56-
}
57-
if (loadvoltage_AVG_cycle == 4) {
58-
loadvoltage_AVG_4 = loadvoltage;
59-
}
60-
if (loadvoltage_AVG_cycle == 5) {
61-
loadvoltage_AVG_5 = loadvoltage;
49+
if (loadvoltage_AVG_cycle == AVG_DEPTH_VOLTAGE) {
6250
loadvoltage_AVG_cycle = 0;
6351
}
6452

65-
// gather current average from 5 samples over 5 seconds
53+
// gather current averages
54+
current_AVG[current_AVG_cycle] = current_mA;
6655
current_AVG_cycle++;
67-
if (current_AVG_cycle == 1) {
68-
current_AVG_1 = current_mA;
69-
}
70-
if (current_AVG_cycle == 2) {
71-
current_AVG_2 = current_mA;
72-
}
73-
if (current_AVG_cycle == 3) {
74-
current_AVG_3 = current_mA;
75-
}
76-
if (current_AVG_cycle == 4) {
77-
current_AVG_4 = current_mA;
78-
}
79-
if (current_AVG_cycle == 5) {
80-
current_AVG_5 = current_mA;
56+
if (current_AVG_cycle == AVG_DEPTH_CURRENT) {
8157
current_AVG_cycle = 0;
8258
}
8359

84-
// gather power average from 5 samples over 5 seconds
60+
// gather power averages
61+
power_AVG[power_AVG_cycle] = power;
8562
power_AVG_cycle++;
86-
if (power_AVG_cycle == 1) {
87-
power_AVG_1 = power;
88-
}
89-
if (power_AVG_cycle == 2) {
90-
power_AVG_2 = power;
91-
}
92-
if (power_AVG_cycle == 3) {
93-
power_AVG_3 = power;
94-
}
95-
if (power_AVG_cycle == 4) {
96-
power_AVG_4 = power;
97-
}
98-
if (power_AVG_cycle == 5) {
99-
power_AVG_5 = power;
63+
if (power_AVG_cycle == AVG_DEPTH_POWER) {
10064
power_AVG_cycle = 0;
10165
}
102-
10366
}
10467

10568
// this function is for updaing the REAL TIME values and is on a timer
10669
void sendINA219valuesREAL() {
107-
// LOAD VOLTAGE (REAL TIME)
70+
// VOLTAGE
10871
Blynk.virtualWrite(vPIN_VOLTAGE_REAL, String(loadvoltage, 4) + String(" V") );
109-
// LOAD POWER (REAL TIME)
72+
// POWER
11073
if (power > 1000 && autoRange == 1) {
11174
Blynk.virtualWrite(vPIN_POWER_REAL, String((power / 1000), 3) + String(" W") );
11275
} else {
11376
Blynk.virtualWrite(vPIN_POWER_REAL, String(power, 3) + String(" mW") );
11477
}
115-
// LOAD CURRENT (REAL TIME)
78+
// CURRENT (REAL TIME)
11679
if (current_mA > 1000 && autoRange == 1) {
11780
Blynk.virtualWrite(vPIN_CURRENT_REAL, String((current_mA / 1000), 3) + String(" A") );
11881
} else {
@@ -122,26 +85,34 @@ void sendINA219valuesREAL() {
12285

12386
// this function is for updaing the AVERGE values and is on a timer
12487
void sendINA219valuesAVG() {
125-
// LOAD VOLTAGE (AVERAGE)
126-
loadvoltage_AVG = (loadvoltage_AVG_1 + loadvoltage_AVG_2 + loadvoltage_AVG_3 + loadvoltage_AVG_4 + loadvoltage_AVG_5) / 5;
127-
Blynk.virtualWrite(vPIN_VOLTAGE_AVG, String(loadvoltage_AVG, 3) + String(" V"));
88+
// VOLTAGE
89+
for (int i = 0; i < (AVG_DEPTH_VOLTAGE - 1); i++) {
90+
loadvoltage_AVG_total += loadvoltage_AVG[i];
91+
}
92+
loadvoltage_AVG_total = loadvoltage_AVG_total / AVG_DEPTH_VOLTAGE;
93+
Blynk.virtualWrite(vPIN_VOLTAGE_AVG, String(loadvoltage_AVG_total, 3) + String(" V"));
12894

129-
// LOAD CURRENT (AVERAGE)
130-
current_AVG = (current_AVG_1 + current_AVG_2 + current_AVG_3 + current_AVG_4 + current_AVG_5) / 5;
131-
if (current_AVG > 1000 && autoRange == 1) {
132-
Blynk.virtualWrite(vPIN_CURRENT_AVG, String((current_AVG / 1000), 2) + String(" A"));
95+
// CURRENT
96+
for (int i = 0; i < (AVG_DEPTH_CURRENT - 1); i++) {
97+
current_AVG_total += current_AVG[i];
98+
}
99+
current_AVG_total = current_AVG_total / AVG_DEPTH_CURRENT;
100+
if (current_AVG_total > 1000 && autoRange == 1) {
101+
Blynk.virtualWrite(vPIN_CURRENT_AVG, String((current_AVG_total / 1000), 2) + String(" A"));
133102
} else {
134-
Blynk.virtualWrite(vPIN_CURRENT_AVG, String(current_AVG, 2) + String(" mA"));
103+
Blynk.virtualWrite(vPIN_CURRENT_AVG, String(current_AVG_total, 2) + String(" mA"));
135104
}
136105

137-
// LOAD POWER (AVERAGE)
138-
power_AVG = (power_AVG_1 + power_AVG_2 + power_AVG_3 + power_AVG_4 + power_AVG_5) / 5;
139-
if (power_AVG > 1000 && autoRange == 1) {
140-
Blynk.virtualWrite(vPIN_POWER_AVG, String((power_AVG / 1000), 2) + String(" W"));
106+
// POWER
107+
for (int i = 0; i < (AVG_DEPTH_POWER - 1); i++) {
108+
power_AVG_total += power_AVG[i];
109+
}
110+
power_AVG_total = power_AVG_total / AVG_DEPTH_POWER;
111+
if (power_AVG_total > 1000 && autoRange == 1) {
112+
Blynk.virtualWrite(vPIN_POWER_AVG, String((power_AVG_total / 1000), 2) + String(" W"));
141113
} else {
142-
Blynk.virtualWrite(vPIN_POWER_AVG, String(power_AVG, 2) + String(" mW"));
114+
Blynk.virtualWrite(vPIN_POWER_AVG, String(power_AVG_total, 2) + String(" mW"));
143115
}
144-
145116
}
146117

147118
// this function is for updaing the MAX values and is on a timer
@@ -183,7 +154,7 @@ void sendINA219valuesENERGY() {
183154
energyPrevious = energy;
184155
// ENERGY COST
185156
energyCost = energyCost + ((energyPrice / 1000 / 100) * energyDifference);
186-
if(energyCost > 9.999){
157+
if (energyCost > 9.999) {
187158
Blynk.virtualWrite(vPIN_ENERGY_COST, String((energyCost), 7));
188159
} else {
189160
Blynk.virtualWrite(vPIN_ENERGY_COST, String((energyCost), 8));
@@ -192,7 +163,7 @@ void sendINA219valuesENERGY() {
192163

193164
// this is feeding raw data to the graph
194165
void sendINA219_GraphValues() {
195-
Blynk.virtualWrite(vPIN_CURRENT_GRAPH, current_AVG);
166+
Blynk.virtualWrite(vPIN_CURRENT_GRAPH, current_AVG_total);
196167
}
197168

198169
// HOLD BUTTON
@@ -213,10 +184,10 @@ BLYNK_WRITE(vPIN_BUTTON_HOLD) {
213184
// this function only runs when in HOLD mode and select AUTO-RANGE
214185
void updateINA219eXtraValues() {
215186
Blynk.virtualWrite(vPIN_VOLTAGE_PEAK, String(loadvoltageMax, 3) + String(" V") );
216-
if (current_AVG > 1000 && autoRange == 1) {
217-
Blynk.virtualWrite(vPIN_CURRENT_PEAK, String((current_AVG / 1000), 2) + String(" A") );
187+
if (current_AVG_total > 1000 && autoRange == 1) {
188+
Blynk.virtualWrite(vPIN_CURRENT_PEAK, String((current_AVG_total / 1000), 2) + String(" A") );
218189
} else {
219-
Blynk.virtualWrite(vPIN_CURRENT_PEAK, String(current_AVG, 2) + String(" mA"));
190+
Blynk.virtualWrite(vPIN_CURRENT_PEAK, String(current_AVG_total, 2) + String(" mA"));
220191
}
221192
if (powerMax > 1000 && autoRange == 1) {
222193
Blynk.virtualWrite(vPIN_POWER_PEAK, String((powerMax / 1000), 2) + String(" W") );
@@ -241,24 +212,15 @@ BLYNK_WRITE(vPIN_BUTTON_RESET_AVG) {
241212
Blynk.virtualWrite(vPIN_VOLTAGE_AVG, "--- V");
242213
Blynk.virtualWrite(vPIN_CURRENT_AVG, "--- mA");
243214
Blynk.virtualWrite(vPIN_POWER_AVG, "--- mW");
244-
loadvoltage_AVG = loadvoltage;
245-
loadvoltage_AVG_1 = loadvoltage;
246-
loadvoltage_AVG_2 = loadvoltage;
247-
loadvoltage_AVG_3 = loadvoltage;
248-
loadvoltage_AVG_4 = loadvoltage;
249-
loadvoltage_AVG_5 = loadvoltage;
250-
current_AVG = current_mA;
251-
current_AVG_1 = current_mA;
252-
current_AVG_2 = current_mA;
253-
current_AVG_3 = current_mA;
254-
current_AVG_4 = current_mA;
255-
current_AVG_5 = current_mA;
256-
power_AVG = power;
257-
power_AVG_1 = power;
258-
power_AVG_2 = power;
259-
power_AVG_3 = power;
260-
power_AVG_4 = power;
261-
power_AVG_5 = power;
215+
for (int i = 0; i < AVG_DEPTH_VOLTAGE; i++) {
216+
loadvoltage_AVG[i] = loadvoltage;
217+
}
218+
for (int i = 0; i < AVG_DEPTH_CURRENT; i++) {
219+
current_AVG[i] = current_mA;
220+
}
221+
for (int i = 0; i < AVG_DEPTH_POWER; i++) {
222+
power_AVG[i] = power;
223+
}
262224
delay(50);
263225
updateINA219eXtraValues();
264226
countdownResetCon = timer.setTimeout(1000, countdownResetConCallback);

settings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
*/
2222
//#define FIXED_ENERGY_PRICE 9.934
2323
#define ENERGY_API "http://192.168.1.2:3000/"
24+
/*
25+
Value Average Settings.
26+
Set the number of samples to take for the average values.
27+
Value = Seconds (default 5 seconds)
28+
*/
29+
#define AVG_DEPTH_VOLTAGE 5
30+
#define AVG_DEPTH_CURRENT 5
31+
#define AVG_DEPTH_POWER 5
2432
/*
2533
Virtual Pins - Base
2634
*/

0 commit comments

Comments
 (0)