diff --git a/html/config.htm b/html/config.htm index 9ca826c..73f063a 100644 --- a/html/config.htm +++ b/html/config.htm @@ -449,6 +449,96 @@ + +
+

+ +

+
+
+ +
+ + +
+ +
+ +
+
(10-100) - default 50 - number of reads for angle
+
+ +
+ +
+ +
+
(50-1000) - default 500 - size of movement
+
+ +
+ +
+ +
+
(1 - 10) - default 1.6 SG - max deviation value when checking formula
+
+ +
+ +
+ +
+ +
+
(0 - 5) skips a number of sleep cycles before pushing data
+
+ +
+ +
+ +
+
(0 - 5) skips a number of sleep cycles before pushing data
+
+ +
+ +
+ +
+
(0 - 5) skips a number of sleep cycles before pushing data
+
+ +
+ +
+ +
+
(0 - 5) skips a number of sleep cycles before pushing data
+
+ +
+ +
+ +
+
(0 - 5) skips a number of sleep cycles before pushing data
+
+ +
+
+ +
+
+
+ +
+
+
+ @@ -610,6 +700,35 @@ $("#push-btn2").prop("disabled", b); $("#format-btn2").prop("disabled", b); $("#test-btn2").prop("disabled", b); + $("#advanced-btn").prop("disabled", b); + } + + // Get the advanced values from the API + function getAdvancedConfig() { + setButtonDisabled( true ); + + var url = "/api/config/advanced"; + //var url = "/test/adv.json"; + $('#spinner').show(); + $.getJSON(url, function (cfg) { + console.log( cfg ); + + $("#gyro-read-count").val(cfg["gyro-read-count"]); + $("#gyro-moving-threashold").val(cfg["gyro-moving-threashold"]); + $("#formula-max-deviation").val(cfg["formula-max-deviation"]); + $("#int-http1").val(cfg["int-http1"]); + $("#int-http2").val(cfg["int-http2"]); + $("#int-http3").val(cfg["int-http3"]); + $("#int-influx").val(cfg["int-influx"]); + $("#int-mqtt").val(cfg["int-mqtt"]); + }) + .fail(function () { + showError('Unable to get data from the device.'); + }) + .always(function() { + $('#spinner').hide(); + setButtonDisabled( false ); + }); } // Get the configuration values from the API @@ -632,6 +751,7 @@ $("#id3").val(cfg["id"]); $("#id4").val(cfg["id"]); $("#id5").val(cfg["id"]); + $("#id6").val(cfg["id"]); $("#mdns").val(cfg["mdns"]); if( cfg["temp-format"] == "C" ) $("#temp-format-c").click(); else $("#temp-format-f").click(); @@ -671,9 +791,10 @@ showError('Unable to get data from the device.'); }) .always(function() { - $('#spinner').hide(); - setButtonDisabled( false ); + //$('#spinner').hide(); + //setButtonDisabled( false ); updateSleepInfo(); + getAdvancedConfig(); }); } diff --git a/html/config.min.htm b/html/config.min.htm index 082c38e..65d02ca 100644 --- a/html/config.min.htm +++ b/html/config.min.htm @@ -1 +1 @@ -Beer Gravity Monitor

Temperature Format:


Gravity Format:

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

Temperature Format:


Gravity Format:

(10-100) - default 50 - number of reads for angle
(50-1000) - default 500 - size of movement
(1 - 10) - default 1.6 SG - max deviation value when checking formula

(0 - 5) skips a number of sleep cycles before pushing data
(0 - 5) skips a number of sleep cycles before pushing data
(0 - 5) skips a number of sleep cycles before pushing data
(0 - 5) skips a number of sleep cycles before pushing data
(0 - 5) skips a number of sleep cycles before pushing data
(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file diff --git a/src/calc.cpp b/src/calc.cpp index 0ce36a8..eac7985 100644 --- a/src/calc.cpp +++ b/src/calc.cpp @@ -95,7 +95,7 @@ int createFormula(RawFormulaData &fd, char *formulaBuffer, double dev = (g - fd.g[i]) < 0 ? (fd.g[i] - g) : (g - fd.g[i]); // If the deviation is more than 2 degress we mark it as failed. - if (dev * 1000 > myHardwareConfig.getMaxFormulaCreationDeviation()) { + if (dev * 1000 > myAdvancedConfig.getMaxFormulaCreationDeviation()) { char s[20]; snprintf(&s[0], sizeof(s), "%.8f", dev); Log.error(F("CALC: Deviation to large: %s" CR), &s[0]); diff --git a/src/calc.hpp b/src/calc.hpp index fa19c3b..19bd786 100644 --- a/src/calc.hpp +++ b/src/calc.hpp @@ -34,7 +34,7 @@ double calculateGravity(double angle, double tempC, const char *tempFormula = 0); double gravityTemperatureCorrectionC( double gravity, double tempC, - double calTempC = myHardwareConfig.getDefaultCalibrationTemp()); + double calTempC = myAdvancedConfig.getDefaultCalibrationTemp()); int createFormula(RawFormulaData &fd, char *formulaBuffer, int formulaBufferSize, int order); diff --git a/src/config.cpp b/src/config.cpp index d0da370..1738d9e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -26,7 +26,7 @@ SOFTWARE. #include Config myConfig; -HardwareConfig myHardwareConfig; +AdvancedConfig myAdvancedConfig; // // Create the config class with default settings. @@ -359,7 +359,7 @@ void Config::checkFileSystem() { // // Save json document to file // -bool HardwareConfig::saveFile() { +bool AdvancedConfig::saveFile() { #if LOG_LEVEL == 6 && !defined(DISABLE_LOGGING) Log.verbose(F("CFG : Saving hardware configuration to file." CR)); #endif @@ -380,6 +380,11 @@ bool HardwareConfig::saveFile() { doc[PARAM_HW_FORMULA_DEVIATION] = this->getMaxFormulaCreationDeviation(); doc[PARAM_HW_WIFI_PORTALTIMEOUT] = this->getWifiPortalTimeout(); doc[PARAM_HW_FORMULA_CALIBRATION_TEMP] = this->getDefaultCalibrationTemp(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP1] = this->getPushIntervalHttp1(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP2] = this->getPushIntervalHttp2(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP3] = this->getPushIntervalHttp3(); + doc[PARAM_HW_PUSH_INTERVAL_INFLUX] = this->getPushIntervalInflux(); + doc[PARAM_HW_PUSH_INTERVAL_MQTT] = this->getPushIntervalMqtt(); #if LOG_LEVEL == 6 && !defined(DISABLE_LOGGING) serializeJson(doc, Serial); @@ -397,7 +402,7 @@ bool HardwareConfig::saveFile() { // // Load config file from disk // -bool HardwareConfig::loadFile() { +bool AdvancedConfig::loadFile() { #if LOG_LEVEL == 6 && !defined(DISABLE_LOGGING) Log.verbose(F("CFG : Loading hardware configuration from file." CR)); #endif @@ -454,6 +459,16 @@ bool HardwareConfig::loadFile() { this->setWifiPortalTimeout(doc[PARAM_HW_WIFI_PORTALTIMEOUT].as()); if (!doc[PARAM_HW_PUSH_TIMEOUT].isNull()) this->setPushTimeout(doc[PARAM_HW_PUSH_TIMEOUT].as()); + if (!doc[PARAM_HW_PUSH_INTERVAL_HTTP1].isNull()) + this->setPushIntervalHttp1(doc[PARAM_HW_PUSH_INTERVAL_HTTP1].as()); + if (!doc[PARAM_HW_PUSH_INTERVAL_HTTP2].isNull()) + this->setPushIntervalHttp2(doc[PARAM_HW_PUSH_INTERVAL_HTTP2].as()); + if (!doc[PARAM_HW_PUSH_INTERVAL_HTTP3].isNull()) + this->setPushIntervalHttp3(doc[PARAM_HW_PUSH_INTERVAL_HTTP3].as()); + if (!doc[PARAM_HW_PUSH_INTERVAL_INFLUX].isNull()) + this->setPushIntervalInflux(doc[PARAM_HW_PUSH_INTERVAL_INFLUX].as()); + if (!doc[PARAM_HW_PUSH_INTERVAL_MQTT].isNull()) + this->setPushIntervalMqtt(doc[PARAM_HW_PUSH_INTERVAL_MQTT].as()); Log.notice(F("CFG : Configuration file " CFG_HW_FILENAME " loaded." CR)); return true; diff --git a/src/config.hpp b/src/config.hpp index 83c05a1..9f5346b 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -54,7 +54,7 @@ struct RawFormulaData { double g[FORMULA_DATA_SIZE]; }; -class HardwareConfig { +class AdvancedConfig { private: int _wifiPortalTimeout = 120; float _maxFormulaCreationDeviation = 1.6; @@ -63,27 +63,55 @@ class HardwareConfig { int _gyroReadCount = 50; int _gyroReadDelay = 3150; // us, empirical, to hold sampling to 200 Hz int _pushTimeout = 10; // seconds + int _pushIntervalHttp1 = 0; + int _pushIntervalHttp2 = 0; + int _pushIntervalHttp3 = 0; + int _pushIntervalInflux = 0; + int _pushIntervalMqtt = 0; public: int getWifiPortalTimeout() { return _wifiPortalTimeout; } void setWifiPortalTimeout(int t) { _wifiPortalTimeout = t; } + float getMaxFormulaCreationDeviation() { return _maxFormulaCreationDeviation; } void setMaxFormulaCreationDeviation(float f) { _maxFormulaCreationDeviation = f; } + float getDefaultCalibrationTemp() { return _defaultCalibrationTemp; } void SetDefaultCalibrationTemp(float t) { _defaultCalibrationTemp = t; } + int getGyroSensorMovingThreashold() { return _gyroSensorMovingThreashold; } void setGyroSensorMovingThreashold(int t) { _gyroSensorMovingThreashold = t; } + int getGyroReadCount() { return _gyroReadCount; } void setGyroReadCount(int c) { _gyroReadCount = c; } + int getGyroReadDelay() { return _gyroReadDelay; } void setGyroReadDelay(int d) { _gyroReadDelay = d; } + int getPushTimeout() { return _pushTimeout; } void setPushTimeout(int t) { _pushTimeout = t; } + int getPushIntervalHttp1() { return _pushIntervalHttp1; } + void setPushIntervalHttp1(int t) { _pushIntervalHttp1 = t; } + + int getPushIntervalHttp2() { return _pushIntervalHttp2; } + void setPushIntervalHttp2(int t) { _pushIntervalHttp2 = t; } + + int getPushIntervalHttp3() { return _pushIntervalHttp3; } + void setPushIntervalHttp3(int t) { _pushIntervalHttp3 = t; } + + int getPushIntervalInflux() { return _pushIntervalInflux; } + void setPushIntervalInflux(int t) { _pushIntervalInflux = t; } + + int getPushIntervalMqtt() { return _pushIntervalMqtt; } + void setPushIntervalMqtt(int t) { _pushIntervalMqtt = t; } + + bool isPushIntervalActive() { return (_pushIntervalHttp1+_pushIntervalHttp2+_pushIntervalHttp3+_pushIntervalInflux+_pushIntervalMqtt) == 0 ? false : true; } + bool saveFile(); bool loadFile(); }; @@ -379,7 +407,7 @@ class Config { }; extern Config myConfig; -extern HardwareConfig myHardwareConfig; +extern AdvancedConfig myAdvancedConfig; #endif // SRC_CONFIG_HPP_ diff --git a/src/gyro.cpp b/src/gyro.cpp index b49ade3..86354bd 100644 --- a/src/gyro.cpp +++ b/src/gyro.cpp @@ -217,7 +217,7 @@ bool GyroSensor::isSensorMoving(RawGyroData &raw) { #endif int x = abs(raw.gx), y = abs(raw.gy), z = abs(raw.gz); - int threashold = myHardwareConfig.getGyroSensorMovingThreashold(); + int threashold = myAdvancedConfig.getGyroSensorMovingThreashold(); if (x > threashold || y > threashold || z > threashold) { Log.notice(F("GYRO: Movement detected (%d)\t%d\t%d\t%d." CR), threashold, x, @@ -239,8 +239,8 @@ bool GyroSensor::read() { if (!_sensorConnected) return false; readSensor( - _lastGyroData, myHardwareConfig.getGyroReadCount(), - myHardwareConfig.getGyroReadDelay()); // Last param is unused if + _lastGyroData, myAdvancedConfig.getGyroReadCount(), + myAdvancedConfig.getGyroReadDelay()); // Last param is unused if // GYRO_USE_INTERRUPT is defined. // If the sensor is unstable we return false to signal we dont have valid diff --git a/src/main.cpp b/src/main.cpp index af69136..8cbc1b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -138,7 +138,7 @@ void setup() { myConfig.checkFileSystem(); myConfig.loadFile(); myWifi.init(); - myHardwareConfig.loadFile(); + myAdvancedConfig.loadFile(); LOG_PERF_STOP("main-config-load"); // Setup watchdog diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 8632050..32949c2 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -32,6 +32,74 @@ SOFTWARE. #include #include +#define PUSHINT_FILENAME "/push.dat" + +// +// Decrease counters +// +void PushIntervalTracker::update(const int index, const int defaultValue) { + if (_counters[index] <= 0) + _counters[index] = defaultValue; + else + _counters[index]--; +} + +// +// Load data from file +// +void PushIntervalTracker::load() { + File intFile = LittleFS.open(PUSHINT_FILENAME, "r"); + int i = 0; + + if (intFile) { + String line = intFile.readStringUntil('\n'); + Log.notice(F("PUSH: Read interval tracker %s." CR), line.c_str()); + + char temp[80]; + char *s, *p = &temp[0]; + int i = 0; + + snprintf(&temp[0], sizeof(temp), "%s", line.c_str()); + while ((s = strtok_r(p, ":", &p)) != NULL) { + _counters[i++] = atoi(s); + } + + intFile.close(); + } + +#if !defined(PUSH_DISABLE_LOGGING) + Log.verbose(F("PUSH: Parsed trackers: %d:%d:%d:%d:%d." CR), _counters[0], _counters[1], _counters[2], _counters[3], _counters[4] ); +#endif +} + +// +// Update and save counters +// +void PushIntervalTracker::save() { + update(0, myAdvancedConfig.getPushIntervalHttp1()); + update(1, myAdvancedConfig.getPushIntervalHttp2()); + update(2, myAdvancedConfig.getPushIntervalHttp3()); + update(3, myAdvancedConfig.getPushIntervalInflux()); + update(4, myAdvancedConfig.getPushIntervalMqtt()); + + // If this feature is disabled we skip saving the file + if (!myAdvancedConfig.isPushIntervalActive()) { +#if !defined(PUSH_DISABLE_LOGGING) + Log.notice(F("PUSH: Variabled push interval disabled." CR)); +#endif + LittleFS.remove(PUSHINT_FILENAME); + } else { + Log.notice(F("PUSH: Variabled push interval enabled, updating counters." CR)); + File intFile = LittleFS.open(PUSHINT_FILENAME, "w"); + + if (intFile) { + // Format=http1:http2:http3:influx:mqtt + intFile.printf("%d:%d:%d:%d:%d\n", _counters[0], _counters[1], _counters[2], _counters[3], _counters[4] ); + intFile.close(); + } + } +} + // // Send the data to targets // @@ -44,35 +112,40 @@ void PushTarget::sendAll(float angle, float gravitySG, float corrGravitySG, TemplatingEngine engine; engine.initialize(angle, gravitySG, corrGravitySG, tempC, runTime); - if (myConfig.isHttpActive()) { + PushIntervalTracker intDelay; + intDelay.load(); + + if (myConfig.isHttpActive() && intDelay.useHttp1()) { LOG_PERF_START("push-http"); sendHttpPost(engine, myConfig.isHttpSSL(), 0); LOG_PERF_STOP("push-http"); } - if (myConfig.isHttp2Active()) { + if (myConfig.isHttp2Active() && intDelay.useHttp2()) { LOG_PERF_START("push-http2"); sendHttpPost(engine, myConfig.isHttp2SSL(), 1); LOG_PERF_STOP("push-http2"); } - if (myConfig.isHttp3Active()) { + if (myConfig.isHttp3Active() && intDelay.useHttp3()) { LOG_PERF_START("push-http3"); sendHttpGet(engine, myConfig.isHttp3SSL()); LOG_PERF_STOP("push-http3"); } - if (myConfig.isInfluxDb2Active()) { + if (myConfig.isInfluxDb2Active() && intDelay.useInflux()) { LOG_PERF_START("push-influxdb2"); sendInfluxDb2(engine, myConfig.isInfluxSSL()); LOG_PERF_STOP("push-influxdb2"); } - if (myConfig.isMqttActive()) { + if (myConfig.isMqttActive() && intDelay.useMqtt()) { LOG_PERF_START("push-mqtt"); sendMqtt(engine, myConfig.isMqttSSL()); LOG_PERF_STOP("push-mqtt"); } + + intDelay.save(); } // @@ -109,11 +182,11 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine, bool isSecure) { #endif _httpSecure.begin(_wifiSecure, serverPath); - _httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); _lastCode = _httpSecure.POST(doc); } else { _http.begin(_wifi, serverPath); - _http.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); _lastCode = _http.POST(doc); } @@ -209,7 +282,7 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, #endif _httpSecure.begin(_wifiSecure, serverPath); - _httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); if (index == 0) { addHttpHeader(_httpSecure, myConfig.getHttpHeader(0)); @@ -222,7 +295,7 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, _lastCode = _httpSecure.POST(doc); } else { _http.begin(_wifi, serverPath); - _http.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); if (index == 0) { addHttpHeader(_http, myConfig.getHttpHeader(0)); @@ -291,11 +364,11 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) { #endif _httpSecure.begin(_wifiSecure, serverPath); - _httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); _lastCode = _httpSecure.GET(); } else { _http.begin(_wifi, serverPath); - _http.setTimeout(myHardwareConfig.getPushTimeout() * 1000); + _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); _lastCode = _http.GET(); } @@ -357,7 +430,7 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { #endif // Send MQQT message(s) - mqtt.setTimeout(myHardwareConfig.getPushTimeout()); // 10 seconds timeout + mqtt.setTimeout(myAdvancedConfig.getPushTimeout()); // 10 seconds timeout int lines = 1; // Find out how many lines are in the document. Each line is one diff --git a/src/pushtarget.hpp b/src/pushtarget.hpp index 294debf..6bfde2b 100644 --- a/src/pushtarget.hpp +++ b/src/pushtarget.hpp @@ -65,6 +65,21 @@ class PushTarget { bool getLastSuccess() { return _lastSuccess; } }; +class PushIntervalTracker { + private: + int _counters[5] = { 0, 0, 0, 0, 0 }; + void update(const int index, const int defaultValue); + + public: + bool useHttp1() { return _counters[0] == 0 ? true : false; } + bool useHttp2() { return _counters[1] == 0 ? true : false; } + bool useHttp3() { return _counters[2] == 0 ? true : false; } + bool useInflux() { return _counters[3] == 0 ? true : false; } + bool useMqtt() { return _counters[4] == 0 ? true : false; } + void load(); + void save(); +}; + #endif // SRC_PUSHTARGET_HPP_ // EOF diff --git a/src/resources.hpp b/src/resources.hpp index 9bb3388..51f53a7 100644 --- a/src/resources.hpp +++ b/src/resources.hpp @@ -81,6 +81,11 @@ SOFTWARE. #define PARAM_HW_FORMULA_CALIBRATION_TEMP "formula-calibration-temp" #define PARAM_HW_WIFI_PORTALTIMEOUT "wifi-portaltimeout" #define PARAM_HW_PUSH_TIMEOUT "push-timeout" +#define PARAM_HW_PUSH_INTERVAL_HTTP1 "int-http1" +#define PARAM_HW_PUSH_INTERVAL_HTTP2 "int-http2" +#define PARAM_HW_PUSH_INTERVAL_HTTP3 "int-http3" +#define PARAM_HW_PUSH_INTERVAL_INFLUX "int-influx" +#define PARAM_HW_PUSH_INTERVAL_MQTT "int-mqtt" #define PARAM_FORMAT_HTTP1 "http-1" #define PARAM_FORMAT_HTTP2 "http-2" #define PARAM_FORMAT_HTTP3 "http-3" diff --git a/src/webserver.cpp b/src/webserver.cpp index debcf12..bee61a5 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -622,18 +622,18 @@ void WebServerHandler::webHandleConfigHardware() { } // -// Update device parameters. +// Update advanced settings. // -void WebServerHandler::webHandleDeviceParam() { - LOG_PERF_START("webserver-api-device-param"); +void WebServerHandler::webHandleConfigAdvancedWrite() { + LOG_PERF_START("webserver-api-config-advanced"); String id = _server->arg(PARAM_ID); - Log.notice(F("WEB : webServer callback for /api/device/param(post)." CR)); + Log.notice(F("WEB : webServer callback for /api/config/advaced(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-device-param"); + LOG_PERF_STOP("webserver-api-config-advanced"); return; } @@ -641,41 +641,61 @@ void WebServerHandler::webHandleDeviceParam() { Log.verbose(F("WEB : %s." CR), getRequestArguments().c_str()); #endif - for (int i = 0; i < _server->args(); i++) { - String s = _server->arg(i); + if (_server->hasArg(PARAM_HW_GYRO_READ_COUNT)) + myAdvancedConfig.setGyroReadCount(_server->arg(PARAM_HW_GYRO_READ_COUNT).toInt()); + if (_server->hasArg(PARAM_HW_GYRO_READ_DELAY)) + myAdvancedConfig.setGyroReadDelay(_server->arg(PARAM_HW_GYRO_READ_DELAY).toInt()); + if (_server->hasArg(PARAM_HW_GYRO_MOVING_THREASHOLD)) + myAdvancedConfig.setGyroSensorMovingThreashold(_server->arg(PARAM_HW_GYRO_MOVING_THREASHOLD).toInt()); + if (_server->hasArg(PARAM_HW_FORMULA_DEVIATION)) + myAdvancedConfig.setMaxFormulaCreationDeviation(_server->arg(PARAM_HW_FORMULA_DEVIATION).toFloat()); + if (_server->hasArg(PARAM_HW_FORMULA_CALIBRATION_TEMP)) + myAdvancedConfig.SetDefaultCalibrationTemp(_server->arg(PARAM_HW_FORMULA_CALIBRATION_TEMP).toFloat()); + if (_server->hasArg(PARAM_HW_WIFI_PORTALTIMEOUT)) + myAdvancedConfig.setWifiPortalTimeout(_server->arg(PARAM_HW_WIFI_PORTALTIMEOUT).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_TIMEOUT)) + myAdvancedConfig.setPushTimeout(_server->arg(PARAM_HW_PUSH_TIMEOUT).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_INTERVAL_HTTP1)) + myAdvancedConfig.setPushIntervalHttp1(_server->arg(PARAM_HW_PUSH_INTERVAL_HTTP1).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_INTERVAL_HTTP2)) + myAdvancedConfig.setPushIntervalHttp2(_server->arg(PARAM_HW_PUSH_INTERVAL_HTTP2).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_INTERVAL_HTTP3)) + myAdvancedConfig.setPushIntervalHttp3(_server->arg(PARAM_HW_PUSH_INTERVAL_HTTP3).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_INTERVAL_INFLUX)) + myAdvancedConfig.setPushIntervalInflux(_server->arg(PARAM_HW_PUSH_INTERVAL_INFLUX).toInt()); + if (_server->hasArg(PARAM_HW_PUSH_INTERVAL_MQTT)) + myAdvancedConfig.setPushIntervalMqtt(_server->arg(PARAM_HW_PUSH_INTERVAL_MQTT).toInt()); - if (_server->argName(i).equalsIgnoreCase(PARAM_HW_GYRO_READ_COUNT)) - myHardwareConfig.setGyroReadCount(s.toInt()); - else if (_server->argName(i).equalsIgnoreCase(PARAM_HW_GYRO_READ_DELAY)) - myHardwareConfig.setGyroReadDelay(s.toInt()); - else if (_server->argName(i).equalsIgnoreCase( - PARAM_HW_GYRO_MOVING_THREASHOLD)) - myHardwareConfig.setGyroSensorMovingThreashold(s.toInt()); - else if (_server->argName(i).equalsIgnoreCase(PARAM_HW_FORMULA_DEVIATION)) - myHardwareConfig.setMaxFormulaCreationDeviation(s.toFloat()); - else if (_server->argName(i).equalsIgnoreCase( - PARAM_HW_FORMULA_CALIBRATION_TEMP)) - myHardwareConfig.SetDefaultCalibrationTemp(s.toFloat()); - else if (_server->argName(i).equalsIgnoreCase(PARAM_HW_WIFI_PORTALTIMEOUT)) - myHardwareConfig.setWifiPortalTimeout(s.toInt()); - else if (_server->argName(i).equalsIgnoreCase(PARAM_HW_PUSH_TIMEOUT)) - myHardwareConfig.setPushTimeout(s.toInt()); - } + myAdvancedConfig.saveFile(); + _server->sendHeader("Location", "/config.htm#collapseAdvanced", true); + _server->send(302, "text/plain", "Advanced config updated"); + LOG_PERF_STOP("webserver-api-config-advanced"); +} - myHardwareConfig.saveFile(); - // Return the current configuration. +// +// Read advanced settings +// +void WebServerHandler::webHandleConfigAdvancedRead() { + LOG_PERF_START("webserver-api-config-advanced"); + Log.notice(F("WEB : webServer callback for /api/config/advanced(get)." CR)); + DynamicJsonDocument doc(512); - doc[PARAM_HW_GYRO_READ_COUNT] = myHardwareConfig.getGyroReadCount(); - doc[PARAM_HW_GYRO_READ_DELAY] = myHardwareConfig.getGyroReadDelay(); + doc[PARAM_HW_GYRO_READ_COUNT] = myAdvancedConfig.getGyroReadCount(); + doc[PARAM_HW_GYRO_READ_DELAY] = myAdvancedConfig.getGyroReadDelay(); doc[PARAM_HW_GYRO_MOVING_THREASHOLD] = - myHardwareConfig.getGyroSensorMovingThreashold(); + myAdvancedConfig.getGyroSensorMovingThreashold(); doc[PARAM_HW_FORMULA_DEVIATION] = - myHardwareConfig.getMaxFormulaCreationDeviation(); - doc[PARAM_HW_WIFI_PORTALTIMEOUT] = myHardwareConfig.getWifiPortalTimeout(); + myAdvancedConfig.getMaxFormulaCreationDeviation(); + doc[PARAM_HW_WIFI_PORTALTIMEOUT] = myAdvancedConfig.getWifiPortalTimeout(); doc[PARAM_HW_FORMULA_CALIBRATION_TEMP] = - myHardwareConfig.getDefaultCalibrationTemp(); + myAdvancedConfig.getDefaultCalibrationTemp(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP1] = myAdvancedConfig.getPushIntervalHttp1(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP2] = myAdvancedConfig.getPushIntervalHttp2(); + doc[PARAM_HW_PUSH_INTERVAL_HTTP3] = myAdvancedConfig.getPushIntervalHttp3(); + doc[PARAM_HW_PUSH_INTERVAL_INFLUX] = myAdvancedConfig.getPushIntervalInflux(); + doc[PARAM_HW_PUSH_INTERVAL_MQTT] = myAdvancedConfig.getPushIntervalMqtt(); #if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING) serializeJson(doc, Serial); @@ -686,7 +706,7 @@ void WebServerHandler::webHandleDeviceParam() { out.reserve(512); serializeJson(doc, out); _server->send(200, "application/json", out.c_str()); - LOG_PERF_STOP("webserver-api-device-param"); + LOG_PERF_STOP("webserver-api-config-advanced"); } // @@ -1245,9 +1265,12 @@ bool WebServerHandler::setupWebServer() { _server->on("/api/config/format", HTTP_POST, std::bind(&WebServerHandler::webHandleConfigFormatWrite, this)); // Change template formats - _server->on("/api/device/param", HTTP_GET, - std::bind(&WebServerHandler::webHandleDeviceParam, - this)); // Change device params + _server->on("/api/config/advanced", HTTP_GET, + std::bind(&WebServerHandler::webHandleConfigAdvancedRead, + this)); // Read advanced settings + _server->on("/api/config/advanced", HTTP_POST, + std::bind(&WebServerHandler::webHandleConfigAdvancedWrite, + this)); // Change advanced params _server->on("/api/test/push", HTTP_GET, std::bind(&WebServerHandler::webHandleTestPush, this)); // diff --git a/src/webserver.hpp b/src/webserver.hpp index 2594f68..6a68ac8 100644 --- a/src/webserver.hpp +++ b/src/webserver.hpp @@ -60,6 +60,8 @@ class WebServerHandler { void webHandleConfig(); void webHandleFormulaWrite(); void webHandleFormulaRead(); + void webHandleConfigAdvancedRead(); + void webHandleConfigAdvancedWrite(); void webHandleConfigHardware(); void webHandleConfigGravity(); void webHandleConfigPush(); @@ -74,7 +76,6 @@ class WebServerHandler { void webHandleCalibrate(); void webHandleUploadFile(); void webHandleUpload(); - void webHandleDeviceParam(); void webHandlePageNotFound(); String readFile(String fname); diff --git a/src/wifi.cpp b/src/wifi.cpp index 7fedb92..ee3678a 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -137,7 +137,7 @@ void WifiConnection::startPortal() { myWifiManager->setMinimumSignalQuality(-1); myWifiManager->setConfigPortalChannel(0); myWifiManager->setConfigPortalTimeout( - myHardwareConfig.getWifiPortalTimeout()); + myAdvancedConfig.getWifiPortalTimeout()); String mdns("

Default mDNS name is: http://"); mdns += myConfig.getMDNS(); diff --git a/src_docs/source/api.rst b/src_docs/source/api.rst index 45400c4..49149cb 100644 --- a/src_docs/source/api.rst +++ b/src_docs/source/api.rst @@ -156,6 +156,35 @@ Retrive the data used for formula calculation data via an HTTP GET command. Payl } +GET: /api/config/advanced +========================= + +Used for adjusting some internal constants and other advanced settings. Should be used with caution. + +.. code-block:: json + + { + "gyro-read-count": 50, + "gyro-read-delay": 3150, + "gyro-moving-threashold": 500, + "formula-max-deviation": 1.6, + "wifi-portaltimeout": 120, + "formula-calibration-temp": 20, + "int-http1": 0, + "int-http2": 0, + "int-http3": 0, + "int-influx": 0, + "int-mqtt": 0 + } + +POST: /api/config/advanced +========================== + +Same parameters as above. + +Payload should be in standard format used for posting a form + + GET: /api/clearwifi =================== diff --git a/src_docs/source/configuration.rst b/src_docs/source/configuration.rst index 2b724c0..456d8ec 100644 --- a/src_docs/source/configuration.rst +++ b/src_docs/source/configuration.rst @@ -275,3 +275,14 @@ This option gives you the possibility to install an new version of the firmware :alt: Update firmware +Advanded Settings ++++++++++++++++++ + +.. image:: images/config5.png + :width: 800 + :alt: Advanced Settings + +* **Header:** + +To be described + diff --git a/src_docs/source/releases.rst b/src_docs/source/releases.rst index bcc8346..5e52a5b 100644 --- a/src_docs/source/releases.rst +++ b/src_docs/source/releases.rst @@ -9,10 +9,15 @@ v1.0.0 * Removed brewfather option (can use standard HTTP options), the old apporach can still be used via changing format template. * Added 5 more points for formula creation, so a total of 10 angles/gravity values can be stored. * Added function on format page so that it's easy to copy a format template from the docs (simplify service integration). -* Added https support for Influxdb +* Added https support for Influxdb (EXPERIMENTAL NOT YET TESTED) +* Added possibility to have variable push intervals for different endpoints. +* Added advanced settings to configuration for adjusting some internal values (gyro reads, accepted formula deviation, moving detection). * Added additional http error codes to troubleshooting documentation * BUG: Fixed issue in formula calculation in case there were a gap in the data series +* TODO: Fix documentation for advanced settings. +* TODO: Update brewfather documentation + v0.9.0 ------ * Added one http push target that uses HTTP GET. This can be used with ubidots or blynk api's. diff --git a/test/adv.json b/test/adv.json new file mode 100644 index 0000000..b9e2398 --- /dev/null +++ b/test/adv.json @@ -0,0 +1,13 @@ +{ + "gyro-read-count": 51, + "gyro-read-delay": 3151, + "gyro-moving-threashold": 501, + "formula-max-deviation": 1.7, + "wifi-portaltimeout": 121, + "formula-calibration-temp": 21, + "int-http1": 1, + "int-http2": 2, + "int-http3": 3, + "int-influx": 4, + "int-mqtt": 5 +} \ No newline at end of file