From e0fd87ca1f6751469cfe40c7b152b6020edf56d0 Mon Sep 17 00:00:00 2001 From: IgorYbema Date: Sun, 1 Sep 2024 19:44:20 +0200 Subject: [PATCH] save wifiscan to flash and remove some unused Strings and code --- HeishaMon/HeishaMon.ino | 4 +-- HeishaMon/src/opentherm/opentherm.cpp | 3 ++- HeishaMon/src/opentherm/opentherm.h | 4 +-- HeishaMon/webfunctions.cpp | 38 ++++++++++++++++----------- HeishaMon/webfunctions.h | 2 +- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/HeishaMon/HeishaMon.ino b/HeishaMon/HeishaMon.ino index 86da6aba..5e3a08d3 100644 --- a/HeishaMon/HeishaMon.ino +++ b/HeishaMon/HeishaMon.ino @@ -104,12 +104,10 @@ Adafruit_NeoPixel pixels(1, LEDPIN); #endif // store actual data -String openTherm[2]; char actData[DATASIZE] = { '\0' }; char actDataExtra[DATASIZE] = { '\0' }; #define OPTDATASIZE 20 char actOptData[OPTDATASIZE] = { '\0' }; -String RESTmsg = ""; // log message to sprintf to char log_msg[256]; @@ -271,7 +269,7 @@ void check_wifi() { WiFi.psk().toCharArray(heishamonSettings.wifi_password, 40); JsonDocument jsonDoc; settingsToJson(jsonDoc, &heishamonSettings); //stores current settings in a json document - saveJsonToConfig(jsonDoc); //save to config file + saveJsonToFile(jsonDoc, "config.json"); //save to config file } ntpReload(&heishamonSettings); diff --git a/HeishaMon/src/opentherm/opentherm.cpp b/HeishaMon/src/opentherm/opentherm.cpp index ae878db5..778dc867 100644 --- a/HeishaMon/src/opentherm/opentherm.cpp +++ b/HeishaMon/src/opentherm/opentherm.cpp @@ -304,7 +304,7 @@ void OpenTherm::end() { detachInterrupt(digitalPinToInterrupt(inPin)); } } - +/* const char *OpenTherm::statusToString(OpenThermResponseStatus status) { switch (status) { case NONE: return "NONE"; @@ -328,6 +328,7 @@ const char *OpenTherm::messageTypeToString(OpenThermMessageType message_type) { default: return "UNKNOWN"; } } +*/ //building requests diff --git a/HeishaMon/src/opentherm/opentherm.h b/HeishaMon/src/opentherm/opentherm.h index 3e6c0fbe..f1bffea0 100644 --- a/HeishaMon/src/opentherm/opentherm.h +++ b/HeishaMon/src/opentherm/opentherm.h @@ -133,7 +133,7 @@ class OpenTherm unsigned long buildRequest(OpenThermMessageType type, OpenThermMessageID id, unsigned int data); unsigned long buildResponse(OpenThermMessageType type, OpenThermMessageID id, unsigned int data); OpenThermResponseStatus getLastResponseStatus(); - const char *statusToString(OpenThermResponseStatus status); + //const char *statusToString(OpenThermResponseStatus status); void handleInterrupt(); void process(); void end(); @@ -141,7 +141,7 @@ class OpenTherm bool parity(unsigned long frame); OpenThermMessageType getMessageType(unsigned long message); OpenThermMessageID getDataID(unsigned long frame); - const char *messageTypeToString(OpenThermMessageType message_type); + //const char *messageTypeToString(OpenThermMessageType message_type); bool isValidRequest(unsigned long request); bool isValidResponse(unsigned long response); diff --git a/HeishaMon/webfunctions.cpp b/HeishaMon/webfunctions.cpp index e4ffd5d2..e682d85d 100755 --- a/HeishaMon/webfunctions.cpp +++ b/HeishaMon/webfunctions.cpp @@ -15,8 +15,6 @@ #define UPTIME_OVERFLOW 4294967295 // Uptime overflow value -static String wifiJsonList = ""; - static uint8_t ntpservers = 0; void log_message(char* string); @@ -35,7 +33,6 @@ int dBmToQuality(int dBm) { void getWifiScanResults(int numSsid) { if (numSsid > 0) { //found wifi networks - wifiJsonList = "["; int indexes[numSsid]; for (int i = 0; i < numSsid; i++) { //fill the sorted list with normal indexes first indexes[i] = i; @@ -59,18 +56,19 @@ void getWifiScanResults(int numSsid) { } } } - bool firstSSID = true; + JsonDocument wifiJsonDoc; + JsonArray wifiJsonArray = wifiJsonDoc.to(); for (int i = 0; i < numSsid; i++) { //then output json if (indexes[i] == -1) { continue; } - if (!firstSSID) { - wifiJsonList = wifiJsonList + ","; - } - wifiJsonList = wifiJsonList + "{\"ssid\":\"" + WiFi.SSID(indexes[i]) + "\", \"rssi\": \"" + dBmToQuality(WiFi.RSSI(indexes[i])) + "%\"}"; - firstSSID = false; + JsonObject wifiJsonObject = wifiJsonArray.add(); + wifiJsonObject["ssid"] = WiFi.SSID(indexes[i]); + String quality = String(dBmToQuality(WiFi.RSSI(indexes[i]))) + "%"; + wifiJsonObject["rssi"] = quality; } - wifiJsonList = wifiJsonList + "]"; + saveJsonToFile(wifiJsonDoc,"wifiscan.json"); + WiFi.scanDelete(); } } @@ -461,9 +459,9 @@ void settingsToJson(JsonDocument &jsonDoc, settingsStruct *heishamonSettings) { jsonDoc["updataAllDallasTime"] = heishamonSettings->updataAllDallasTime; } -void saveJsonToConfig(JsonDocument &jsonDoc) { +void saveJsonToFile(JsonDocument &jsonDoc, const char* filename) { if (LittleFS.begin()) { - File configFile = LittleFS.open("/config.json", "w"); + File configFile = LittleFS.open(filename, "w"); if (configFile) { serializeJson(jsonDoc, configFile); configFile.close(); @@ -615,7 +613,7 @@ int saveSettings(struct webserver_t *client, settingsStruct *heishamonSettings) jsonDoc["wifi_password"] = String(wifi_password); } - saveJsonToConfig(jsonDoc); //save to config file + saveJsonToFile(jsonDoc, "config.json"); //save to config file loadSettings(heishamonSettings); //load config file to current settings while (client->userdata) { @@ -972,8 +970,18 @@ int handleWifiScan(struct webserver_t *client) { #endif if (client->content == 0) { webserver_send(client, 200, (char *)"application/json", 0); - char *str = (char *)wifiJsonList.c_str(); - webserver_send_content(client, str, strlen(str)); + if (LittleFS.begin()) { + File scanfile = LittleFS.open("wifiscan.json", "r"); + if (scanfile) { + size_t size = scanfile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + scanfile.readBytes(buf.get(), size); + webserver_send_content(client, buf.get(), size); + scanfile.close(); + } + } + } //initatie a new async scan for next try #if defined(ESP8266) diff --git a/HeishaMon/webfunctions.h b/HeishaMon/webfunctions.h index 444d8163..62854f89 100644 --- a/HeishaMon/webfunctions.h +++ b/HeishaMon/webfunctions.h @@ -88,7 +88,7 @@ int handleFactoryReset(struct webserver_t *client); int handleReboot(struct webserver_t *client); int handleDebug(struct webserver_t *client, char *hex, byte hex_len); void settingsToJson(JsonDocument &jsonDoc, settingsStruct *heishamonSettings); -void saveJsonToConfig(JsonDocument &jsonDoc); +void saveJsonToFile(JsonDocument &jsonDoc, const char *filename); void loadSettings(settingsStruct *heishamonSettings); int getSettings(struct webserver_t *client, settingsStruct *heishamonSettings); int handleSettings(struct webserver_t *client);