+#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