From e9fa988918ffbdde466c12f21b9b901a8d4bae20 Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 8 Jul 2022 22:53:23 +0200 Subject: [PATCH] use RTC_NOINIT_ATTR to boot into wifimanager config portal --- CMakeLists.txt | 1 - NetworkLock.h | 1 - NetworkOpener.h | 1 - SpiffsCookie.cpp | 43 ----------------------------------- SpiffsCookie.h | 13 ----------- main.cpp | 4 ++-- networkDevices/WifiDevice.cpp | 12 ++++++---- networkDevices/WifiDevice.h | 4 ++-- 8 files changed, 12 insertions(+), 67 deletions(-) delete mode 100644 SpiffsCookie.cpp delete mode 100644 SpiffsCookie.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b2e6563..05203d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ file(GLOB SRCFILES WebCfgServer.cpp PresenceDetection.cpp PreferencesKeys.h - SpiffsCookie.cpp Gpio.cpp Version.h # include/RTOS.h diff --git a/NetworkLock.h b/NetworkLock.h index cc066d3..16ddba7 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -7,7 +7,6 @@ #include #include #include "NukiConstants.h" -#include "SpiffsCookie.h" #include "NukiLockConstants.h" #include "Network.h" diff --git a/NetworkOpener.h b/NetworkOpener.h index 7eec66c..4dcc428 100644 --- a/NetworkOpener.h +++ b/NetworkOpener.h @@ -7,7 +7,6 @@ #include #include #include "NukiConstants.h" -#include "SpiffsCookie.h" #include "NukiOpenerConstants.h" #include "NetworkLock.h" diff --git a/SpiffsCookie.cpp b/SpiffsCookie.cpp deleted file mode 100644 index 95d592f..0000000 --- a/SpiffsCookie.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "SpiffsCookie.h" -#include "FS.h" -#include "SPIFFS.h" - -SpiffsCookie::SpiffsCookie() -{ - if(!SPIFFS.begin(true)) - { - Serial.println(F("SPIFFS Mount Failed")); - } -} - -void SpiffsCookie::set() -{ - File file = SPIFFS.open("/cookie", FILE_WRITE); - if(!file) - { - Serial.println(F("- failed to open file for writing")); - return; - } - - if(file.write('#')) - { - Serial.println(F("- file written")); - } else { - Serial.println(F("- write failed")); - } - file.close(); -} - -void SpiffsCookie::clear() -{ - if(!SPIFFS.remove("/cookie")) - { - Serial.println(F("Failed to remove file")); - } - -} - -const bool SpiffsCookie::isSet() -{ - return SPIFFS.exists("/cookie"); -} diff --git a/SpiffsCookie.h b/SpiffsCookie.h deleted file mode 100644 index 25969ed..0000000 --- a/SpiffsCookie.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -class SpiffsCookie -{ -public: - SpiffsCookie(); - virtual ~SpiffsCookie() = default; - - void set(); - void clear(); - const bool isSet(); - -}; diff --git a/main.cpp b/main.cpp index 1b598f1..0e2e4e5 100644 --- a/main.cpp +++ b/main.cpp @@ -173,8 +173,8 @@ void setup() restartTs = preferences->getInt(preference_restart_timer) * 60 * 1000; } -// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi; - const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500; + const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi; +// const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500; network = new Network(networkDevice, preferences); network->initialize(); diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 2ceff42..8c3c564 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -2,9 +2,15 @@ #include "WifiDevice.h" #include "../PreferencesKeys.h" +RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17]; + WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) : NetworkDevice(hostname) { + WiFiDevice_reconfdetect[16] = 0x00; + _startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0; + memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect); + _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); size_t caLength = _preferences->getString(preference_mqtt_ca,_ca,TLS_CA_MAX_SIZE); @@ -52,10 +58,9 @@ void WifiDevice::initialize() bool res = false; - if(_cookie.isSet()) + if(_startAp) { Serial.println(F("Opening WiFi configuration portal.")); - _cookie.clear(); res = _wm.startConfigPortal(); } else @@ -86,8 +91,7 @@ void WifiDevice::initialize() void WifiDevice::reconfigure() { - _cookie.set(); - delay(200); + strcpy(WiFiDevice_reconfdetect, "reconfigure_wifi"); ESP.restart(); } diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 5d1c4ea..9161848 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -4,7 +4,6 @@ #include #include #include "NetworkDevice.h" -#include "../SpiffsCookie.h" #include "WiFiManager.h" class WifiDevice : public NetworkDevice @@ -30,8 +29,9 @@ private: WiFiClient* _wifiClient = nullptr; WiFiClientSecure* _wifiClientSecure = nullptr; PubSubClient* _mqttClient = nullptr; - SpiffsCookie _cookie; +// SpiffsCookie _cookie; bool _restartOnDisconnect = false; + bool _startAp = false; char _ca[TLS_CA_MAX_SIZE]; char _cert[TLS_CERT_MAX_SIZE];