diff --git a/src/config.cpp b/src/config.cpp index b9cda42..b676588 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -417,6 +417,8 @@ bool HardwareConfig::loadFile() { doc[PARAM_HW_FORMULA_CALIBRATION_TEMP].as()); if (!doc[PARAM_HW_WIFI_PORTALTIMEOUT].isNull()) this->setWifiPortalTimeout(doc[PARAM_HW_WIFI_PORTALTIMEOUT].as()); + if (!doc[PARAM_HW_PUSH_TIMEOUT].isNull()) + this->setPushTimeout(doc[PARAM_HW_PUSH_TIMEOUT].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 4a1e789..4f74879 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -60,6 +60,7 @@ class HardwareConfig { int _gyroSensorMovingThreashold = 500; int _gyroReadCount = 50; int _gyroReadDelay = 3150; // us, empirical, to hold sampling to 200 Hz + int _pushTimeout = 10; // seconds public: int getWifiPortalTimeout() { return _wifiPortalTimeout; } @@ -78,6 +79,8 @@ class HardwareConfig { 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; } bool saveFile(); bool loadFile(); diff --git a/src/helper.cpp b/src/helper.cpp index e1e0487..b38d862 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -155,7 +155,6 @@ void FloatHistoryLog::save() { // Print the heap information. // void printHeap(String prefix) { -#if LOG_LEVEL == 6 || LOG_LEVEL == 5 #if defined(ESP8266) Log.notice( F("%s: Free-heap %d kb, Heap-rag %d %%, Max-block %d kb Stack=%d b." CR), @@ -168,7 +167,6 @@ void printHeap(String prefix) { Log.verbose(F("HELP: Heap %d kb, FreeSketch %d kb." CR), ESP.getFreeHeap() / 1024, ESP.getFreeSketchSpace() / 1024); #endif -#endif } // @@ -325,11 +323,12 @@ void PerfLogging::pushInflux() { // Create body for influxdb2, format used // key,host=mdns value=0.0 String body; + body.reserve(500); // Create the payload with performance data. // ------------------------------------------------------------------------------------------ PerfEntry* pe = first; - char buf[100]; + char buf[150]; snprintf(&buf[0], sizeof(buf), "perf,host=%s,device=%s ", myConfig.getMDNS(), myConfig.getID()); body += &buf[0]; @@ -351,15 +350,23 @@ void PerfLogging::pushInflux() { snprintf(&buf[0], sizeof(buf), "\ndebug,host=%s,device=%s ", myConfig.getMDNS(), myConfig.getID()); body += &buf[0]; +#if defined (ESP8266) snprintf( &buf[0], sizeof(buf), - "angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f", + "angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f,heap=%d,heap-frag=%d,heap-max=%d,stack=%d", myGyro.getAngle(), myGyro.getLastGyroData().ax, myGyro.getLastGyroData().ay, myGyro.getLastGyroData().az, - myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp())); - body += &buf[0]; + myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(), ESP.getHeapFragmentation(), ESP.getMaxFreeBlockSize(), ESP.getFreeContStack()); +#else // defined (ESP32) + snprintf( + &buf[0], sizeof(buf), + "angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f,heap=%d,heap-frag=%d,heap-max=%d", + myGyro.getAngle(), myGyro.getLastGyroData().ax, + myGyro.getLastGyroData().ay, myGyro.getLastGyroData().az, + myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(), 0, ESP.getMaxAllocHeap()); +#endif - // Log.notice(F("PERF: data %s." CR), body.c_str() ); + body += &buf[0]; #if LOG_LEVEL == 6 && !defined(HELPER_DISABLE_LOGGING) Log.verbose(F("PERF: url %s." CR), serverPath.c_str()); @@ -369,6 +376,7 @@ void PerfLogging::pushInflux() { // Send HTTP POST request String auth = "Token " + String(myConfig.getInfluxDb2PushToken()); http.addHeader(F("Authorization"), auth.c_str()); + http.setTimeout(myHardwareConfig.getPushTimeout()); int httpResponseCode = http.POST(body); if (httpResponseCode == 204) { @@ -413,7 +421,9 @@ float reduceFloatPrecision(float f, int dec) { // https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/ // String urlencode(String str) { - String encodedString = ""; + String encodedString; + encodedString.reserve(str.length()*2); + encodedString = ""; char c; char code0; char code1; @@ -453,8 +463,13 @@ unsigned char h2int(char c) { return (0); } +// +// urlencode string +// String urldecode(String str) { - String encodedString = ""; + String encodedString; + encodedString.reserve(str.length()); + encodedString = ""; char c; char code0; char code1; diff --git a/src/main.cpp b/src/main.cpp index ffbbf7d..2d1bb93 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -258,7 +258,10 @@ bool loopReadGravity() { push.send(angle, gravitySG, corrGravitySG, tempC, (millis() - runtimeMillis) / 1000); LOG_PERF_STOP("loop-push"); - LOG_PERF_PUSH(); + // Send stats to influx after each push run. + if (runMode == RunMode::configurationMode) { + LOG_PERF_PUSH(); + } } return true; } else { diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 2f087b2..2091e15 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -37,7 +37,7 @@ SOFTWARE. // void PushTarget::send(float angle, float gravitySG, float corrGravitySG, float tempC, float runTime) { - printHeap("StartPush"); + printHeap("PUSH"); http.setReuse(false); httpSecure.setReuse(false); @@ -50,14 +50,12 @@ void PushTarget::send(float angle, float gravitySG, float corrGravitySG, LOG_PERF_STOP("push-brewfather"); } - printHeap("http1"); if (myConfig.isHttpActive()) { LOG_PERF_START("push-http"); sendHttp(engine, myConfig.isHttpSSL(), 0); LOG_PERF_STOP("push-http"); } - printHeap("http2"); if (myConfig.isHttp2Active()) { LOG_PERF_START("push-http2"); sendHttp(engine, myConfig.isHttp2SSL(), 1); @@ -92,6 +90,7 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine) { String doc = engine.create(TemplatingEngine::TEMPLATE_INFLUX); http.begin(wifi, serverPath); + http.setTimeout(myHardwareConfig.getPushTimeout()); #if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); @@ -128,6 +127,7 @@ void PushTarget::sendBrewfather(TemplatingEngine& engine) { String doc = engine.create(TemplatingEngine::TEMPLATE_BREWFATHER); http.begin(wifi, serverPath); + http.setTimeout(myHardwareConfig.getPushTimeout()); #if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); @@ -200,6 +200,7 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) { Log.notice(F("PUSH: HTTP, SSL enabled without validation." CR)); wifiSecure.setInsecure(); httpSecure.begin(wifiSecure, serverPath); + httpSecure.setTimeout(myHardwareConfig.getPushTimeout()); if (index == 0) { addHttpHeader(httpSecure, myConfig.getHttpHeader(0)); @@ -212,6 +213,7 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) { httpResponseCode = httpSecure.POST(doc); } else { http.begin(wifi, serverPath); + http.setTimeout(myHardwareConfig.getPushTimeout()); if (index == 0) { addHttpHeader(http, myConfig.getHttpHeader(0)); @@ -274,7 +276,7 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { #endif // Send MQQT message(s) - mqtt.setTimeout(10); // 10 seconds timeout + mqtt.setTimeout(myHardwareConfig.getPushTimeout()); // 10 seconds timeout int lines = 1; // Find out how many lines are in the document. Each line is one diff --git a/src/resources.hpp b/src/resources.hpp index 3263b10..e317fb4 100644 --- a/src/resources.hpp +++ b/src/resources.hpp @@ -73,6 +73,7 @@ SOFTWARE. #define PARAM_HW_FORMULA_DEVIATION "formula-max-deviation" #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_FORMAT_HTTP1 "http-1" #define PARAM_FORMAT_HTTP2 "http-2" #define PARAM_FORMAT_BREWFATHER "brewfather" diff --git a/src/templating.cpp b/src/templating.cpp index 6c3debb..9e79b25 100644 --- a/src/templating.cpp +++ b/src/templating.cpp @@ -139,6 +139,7 @@ void TemplatingEngine::initialize(float angle, float gravitySG, float corrGravit // const String& TemplatingEngine::create(TemplatingEngine::Templates idx) { String fname; + baseTemplate.reserve(600); // Load templates from memory switch (idx) { diff --git a/src/webserver.cpp b/src/webserver.cpp index d872d9e..4d2c43c 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -541,6 +541,8 @@ void WebServerHandler::webHandleDeviceParam() { 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()); } myHardwareConfig.saveFile();