Added runtime time logger

This commit is contained in:
Magnus Persson
2022-01-30 22:54:48 +01:00
parent 2e67bd1d57
commit 22ade61af8
7 changed files with 124 additions and 57 deletions

View File

@ -52,23 +52,11 @@ Config::Config() {
_mDNS.c_str()); _mDNS.c_str());
#endif #endif
setTempFormat('C');
setGravityFormat('G');
setSleepInterval(900); // 15 minutes
#if defined(ESP8266) #if defined(ESP8266)
setVoltageFactor(1.59); // Conversion factor for battery on ESP8266 setVoltageFactor(1.59); // Conversion factor for battery on ESP8266
#else // defined (ESP32) #else // defined (ESP32)
setVoltageFactor(1.43); // Conversion factor for battery on ESP32 setVoltageFactor(1.43); // Conversion factor for battery on ESP32
#endif #endif
setTempSensorAdjC(0.0);
setGravityTempAdj(false);
_gyroCalibration = {0, 0, 0, 0, 0, 0};
_formulaData = {{0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}};
_gyroTemp = false;
_saveNeeded = false;
_mqttPort = 1883;
_httpHeader[0] = F("Content-Type: application/json");
_http2Header[0] = F("Content-Type: application/json");
} }
// //

View File

@ -85,48 +85,48 @@ class HardwareConfig {
class Config { class Config {
private: private:
bool _saveNeeded; bool _saveNeeded = false;
// Device configuration // Device configuration
String _id; String _id = "";
String _mDNS; String _mDNS = "";
String _otaURL; String _otaURL = "";
char _tempFormat; char _tempFormat = 'C';
float _voltageFactor; float _voltageFactor = 0;
float _tempSensorAdjC; float _tempSensorAdjC = 0;
int _sleepInterval; int _sleepInterval = 900;
bool _gyroTemp; bool _gyroTemp = false;
// Wifi Config // Wifi Config
String _wifiSSID; String _wifiSSID = "";
String _wifiPASS; String _wifiPASS = "";
// Push target settings // Push target settings
String _brewfatherPushUrl; String _brewfatherPushUrl = "";
String _httpUrl; String _httpUrl = "";
String _httpHeader[2]; String _httpHeader[2] = { "Content-Type: application/json", "" };
String _http2Url; String _http2Url = "";
String _http2Header[2]; String _http2Header[2] = { "Content-Type: application/json", "" };
String _influxDb2Url; String _influxDb2Url = "";
String _influxDb2Org; String _influxDb2Org = "";
String _influxDb2Bucket; String _influxDb2Bucket = "";
String _influxDb2Token; String _influxDb2Token = "";
String _mqttUrl; String _mqttUrl = "";
int _mqttPort; int _mqttPort = 1883;
String _mqttUser; String _mqttUser = "";
String _mqttPass; String _mqttPass = "";
// Gravity and temperature calculations // Gravity and temperature calculations
String _gravityFormula; String _gravityFormula = "";
bool _gravityTempAdj; bool _gravityTempAdj = false;
char _gravityFormat; char _gravityFormat = 'G';
// Gyro calibration and formula calculation data // Gyro calibration and formula calculation data
RawGyroData _gyroCalibration; RawGyroData _gyroCalibration = {0, 0, 0, 0, 0, 0};
RawFormulaData _formulaData; RawFormulaData _formulaData = {{0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}};
void formatFileSystem(); void formatFileSystem();

View File

@ -69,25 +69,20 @@ ErrorFileLog::ErrorFileLog() {
if (errFile) { if (errFile) {
do { do {
errors[i] = errFile.readStringUntil('\n'); _errors[i] = errFile.readStringUntil('\n');
} while (errors[i++].length()); } while (_errors[i++].length());
errFile.close(); errFile.close();
} }
} }
//
//
//
const char* ErrorFileLog::getEntry(int idx) { return errors[idx].c_str(); }
// //
// //
// //
void ErrorFileLog::addEntry(String err) { void ErrorFileLog::addEntry(String err) {
for (int i = (ERR_COUNT - 1); i > 0; i--) { for (int i = (ERR_COUNT - 1); i > 0; i--) {
errors[i] = errors[i - 1]; _errors[i] = _errors[i - 1];
} }
errors[0] = err; _errors[0] = err;
Log.errorln(err.c_str()); Log.errorln(err.c_str());
save(); save();
} }
@ -99,12 +94,62 @@ void ErrorFileLog::save() {
File errFile = LittleFS.open(ERR_FILENAME, "w"); File errFile = LittleFS.open(ERR_FILENAME, "w");
if (errFile) { if (errFile) {
for (int i = 0; i < ERR_COUNT; i++) { for (int i = 0; i < ERR_COUNT; i++) {
errFile.println(errors[i]); errFile.println(_errors[i]);
} }
errFile.close(); errFile.close();
} }
} }
//
//
//
FloatHistoryLog::FloatHistoryLog(String fName) {
/*File debug = LittleFS.open(fName, "r");
String s = debug.readString();
Serial.println( s.c_str() );
debug.close();*/
_fName = fName;
File runFile = LittleFS.open(_fName, "r");
int i = 0;
if (runFile) {
for(int i = 0; i<10; i++) {
_runTime[i] = runFile.readStringUntil('\n').toFloat();
if (_runTime[i]) {
_average += _runTime[i];
_count++;
}
}
runFile.close();
_average = _average/_count;
}
}
//
//
//
void FloatHistoryLog::addEntry(float time) {
for (int i = (10 - 1); i > 0; i--) {
_runTime[i] = _runTime[i - 1];
}
_runTime[0] = time;
save();
}
//
//
//
void FloatHistoryLog::save() {
File runFile = LittleFS.open(_fName, "w");
if (runFile) {
for (int i = 0; i < 10; i++) {
runFile.println(_runTime[i], 2);
}
runFile.close();
}
}
// //
// Print the heap information. // Print the heap information.
// //

View File

@ -30,6 +30,8 @@ SOFTWARE.
#define ERR_FILENAME "/error.log" #define ERR_FILENAME "/error.log"
#define ERR_COUNT 15 #define ERR_COUNT 15
#define RUNTIME_FILENAME "/runtime.log"
// Sleep mode // Sleep mode
void deepSleep(int t); void deepSleep(int t);
@ -64,15 +66,28 @@ class SerialDebug {
class ErrorFileLog { class ErrorFileLog {
private: private:
String errors[ERR_COUNT]; String _errors[ERR_COUNT];
public: public:
ErrorFileLog(); ErrorFileLog();
const char* getEntry(int idx);
void addEntry(String error); void addEntry(String error);
void save(); void save();
}; };
class FloatHistoryLog {
private:
String _fName;
float _average = 0;
float _runTime[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int _count = 0;
void save();
public:
FloatHistoryLog(String fName);
void addEntry(float time);
float getAverage() { return _average; }
};
class BatteryVoltage { class BatteryVoltage {
private: private:
float _batteryLevel; float _batteryLevel;

View File

@ -274,6 +274,8 @@ void loopGravityOnInterval() {
} }
} }
bool skipRunTimeLog = false;
// //
// Main loop that determines if device should go to sleep // Main loop that determines if device should go to sleep
// //
@ -281,6 +283,11 @@ void goToSleep(int sleepInterval) {
float volt = myBatteryVoltage.getVoltage(); float volt = myBatteryVoltage.getVoltage();
float runtime = (millis() - runtimeMillis); float runtime = (millis() - runtimeMillis);
if (!skipRunTimeLog) {
FloatHistoryLog runLog(RUNTIME_FILENAME);
runLog.addEntry(runtime);
}
Log.notice(F("MAIN: Entering deep sleep for %ds, run time %Fs, " Log.notice(F("MAIN: Entering deep sleep for %ds, run time %Fs, "
"battery=%FV." CR), "battery=%FV." CR),
sleepInterval, reduceFloatPrecision(runtime / 1000, 2), volt); sleepInterval, reduceFloatPrecision(runtime / 1000, 2), volt);
@ -301,6 +308,10 @@ void loop() {
myWebServerHandler.loop(); myWebServerHandler.loop();
myWifi.loop(); myWifi.loop();
loopGravityOnInterval(); loopGravityOnInterval();
// If we switched mode, dont include this in the log.
if (runMode!=RunMode::configurationMode)
skipRunTimeLog = true;
break; break;
case RunMode::gravityMode: case RunMode::gravityMode:

View File

@ -199,13 +199,20 @@ void WebServerHandler::webHandleCalibrate() {
// //
// Callback from webServer when / has been accessed. // Callback from webServer when / has been accessed.
// //
void WebServerHandler::webHandleFactoryReset() { void WebServerHandler::webHandleFactoryDefaults() {
String id = _server->arg(PARAM_ID); String id = _server->arg(PARAM_ID);
Log.notice(F("WEB : webServer callback for /api/factory." CR)); Log.notice(F("WEB : webServer callback for /api/factory." CR));
if (!id.compareTo(myConfig.getID())) { if (!id.compareTo(myConfig.getID())) {
_server->send(200, "text/plain", "Doing reset..."); _server->send(200, "text/plain", "Removing configuration and restarting...");
LittleFS.remove(CFG_FILENAME); LittleFS.remove(CFG_FILENAME);
LittleFS.remove(CFG_HW_FILENAME);
LittleFS.remove(ERR_FILENAME);
LittleFS.remove(RUNTIME_FILENAME);
LittleFS.remove(TPL_FNAME_HTTP1);
LittleFS.remove(TPL_FNAME_HTTP2);
LittleFS.remove(TPL_FNAME_INFLUXDB);
LittleFS.remove(TPL_FNAME_MQTT);
LittleFS.end(); LittleFS.end();
delay(500); delay(500);
ESP_RESET(); ESP_RESET();
@ -951,6 +958,7 @@ bool WebServerHandler::setupWebServer() {
} }
#endif #endif
_server->serveStatic("/log", LittleFS, ERR_FILENAME); _server->serveStatic("/log", LittleFS, ERR_FILENAME);
_server->serveStatic("/runtime", LittleFS, RUNTIME_FILENAME);
// Dynamic content // Dynamic content
_server->on( _server->on(
@ -969,7 +977,7 @@ bool WebServerHandler::setupWebServer() {
std::bind(&WebServerHandler::webHandleCalibrate, std::bind(&WebServerHandler::webHandleCalibrate,
this)); // Run calibration routine (param id) this)); // Run calibration routine (param id)
_server->on("/api/factory", HTTP_GET, _server->on("/api/factory", HTTP_GET,
std::bind(&WebServerHandler::webHandleFactoryReset, std::bind(&WebServerHandler::webHandleFactoryDefaults,
this)); // Reset the device this)); // Reset the device
_server->on("/api/status", HTTP_GET, _server->on("/api/status", HTTP_GET,
std::bind(&WebServerHandler::webHandleStatus, std::bind(&WebServerHandler::webHandleStatus,

View File

@ -64,7 +64,7 @@ class WebServerHandler {
void webHandleStatusSleepmode(); void webHandleStatusSleepmode();
void webHandleClearWIFI(); void webHandleClearWIFI();
void webHandleStatus(); void webHandleStatus();
void webHandleFactoryReset(); void webHandleFactoryDefaults();
void webHandleCalibrate(); void webHandleCalibrate();
void webHandleUploadFile(); void webHandleUploadFile();
void webHandleUpload(); void webHandleUpload();