From 57f5816f63cdff7f6b52f540eb2dffdbc5029025 Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Tue, 11 Jan 2022 09:04:09 +0100 Subject: [PATCH] Enabled gyro temp --- html/config.htm | 11 ++++++++++- html/config.min.htm | 2 +- platformio.ini | 4 ++-- src/config.cpp | 7 ++++++- src/config.hpp | 11 ++++++++++- src/helper.cpp | 2 +- src/main.cpp | 14 +++++++------- src/tempsensor.cpp | 32 +++++++++++++------------------ src/tempsensor.hpp | 10 +++++++--- src/webserver.cpp | 25 +++++++++++++++--------- src_docs/source/compiling.rst | 2 -- src_docs/source/configuration.rst | 15 ++++++++++++--- src_docs/source/releases.rst | 2 ++ test/config.json | 1 + test/configure.py | 1 + 15 files changed, 89 insertions(+), 50 deletions(-) diff --git a/html/config.htm b/html/config.htm index 772368b..45a780e 100644 --- a/html/config.htm +++ b/html/config.htm @@ -283,6 +283,14 @@ +
+ +
+ +
+
@@ -332,7 +340,7 @@ var i = $("#sleep-interval").val() $("#sleep-interval-info").text( Math.floor(i/60) + " m " + (i%60) + " s" ) - if(i<300) + if(i>0 && i<300) showWarning("A sleep-interval of <300s will reduce battery life, consider using 900s"); else hideWarning(); @@ -380,6 +388,7 @@ $("#gravity-formula").val(cfg["gravity-formula"]); $("#temp-adjustment-value").val(cfg["temp-adjustment-value"]); $("#gravity-temp-adjustment").prop( "checked", cfg["gravity-temp-adjustment"] ); + $("#gyro-temp").prop( "checked", cfg["gyro-temp"] ); $("#gyro-calibration-data").text( cfg["gyro-calibration-data"]["ax"] + "," + cfg["gyro-calibration-data"]["ay"] + "," + cfg["gyro-calibration-data"]["az"] + "," + cfg["gyro-calibration-data"]["gx"] + "," + cfg["gyro-calibration-data"]["gy"] + "," + cfg["gyro-calibration-data"]["gz"] ); $("#battery").text(cfg["battery"] + " V"); $("#angle").text(cfg["angle"]); diff --git a/html/config.min.htm b/html/config.min.htm index 6de3b57..81931bc 100644 --- a/html/config.min.htm +++ b/html/config.min.htm @@ -1 +1 @@ -Beer Gravity Monitor

Temperature Format:




(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file +Beer Gravity Monitor

Temperature Format:




(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 06ad259..e78aa20 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,7 +23,6 @@ build_flags = -Wl,-Map,output.map -D BAUD=${common_env_data.monitor_speed} -D ACTIVATE_OTA - #-D USE_GYRO_TEMP # If this is enabled the DS18 will not be used, temp is read from the gyro. #-D DEBUG_ESP_HTTP_CLIENT #-D DEBUG_ESP_HTTP_SERVER #-D DEBUG_ESP_PORT=Serial @@ -66,9 +65,10 @@ extra_scripts = build_unflags = ${common_env_data.build_unflags} -D MAIN_DISABLE_LOGGING + -D WEB_DISABLE_LOGGING build_flags = ${common_env_data.build_flags} - #-D PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS + -D PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS #-D SKIP_SLEEPMODE -D DOUBLERESETDETECTOR_DEBUG=true -D COLLECT_PERFDATA # This option will collect runtime data for a few defined methods to measure time, dumped to serial and/or influxdb diff --git a/src/config.cpp b/src/config.cpp index 0c53ea4..3abecc2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -52,6 +52,7 @@ Config::Config() { setGravityTempAdj(false); gyroCalibration = {0, 0, 0, 0, 0, 0}; formulaData = {{0, 0, 0, 0, 0}, {1, 1, 1, 1, 1}}; + gyroTemp = false; saveNeeded = false; } @@ -79,6 +80,7 @@ void Config::createJson(DynamicJsonDocument& doc) { doc[CFG_PARAM_GRAVITY_FORMAT] = String(getGravityFormat()); doc[CFG_PARAM_TEMP_ADJ] = getTempSensorAdj(); doc[CFG_PARAM_GRAVITY_TEMP_ADJ] = isGravityTempAdj(); + doc[CFG_PARAM_GYRO_TEMP] = isGyroTemp(); JsonObject cal = doc.createNestedObject(CFG_PARAM_GYRO_CALIBRATION); cal["ax"] = gyroCalibration.ax; @@ -213,6 +215,8 @@ bool Config::loadFile() { setGravityFormula(doc[CFG_PARAM_GRAVITY_FORMULA]); if (!doc[CFG_PARAM_GRAVITY_TEMP_ADJ].isNull()) setGravityTempAdj(doc[CFG_PARAM_GRAVITY_TEMP_ADJ].as()); + if (!doc[CFG_PARAM_GYRO_TEMP].isNull()) + setGyroTemp(doc[CFG_PARAM_GYRO_TEMP].as()); if (!doc[CFG_PARAM_GRAVITY_FORMAT].isNull()) { String s = doc[CFG_PARAM_GRAVITY_FORMAT]; setGravityFormat(s.charAt(0)); @@ -253,7 +257,7 @@ bool Config::loadFile() { if (!doc[CFG_PARAM_FORMULA_DATA]["g4"].isNull()) formulaData.g[3] = doc[CFG_PARAM_FORMULA_DATA]["g4"].as(); if (!doc[CFG_PARAM_FORMULA_DATA]["g5"].isNull()) - formulaData.g[4] = doc[CFG_PARAM_FORMULA_DATA]["g5"]; + formulaData.g[4] = doc[CFG_PARAM_FORMULA_DATA]["g5"].as(); myConfig.debug(); saveNeeded = false; // Reset save flag @@ -303,6 +307,7 @@ void Config::debug() { Log.verbose(F("CFG : Gravity format; '%c'." CR), getGravityFormat()); Log.verbose(F("CFG : Gravity temp adj; %s." CR), isGravityTempAdj() ? "true" : "false"); + Log.verbose(F("CFG : Gyro temp; %s." CR), isGyroTemp() ? "true" : "false"); Log.verbose(F("CFG : Push brewfather; '%s'." CR), getBrewfatherPushUrl()); Log.verbose(F("CFG : Push http; '%s'." CR), getHttpPushUrl()); Log.verbose(F("CFG : Push http2; '%s'." CR), getHttpPushUrl2()); diff --git a/src/config.hpp b/src/config.hpp index 59c7278..7c9fb27 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -58,7 +58,7 @@ SOFTWARE. #define CFG_PARAM_PUSH_INFLUXDB2_BUCKET "influxdb2-bucket" // URL #define CFG_PARAM_PUSH_INFLUXDB2_AUTH "influxdb2-auth" // URL #define CFG_PARAM_SLEEP_INTERVAL "sleep-interval" // Sleep interval -#define CFG_PARAM_TEMPFORMAT "temp-format" // C or F +#define CFG_PARAM_TEMPFORMAT "temp-format" // C or F #define CFG_PARAM_VOLTAGEFACTOR \ "voltage-factor" // Factor to calculate the battery voltage #define CFG_PARAM_GRAVITY_FORMULA \ @@ -69,6 +69,8 @@ SOFTWARE. #define CFG_PARAM_TEMP_ADJ \ "temp-adjustment-value" // Correction value for temp sensor #define CFG_PARAM_GYRO_CALIBRATION "gyro-calibration-data" // READ ONLY +#define CFG_PARAM_GYRO_TEMP \ + "gyro-temp" // True/False. Use temp sensor in gyro (only in gravity mode) #define CFG_PARAM_FORMULA_DATA \ "formula-calculation-data" // Raw data for the formula calculation @@ -116,6 +118,7 @@ class Config { float voltageFactor; float tempSensorAdj; // This value will be added to the read sensor value int sleepInterval; + bool gyroTemp; // Experimental feature // Wifi Config String wifiSSID; @@ -155,6 +158,12 @@ class Config { saveNeeded = true; } + const bool isGyroTemp() { return gyroTemp; } + void setGyroTemp(bool b) { + gyroTemp = b; + saveNeeded = true; + } + const char* getOtaURL() { return otaURL.c_str(); } void setOtaURL(String s) { otaURL = s; diff --git a/src/helper.cpp b/src/helper.cpp index ca2516a..9785334 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -220,7 +220,7 @@ void PerfLogging::pushInflux() { "angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f", myGyro.getAngle(), myGyro.getLastGyroData().ax, myGyro.getLastGyroData().ay, myGyro.getLastGyroData().az, - myGyro.getSensorTempC(), myTempSensor.getTempC()); + myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp())); body += &buf[0]; // Log.notice(F("PERF: data %s." CR), body.c_str() ); diff --git a/src/main.cpp b/src/main.cpp index afc77c0..1872ae9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,7 +102,7 @@ void setup() { #if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING) // Add a delay so that serial is started. - // delay(3000); + // delay(3000); Log.verbose(F("Main: Reset reason %s." CR), ESP.getResetInfo().c_str()); #endif // Main startup @@ -207,12 +207,12 @@ bool loopReadGravity() { stableGyroMillis = millis(); // Reset timer LOG_PERF_START("loop-temp-read"); - float temp = myTempSensor.getTempC(); + float temp = myTempSensor.getTempC(myConfig.isGyroTemp()); LOG_PERF_STOP("loop-temp-read"); - float gravity = calculateGravity(angle, temp); - float corrGravity = gravityTemperatureCorrection( - gravity, temp, myConfig.getTempFormat()); + float gravity = calculateGravity(angle, temp); + float corrGravity = + gravityTemperatureCorrection(gravity, temp, myConfig.getTempFormat()); #if LOG_LEVEL == 6 && !defined(MAIN_DISABLE_LOGGING) Log.verbose(F("Main: Sensor values gyro angle=%F, temp=%F, gravity=%F, " @@ -249,6 +249,7 @@ void loopGravityOnInterval() { LOG_PERF_START("loop-gyro-read"); myGyro.read(); LOG_PERF_STOP("loop-gyro-read"); + myBatteryVoltage.read(); checkSleepMode(myGyro.getAngle(), myBatteryVoltage.getVoltage()); LOG_PERF_PUSH(); } @@ -263,7 +264,7 @@ void goToSleep(int sleepInterval) { Log.notice(F("MAIN: Entering deep sleep for %ds, run time %Fs, " "battery=%FV." CR), - sleepInterval, reduceFloatPrecision(runtime/1000, 2), volt); + sleepInterval, reduceFloatPrecision(runtime / 1000, 2), volt); LittleFS.end(); myGyro.enterSleep(); LOG_PERF_STOP("run-time"); @@ -281,7 +282,6 @@ void loop() { myWebServer.loop(); myWifi.loop(); loopGravityOnInterval(); - myBatteryVoltage.read(); break; case RunMode::gravityMode: diff --git a/src/tempsensor.cpp b/src/tempsensor.cpp index 9e0c723..d8f722a 100644 --- a/src/tempsensor.cpp +++ b/src/tempsensor.cpp @@ -22,8 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include +#include #include #include @@ -35,16 +35,14 @@ SOFTWARE. // float convertCtoF(float t) { return (t * 1.8) + 32.0; } -#if !defined(USE_GYRO_TEMP) OneWire myOneWire(D6); DallasTemperature mySensors(&myOneWire); #define TEMPERATURE_PRECISION 9 -#endif TempSensor myTempSensor; // -// Setup temp sensors +// Setup DS18B20 temp sensor. Doing setup is not that time consuming. // void TempSensor::setup() { #if defined(SIMULATE_TEMP) @@ -52,9 +50,6 @@ void TempSensor::setup() { return; #endif -#if defined(USE_GYRO_TEMP) - Log.notice(F("TSEN: Using temperature from gyro." CR)); -#else #if LOG_LEVEL == 6 && !defined(TSEN_DISABLE_LOGGING) Log.verbose(F("TSEN: Looking for temp sensors." CR)); #endif @@ -67,7 +62,6 @@ void TempSensor::setup() { #endif mySensors.setResolution(TEMPERATURE_PRECISION); } -#endif float t = myConfig.getTempSensorAdj(); @@ -89,21 +83,22 @@ void TempSensor::setup() { // // Retrieving value from sensor, value is in Celcius // -float TempSensor::getValue() { +float TempSensor::getValue(bool useGyro) { #if defined(SIMULATE_TEMP) return 21; #endif -#if defined(USE_GYRO_TEMP) - // When using the gyro temperature only the first read value will be accurate - // so we will use this for processing. - float c = myGyro.getInitialSensorTempC(); - hasSensor = true; - return c; + if (useGyro) { + // When using the gyro temperature only the first read value will be + // accurate so we will use this for processing. + float c = myGyro.getInitialSensorTempC(); #if LOG_LEVEL == 6 && !defined(TSEN_DISABLE_LOGGING) - Log.verbose(F("TSEN: Reciving temp value for gyro sensor %F C." CR), c); + Log.verbose(F("TSEN: Reciving temp value for gyro sensor %F C." CR), c); #endif -#else + hasSensor = true; + return c; + } + // If we dont have sensors just return 0 if (!mySensors.getDS18Count()) { Log.error(F("TSEN: No temperature sensors found. Skipping read." CR)); @@ -119,12 +114,11 @@ float TempSensor::getValue() { c = mySensors.getTempCByIndex(0); #if LOG_LEVEL == 6 && !defined(TSEN_DISABLE_LOGGING) - Log.verbose(F("TSEN: Reciving temp value for sensor %F C." CR), c); + Log.verbose(F("TSEN: Reciving temp value for DS18B20 sensor %F C." CR), c); #endif hasSensor = true; } return c; -#endif } // EOF diff --git a/src/tempsensor.hpp b/src/tempsensor.hpp index 471a349..112d2af 100644 --- a/src/tempsensor.hpp +++ b/src/tempsensor.hpp @@ -33,13 +33,17 @@ class TempSensor { bool hasSensor = false; float tempSensorAdjF = 0; float tempSensorAdjC = 0; - float getValue(); + float getValue(bool useGyro); public: void setup(); bool isSensorAttached() { return hasSensor; } - float getTempC() { return getValue() + tempSensorAdjC; } - float getTempF() { return convertCtoF(getValue()) + tempSensorAdjF; } + float getTempC(bool useGyro = false) { + return getValue(useGyro) + tempSensorAdjC; + } + float getTempF(bool useGyro = false) { + return convertCtoF(getValue(useGyro)) + tempSensorAdjF; + } }; // Global instance created diff --git a/src/webserver.cpp b/src/webserver.cpp index 560a2dc..923621a 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -41,7 +41,7 @@ extern bool sleepModeAlwaysSkip; void WebServer::webHandleDevice() { LOG_PERF_START("webserver-api-device"); #if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING) - Log.verbose(F("WEB : webServer callback for /api/config." CR)); + Log.verbose(F("WEB : webServer callback for /api/device." CR)); #endif DynamicJsonDocument doc(100); @@ -69,8 +69,10 @@ void WebServer::webHandleConfig() { DynamicJsonDocument doc(CFG_JSON_BUFSIZE); myConfig.createJson(doc); + doc[CFG_PARAM_PASS] = ""; // dont show the wifi password + double angle = myGyro.getAngle(); - double temp = myTempSensor.getTempC(); + double temp = myTempSensor.getTempC(myConfig.isGyroTemp()); double gravity = calculateGravity(angle, temp); doc[CFG_PARAM_ANGLE] = reduceFloatPrecision(angle); @@ -137,9 +139,9 @@ void WebServer::webHandleUploadFile() { } #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"); + Log.verbose(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) { @@ -213,7 +215,7 @@ void WebServer::webHandleStatus() { DynamicJsonDocument doc(256); double angle = myGyro.getAngle(); - double temp = myTempSensor.getTempC(); + double temp = myTempSensor.getTempC(myConfig.isGyroTemp()); double gravity = calculateGravity(angle, temp); doc[CFG_PARAM_ID] = myConfig.getID(); @@ -225,7 +227,8 @@ void WebServer::webHandleStatus() { else doc[CFG_PARAM_GRAVITY] = reduceFloatPrecision(gravity, 4); doc[CFG_PARAM_TEMP_C] = reduceFloatPrecision(temp, 1); - doc[CFG_PARAM_TEMP_F] = reduceFloatPrecision(myTempSensor.getTempF(), 1); + doc[CFG_PARAM_TEMP_F] = + reduceFloatPrecision(myTempSensor.getTempF(myConfig.isGyroTemp()), 1); doc[CFG_PARAM_BATTERY] = reduceFloatPrecision(myBatteryVoltage.getVoltage()); doc[CFG_PARAM_TEMPFORMAT] = String(myConfig.getTempFormat()); doc[CFG_PARAM_SLEEP_MODE] = sleepModeAlwaysSkip; @@ -411,15 +414,18 @@ void WebServer::webHandleConfigHardware() { } #if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING) - Log.verbose(F("WEB : vf=%s, tempadj=%s, ota=%s." CR), + Log.verbose(F("WEB : vf=%s, tempadj=%s, ota=%s gyrotemp=%s." CR), server->arg(CFG_PARAM_VOLTAGEFACTOR).c_str(), server->arg(CFG_PARAM_TEMP_ADJ).c_str(), - server->arg(CFG_PARAM_OTA).c_str()); + server->arg(CFG_PARAM_OTA).c_str(), + server->arg(CFG_PARAM_GYRO_TEMP).c_str()); #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()); + myConfig.setGyroTemp( + server->arg(CFG_PARAM_GYRO_TEMP).equalsIgnoreCase("on") ? true : false); myConfig.saveFile(); server->sendHeader("Location", "/config.htm#collapseFour", true); server->send(302, "text/plain", "Hardware config updated"); @@ -559,6 +565,7 @@ void WebServer::webHandleFormulaWrite() { server->send(302, "text/plain", "Formula updated"); LOG_PERF_STOP("webserver-api-formula-write"); } + // // Helper function to check if files exist on file system. // diff --git a/src_docs/source/compiling.rst b/src_docs/source/compiling.rst index b0a2ee9..d8dbfbf 100644 --- a/src_docs/source/compiling.rst +++ b/src_docs/source/compiling.rst @@ -82,8 +82,6 @@ This is a list of C++ defines that is used to enable/disable functions in the co - description * - ACTIVATE_OTA - Enables the OTA functionallity in the code - * - USE_GYRO_TEMP - - Uses temperature from gyro instead of DS18B20 (experimental) * - SKIP_SLEEPMODE - THe device never goes into sleep mode, useful when developing. * - CFG_DISABLE_LOGGING diff --git a/src_docs/source/configuration.rst b/src_docs/source/configuration.rst index 91581e3..7df901f 100644 --- a/src_docs/source/configuration.rst +++ b/src_docs/source/configuration.rst @@ -165,7 +165,13 @@ Hardware Settings * **Temperature correction:** - This value will be added to the temperature reading (negative value will reduce temperature reading). + This value will be added to the temperature reading (negative value will reduce temperature reading). This is applied + when the device starts. So changing this will not take affect until the device is restarted. + +* **Gyro Temperature:** + + Enable this feature will use the temp sensor i the gyro instead of the DS18B20, the benefit is shorter run time and + longer battery life (this is an experimental feature). * **OTA URL:** @@ -235,6 +241,7 @@ Other parameters are the same as in the configuration guide. "gravity-format": "G", "temp-adjustment-value": 0, "gravity-temp-adjustment": false, + "gyro-temp": true, "gyro-calibration-data": { "ax": -330, "ay": -2249, @@ -367,8 +374,8 @@ Used to update gravity settings via an HTTP POST command. Payload is in JSON for } -POST: /api/config/gravity -========================= +POST: /api/config/hardware +========================== Used to update hardware settings via an HTTP POST command. Payload is in JSON format. @@ -378,6 +385,7 @@ Used to update hardware settings via an HTTP POST command. Payload is in JSON fo "id": "ee1bfc", "voltage-factor": 1.59, "temp-adjustment": 0, + "gyro-temp": "off", "ota-url": "http://192.168.1.50/firmware/gravmon/" } @@ -461,6 +469,7 @@ present or the API call will fail. json = { "id": id, "voltage-factor": 1.59, # Default value for voltage calculation "temp-adjustment": 0, # If temp sensor needs to be corrected + "gyro-temp": true, # Use the temp sensor in the gyro instead "ota-url": "" # if the device should seach for a new update when active } set_config( url, json ) diff --git a/src_docs/source/releases.rst b/src_docs/source/releases.rst index 2fc2a08..569649b 100644 --- a/src_docs/source/releases.rst +++ b/src_docs/source/releases.rst @@ -13,6 +13,8 @@ This is features for the next release. * Refactored main.cpp to make it easier to read * Tested runtime performance * Improved documentation +* Added warning on config page when sleep is <300 +* Enabled selection of gyro temperature sensor under Hardware settings. v0.5.0 ------ diff --git a/test/config.json b/test/config.json index 8e94072..31bfe9d 100644 --- a/test/config.json +++ b/test/config.json @@ -16,6 +16,7 @@ "gravity-format": "G", "temp-adjustment-value": 0, "gravity-temp-adjustment": false, + "gyro-temp": true, "gyro-calibration-data": { "ax": -330, "ay": -2249, diff --git a/test/configure.py b/test/configure.py index 7ec5fa2..23eaf09 100644 --- a/test/configure.py +++ b/test/configure.py @@ -58,6 +58,7 @@ url = "http://" + host + "/api/config/hardware" json = { "id": id, "voltage-factor": 1.59, # Default value for voltage calculation "temp-adjustment": 0, # If temp sensor needs to be corrected + "gyro-temp": "off", # Use the temp sensor in the gyro "ota-url": "" # if the device should seach for a new update when active } set_config( url, json )