add button to restart to wifi configuration

This commit is contained in:
technyon
2022-04-08 22:31:51 +02:00
parent 4303b0d877
commit 17461a2a84
6 changed files with 99 additions and 2 deletions

View File

@@ -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
)

View File

@@ -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();
}

View File

@@ -4,6 +4,7 @@
#include <WiFiClient.h>
#include <Preferences.h>
#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;

43
SpiffsCookie.cpp Normal file
View File

@@ -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");
}

13
SpiffsCookie.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
class SpiffsCookie
{
public:
SpiffsCookie();
virtual ~SpiffsCookie() = default;
void set();
void clear();
const bool isSet();
};

View File

@@ -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("<button type=\"submit\">Edit</button>");
response.concat("</form>");
response.concat("<FORM ACTION=method=get >");
response.concat("<BR><BR><h3>Wifi</h3>");
response.concat("<table>");
printInputField(response, "WIFICONF", "Type password to confirm", "", 20, true);
response.concat("</table>");
response.concat("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Restart and configure WiFi\">");
response.concat("</FORM><BR><BR>");
response.concat("</BODY>\n");
response.concat("</HTML>\n");
}