From 088a37eaf39ac9fb5507afc78cc0985a3f049fe3 Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Tue, 4 Jan 2022 21:13:34 +0100 Subject: [PATCH] Reduced logging messages --- platformio.ini | 2 +- src/config.cpp | 36 +++++------ src/gyro.cpp | 59 ++++++++++-------- src/main.cpp | 34 ++++++----- src/pushtarget.cpp | 31 ++++++---- src/tempsensor.cpp | 27 ++++----- src/webserver.cpp | 147 +++++++++++++++++++++++---------------------- src/wifi.cpp | 17 +++--- 8 files changed, 189 insertions(+), 164 deletions(-) diff --git a/platformio.ini b/platformio.ini index 59e5cea..17f3e1d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -33,7 +33,7 @@ build_flags = #-D SKIP_SLEEPMODE #-D DOUBLERESETDETECTOR_DEBUG true -D USE_LITTLEFS=true - #-D EMBED_HTML # If this is not used the html files needs to be on the file system (can be uploaded) + -D EMBED_HTML # If this is not used the html files needs to be on the file system (can be uploaded) -D USER_SSID=\""\"" # =\""myssid\"" -D USER_SSID_PWD=\""\"" # =\""mypwd\"" -D CFG_APPVER="\"0.4.9\"" diff --git a/src/config.cpp b/src/config.cpp index e5f5f4f..d36dbdb 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -25,6 +25,8 @@ SOFTWARE. #include "helper.h" #include +#define CFG_DISABLE_LOGGING + Config myConfig; // @@ -38,9 +40,9 @@ Config::Config() { sprintf(&buf[0], "" WIFI_MDNS "%s", getID() ); mDNS = String( &buf[0] ); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Created config for %s (%s)." CR), id.c_str(), mDNS.c_str() ); -#endif + #endif setTempFormat('C'); setGravityFormat('G'); @@ -105,15 +107,15 @@ void Config::createJson(DynamicJsonDocument& doc) { // bool Config::saveFile() { if( !saveNeeded ) { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Skipping save, not needed." CR)); -#endif + #endif return true; } -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Saving configuration to file." CR)); -#endif + #endif File configFile = LittleFS.open(CFG_FILENAME, "w"); @@ -124,10 +126,12 @@ bool Config::saveFile() { DynamicJsonDocument doc(CFG_JSON_BUFSIZE); createJson( doc ); -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) serializeJson(doc, Serial); Serial.print( CR ); -#endif + #endif + serializeJson(doc, configFile); configFile.flush(); configFile.close(); @@ -142,9 +146,9 @@ bool Config::saveFile() { // Load config file from disk // bool Config::loadFile() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Loading configuration from file." CR)); -#endif + #endif if (!LittleFS.exists(CFG_FILENAME)) { Log.error(F("CFG : Configuration file does not exist " CFG_FILENAME "." CR)); @@ -264,9 +268,7 @@ bool Config::loadFile() { // Check if file system can be mounted, if not we format it. // void Config::formatFileSystem() { -#if LOG_LEVEL==6 - Log.verbose(F("CFG : Formating filesystem." CR)); -#endif + Log.notice(F("CFG : Formating filesystem." CR)); LittleFS.format(); } @@ -274,9 +276,9 @@ void Config::formatFileSystem() { // Check if file system can be mounted, if not we format it. // void Config::checkFileSystem() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Checking if filesystem is valid." CR)); -#endif + #endif if (LittleFS.begin()) { Log.notice(F("CFG : Filesystem mounted." CR)); @@ -290,7 +292,7 @@ void Config::checkFileSystem() { // Dump the configuration to the serial port // void Config::debug() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( CFG_DISABLE_LOGGING ) Log.verbose(F("CFG : Dumping configration " CFG_FILENAME "." CR)); Log.verbose(F("CFG : ID; '%s'." CR), getID()); Log.verbose(F("CFG : WIFI; '%s', '%s'." CR), getWifiSSID(), getWifiPass() ); @@ -310,7 +312,7 @@ void Config::debug() { getInfluxDb2PushBucket(), getInfluxDb2PushToken() ); // Log.verbose(F("CFG : Accel offset\t%d\t%d\t%d" CR), gyroCalibration.ax, gyroCalibration.ay, gyroCalibration.az ); // Log.verbose(F("CFG : Gyro offset \t%d\t%d\t%d" CR), gyroCalibration.gx, gyroCalibration.gy, gyroCalibration.gz ); -#endif + #endif } // EOF \ No newline at end of file diff --git a/src/gyro.cpp b/src/gyro.cpp index b96bc0a..9f2c331 100644 --- a/src/gyro.cpp +++ b/src/gyro.cpp @@ -24,6 +24,8 @@ SOFTWARE. #include "gyro.h" #include "helper.h" +#define GYRO_DISABLE_LOGGING + GyroSensor myGyro; #define GYRO_USE_INTERRUPT // Use interrupt to detect when new sample is ready @@ -38,9 +40,9 @@ GyroSensor myGyro; // Initialize the sensor chip. // bool GyroSensor::setup() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Setting up hardware." CR)); -#endif + #endif Wire.begin(D3, D4); Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties accelgyro.initialize(); @@ -50,7 +52,9 @@ bool GyroSensor::setup() { sensorConnected = false; } else { + #if !defined( GYRO_DISABLE_LOGGING ) Log.notice(F("GYRO: Connected to MPU6050 (gyro)." CR)); + #endif sensorConnected = true; // Configure the sensor @@ -76,7 +80,6 @@ bool GyroSensor::setup() { calibrationOffset = myConfig.getGyroCalibration(); applyCalibration(); } - return sensorConnected; } @@ -84,9 +87,9 @@ bool GyroSensor::setup() { // Set sensor in sleep mode to conserve battery // void GyroSensor::enterSleep() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Setting up hardware." CR)); -#endif + #endif accelgyro.setSleepEnabled( true ); } @@ -96,9 +99,9 @@ void GyroSensor::enterSleep() { void GyroSensor::readSensor(RawGyroData &raw, const int noIterations, const int delayTime) { RawGyroDataL average = { 0, 0, 0, 0, 0, 0 }; -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Reading sensor with %d iterations %d us delay." CR), noIterations, delayTime ); -#endif + #endif // Set some initial values #if defined( GYRO_SHOW_MINMAX ) @@ -162,7 +165,7 @@ void GyroSensor::readSensor(RawGyroData &raw, const int noIterations, const int raw.gz = average.gz/noIterations; raw.temp = average.temp/noIterations; -#if LOG_LEVEL==6 +#if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) #if defined( GYRO_SHOW_MINMAX ) Log.verbose(F("GYRO: Min \t%d\t%d\t%d\t%d\t%d\t%d\t%d." CR), min.ax, min.ay, min.az, min.gx, min.gy, min.gz, min.temp ); Log.verbose(F("GYRO: Max \t%d\t%d\t%d\t%d\t%d\t%d\t%d." CR), max.ax, max.ay, max.az, max.gx, max.gy, max.gz, max.temp ); @@ -177,9 +180,9 @@ void GyroSensor::readSensor(RawGyroData &raw, const int noIterations, const int // Calcuate the angles (tilt) // float GyroSensor::calculateAngle(RawGyroData &raw) { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Calculating the angle." CR) ); -#endif + #endif // Smooth out the readings to we can have a more stable angle/tilt. // ------------------------------------------------------------------------------------------------------------ @@ -195,9 +198,9 @@ float GyroSensor::calculateAngle(RawGyroData &raw) { //double v = (acos( raw.az / sqrt( raw.ax*raw.ax + raw.ay*raw.ay + raw.az*raw.az ) ) *180.0 / PI); //Log.notice(F("GYRO: angle = %F." CR), v ); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: angle = %F." CR), v ); -#endif + #endif return v; } @@ -205,9 +208,9 @@ float GyroSensor::calculateAngle(RawGyroData &raw) { // Check if the values are high that indicate that the sensor is moving. // bool GyroSensor::isSensorMoving(RawGyroData &raw) { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Checking for sensor movement." CR) ); -#endif + #endif int x = abs(raw.gx), y = abs(raw.gy), z = abs(raw.gz); @@ -223,9 +226,10 @@ bool GyroSensor::isSensorMoving(RawGyroData &raw) { // Read the tilt angle from the gyro. // bool GyroSensor::read() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Getting new gyro position." CR) ); -#endif + #endif + if( !sensorConnected ) return false; @@ -233,12 +237,16 @@ bool GyroSensor::read() { // If the sensor is unstable we return false to signal we dont have valid value if( isSensorMoving(lastGyroData) ) { + #if !defined( GYRO_DISABLE_LOGGING ) Log.notice(F("GYRO: Sensor is moving." CR) ); + #endif validValue = false; } else { validValue = true; angle = calculateAngle( lastGyroData ); - //Log.notice(F("GYRO: Sensor values %d,%d,%d\t%F" CR), raw.ax, raw.ay, raw.az, angle ); + #if !defined( GYRO_DISABLE_LOGGING ) + Log.notice(F("GYRO: Sensor values %d,%d,%d\t%F" CR), raw.ax, raw.ay, raw.az, angle ); + #endif } sensorTemp = ((float) lastGyroData.temp) / 340 + 36.53; @@ -255,19 +263,20 @@ bool GyroSensor::read() { // Dump the stored calibration values. // void GyroSensor::dumpCalibration() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Accel offset\t%d\t%d\t%d" CR), calibrationOffset.ax, calibrationOffset.ay, calibrationOffset.az ); Log.verbose(F("GYRO: Gyro offset \t%d\t%d\t%d" CR), calibrationOffset.gx, calibrationOffset.gy, calibrationOffset.gz ); -#endif + #endif } // // Update the sensor with out calculated offsets. // void GyroSensor::applyCalibration() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Applying calibration offsets to sensor." CR) ); -#endif + #endif + if( ( calibrationOffset.ax + calibrationOffset.ay + calibrationOffset.az + calibrationOffset.gx + calibrationOffset.gy + calibrationOffset.gz ) == 0 ) { Log.error(F("GYRO: No valid calibraion values exist, aborting." CR) ); return; @@ -285,9 +294,9 @@ void GyroSensor::applyCalibration() { // Calculate the offsets for calibration. // void GyroSensor::calibrateSensor() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Calibrating sensor" CR) ); -#endif + #endif //accelgyro.PrintActiveOffsets(); //Serial.print( CR ); @@ -314,7 +323,7 @@ void GyroSensor::calibrateSensor() { // Calibrate the device. // void GyroSensor::debug() { -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( GYRO_DISABLE_LOGGING ) Log.verbose(F("GYRO: Debug - Clock src %d." CR), accelgyro.getClockSource() ); Log.verbose(F("GYRO: Debug - Device ID %d." CR), accelgyro.getDeviceID() ); Log.verbose(F("GYRO: Debug - DHPF Mode %d." CR), accelgyro.getDHPFMode() ); @@ -355,7 +364,7 @@ void GyroSensor::debug() { Log.verbose(F("GYRO: Debug - Gyr OffX %d\t%d." CR), accelgyro.getXGyroOffset(), calibrationOffset.gx ); Log.verbose(F("GYRO: Debug - Gyr OffY %d\t%d." CR), accelgyro.getYGyroOffset(), calibrationOffset.gy ); Log.verbose(F("GYRO: Debug - Gyr OffZ %d\t%d." CR), accelgyro.getZGyroOffset(), calibrationOffset.gz ); -#endif + #endif } // EOF \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c8d08be..14c2c1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,8 @@ SOFTWARE. #include "pushtarget.h" #include +#define MAIN_DISABLE_LOGGING + // Settings for double reset detector. #define ESP8266_DRD_USE_RTC true #define DRD_TIMEOUT 2 @@ -83,9 +85,9 @@ void checkSleepMode( float angle, float volt ) { // sleep mode active when flat //sleepModeActive = ( angle<85 && angle>5 ) ? true : false; -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) Log.verbose(F("MAIN: Deep sleep mode %s (angle=%F volt=%F)." CR), sleepModeActive ? "true":"false", angle, volt ); -#endif + #endif } // @@ -98,9 +100,9 @@ void setup() { drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS); bool dt = drd->detectDoubleReset(); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) Log.verbose(F("Main: Reset reason %s." CR), ESP.getResetInfo().c_str() ); -#endif + #endif // Main startup Log.notice(F("Main: Started setup for %s." CR), String( ESP.getChipId(), HEX).c_str() ); printBuildOptions(); @@ -176,9 +178,11 @@ void loop() { float volt = myBatteryVoltage.getVoltage(); //float sensorTemp = 0; loopCounter++; -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) Log.verbose(F("Main: Entering main loop." CR) ); -#endif + #endif + // Process the sensor values and push data to targets. // ------------------------------------------------------------------------------------------------ // If we dont get any readings we just skip this and try again the next interval. @@ -201,9 +205,10 @@ void loop() { float corrGravity = gravityTemperatureCorrection( gravity, temp, myConfig.getTempFormat() ); //LOG_PERF_STOP("loop-gravity-corr"); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) Log.verbose(F("Main: Sensor values gyro angle=%F, temp=%F, gravity=%F, corr=%F." CR), angle, temp, gravity, corrGravity ); -#endif + #endif + // Limit the printout when sleep mode is not active. if( loopCounter%10 == 0 || sleepModeActive ) { Log.notice(F("Main: angle=%F, temp=%F, gravity=%F, corrGravity=%F, batt=%F." CR), angle, temp, gravity, corrGravity ,volt ); @@ -220,9 +225,10 @@ void loop() { Log.error(F("Main: No gyro value." CR) ); } -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) Log.verbose(F("Main: Sleep mode not active." CR) ); -#endif + #endif + int sleepInterval = myConfig.getSleepInterval(); // If we didnt get a wifi connection, we enter sleep for a short time to conserve battery. @@ -272,13 +278,11 @@ void loop() { //LOG_PERF_STOP("loop-batt-read"); loopMillis = millis(); -//#if LOG_LEVEL==6 - Log.verbose(F("Main: Heap %d kb FreeSketch %d kb." CR), ESP.getFreeHeap()/1024, ESP.getFreeSketchSpace()/1024 ); - Log.verbose(F("Main: HeapFrag %d %%." CR), ESP.getHeapFragmentation() ); -//#endif + //#if LOG_LEVEL==6 && !defined( MAIN_DISABLE_LOGGING ) + Log.verbose(F("Main: Heap %d kb FreeSketch %d kb HeapFrag %d %%." CR), ESP.getFreeHeap()/1024, ESP.getFreeSketchSpace()/1024, ESP.getHeapFragmentation() ); + //#endif } - //if( myWifi.isConnected() ) myWebServer.loop(); } diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 9dcc709..0495d5c 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -25,6 +25,8 @@ SOFTWARE. #include "config.h" #include "gyro.h" // For testing the tempsensor in the gyro +#define PUSH_DISABLE_LOGGING + PushTarget myPushTarget; // @@ -35,15 +37,16 @@ void PushTarget::send(float angle, float gravity, float corrGravity, float temp, unsigned long interval = myConfig.getSleepInterval()*1000; if( ( timePassed < interval ) && !force) { -#if LOG_LEVEL==6 - Log.verbose(F("PUSH: Timer has not expired %l vs %l." CR), timePassed, interval ); -#endif + #if LOG_LEVEL==6 && !defined( PUSH_DISABLE_LOGGING ) + Log.verbose(F("PUSH: Timer has not expired %l vs %l." CR), timePassed, interval ); + #endif return; } -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( PUSH_DISABLE_LOGGING ) Log.verbose(F("PUSH: Sending data." CR) ); -#endif + #endif + ms = millis(); if( myConfig.isBrewfatherActive() ) { @@ -75,7 +78,9 @@ void PushTarget::send(float angle, float gravity, float corrGravity, float temp, // Send to influx db v2 // void PushTarget::sendInfluxDb2(float angle, float gravity, float corrGravity, float temp, float runTime) { + #if !defined( PUSH_DISABLE_LOGGING ) Log.notice(F("PUSH: Sending values to influxdb2 angle=%F, gravity=%F, temp=%F." CR), angle, gravity, temp ); + #endif WiFiClient client; HTTPClient http; @@ -94,10 +99,10 @@ void PushTarget::sendInfluxDb2(float angle, float gravity, float corrGravity, fl myConfig.isGravityTempAdj() ? corrGravity : gravity, corrGravity, angle, temp, myBatteryVoltage.getVoltage(), WiFi.RSSI(), myGyro.getSensorTempC() ); // For comparing gyro tempsensor vs DSB1820 -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( PUSH_DISABLE_LOGGING ) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); Log.verbose(F("PUSH: data %s." CR), &buf[0] ); -#endif + #endif // Send HTTP POST request String auth = "Token " + String( myConfig.getInfluxDb2PushToken() ); @@ -117,7 +122,9 @@ void PushTarget::sendInfluxDb2(float angle, float gravity, float corrGravity, fl // Send data to brewfather // void PushTarget::sendBrewfather(float angle, float gravity, float corrGravity, float temp ) { + #if !defined( PUSH_DISABLE_LOGGING ) Log.notice(F("PUSH: Sending values to brewfather angle=%F, gravity=%F, temp=%F." CR), angle, gravity, temp ); + #endif DynamicJsonDocument doc(300); // @@ -154,10 +161,10 @@ void PushTarget::sendBrewfather(float angle, float gravity, float corrGravity, f http.begin( client, serverPath); String json; serializeJson(doc, json); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( PUSH_DISABLE_LOGGING ) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); Log.verbose(F("PUSH: json %s." CR), json.c_str()); -#endif + #endif // Send HTTP POST request http.addHeader(F("Content-Type"), F("application/json") ); @@ -176,7 +183,9 @@ void PushTarget::sendBrewfather(float angle, float gravity, float corrGravity, f // Send data to http target // void PushTarget::sendHttp( String serverPath, float angle, float gravity, float corrGravity, float temp, float runTime ) { + #if !defined( PUSH_DISABLE_LOGGING ) Log.notice(F("PUSH: Sending values to http angle=%F, gravity=%F, temp=%F." CR), angle, gravity, temp ); + #endif DynamicJsonDocument doc(256); @@ -205,10 +214,10 @@ void PushTarget::sendHttp( String serverPath, float angle, float gravity, float http.begin( client, serverPath); String json; serializeJson(doc, json); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( PUSH_DISABLE_LOGGING ) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); Log.verbose(F("PUSH: json %s." CR), json.c_str()); -#endif + #endif // Send HTTP POST request http.addHeader(F("Content-Type"), F("application/json") ); diff --git a/src/tempsensor.cpp b/src/tempsensor.cpp index 17e18e5..10f07aa 100644 --- a/src/tempsensor.cpp +++ b/src/tempsensor.cpp @@ -29,6 +29,8 @@ SOFTWARE. #include #include +#define TSEN_DISABLE_LOGGING + // // Conversion between C and F // @@ -57,19 +59,15 @@ void TempSensor::setup() { #if defined( USE_GYRO_TEMP ) Log.notice(F("TSEN: Using temperature from gyro." CR)); #else - // This code is used to read the DS18 temp sensor - /*if( !mySensors.getDS18Count() ) { - Log.error(F("TSEN: No temperature sensors found." CR)); - return; - }*/ - -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( TSEN_DISABLE_LOGGING ) Log.verbose(F("TSEN: Looking for temp sensors." CR)); -#endif + #endif mySensors.begin(); if( mySensors.getDS18Count() ) { + #if !defined( TSEN_DISABLE_LOGGING ) Log.notice(F("TSEN: Found %d temperature sensor(s)." CR), mySensors.getDS18Count()); + #endif mySensors.setResolution(TEMPERATURE_PRECISION); } #endif @@ -85,9 +83,9 @@ void TempSensor::setup() { tempSensorAdjC = t * 0.556; // Convert the adjustent value to F } -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( TSEN_DISABLE_LOGGING ) Log.verbose(F("TSEN: Adjustment values for temp sensor %F C, %F F." CR), tempSensorAdjC, tempSensorAdjF ); -#endif + #endif } // @@ -105,9 +103,9 @@ float TempSensor::getValue() { //LOG_PERF_STOP("temp-get"); hasSensor = true; return c; -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( TSEN_DISABLE_LOGGING ) Log.verbose(F("TSEN: Reciving temp value for gyro sensor %F C." CR), c); -#endif + #endif #else // If we dont have sensors just return 0 if( !mySensors.getDS18Count() ) { @@ -127,12 +125,11 @@ float TempSensor::getValue() { c = mySensors.getTempCByIndex(0); //LOG_PERF_STOP("temp-get"); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( TSEN_DISABLE_LOGGING ) Log.verbose(F("TSEN: Reciving temp value for sensor %F C." CR), c); -#endif + #endif hasSensor = true; } - return c; #endif } diff --git a/src/webserver.cpp b/src/webserver.cpp index 96fd8e5..00db470 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -30,6 +30,8 @@ SOFTWARE. #include #include +#define WEB_DISABLE_LOGGING + WebServer myWebServer; // My wrapper class fr webserver functions extern bool sleepModeActive; extern bool sleepModeAlwaysSkip; @@ -39,9 +41,10 @@ extern bool sleepModeAlwaysSkip; // void WebServer::webHandleDevice() { LOG_PERF_START("webserver-api-device"); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : webServer callback for /api/config." CR)); -#endif + #endif + DynamicJsonDocument doc(100); doc[ CFG_PARAM_ID ] = myConfig.getID(); doc[ CFG_PARAM_APP_NAME ] = CFG_APPNAME; @@ -62,9 +65,8 @@ void WebServer::webHandleDevice() { // void WebServer::webHandleConfig() { LOG_PERF_START("webserver-api-config"); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/config." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/config." CR)); + DynamicJsonDocument doc(CFG_JSON_BUFSIZE); myConfig.createJson( doc ); @@ -79,10 +81,11 @@ void WebServer::webHandleConfig() { doc[ CFG_PARAM_GRAVITY ] = reduceFloatPrecision( gravity, 4); doc[ CFG_PARAM_BATTERY ] = reduceFloatPrecision( myBatteryVoltage.getVoltage()); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) serializeJson(doc, Serial); Serial.print( CR ); -#endif + #endif + String out; serializeJson(doc, out); server->send(200, "application/json", out.c_str() ); @@ -94,9 +97,6 @@ void WebServer::webHandleConfig() { // void WebServer::webHandleUpload() { LOG_PERF_START("webserver-api-upload"); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/upload." CR)); -#endif Log.notice(F("WEB : webServer callback for /api/upload." CR)); DynamicJsonDocument doc(100); @@ -106,10 +106,11 @@ void WebServer::webHandleUpload() { doc[ "calibration" ] = myWebServer.checkHtmlFile( WebServer::HTML_CALIBRATION ); doc[ "about" ] = myWebServer.checkHtmlFile( WebServer::HTML_ABOUT ); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) serializeJson(doc, Serial); Serial.print( CR ); -#endif + #endif + String out; serializeJson(doc, out); server->send(200, "application/json", out.c_str() ); @@ -121,9 +122,7 @@ void WebServer::webHandleUpload() { // void WebServer::webHandleUploadFile() { LOG_PERF_START("webserver-api-upload-file"); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/upload/file." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/upload/file." CR)); HTTPUpload& upload = server->upload(); String f = upload.filename; bool validFilename = false; @@ -133,7 +132,9 @@ void WebServer::webHandleUploadFile() { validFilename = true; } - Log.notice(F("WEB : webServer callback for /api/upload, receiving file %s, valid=%s." CR), f.c_str(), validFilename?"yes":"no"); + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) + Log.debug(F("WEB : webServer callback for /api/upload, receiving file %s, valid=%s." CR), f.c_str(), validFilename?"yes":"no"); + #endif if(upload.status == UPLOAD_FILE_START) { Log.notice(F("WEB : Start upload." CR) ); @@ -163,9 +164,8 @@ void WebServer::webHandleUploadFile() { void WebServer::webHandleCalibrate() { LOG_PERF_START("webserver-api-calibrate"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/calibrate." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/calibrate." CR)); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); @@ -182,9 +182,8 @@ void WebServer::webHandleCalibrate() { // void WebServer::webHandleFactoryReset() { String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/factory." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/factory." CR)); + if( !id.compareTo( myConfig.getID() ) ) { server->send(200, "text/plain", "Doing reset..."); LittleFS.remove(CFG_FILENAME); @@ -201,9 +200,8 @@ void WebServer::webHandleFactoryReset() { // void WebServer::webHandleStatus() { LOG_PERF_START("webserver-api-status"); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/status." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/status." CR)); + DynamicJsonDocument doc(256); double angle = myGyro.getAngle(); @@ -222,10 +220,12 @@ void WebServer::webHandleStatus() { doc[ CFG_PARAM_TEMPFORMAT ] = String( myConfig.getTempFormat() ); doc[ CFG_PARAM_SLEEP_MODE ] = sleepModeAlwaysSkip; doc[ CFG_PARAM_RSSI ] = WiFi.RSSI(); -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) serializeJson(doc, Serial); Serial.print( CR ); -#endif + #endif + String out; serializeJson(doc, out); server->send(200, "application/json", out.c_str() ); @@ -237,9 +237,8 @@ void WebServer::webHandleStatus() { // void WebServer::webHandleClearWIFI() { String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/clearwifi." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/clearwifi." CR)); + if( !id.compareTo( myConfig.getID() ) ) { server->send(200, "text/plain", "Clearing WIFI credentials and doing reset..."); delay(1000); @@ -256,18 +255,19 @@ void WebServer::webHandleClearWIFI() { void WebServer::webHandleStatusSleepmode() { LOG_PERF_START("webserver-api-sleepmode"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/status/sleepmode." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/status/sleepmode." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-sleepmode"); return; } -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : sleep-mode=%s." CR), server->arg( CFG_PARAM_SLEEP_MODE ).c_str() ); -#endif + #endif + if( server->arg( CFG_PARAM_SLEEP_MODE ).equalsIgnoreCase( "true" ) ) sleepModeAlwaysSkip = true; else @@ -282,18 +282,19 @@ void WebServer::webHandleStatusSleepmode() { void WebServer::webHandleConfigDevice() { LOG_PERF_START("webserver-api-config-device"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/config/device." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/config/device." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-config-device"); return; } -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : mdns=%s, temp-format=%s." CR), server->arg( CFG_PARAM_MDNS ).c_str(), server->arg( CFG_PARAM_TEMPFORMAT ).c_str() ); -#endif + #endif + myConfig.setMDNS( server->arg( CFG_PARAM_MDNS ).c_str() ); myConfig.setTempFormat( server->arg( CFG_PARAM_TEMPFORMAT ).charAt(0) ); myConfig.setSleepInterval( server->arg( CFG_PARAM_SLEEP_INTERVAL ).c_str() ); @@ -309,22 +310,22 @@ void WebServer::webHandleConfigDevice() { void WebServer::webHandleConfigPush() { LOG_PERF_START("webserver-api-config-push"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/config/push." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/config/push." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-config-push"); return; } -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : http=%s,%s, bf=%s influx2=%s, %s, %s, %s." CR), server->arg( CFG_PARAM_PUSH_HTTP ).c_str(), server->arg( CFG_PARAM_PUSH_HTTP2 ).c_str(), server->arg( CFG_PARAM_PUSH_BREWFATHER ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2 ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2_ORG ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2_BUCKET ).c_str(), server->arg( CFG_PARAM_PUSH_INFLUXDB2_AUTH ).c_str() ); -#endif + #endif + myConfig.setHttpPushUrl( server->arg( CFG_PARAM_PUSH_HTTP ).c_str() ); myConfig.setHttpPushUrl2( server->arg( CFG_PARAM_PUSH_HTTP2 ).c_str() ); myConfig.setBrewfatherPushUrl( server->arg( CFG_PARAM_PUSH_BREWFATHER ).c_str() ); @@ -344,18 +345,19 @@ void WebServer::webHandleConfigPush() { void WebServer::webHandleConfigGravity() { LOG_PERF_START("webserver-api-config-gravity"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/config/gravity." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/config/gravity." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-config-gravity"); return; } -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : formula=%s, temp-corr=%s." CR), server->arg( CFG_PARAM_GRAVITY_FORMULA ).c_str(), server->arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).c_str() ); -#endif + #endif + myConfig.setGravityFormula( server->arg( CFG_PARAM_GRAVITY_FORMULA ).c_str() ); myConfig.setGravityTempAdj( server->arg( CFG_PARAM_GRAVITY_TEMP_ADJ ).equalsIgnoreCase( "on" ) ? true:false); myConfig.saveFile(); @@ -370,18 +372,19 @@ void WebServer::webHandleConfigGravity() { void WebServer::webHandleConfigHardware() { LOG_PERF_START("webserver-api-config-hardware"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/config/hardware." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/config/hardware." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-config-hardware"); return; } -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : vf=%s, tempadj=%s, ota=%s." CR), server->arg( CFG_PARAM_VOLTAGEFACTOR ).c_str(), server->arg( CFG_PARAM_TEMP_ADJ ).c_str(), server->arg( CFG_PARAM_OTA ).c_str() ); -#endif + #endif + myConfig.setVoltageFactor( server->arg( CFG_PARAM_VOLTAGEFACTOR ).toFloat() ); myConfig.setTempSensorAdj( server->arg( CFG_PARAM_TEMP_ADJ ).toFloat() ); myConfig.setOtaURL( server->arg( CFG_PARAM_OTA ).c_str() ); @@ -396,16 +399,15 @@ void WebServer::webHandleConfigHardware() { // void WebServer::webHandleFormulaRead() { LOG_PERF_START("webserver-api-formula-read"); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/formula/get." CR)); -#endif + Log.notice(F("WEB : webServer callback for /api/formula/get." CR)); + DynamicJsonDocument doc(250); const RawFormulaData& fd = myConfig.getFormulaData(); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : %F %F %F %F %F." CR), fd.a[0], fd.a[1], fd.a[2], fd.a[3], fd.a[4] ); Log.verbose(F("WEB : %F %F %F %F %F." CR), fd.g[0], fd.g[1], fd.g[2], fd.g[3], fd.g[4] ); -#endif + #endif doc[ CFG_PARAM_ID ] = myConfig.getID(); doc[ CFG_PARAM_ANGLE ] = reduceFloatPrecision( myGyro.getAngle() ); @@ -437,10 +439,11 @@ void WebServer::webHandleFormulaRead() { doc[ "g4" ] = reduceFloatPrecision( fd.g[3], 4); doc[ "g5" ] = reduceFloatPrecision( fd.g[4], 4); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) serializeJson(doc, Serial); Serial.print( CR ); -#endif + #endif + String out; serializeJson(doc, out); server->send(200, "application/json", out.c_str() ); @@ -453,19 +456,20 @@ void WebServer::webHandleFormulaRead() { void WebServer::webHandleFormulaWrite() { LOG_PERF_START("webserver-api-formula-write"); String id = server->arg( CFG_PARAM_ID ); -#if LOG_LEVEL==6 - Log.verbose(F("WEB : webServer callback for /api/formula/post." CR) ); -#endif + Log.notice(F("WEB : webServer callback for /api/formula/post." CR) ); + if( !id.equalsIgnoreCase( myConfig.getID() ) ) { Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(), myConfig.getID()); server->send(400, "text/plain", "Invalid ID."); LOG_PERF_STOP("webserver-api-formula-write"); return; } -#if LOG_LEVEL==6 + + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : angles=%F,%F,%F,%F,%F." CR), server->arg( "a1" ).toFloat(), server->arg( "a2" ).toFloat(), server->arg( "a3" ).toFloat(), server->arg( "a4" ).toFloat(), server->arg( "a5" ).toFloat() ); Log.verbose(F("WEB : gravity=%F,%F,%F,%F,%F." CR), server->arg( "g1" ).toFloat(), server->arg( "g2" ).toFloat(), server->arg( "g3" ).toFloat(), server->arg( "g4" ).toFloat(), server->arg( "g5" ).toFloat() ); -#endif + #endif + RawFormulaData fd; fd.a[0] = server->arg( "a1" ).toDouble(); fd.a[1] = server->arg( "a2" ).toDouble(); @@ -516,9 +520,8 @@ void WebServer::webHandleFormulaWrite() { // Helper function to check if files exist on file system. // const char* WebServer::getHtmlFileName( HtmlFile item ) { -#if LOG_LEVEL==6 - Log.verbose(F("WEB : Looking up filename for %d." CR), item); -#endif + Log.notice(F("WEB : Looking up filename for %d." CR), item); + switch( item ) { case HTML_INDEX: return "index.min.htm"; @@ -541,9 +544,9 @@ const char* WebServer::getHtmlFileName( HtmlFile item ) { bool WebServer::checkHtmlFile( HtmlFile item ) { const char *fn = getHtmlFileName( item ); -#if LOG_LEVEL==6 + #if LOG_LEVEL==6 && !defined( WEB_DISABLE_LOGGING ) Log.verbose(F("WEB : Checking for file %s." CR), fn ); -#endif + #endif // TODO: We might need to add more checks here like zero file size etc. But for now we only check if the file exist. diff --git a/src/wifi.cpp b/src/wifi.cpp index 3d7b022..8f2eef5 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -52,7 +52,8 @@ bool Wifi::connect( bool showPortal ) { Log.info(F("WIFI: No SSID seams to be stored, forcing portal to start." CR)); showPortal = true; } else { - Log.info(F("WIFI: Using SSID=%s and %s." CR), myConfig.getWifiSSID(), myConfig.getWifiPass() ); + //Log.info(F("WIFI: Using SSID=%s and %s." CR), myConfig.getWifiSSID(), myConfig.getWifiPass() ); + Log.info(F("WIFI: Using SSID=%s and %s." CR), myConfig.getWifiSSID(), "*****" ); } if( strlen(userSSID)==0 && showPortal ) { @@ -66,7 +67,8 @@ bool Wifi::connect( bool showPortal ) { myWifiManager.setConfigPortalTimeout( 120 ); // Keep it open for 120 seconds bool f = myWifiManager.startConfigPortal( WIFI_DEFAULT_SSID, WIFI_DEFAULT_PWD ); if( f ) { - Log.notice(F("WIFI: Success got values from WIFI portal=%s,%s." CR), myWifiManager.getWiFiSSID(), myWifiManager.getWiFiPass() ); + //Log.notice(F("WIFI: Success got values from WIFI portal=%s,%s." CR), myWifiManager.getWiFiSSID(), myWifiManager.getWiFiPass() ); + Log.notice(F("WIFI: Success got values from WIFI portal=%s,%s." CR), myWifiManager.getWiFiSSID(), "*****" ); myConfig.setWifiSSID( myWifiManager.getWiFiSSID() ); myConfig.setWifiPass( myWifiManager.getWiFiPass() ); myConfig.saveFile(); @@ -90,18 +92,17 @@ bool Wifi::connect( bool showPortal ) { WiFi.begin(myConfig.getWifiSSID(), myConfig.getWifiPass()); } - WiFi.printDiag(Serial); + //WiFi.printDiag(Serial); while( WiFi.status() != WL_CONNECTED ) { - yield(); - delay(100); + delay(200); Serial.print( "." ); - /*if( i++ > 200 ) { // Try for 20 seconds. + if( i++ > 100 ) { // Try for 20 seconds. Log.error(F("WIFI: Failed to connect to wifi %d, aborting %s." CR), WiFi.status(), getIPAddress().c_str() ); - //WiFi.disconnect(); + WiFi.disconnect(); return connectedFlag; // Return to main that we have failed to connect. - }*/ + } } Serial.print( CR ); connectedFlag = true;