I have an ESP32 that gets pulses from my electricity meter. The pulses are aggregated over a day and the system provides a readout of values on a web page, and saves daily totals in a SPIFFS file named yyyy_mm.csv.
Its been working fine but on 11 May it lost its data, as shown here. dTotalkWh and mWatts are both incorrect - but NOT zero as they would be if zeroStats had been called.
The device is powered from a USB wallwart but (as far as I know) we have not had any power outages.
I've attached the rest of the sketch.
datavalues.ino (3.1 KB)
SolarLogger.h (6.9 KB)
SolarLoggerFinal2.ino (5.4 KB)
spiffs.ino (704 Bytes)
webpage.ino (1.1 KB)
wifi.ino (875 Bytes)
Day,total kWh,maxWatts
1, 15.292, 2990
2, 8.395, 3070
3, 8.555, 2050
4, 5.414, 1500
5, 19.441, 3180
6, 18.462, 3080
7, 14.466, 3200
8, 3.193, 970
9, 14.187, 3160
10, 13.143, 3190
11, 0.031, 50
12, 10.214, 3160
I've pored over the sketch but I cant see how that can happen. Here is the code (datavalues.ino) that does the calculation.
//preparation and calculations for data
// detect and count up pulses
void IRAM_ATTR ISR() {
// The IRAM_ATTR attribute places compiled code in the (faster) Internal RAM (IRAM) of the ESP32.
tNow = millis();
//max pulse rate is 1 per sec; 3600 watts in 3600 sec = 3.6kWh
if (tNow - tLast > 300) { // prevent bounce
pCount++; //increment count
cWatts = pCount * 10; //cWatts, pWatts, mWatts are adjusted (*10) as count is over 6 min not 60min
tLast = tNow;
}
}
//calculate wattage and power values
void zeroStats() { //zeroStats is called by setup and in newDay
pCount = 0; //reset pulse count
cWatts = 0; //reset current watts
mWatts = 0; //reset maximum watts
pWatts = 0; //reset previous period
dTotalkWh = 0; //reset total for current day
}
//every 6 minutes:
void checkPeriod() {
if (timeinfo.tm_min != cMin) { // new minute
cMin = timeinfo.tm_min;
if ((cMin % 6) == 0) { // end of previous period
pWatts = cWatts;
if (cWatts > mWatts) {
mWatts = cWatts; // update maximum value
}
dTotalkWh += pCount / 1000.0; //convert watts to kW & add to day total
Serial.println(&timeinfo, "%H:%M:%S"); //eg 07:04:45
Serial.print("cWatts: ");
Serial.print(cWatts);
Serial.print("; mWatts: ");
Serial.print(mWatts);
pCount = 0; //reset pulse counter
}
}
}
void newDay() {
//format & save 1 line of data
snprintf(dataString, sizeof(dataString), "\n%3u, %6.3f, %3u", currDay, dTotalkWh, mWatts);
appendFile(SPIFFS, fileName, dataString);
Serial.print("newDay: ");
Serial.println(dataString);
yTotalkWh = dTotalkWh; //store current total as "yesterdays total"
zeroStats();
}
void newMonth() {
//create new filename for saving data and append header line; handle new day first; filename needs leading /
strftime (fileName, sizeof(fileName), "/%Y_%m.csv", &timeinfo); // "/" + 4 digit year + "_" + 2 digit month +".csv" + null = 12 chars
if (SPIFFS.exists(fileName)) {}
else { //no file ready
writeFile(SPIFFS, fileName, headerString); //char headerString[30] = "Day,total kWh,maxWatts \n";
Serial.print("Filename is ");
Serial.println(fileName);
}
currMonth = timeinfo.tm_mon; //update current month
Serial.print("newMonth: ");
Serial.println(headerString);
}
grateful for any help.