From 17461a2a843f57d368d0f4c181f5750709c9f86b Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 8 Apr 2022 22:31:51 +0200 Subject: [PATCH] add button to restart to wifi configuration --- CMakeLists.txt | 3 ++- Network.cpp | 20 +++++++++++++++++++- Network.h | 4 ++++ SpiffsCookie.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ SpiffsCookie.h | 13 +++++++++++++ WebCfgServer.cpp | 18 ++++++++++++++++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 SpiffsCookie.cpp create mode 100644 SpiffsCookie.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 61f8baa..bbe83a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ file(GLOB SRCFILES WebCfgServer.cpp PresenceDetection.cpp PreferencesKeys.h + SpiffsCookie.cpp Version.h lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp lib/ESP32_BLE_Arduino-1.0.1/src/*.h @@ -59,9 +60,9 @@ target_link_arduino_libraries(${PROJECT_NAME} WebServer DNSServer Preferences + SPIFFS # esp32 # Wire -# SPIFFS # FS ) diff --git a/Network.cpp b/Network.cpp index db36112..78824e8 100644 --- a/Network.cpp +++ b/Network.cpp @@ -26,7 +26,18 @@ void Network::initialize() // these are stored by the esp library //wm.resetSettings(); - bool res = wm.autoConnect(); // password protected ap + bool res = false; + + if(_cookie.isSet()) + { + Serial.println(F("Opening WiFi configuration portal.")); + res = wm.startConfigPortal(); + _cookie.clear(); + } + else + { + res = wm.autoConnect(); // password protected ap + } if(!res) { Serial.println(F("Failed to connect. Wait for ESP restart.")); @@ -318,3 +329,10 @@ void Network::buildMqttPath(const char* path, char* outPath) } outPath[i+1] = 0x00; } + +void Network::restartAndConfigureWifi() +{ + _cookie.set(); + delay(200); + ESP.restart(); +} diff --git a/Network.h b/Network.h index abecfdb..f4c5df1 100644 --- a/Network.h +++ b/Network.h @@ -4,6 +4,7 @@ #include #include #include "NukiConstants.h" +#include "SpiffsCookie.h" class Network { @@ -22,6 +23,8 @@ public: void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value)); + void restartAndConfigureWifi(); + private: static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length); void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length); @@ -38,6 +41,7 @@ private: PubSubClient _mqttClient; WiFiClient _wifiClient; Preferences* _preferences; + SpiffsCookie _cookie; bool _mqttConnected = false; diff --git a/SpiffsCookie.cpp b/SpiffsCookie.cpp new file mode 100644 index 0000000..95d592f --- /dev/null +++ b/SpiffsCookie.cpp @@ -0,0 +1,43 @@ +#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 new file mode 100644 index 0000000..25969ed --- /dev/null +++ b/SpiffsCookie.h @@ -0,0 +1,13 @@ +#pragma once + +class SpiffsCookie +{ +public: + SpiffsCookie(); + virtual ~SpiffsCookie() = default; + + void set(); + void clear(); + const bool isSet(); + +}; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c42970e..6977677 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -152,6 +152,13 @@ bool WebCfgServer::processArgs() _preferences->putString(preference_cred_password, value); configChanged = true; } + else if(key == "WIFICONF") + { + if(value == _preferences->getString(preference_cred_password)) + { + _network->restartAndConfigureWifi(); + } + } } if(clearMqttCredentials) @@ -234,6 +241,17 @@ void WebCfgServer::buildHtml(String& response) response.concat(""); response.concat(""); + response.concat("
"); + + response.concat("

Wifi

"); + response.concat(""); + printInputField(response, "WIFICONF", "Type password to confirm", "", 20, true); + response.concat("
"); + + response.concat("
"); + + response.concat("


"); + response.concat("\n"); response.concat("\n"); }