From b15043e61c3f9a22859865a96105974fa5290e47 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sat, 22 Feb 2020 20:11:33 +0100 Subject: [PATCH] Memory Optimizations --- platformio.ini | 7 ++-- src/hasp.cpp | 4 +-- src/hasp_config.cpp | 10 ++++++ src/hasp_config.h | 1 + src/hasp_debug.cpp | 16 ++++++--- src/hasp_debug.h | 3 +- src/hasp_gui.cpp | 4 +-- src/hasp_hal.cpp | 4 +-- src/hasp_http.cpp | 13 +++---- src/hasp_log.cpp | 19 ++++++++++- src/hasp_log.h | 5 ++- src/hasp_mqtt.cpp | 75 ++++++++++++++++++++-------------------- src/hasp_spiffs.cpp | 45 +++++++++++++----------- src/hasp_wifi.cpp | 83 +++++++++++++++++++++++++-------------------- 14 files changed, 173 insertions(+), 116 deletions(-) diff --git a/platformio.ini b/platformio.ini index c611b799..3049a9ba 100644 --- a/platformio.ini +++ b/platformio.ini @@ -64,7 +64,8 @@ upload_speed = 921600 ; -- Shared library dependencies in all environments lib_deps = ;lvgl@^7.0.0 ; Not in library yet - TFT_eSPI@^2.1.3 ; Tft SPI drivers + ;TFT_eSPI@^2.1.3 ; Tft SPI drivers + TFT_eSPI@^1.4.20 ; Tft SPI drivers PubSubClient@^2.7.0 ; MQTT client ArduinoJson@^6.14.1,>6.14.0 ; needs at least 6.14.1 Syslog@^2.0.0 @@ -159,8 +160,8 @@ build_flags = [env:lolind32pro-lolintft24] platform = espressif32 board = lolin_d32_pro -upload_port = COM8 ; Change to the correct port -monitor_port = COM8 ; Change to the correct port +upload_port = COM7 ; Change to the correct port +monitor_port = COM7 ; Change to the correct port monitor_speed = 115200 board_build.partitions = default.csv build_flags = diff --git a/src/hasp.cpp b/src/hasp.cpp index a978576e..7f4b712a 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -1473,7 +1473,7 @@ void haspNewObject(const JsonObject & config) } /** testing end **/ - char msg[64]; + char msg[127]; sprintf_P(msg, PSTR("HASP: Created object p[%u].b[%u]"), pageid, temp); debugPrintln(msg); @@ -1488,7 +1488,7 @@ void haspNewObject(const JsonObject & config) void haspLoadPage(String pages) { - char msg[92]; + char msg[127]; if(!SPIFFS.begin()) { errorPrintln(String(F("HASP: %sFS not mounted. Failed to load ")) + pages.c_str()); diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index 6f917521..df82b284 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -126,4 +126,14 @@ void configSetup(JsonDocument & settings) } else { configGetConfig(settings, true); } +} + +void configOutput(const JsonObject & settings) +{ + String output((char *)0); + output.reserve(127); + serializeJson(settings, output); + String passmask = F("********"); + output.replace(settings[F("pass")].as(), passmask); + debugPrintln(String(F("CONF: ")) + output); } \ No newline at end of file diff --git a/src/hasp_config.h b/src/hasp_config.h index cadfc462..f0a9aa08 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -29,5 +29,6 @@ void configSetConfig(JsonObject & settings); void configGetConfig(JsonDocument & settings); void configWriteConfig(); bool configChanged(void); +void configOutput(const JsonObject & settings); #endif \ No newline at end of file diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index e554dcc7..5dcb7d2b 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -74,20 +74,28 @@ void debugSetup() void debugLoop() {} -void serialPrintln(String debugText) +void serialPrintln(const char * debugText) { String debugTimeText((char *)0); - debugTimeText.reserve(128); + debugTimeText.reserve(127); debugTimeText = F("["); debugTimeText += String(float(millis()) / 1000, 3); debugTimeText += F("s] "); + debugTimeText += ESP.getMaxFreeBlockSize(); + debugTimeText += F("/"); debugTimeText += ESP.getFreeHeap(); debugTimeText += F(" "); debugTimeText += halGetHeapFragmentation(); debugTimeText += F(" "); - debugTimeText += debugText; - Serial.println(debugTimeText); + + Serial.print(debugTimeText); + Serial.println(debugText); +} + +void serialPrintln(String & debugText) +{ + serialPrintln(debugText.c_str()); } #if HASP_USE_SYSLOG != 0 diff --git a/src/hasp_debug.h b/src/hasp_debug.h index c7519ea2..fc1c3533 100644 --- a/src/hasp_debug.h +++ b/src/hasp_debug.h @@ -5,7 +5,8 @@ void debugSetup(void); void debugLoop(void); void debugStop(void); -void serialPrintln(String debugText); +void serialPrintln(String & debugText); +void serialPrintln(const char * debugText); void syslogSend(uint8_t log, const char * debugText); diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index b762ffd9..dbe8c202 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -72,7 +72,7 @@ bool IRAM_ATTR guiCheckSleep() /* Serial debugging */ void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * dsc) { - char msg[128]; + char msg[127]; sprintf(msg, PSTR("LVGL: %s@%d->%s"), file, line, dsc); debugPrintln(msg); } @@ -341,7 +341,7 @@ void guiSetup(TFT_eSPI & screen, JsonObject settings) /* Setup Backlight Control Pin */ if(guiBacklightPin >= 0) { - char msg[128]; + char msg[127]; sprintf(msg, PSTR("LVGL: Backlight Pin = %i"), guiBacklightPin); debugPrintln(msg); diff --git a/src/hasp_hal.cpp b/src/hasp_hal.cpp index 9a0cc2fc..3879af07 100644 --- a/src/hasp_hal.cpp +++ b/src/hasp_hal.cpp @@ -13,7 +13,7 @@ String esp32ResetReason(uint8_t cpuid) RESET_REASON reason = rtc_get_reset_reason(cpuid); String resetReason((char *)0); - resetReason.reserve(32); + resetReason.reserve(127); resetReason += F("CPU"); resetReason += cpuid; @@ -78,7 +78,7 @@ String halGetResetInfo() { #if defined(ARDUINO_ARCH_ESP32) String resetReason((char *)0); - resetReason.reserve(64); + resetReason.reserve(127); resetReason += String(esp32ResetReason(0)); resetReason += F(" / "); diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index bdf1347e..a7ba5007 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -28,7 +28,7 @@ uint16_t httpPort = 80; FS * filesystem = &SPIFFS; File fsUploadFile; char httpUser[32] = ""; -char httpPassword[64] = ""; +char httpPassword[32] = ""; #if defined(ARDUINO_ARCH_ESP8266) #include @@ -99,6 +99,7 @@ bool httpIsAuthenticated(const String & page) return false; } } + char buffer[127]; snprintf(buffer, sizeof(buffer), PSTR("HTTP: Sending %s page to client connected from: %s"), page.c_str(), webServer.client().remoteIP().toString().c_str()); @@ -557,7 +558,7 @@ void handleFileList() } output += F("\"}"); - char msg[64]; + char msg[127]; sprintf(msg, PSTR("HTTP: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); debugPrintln(msg); @@ -1107,9 +1108,7 @@ bool httpGetConfig(const JsonObject & settings) settings[FPSTR(F_CONFIG_USER)] = httpUser; settings[FPSTR(F_CONFIG_PASS)] = httpPassword; - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return true; } @@ -1138,8 +1137,6 @@ bool httpSetConfig(const JsonObject & settings) httpPort = settings[FPSTR(F_CONFIG_PORT)].as(); } - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return changed; } \ No newline at end of file diff --git a/src/hasp_log.cpp b/src/hasp_log.cpp index f40c30b9..0cb1f1f7 100644 --- a/src/hasp_log.cpp +++ b/src/hasp_log.cpp @@ -15,7 +15,24 @@ #include "hasp_log.h" #include "hasp_debug.h" -void debugPrintln(String debugText) +void debugPrintln(String & debugText) +{ + serialPrintln(debugText); + +#if HASP_USE_SYSLOG != 0 + syslogSend(0, debugText.c_str()); +#endif +} + +void debugPrintln(const __FlashStringHelper * debugText) +{ + String buffer((char *)0); + buffer.reserve(127); + buffer = debugText; + debugPrintln(buffer); +} + +void debugPrintln(const char * debugText) { serialPrintln(debugText); diff --git a/src/hasp_log.h b/src/hasp_log.h index f123ba3f..6d6bde0d 100644 --- a/src/hasp_log.h +++ b/src/hasp_log.h @@ -1,7 +1,10 @@ #ifndef HASP_LOG_H #define HASP_LOG_H -void debugPrintln(String debugText); +void debugPrintln(String & debugText); +void debugPrintln(const __FlashStringHelper * debugText); +void debugPrintln(const char * debugText); + void errorPrintln(String debugText); void warningPrintln(String debugText); diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index 8470c451..598ff7d4 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #endif #include @@ -24,7 +23,7 @@ #include "user_config_override.h" #endif -String mqttClientId; // Auto-generated MQTT ClientID +String mqttClientId((char *)0); // Auto-generated MQTT ClientID /* String mqttGetSubtopic; // MQTT subtopic for incoming commands requesting .val String mqttGetSubtopicJSON; // MQTT object buffer for JSON status when requesting .val @@ -41,8 +40,8 @@ String mqttLightBrightCommandTopic; // MQTT topic for incoming panel backlight d String mqttLightBrightStateTopic; // MQTT topic for outgoing panel backlight dimmer state // String mqttMotionStateTopic; // MQTT topic for outgoing motion sensor state -String mqttNodeTopic; -String mqttGroupTopic; +String mqttNodeTopic((char *)0); +String mqttGroupTopic((char *)0); bool mqttEnabled; //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -89,29 +88,29 @@ void IRAM_ATTR mqttSendState(const char * subtopic, const char * payload) // light = 0/1 // brightness = 100 - char topic[128]; - sprintf_P(topic, PSTR("%sstate/%s"), mqttNodeTopic.c_str(), subtopic); + char topic[127]; + snprintf_P(topic, sizeof(topic), PSTR("%sstate/%s"), mqttNodeTopic.c_str(), subtopic); mqttClient.publish(topic, payload); debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(payload)); // as json - char value[256]; - sprintf_P(topic, PSTR("%sstate/json"), mqttNodeTopic.c_str()); - sprintf_P(value, PSTR("{\"%s\":\"%s\"}"), subtopic, payload); + char value[254]; + snprintf_P(topic, sizeof(topic), PSTR("%sstate/json"), mqttNodeTopic.c_str()); + snprintf_P(value, sizeof(value), PSTR("{\"%s\":\"%s\"}"), subtopic, payload); mqttClient.publish(topic, value); debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(value)); } void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, const char * attribute, String txt) { - char subtopic[32]; - sprintf_P(subtopic, PSTR("p[%u].b[%u].%s"), pageid, btnid, attribute); + char subtopic[127]; + snprintf_P(subtopic, sizeof(subtopic), PSTR("p[%u].b[%u].%s"), pageid, btnid, attribute); mqttSendState(subtopic, txt.c_str()); } void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, int32_t val) { - char value[16]; + char value[127]; itoa(val, value, 10); mqttSendNewValue(pageid, btnid, "val", value); } @@ -123,7 +122,7 @@ void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, String txt) void IRAM_ATTR mqttSendNewEvent(uint8_t pageid, uint8_t btnid, int32_t val) { - char value[16]; + char value[127]; itoa(val, value, 10); mqttSendNewValue(pageid, btnid, "event", value); } @@ -268,8 +267,8 @@ void mqttCallback(char * topic, byte * payload, unsigned int length) if(strTopic == F("status") && strPayload == F("OFF")) { // catch a dangling LWT from a previous connection if it appears - char topicBuffer[64]; - sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); + char topicBuffer[127]; + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); debugPrintln(String(F("MQTT: binary_sensor state: [")) + topicBuffer + "] : ON"); mqttClient.publish(topicBuffer, "ON", true); return; @@ -281,13 +280,17 @@ void mqttReconnect() static uint8_t mqttReconnectCount = 0; bool mqttFirstConnect = true; String nodeName = haspGetNodename(); - // Generate an MQTT client ID as haspNode + our MAC address - mqttClientId = nodeName + "-" + WiFi.macAddress(); + char topicBuffer[127]; - char topicBuffer[64]; - sprintf_P(topicBuffer, PSTR("hasp/%s/"), nodeName.c_str()); + // Generate an MQTT client ID as haspNode + our MAC address + mqttClientId = nodeName; + mqttClientId += F("-"); + mqttClientId += wifiGetMacAddress(3, ""); + WiFi.macAddress(); + + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("hasp/%s/"), nodeName.c_str()); mqttNodeTopic = topicBuffer; - sprintf_P(topicBuffer, PSTR("hasp/%s/"), mqttGroupName.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("hasp/%s/"), mqttGroupName.c_str()); mqttGroupTopic = topicBuffer; // haspSetPage(0); @@ -295,7 +298,7 @@ void mqttReconnect() String(F(" as clientID ")) + mqttClientId); // Attempt to connect and set LWT and Clean Session - sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); if(!mqttClient.connect(mqttClientId.c_str(), mqttUser.c_str(), mqttPassword.c_str(), topicBuffer, 0, false, "OFF", true)) { // Retry until we give up and restart after connectTimeout seconds @@ -334,24 +337,24 @@ void mqttReconnect() // Attempt to connect to broker, setting last will and testament // Subscribe to our incoming topics - sprintf_P(topicBuffer, PSTR("%scommand/#"), mqttGroupTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttGroupTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); } - sprintf_P(topicBuffer, PSTR("%scommand/#"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); } - sprintf_P(topicBuffer, PSTR("%slight/#"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%slight/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); } - sprintf_P(topicBuffer, PSTR("%sbrightness/#"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sbrightness/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); } - sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); } @@ -367,6 +370,10 @@ void mqttReconnect() void mqttSetup(const JsonObject & settings) { + mqttClientId.reserve(127); + mqttNodeTopic.reserve(127); + mqttGroupTopic.reserve(127); + mqttSetConfig(settings); mqttEnabled = mqttServer != "" && mqttPort > 0; @@ -394,16 +401,16 @@ bool mqttIsConnected() void mqttStop() { if(mqttClient.connected()) { - char topicBuffer[64]; + char topicBuffer[127]; - sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); mqttClient.publish(topicBuffer, "OFF"); - sprintf_P(topicBuffer, PSTR("%ssensor"), mqttNodeTopic.c_str()); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%ssensor"), mqttNodeTopic.c_str()); mqttClient.publish(topicBuffer, "{\"status\": \"unavailable\"}"); mqttClient.disconnect(); - debugPrintln(String(F("MQTT: Disconnected from broker"))); + debugPrintln(F("MQTT: Disconnected from broker")); } } @@ -415,9 +422,7 @@ bool mqttGetConfig(const JsonObject & settings) settings[FPSTR(F_CONFIG_USER)] = String(mqttUser.c_str()); settings[FPSTR(F_CONFIG_PASS)] = String(mqttPassword.c_str()); - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return true; } @@ -472,8 +477,6 @@ bool mqttSetConfig(const JsonObject & settings) mqttPassword = settings[FPSTR(F_CONFIG_PASS)].as().c_str(); } - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return changed; } diff --git a/src/hasp_spiffs.cpp b/src/hasp_spiffs.cpp index 4019b040..78ff512a 100644 --- a/src/hasp_spiffs.cpp +++ b/src/hasp_spiffs.cpp @@ -9,29 +9,29 @@ #if defined(ARDUINO_ARCH_ESP32) #include "SPIFFS.h" #endif -#include // Include the SPIFFS library +#include #endif void spiffsList() { -#if defined(ARDUINO_ARCH_ESP32) + char buffer[127]; debugPrintln(PSTR("FILE: Listing files on the internal flash:")); + +#if defined(ARDUINO_ARCH_ESP32) File root = SPIFFS.open("/"); File file = root.openNextFile(); while(file) { - char msg[64]; - sprintf(msg, PSTR("FILE: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); - debugPrintln(msg); + snprintf(buffer, sizeof(buffer), PSTR("FILE: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); + debugPrintln(buffer); file = root.openNextFile(); } #endif #if defined(ARDUINO_ARCH_ESP8266) - debugPrintln(PSTR("FILE: Listing files on the internal flash:")); Dir dir = SPIFFS.openDir("/"); while(dir.next()) { - char msg[64]; - sprintf(msg, PSTR("FILE: * %s (%u bytes)"), dir.fileName().c_str(), (uint32_t)dir.fileSize()); - debugPrintln(msg); + snprintf(buffer, sizeof(buffer), PSTR("FILE: * %s (%u bytes)"), dir.fileName().c_str(), + (uint32_t)dir.fileSize()); + debugPrintln(buffer); } #endif } @@ -41,18 +41,17 @@ void spiffsSetup() // no SPIFFS settings, as settings depend on SPIFFS #if HASP_USE_SPIFFS - char msg[64]; + char buffer[127]; #if defined(ARDUINO_ARCH_ESP8266) if(!SPIFFS.begin()) { #else if(!SPIFFS.begin(true)) { #endif - sprintf(msg, PSTR("FILE: %%sSPI flash init failed. Unable to mount FS.")); - errorPrintln(msg); + snprintf(buffer, sizeof(buffer), PSTR("FILE: %%sSPI flash init failed. Unable to mount FS.")); + errorPrintln(buffer); } else { - sprintf(msg, PSTR("FILE: [SUCCESS] SPI flash FS mounted")); - debugPrintln(msg); - // spiffsList(); // Wait on debugSetup() + snprintf(buffer, sizeof(buffer), PSTR("FILE: SPI Flash FS mounted")); + debugPrintln(buffer); } #endif } @@ -62,13 +61,21 @@ void spiffsLoop() String spiffsFormatBytes(size_t bytes) { + String output((char *)0); + output.reserve(127); + if(bytes < 1024) { - return String(bytes) + "B"; + output += bytes; } else if(bytes < (1024 * 1024)) { - return String(bytes / 1024.0) + "KB"; + output += bytes / 1024.0; + output += "K"; } else if(bytes < (1024 * 1024 * 1024)) { - return String(bytes / 1024.0 / 1024.0) + "MB"; + output += bytes / 1024.0 / 1024.0; + output += "M"; } else { - return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; + output += bytes / 1024.0 / 1024.0 / 1024.0; + output += "G"; } + output += "B"; + return output; } \ No newline at end of file diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index e13f84e2..d4e20fb4 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -27,28 +27,30 @@ static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler; #endif #ifdef WIFI_SSID -std::string wifiSsid = WIFI_SSID; +char wifiSsid[32] = WIFI_SSID; +// std::string wifiSsid = WIFI_SSID; #else -std::string wifiSsid = ""; +char wifiSsid[32] = ""; +// std::string wifiSsid = ""; #endif #ifdef WIFI_PASSW -std::string wifiPassword = WIFI_PASSW; +char wifiPassword[32] = WIFI_PASSW; +// std::string wifiPassword = WIFI_PASSW; #else -std::string wifiPassword = ""; +char wifiPassword[32] = ""; +// std::string wifiPassword = ""; #endif -const byte DNS_PORT = 53; +// const byte DNS_PORT = 53; // DNSServer dnsServer; -// long wifiPrevMillis = 0; -// bool wifiWasConnected = false; -// int8_t wifiReconnectAttempt = -20; - String wifiGetMacAddress(int start, const char * seperator) { byte mac[6]; WiFi.macAddress(mac); - String cMac = ""; + String cMac((char *)0); + cMac.reserve(127); + for(int i = start; i < 6; ++i) { if(mac[i] < 0x10) cMac += "0"; cMac += String(mac[i], HEX); @@ -62,9 +64,9 @@ void wifiConnected(IPAddress ipaddress) { bool isConnected = WiFi.status() == WL_CONNECTED; char buffer[127]; - sprintf_P(buffer, PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str()); debugPrintln(buffer); - sprintf_P(buffer, PSTR("WIFI: Connected = %s"), isConnected ? PSTR("yes") : PSTR("no")); + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connected = %s"), isConnected ? PSTR("yes") : PSTR("no")); debugPrintln(buffer); if(isConnected) { @@ -77,7 +79,7 @@ void wifiConnected(IPAddress ipaddress) void wifiDisconnected(const char * ssid, uint8_t reason) { char buffer[127]; - sprintf_P(buffer, PSTR("WIFI: Disconnected from %s (Reason: %d)"), ssid, reason); + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Disconnected from %s (Reason: %d)"), ssid, reason); debugPrintln(buffer); WiFi.reconnect(); } @@ -85,7 +87,7 @@ void wifiDisconnected(const char * ssid, uint8_t reason) void wifiSsidConnected(const char * ssid) { char buffer[127]; - sprintf_P(buffer, PSTR("WIFI: Connected to SSID %s. Requesting IP..."), ssid); + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connected to SSID %s. Requesting IP..."), ssid); debugPrintln(buffer); } @@ -156,7 +158,7 @@ void wifiSetup(JsonObject settings) } WiFi.mode(WIFI_STA); - sprintf_P(buffer, PSTR("WIFI: Connecting to : %s"), wifiSsid.c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connecting to : %s"), wifiSsid); debugPrintln(buffer); #if defined(ARDUINO_ARCH_ESP8266) @@ -170,7 +172,7 @@ void wifiSetup(JsonObject settings) WiFi.setSleep(false); #endif - WiFi.begin(wifiSsid.c_str(), wifiPassword.c_str()); + WiFi.begin(wifiSsid, wifiPassword); } bool wifiLoop() @@ -208,12 +210,10 @@ bool wifiLoop() bool wifiGetConfig(const JsonObject & settings) { - settings[FPSTR(F_CONFIG_SSID)] = String(wifiSsid.c_str()); - settings[FPSTR(F_CONFIG_PASS)] = String(wifiPassword.c_str()); - - serializeJson(settings, Serial); - Serial.println(); + settings[FPSTR(F_CONFIG_SSID)] = wifiSsid; // String(wifiSsid.c_str()); + settings[FPSTR(F_CONFIG_PASS)] = wifiPassword; // String(wifiPassword.c_str()); + configOutput(settings); return true; } @@ -223,27 +223,36 @@ bool wifiSetConfig(const JsonObject & settings) { bool changed = false; + /* if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) { + if(wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as().c_str()) { + debugPrintln(F("wifiSsid set")); + } + changed |= wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as().c_str(); + + wifiSsid = settings[FPSTR(F_CONFIG_SSID)].as().c_str(); + } + + if(!settings[FPSTR(F_CONFIG_PASS)].isNull() && settings[FPSTR(F_CONFIG_PASS)].as() != F("********")) { + if(wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as().c_str()) { + debugPrintln(F("wifiPassword set")); + } + changed |= wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as().c_str(); + + wifiPassword = settings[FPSTR(F_CONFIG_PASS)].as().c_str(); + } + */ + if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) { - if(wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as().c_str()) { - debugPrintln(F("wifiSsid set")); - } - changed |= wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as().c_str(); - - wifiSsid = settings[FPSTR(F_CONFIG_SSID)].as().c_str(); + changed |= strcmp(wifiSsid, settings[FPSTR(F_CONFIG_SSID)]) != 0; + strncpy(wifiSsid, settings[FPSTR(F_CONFIG_SSID)], sizeof(wifiSsid)); } - if(!settings[FPSTR(F_CONFIG_PASS)].isNull() && settings[FPSTR(F_CONFIG_PASS)].as() != F("********")) { - if(wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as().c_str()) { - debugPrintln(F("wifiPassword set")); - } - changed |= wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as().c_str(); - - wifiPassword = settings[FPSTR(F_CONFIG_PASS)].as().c_str(); + if(!settings[FPSTR(F_CONFIG_PASS)].isNull()) { + changed |= strcmp(wifiPassword, settings[FPSTR(F_CONFIG_PASS)]) != 0; + strncpy(wifiPassword, settings[FPSTR(F_CONFIG_PASS)], sizeof(wifiPassword)); } - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return changed; }