diff --git a/src/mqtt/hasp_mqtt_esp.cpp b/src/mqtt/hasp_mqtt_esp.cpp index ee4143c6..2754a7ce 100644 --- a/src/mqtt/hasp_mqtt_esp.cpp +++ b/src/mqtt/hasp_mqtt_esp.cpp @@ -8,6 +8,7 @@ #include "mqtt_client.h" #include "esp_crt_bundle.h" +#include "Preferences.h" #include "hasp/hasp.h" #include "hasp_mqtt.h" @@ -429,8 +430,14 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) void mqttSetup() { + Preferences preferences; + preferences.begin("mqtt", true); + String password = preferences.getString(FP_CONFIG_PASS, MQTT_PASSWORD); + strncpy(mqttPassword, password.c_str(), sizeof(mqttPassword)); + LOG_DEBUG(TAG_MQTT, F(D_BULLET "Read %s => %s (%d bytes)"), FP_CONFIG_PASS, password.c_str(), password.length()); + queue = xQueueCreate(64, sizeof(mqtt_message_t)); - esp_crt_bundle_set(rootca_crt_bundle_start); + arduino_esp_crt_bundle_set(rootca_crt_bundle_start); mqttStart(); } @@ -619,8 +626,10 @@ bool mqttGetConfig(const JsonObject& settings) if(strcmp(mqttUsername, settings[FPSTR(FP_CONFIG_USER)].as().c_str()) != 0) changed = true; settings[FPSTR(FP_CONFIG_USER)] = mqttUsername; - if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; - settings[FPSTR(FP_CONFIG_PASS)] = mqttPassword; + // if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; + // settings[FPSTR(FP_CONFIG_PASS)] = mqttPassword; + if(strcmp(D_PASSWORD_MASK, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; + settings[FPSTR(FP_CONFIG_PASS)] = D_PASSWORD_MASK; if(changed) configOutput(settings, TAG_MQTT); return changed; @@ -636,6 +645,9 @@ bool mqttGetConfig(const JsonObject& settings) **/ bool mqttSetConfig(const JsonObject& settings) { + Preferences preferences; + preferences.begin("mqtt", false); + configOutput(settings, TAG_MQTT); bool changed = false; @@ -680,6 +692,7 @@ bool mqttSetConfig(const JsonObject& settings) settings[FPSTR(FP_CONFIG_PASS)].as() != String(FPSTR(D_PASSWORD_MASK))) { changed |= strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0; strncpy(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(mqttPassword)); + nvsUpdateString(preferences, FP_CONFIG_PASS, settings[FPSTR(FP_CONFIG_PASS)]); } snprintf_P(mqttNodeTopic, sizeof(mqttNodeTopic), PSTR(MQTT_PREFIX "/%s/"), haspDevice.get_hostname()); diff --git a/src/sys/net/hasp_wifi.cpp b/src/sys/net/hasp_wifi.cpp index 48be7be1..3f45dc6c 100644 --- a/src/sys/net/hasp_wifi.cpp +++ b/src/sys/net/hasp_wifi.cpp @@ -23,6 +23,7 @@ #endif #include +#include "Preferences.h" #elif defined(ARDUINO_ARCH_ESP8266) #include #include "user_interface.h" // Wifi Reasons @@ -36,7 +37,7 @@ static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler; SPIClass espSPI(ESPSPI_MOSI, ESPSPI_MISO, ESPSPI_SCLK); // SPI port where esp is connected #endif -//#include "DNSserver.h" +// #include "DNSserver.h" char wifiSsid[MAX_SSID_LEN] = WIFI_SSID; char wifiPassword[MAX_PASSPHRASE_LEN] = WIFI_PASSWORD; @@ -482,6 +483,13 @@ void wifiSetup() disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected); #elif defined(ARDUINO_ARCH_ESP32) WiFi.onEvent(wifi_callback); + + Preferences preferences; + preferences.begin("wifi", true); + String password = preferences.getString(FP_CONFIG_PASS, WIFI_PASSWORD); + strncpy(wifiPassword, password.c_str(), sizeof(wifiPassword)); + LOG_DEBUG(TAG_WIFI, F(D_BULLET "Read %s => %s (%d bytes)"), FP_CONFIG_PASS, password.c_str(), + password.length()); #endif wifiReconnect(); @@ -638,8 +646,10 @@ bool wifiGetConfig(const JsonObject& settings) if(strcmp(wifiSsid, settings[FPSTR(FP_CONFIG_SSID)].as().c_str()) != 0) changed = true; settings[FPSTR(FP_CONFIG_SSID)] = wifiSsid; - if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; - settings[FPSTR(FP_CONFIG_PASS)] = wifiPassword; + // if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; + // settings[FPSTR(FP_CONFIG_PASS)] = wifiPassword; + if(strcmp(D_PASSWORD_MASK, settings[FPSTR(FP_CONFIG_PASS)].as().c_str()) != 0) changed = true; + settings[FPSTR(FP_CONFIG_PASS)] = D_PASSWORD_MASK; if(changed) configOutput(settings, TAG_WIFI); return changed; @@ -655,6 +665,9 @@ bool wifiGetConfig(const JsonObject& settings) **/ bool wifiSetConfig(const JsonObject& settings) { + Preferences preferences; + preferences.begin("wifi", false); + configOutput(settings, TAG_WIFI); bool changed = false; @@ -667,6 +680,7 @@ bool wifiSetConfig(const JsonObject& settings) settings[FPSTR(FP_CONFIG_PASS)].as() != String(FPSTR(D_PASSWORD_MASK))) { changed |= strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0; strncpy(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(wifiPassword)); + nvsUpdateString(preferences, FP_CONFIG_PASS, settings[FPSTR(FP_CONFIG_PASS)]); } return changed; diff --git a/src/sys/net/hasp_wifi.h b/src/sys/net/hasp_wifi.h index e5d0269d..cc168d18 100644 --- a/src/sys/net/hasp_wifi.h +++ b/src/sys/net/hasp_wifi.h @@ -36,9 +36,9 @@ bool wifiSetConfig(const JsonObject& settings); #ifndef WIFI_PASSWORD #ifndef WIFI_PASSW -#define WIFI_PASSWORD ""; +#define WIFI_PASSWORD "" #else -#define WIFI_PASSWORD WIFI_PASSW; +#define WIFI_PASSWORD WIFI_PASSW #endif #endif diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index f5cb9ee3..072f7837 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -8,6 +8,7 @@ #if defined(ARDUINO_ARCH_ESP32) #include "Update.h" +#include "Preferences.h" #include "sdkconfig.h" // for CONFIG_IDF_TARGET_ESP32* defines #include #include