From a5fedbf1ba923e4ed71d0a3152c8ddb73dece930 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 11 Dec 2019 10:14:23 +0100 Subject: [PATCH] redo websocket mem fix - #250 and #258 --- platformio.ini | 3 ++- src/MyESP.cpp | 31 +++++++++++++++---------------- src/MyESP.h | 8 +++++--- src/version.h | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/platformio.ini b/platformio.ini index 0dc5ab602..747006030 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,7 +37,8 @@ build_flags = ${common.general_flags} ${common.build_flags_4m1m} [env] framework = arduino -platform = espressif8266@2.2.2 ; arduino core 2.5.2 +;platform = espressif8266@2.2.2 ; arduino core 2.5.2 +platform = espressif8266 ;platform = https://github.com/platformio/platform-espressif8266#develop ;platform = https://github.com/platformio/platform-espressif8266#feature/stage lib_deps = diff --git a/src/MyESP.cpp b/src/MyESP.cpp index a01222dc6..eba5cde68 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -1472,8 +1472,8 @@ void MyESP::_heartbeatCheck(bool force) { uint32_t free_memory = ESP.getFreeHeap(); uint8_t mem_available = 100 * free_memory / total_memory; // as a % - StaticJsonDocument<200> doc; - JsonObject rootHeartbeat = doc.to(); + StaticJsonDocument doc; + JsonObject rootHeartbeat = doc.to(); rootHeartbeat["version"] = _app_version; rootHeartbeat["IP"] = WiFi.localIP().toString(); @@ -2312,8 +2312,8 @@ void MyESP::_onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, A // handle ws from browser void MyESP::_procMsg(AsyncWebSocketClient * client, size_t sz) { // We should always get a JSON object from browser, so parse it - StaticJsonDocument<500> doc; - char json[sz + 1]; + StaticJsonDocument doc; + char json[sz + 1]; memcpy(json, (char *)(client->_tempObject), sz); json[sz] = '\0'; @@ -2410,8 +2410,7 @@ bool MyESP::_fs_sendConfig() { // send custom status via ws void MyESP::_sendCustomStatus() { - // StaticJsonDocument<300> doc; - DynamicJsonDocument doc(MYESP_JSON_MAXSIZE); + DynamicJsonDocument doc(MYESP_JSON_MAXSIZE_LARGE); JsonObject root = doc.to(); @@ -2427,7 +2426,7 @@ void MyESP::_sendCustomStatus() { (_web_callback_f)(root); } - char buffer[MYESP_JSON_MAXSIZE]; + char buffer[MYESP_JSON_MAXSIZE_LARGE]; size_t len = serializeJson(root, buffer); #ifdef MYESP_DEBUG @@ -2536,9 +2535,9 @@ void MyESP::_printScanResult(int networksFound) { } } - StaticJsonDocument<400> doc; - JsonObject root = doc.to(); - root["command"] = "ssidlist"; + StaticJsonDocument doc; + JsonObject root = doc.to(); + root["command"] = "ssidlist"; JsonArray list = doc.createNestedArray("list"); for (int i = 0; i <= 5 && i < networksFound; ++i) { @@ -2548,7 +2547,7 @@ void MyESP::_printScanResult(int networksFound) { item["rssi"] = WiFi.RSSI(indices[i]); } - char buffer[400]; + char buffer[MYESP_JSON_MAXSIZE_MEDIUM]; size_t len = serializeJson(root, buffer); _ws->textAll(buffer, len); } @@ -2750,12 +2749,12 @@ void MyESP::_addMQTTLog(const char * topic, const char * payload, const MYESP_MQ // send UTC time via ws void MyESP::_sendTime() { - StaticJsonDocument<100> doc; - JsonObject root = doc.to(); - root["command"] = "gettime"; - root["epoch"] = now(); + StaticJsonDocument doc; + JsonObject root = doc.to(); + root["command"] = "gettime"; + root["epoch"] = now(); - char buffer[100]; + char buffer[MYESP_JSON_MAXSIZE_SMALL]; size_t len = serializeJson(root, buffer); _ws->textAll(buffer, len); } diff --git a/src/MyESP.h b/src/MyESP.h index 899a35a96..b79bd64c6 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -103,9 +103,11 @@ extern struct rst_info resetInfo; #define MQTT_DISCONNECT_EVENT 1 #define MQTT_MESSAGE_EVENT 2 -#define MYESP_JSON_MAXSIZE 2000 // for large Dynamic json files -#define MYESP_MQTTLOG_MAX 60 // max number of log entries for MQTT publishes and subscribes -#define MYESP_JSON_LOG_MAXSIZE 300 // max size of an JSON log entry +#define MYESP_JSON_MAXSIZE_LARGE 2000 // for large Dynamic json files +#define MYESP_JSON_MAXSIZE_MEDIUM 800 // for medium Dynamic json files +#define MYESP_JSON_MAXSIZE_SMALL 200 // for smaller Static json documents + +#define MYESP_MQTTLOG_MAX 60 // max number of log entries for MQTT publishes and subscribes #define MYESP_MQTT_PAYLOAD_ON '1' // for MQTT switch on #define MYESP_MQTT_PAYLOAD_OFF '0' // for MQTT switch off diff --git a/src/version.h b/src/version.h index 47b694c88..1e9e1751b 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define APP_VERSION "1.9.4b24" +#define APP_VERSION "1.9.4b25"