diff options
author | KaufHA <[email protected]> | 2021-10-18 22:14:53 -0700 |
---|---|---|
committer | GitHub <[email protected]> | 2021-10-18 22:14:53 -0700 |
commit | 53a2ddb110d7726ea823728c9983624905d4fb78 (patch) | |
tree | 02ff748fe189f44e4325f2513c3f621b23016bea | |
parent | f1c7c4b532e6d1c8521a1f27d4a5d84d883b23f0 (diff) | |
download | PLF10-1.7.tar.gz PLF10-1.7.zip |
1.7 Upload 21.7
-rw-r--r-- | ESPHome Source Files/components/captive_portal.cpp | 4 | ||||
-rw-r--r-- | ESPHome Source Files/components/web_server.cpp | 2 | ||||
-rw-r--r-- | ESPHome Source Files/components/wifi_component.cpp | 19 | ||||
-rw-r--r-- | ESPHome Source Files/components/wifi_component.h | 3 | ||||
-rw-r--r-- | ESPHome Source Files/core/preferences.cpp | 79 |
5 files changed, 101 insertions, 6 deletions
diff --git a/ESPHome Source Files/components/captive_portal.cpp b/ESPHome Source Files/components/captive_portal.cpp index 2a0fd2d..42be3a9 100644 --- a/ESPHome Source Files/components/captive_portal.cpp +++ b/ESPHome Source Files/components/captive_portal.cpp @@ -20,8 +20,10 @@ void CaptivePortal::handle_index(AsyncWebServerRequest *request) { stream->print(F("<body><div class=\"main\"><h1>WiFi Networks</h1>")); if (request->hasArg("save")) { - stream->print(F("<div class=\"info\">The ESP will now try to connect to the network...<br/>Please give it some " + // KAUF edit + stream->print(F("<div class=\"info\">The KAUF Smart Plug will now try to connect to the network...<br/><br/>Please give it some " "time to connect.</div>")); + // KAUF edit end } for (auto &scan : wifi::global_wifi_component->get_scan_result()) { diff --git a/ESPHome Source Files/components/web_server.cpp b/ESPHome Source Files/components/web_server.cpp index ea0d054..851ddc1 100644 --- a/ESPHome Source Files/components/web_server.cpp +++ b/ESPHome Source Files/components/web_server.cpp @@ -177,7 +177,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) { stream->print(F("<p>KAUF Plug by <a href=\"https://kaufha.com\" target=\"_blank\" rel=\"noopener noreferrer\">Kaufman Home Automation</a></p>")); stream->print(F("<p>Firmware made using <a href=\"https://esphome.io\" target=\"_blank\" rel=\"noopener noreferrer\">ESPHome</a></p>")); - stream->print(F("<p>KAUF Plug firmware version 1.6, ESPHome version 2021.9.2</p>")); + stream->print(F("<p>KAUF Plug firmware version 1.7, ESPHome version 2021.9.3</p>")); diff --git a/ESPHome Source Files/components/wifi_component.cpp b/ESPHome Source Files/components/wifi_component.cpp index b768231..19199a1 100644 --- a/ESPHome Source Files/components/wifi_component.cpp +++ b/ESPHome Source Files/components/wifi_component.cpp @@ -33,12 +33,26 @@ static const char *const TAG = "wifi"; float WiFiComponent::get_setup_priority() const { return setup_priority::WIFI; } +void WiFiComponent::clear_stored_creds() { + ESP_LOGD(TAG, "------------------->>>>>>>>>>>>>>>>> Clearing saved WiFi credentials"); + + // create new preferences object with invalid hash + this->pref_ = global_preferences.make_preference<wifi::SavedWifiSettings>(0, true); + + SavedWifiSettings save{}; + strncpy(save.ssid, "", sizeof(save.ssid)); + strncpy(save.password, "", sizeof(save.password)); + this->pref_.save(&save); + +} + void WiFiComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up WiFi..."); this->last_connected_ = millis(); this->wifi_pre_setup_(); - // hard code time from first public release (v1.5) as hash argument so all firmware versions always have the same hash. + // hard code build date/time from first public release (v1.5) as hash argument so that + // all firmware versions always have the same hash. uint32_t hash = fnv1_hash("Aug 19 2021, 16:12:23"); this->pref_ = global_preferences.make_preference<wifi::SavedWifiSettings>(hash, true); @@ -51,9 +65,12 @@ void WiFiComponent::setup() { sta.set_password(save.password); this->set_sta(sta); + // tells ESPHome yaml file boot routine that WiFi credentials were successfully loaded loaded_creds = true; } + // tells ESPHome yaml file boot routine that an attempt was made to load credentials + // whether or not successful. tried_loading_creds = true; if (this->has_sta()) { diff --git a/ESPHome Source Files/components/wifi_component.h b/ESPHome Source Files/components/wifi_component.h index f4f1261..003311e 100644 --- a/ESPHome Source Files/components/wifi_component.h +++ b/ESPHome Source Files/components/wifi_component.h @@ -169,6 +169,9 @@ class WiFiComponent : public Component { void add_sta(const WiFiAP &ap); void clear_sta(); + // add function to clear stored wifi credentials to force AP without taking down wifi network. + void clear_stored_creds(); + /** Setup an Access Point that should be created if no connection to a station can be made. * * This can also be used without set_sta(). Then the AP will always be active. diff --git a/ESPHome Source Files/core/preferences.cpp b/ESPHome Source Files/core/preferences.cpp index 9358fe7..9ceb681 100644 --- a/ESPHome Source Files/core/preferences.cpp +++ b/ESPHome Source Files/core/preferences.cpp @@ -52,6 +52,8 @@ bool ESPPreferenceObject::save_() { return true; } +#ifdef ARDUINO_ARCH_ESP8266 + static const uint32_t ESP_RTC_USER_MEM_START = 0x60001200; #define ESP_RTC_USER_MEM ((uint32_t *) ESP_RTC_USER_MEM_START) static const uint32_t ESP_RTC_USER_MEM_SIZE_WORDS = 128; @@ -186,13 +188,15 @@ ESPPreferenceObject ESPPreferences::make_preference(size_t length, uint32_t type uint32_t start; // ESPHome just assigns addresses as they pop up, but we want to preserve all addresses so they always remain - // the same after an update and value are always loaded properly. Address is based on type/hash we receive, - // which will always be different for each entity. + // the same after an update and values are always loaded properly after update. Address is based on type/hash + // we receive, which will always be the same for each entity based on its name. Except WiFi, which is based on + // compile time but we have hard coded to the original compile time of the first public release. if ( type == 817087403 ) { start = 0; } // v1.5 - Blue LED else if ( type == 41191675 ) { start = 2; } // v1.5 - Relay else if ( type == 3932521563 ) { start = 4; } // v1.5 - Use Threshold else if ( type == 1903527169 ) { start = 6; } // v1.5 - Total Daily Energy - else if ( type == 1432266978 ) { start = 8; } // v1.5 - Wi-Fi Credentials + else if ( type == 1432266978 ) { start = 8; } // v1.5 - WiFi Credentials + else if ( type == 0 ) { start = 8; } // v1.7 - Clear Wifi Credentials else if ( type == 3616613942 ) { start = 34; } // v1.6 - Select 1 (Button or LED) else if ( type == 3104663617 ) { start = 36; } // v1.6 - Select 2 (Button or LED) else if ( type == 629479035 ) { start = 38; } // v1.6 - Force AP Global Variable @@ -245,7 +249,76 @@ ESPPreferenceObject ESPPreferences::make_preference(size_t length, uint32_t type } void ESPPreferences::prevent_write(bool prevent) { this->prevent_write_ = prevent; } bool ESPPreferences::is_prevent_write() { return this->prevent_write_; } +#endif + +#ifdef ARDUINO_ARCH_ESP32 +bool ESPPreferenceObject::save_internal_() { + if (global_preferences.nvs_handle_ == 0) + return false; + + char key[32]; + sprintf(key, "%u", this->offset_); + uint32_t len = (this->length_words_ + 1) * 4; + esp_err_t err = nvs_set_blob(global_preferences.nvs_handle_, key, this->data_, len); + if (err) { + ESP_LOGV(TAG, "nvs_set_blob('%s', len=%u) failed: %s", key, len, esp_err_to_name(err)); + return false; + } + err = nvs_commit(global_preferences.nvs_handle_); + if (err) { + ESP_LOGV(TAG, "nvs_commit('%s', len=%u) failed: %s", key, len, esp_err_to_name(err)); + return false; + } + return true; +} +bool ESPPreferenceObject::load_internal_() { + if (global_preferences.nvs_handle_ == 0) + return false; + + char key[32]; + sprintf(key, "%u", this->offset_); + size_t len = (this->length_words_ + 1) * 4; + size_t actual_len; + esp_err_t err = nvs_get_blob(global_preferences.nvs_handle_, key, nullptr, &actual_len); + if (err) { + ESP_LOGV(TAG, "nvs_get_blob('%s'): %s - the key might not be set yet", key, esp_err_to_name(err)); + return false; + } + if (actual_len != len) { + ESP_LOGVV(TAG, "NVS length does not match. Assuming key changed (%u!=%u)", actual_len, len); + return false; + } + err = nvs_get_blob(global_preferences.nvs_handle_, key, this->data_, &len); + if (err) { + ESP_LOGV(TAG, "nvs_get_blob('%s') failed: %s", key, esp_err_to_name(err)); + return false; + } + return true; +} +ESPPreferences::ESPPreferences() : current_offset_(0) {} +void ESPPreferences::begin() { + auto ns = truncate_string(App.get_name(), 15); + esp_err_t err = nvs_open(ns.c_str(), NVS_READWRITE, &this->nvs_handle_); + if (err) { + ESP_LOGW(TAG, "nvs_open failed: %s - erasing NVS...", esp_err_to_name(err)); + nvs_flash_deinit(); + nvs_flash_erase(); + nvs_flash_init(); + + err = nvs_open(ns.c_str(), NVS_READWRITE, &this->nvs_handle_); + if (err) { + this->nvs_handle_ = 0; + } + } +} + +ESPPreferenceObject ESPPreferences::make_preference(size_t length, uint32_t type, bool in_flash) { + auto pref = ESPPreferenceObject(this->current_offset_, length, type); + this->current_offset_++; + return pref; +} +#endif uint32_t ESPPreferenceObject::calculate_crc_() const { uint32_t crc = this->type_; for (size_t i = 0; i < this->length_words_; i++) { |