Save Settings to flash

This commit is contained in:
fvanroie 2020-01-25 01:47:49 +01:00
parent d3265a0d98
commit 167f45e630
8 changed files with 299 additions and 199 deletions

View File

@ -121,7 +121,7 @@ build_flags =
-D TFT_SCLK=14 ;D5
-D TFT_DC=15 ;D8
-D TFT_CS=16 ;D0
-D TFT_BCKL=5 ;D1
-D TFT_BCKL=5 ;D1
-D TFT_RST=-1 ;RST
-D TOUCH_CS=0 ;D3 (can also be D1 or D4)
-D SPI_FREQUENCY=40000000

View File

@ -24,6 +24,7 @@
#include "hasp_debug.h"
#include "hasp_config.h"
#include "hasp_mqtt.h"
#include "hasp_wifi.h"
#include "hasp_gui.h"
#include "hasp_tft.h"
#include "hasp.h"
@ -83,12 +84,15 @@ static const char * btnm_map2[] = {"0", "1", "\n", "2", "3", "\n", "4", "5",
"\n", "6", "7", "\n", "P1", "P2", "P3", ""};
*/
static lv_font_t * haspFonts[6];
#if defined(ARDUINO_ARCH_ESP8266)
lv_obj_t * pages[4];
// lv_style_t styles[6];
static lv_obj_t * pages[4];
static lv_style_t labelStyles[6];
static lv_style_t rollerStyles[6];
#else
lv_obj_t * pages[12];
// lv_style_t styles[20];
static lv_obj_t * pages[12];
static lv_style_t labelStyles[6];
static lv_style_t rollerStyles[6];
#endif
uint16_t current_page = 0;
// uint16_t current_style = 0;
@ -1019,10 +1023,12 @@ void haspReset()
{
mqttStop(); // Stop the MQTT Client first
configWriteConfig();
debugPrintln(F("HASP: Reboot MCU"));
debugStop();
delay(1000);
ESP.restart(); // nextionReset();
delay(250);
wifiStop();
debugPrintln(F("HASP: Properly Rebooting the MCU now!"));
debugPrintln(F("-------------------------------------"));
ESP.restart();
delay(5000);
}

View File

@ -112,10 +112,10 @@ void configWriteConfig()
changed |= haspGetConfig(settings[F("hasp")].to<JsonObject>());
// changed |= httpGetConfig(settings[F("http")].to<JsonObject>());
// changed |= mdnsGetConfig(settings[F("mdns")].to<JsonObject>());
// changed |= mqttGetConfig(settings[F("mqtt")].to<JsonObject>());
changed |= mqttGetConfig(settings[F("mqtt")].to<JsonObject>());
// changed |= otaGetConfig(settings[F("ota")].to<JsonObject>());
// changed |= tftGetConfig(settings[F("tft")].to<JsonObject>());
// changed |= wifiGetConfig(settings[F("wifi")].to<JsonObject>());
changed |= wifiGetConfig(settings[F("wifi")].to<JsonObject>());
if(changed) {
File file = SPIFFS.open(HASP_CONFIG_FILE, "w");

View File

@ -246,6 +246,26 @@ void webHandleRoot()
webServer.sendContent_P(HTTP_END); // 20
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void httpHandleReboot()
{ // http://plate01/reboot
if(!httpIsAuthenticated(F("/reboot"))) return;
String nodename = haspGetNodename();
String httpMessage = F("Rebooting Device");
webSendPage(nodename, httpMessage.length(), true);
webServer.sendContent(httpMessage); // len
webServer.sendContent_P(HTTP_END); // 20
delay(500);
debugPrintln(PSTR("HTTP: Reboot device"));
haspSetPage(0);
haspSetAttr(F("p[0].b[1].txt"), F("\"Rebooting...\""));
delay(500);
haspReset();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void webHandleAbout()
{ // http://plate01/about
@ -603,27 +623,28 @@ void webHandleConfig()
if(webServer.method() == HTTP_POST) {
if(webServer.hasArg(F("save"))) {
DynamicJsonDocument settings(256);
for(int i = 0; i < webServer.args(); i++) settings[webServer.argName(i)] = webServer.arg(i);
if(webServer.arg(F("save")) == String(F("hasp"))) {
/*
if(webServer.hasArg(F_CONFIG_THEME) settings[F_CONFIG_THEME] = webServer.arg(F_CONFIG_THEME).toInt();
if(webServer.hasArg(F_CONFIG_HUE) settings[F_CONFIG_HUE] = webServer.arg(F_CONFIG_HUE.toInt();
if(webServer.hasArg(F_CONFIG_ZIFONT) settings[F_CONFIG_ZIFONT] = webServer.arg(F_CONFIG_ZIFONT;
if(webServer.hasArg(F_CONFIG_PAGES) settings[F_CONFIG_PAGES] = webServer.arg(F_CONFIG_PAGES;
if(webServer.hasArg(F_CONFIG_STARTPAGE)
settings[F_CONFIG_STARTPAGE] = webServer.arg(F_CONFIG_STARTPAGE).toInt();*/
for(int i = 0; i < webServer.args(); i++) settings[webServer.argName(i)] = webServer.arg(i);
haspSetConfig(settings.as<JsonObject>());
} else if(webServer.arg(F("save")) == String(F("mqtt"))) {
mqttSetConfig(settings.as<JsonObject>());
} else if(webServer.arg(F("save")) == String(F("http"))) {
} else if(webServer.arg(F("save")) == String(F("wifi"))) {
wifiSetConfig(settings.as<JsonObject>());
}
}
}
if(WiFi.getMode() == WIFI_AP) {
httpHandleReboot();
}
char buffer[64];
String nodename = haspGetNodename();
String httpMessage((char *)0);
@ -666,101 +687,68 @@ void webHandleMqttConfig()
{ // http://plate01/config/mqtt
if(!httpIsAuthenticated(F("/config/mqtt"))) return;
DynamicJsonDocument settings(256);
mqttGetConfig(settings.to<JsonObject>());
char buffer[64];
String nodename = haspGetNodename();
String httpMessage((char *)0);
httpMessage.reserve(1024);
httpMessage += String(F("<form method='POST' action='/saveConfig'>"));
httpMessage += String(F("<b>WiFi SSID</b> <i><small>(required)</small></i><input id='wifiSSID' required "
"name='wifiSSID' maxlength=32 placeholder='WiFi SSID' value='")) +
String(WiFi.SSID()) + "'>";
httpMessage += String(F("<br/><b>WiFi Password</b> <i><small>(required)</small></i><input id='wifiPass' required "
"name='wifiPass' type='password' maxlength=64 placeholder='WiFi Password' value='")) +
String("********") + "'>";
httpMessage +=
F("<br/><br/><b>HASP Node Name</b> <i><small>(required. lowercase letters, numbers, and _ only)</small>"
"</i><input id='haspGetNodename()' required name='haspGetNodename()' maxlength=15 "
"placeholder='HASP Node Name' pattern='[a-z0-9_]*' value='");
httpMessage += nodename + "'>";
httpMessage += F("<br/><br/><b>Group Name</b> <i><small>(required)</small></i><input id='groupName' required "
"name='groupName' maxlength=15 placeholder='Group Name' value='");
// httpMessage += mqttGetGroup() + "'>";
httpMessage += F("<br/><br/><b>MQTT Broker</b> <i><small>(required)</small></i><input id='mqttServer' required "
"name='mqttServer' maxlength=63 placeholder='mqttServer' value='");
// httpMessage += mqttGetServer() + "'>";
httpMessage += F("<br/><b>MQTT Port</b> <i><small>(required)</small></i><input id='mqttPort' required "
"name='mqttPort' type='number' maxlength=5 placeholder='mqttPort' value='");
// httpMessage += String(mqttGetPort()) + "'>";
httpMessage += F("<br/><b>MQTT User</b> <i><small>(optional)</small></i><input id='mqttUser' name='mqttUser' "
"maxlength=31 placeholder='mqttUser' value='");
// httpMessage += mqttGetUser() + "'>";
httpMessage += F("<br/><b>MQTT Password</b> <i><small>(optional)</small></i><input id='mqttPassword' "
"name='mqttPassword' type='password' maxlength=31 placeholder='mqttPassword' value='");
// if(mqttGetPassword() != "") httpMessage += String("********");
httpMessage += String(F("'><br/><br/><b>HASP Admin Username</b> <i><small>(optional)</small></i><input "
"id='httpUser' name='httpUser' maxlength=31 placeholder='Admin User' value='")) +
String(httpUser) + "'>";
httpMessage +=
String(F("<br/><b>HASP Admin Password</b> <i><small>(optional)</small></i><input id='httpPassword' "
"name='httpPassword' type='password' maxlength=31 placeholder='Admin User Password' value='"));
if(httpPassword.length() != 0) {
httpMessage += String("********");
}
httpMessage +=
String(F("'><br/><hr><b>Motion Sensor Pin:&nbsp;</b><select id='motionPinConfig' name='motionPinConfig'>"));
httpMessage += String(F("<option value='0'"));
if(!motionPin) {
httpMessage += String(F(" selected"));
}
httpMessage += String(F(">disabled/not installed</option><option value='D0'"));
if(motionPin == D0) {
httpMessage += String(F(" selected"));
}
httpMessage += String(F(">D0</option><option value='D1'"));
if(motionPin == D1) {
httpMessage += String(F(" selected"));
}
httpMessage += String(F(">D1</option><option value='D2'"));
if(motionPin == D2) {
httpMessage += String(F(" selected"));
}
httpMessage += String(F(">D2</option></select>"));
httpMessage += String(F("<br/><b>Serial debug output enabled:</b><input id='debugSerialEnabled' "
"name='debugSerialEnabled' type='checkbox'"));
if(debugSerialEnabled) {
httpMessage += String(F(" checked='checked'"));
}
httpMessage += String(F("><br/><b>Telnet debug output enabled:</b><input id='debugTelnetEnabled' "
"name='debugTelnetEnabled' type='checkbox'"));
if(debugTelnetEnabled) {
httpMessage += String(F(" checked='checked'"));
}
httpMessage += String(F("><br/><b>mDNS enabled:</b><input id='mdnsEnabled' name='mdnsEnabled' type='checkbox'>"));
/*if (mdnsEnabled)
{
httpMessage += String(F(" checked='checked'"));
}
httpMessage += String(F("><br/><hr><button type='submit'>save settings</button></form>"));
if (updateEspAvailable)
{
httpMessage += String(F("<br/><hr><font color='green'><center><h3>HASP Update
available!</h3></center></font>")); httpMessage += String(F("<form method='get' action='espfirmware'>"));
httpMessage += String(F("<input id='espFirmwareURL' type='hidden' name='espFirmware' value='")) +
espFirmwareUrl
+ "'>"; httpMessage += String(F("<button type='submit'>update HASP to v")) + String(updateEspAvailableVersion) +
String(F("</button></form>"));
}*/
httpMessage += F("<button type='submit' name='save' "
"value='mqtt'>Save Settings</button></form>");
httpMessage += F("<p><form method='get' action='resetConfig'><button class='red' type='submit'>Factory Reset "
"All Settings</button></form></p>");
httpMessage += String(F("<form method='POST' action='/config'>"));
httpMessage += F("<b>HASP Node Name</b> <i><small>(required. lowercase letters, numbers, and _ only)</small>"
"</i><input id='haspGetNodename()' required name='haspGetNodename()' maxlength=15 "
"placeholder='HASP Node Name' pattern='[a-z0-9_]*' value='");
httpMessage += nodename;
httpMessage += F("'><br/><br/><b>Group Name</b> <i><small>(required)</small></i><input id='group' required "
"name='group' maxlength=15 placeholder='Group Name' value='");
httpMessage += settings[FPSTR(F_CONFIG_GROUP)].as<String>();
httpMessage += F("'><br/><br/><b>MQTT Broker</b> <i><small>(required)</small></i><input id='host' required "
"name='host' maxlength=63 placeholder='mqttServer' value='");
httpMessage += settings[FPSTR(F_CONFIG_HOST)].as<String>();
httpMessage += F("'><br/><b>MQTT Port</b> <i><small>(required)</small></i><input id='port' required "
"name='port' type='number' maxlength=5 placeholder='mqttPort' value='");
httpMessage += settings[FPSTR(F_CONFIG_PORT)].as<uint16_t>();
httpMessage += F("'><br/><b>MQTT User</b> <i><small>(optional)</small></i><input id='mqttUser' name='user' "
"maxlength=31 placeholder='user' value='");
httpMessage += settings[FPSTR(F_CONFIG_USER)].as<String>();
httpMessage += F("'><br/><b>MQTT Password</b> <i><small>(optional)</small></i><input id='pass' "
"name='pass' type='password' maxlength=31 placeholder='mqttPassword' value='");
if(settings[FPSTR(F_CONFIG_PASS)].as<String>() != "") httpMessage += F("********");
httpMessage += F("'><button type='submit' name='save' value='mqtt'>Save Settings</button></form>");
httpMessage += F("<form method='get' action='/config'><button type='submit'>Configuration</button></form>");
webSendPage(nodename, httpMessage.length(), false);
webServer.sendContent(httpMessage); // len
webServer.sendContent_P(HTTP_END); // 20
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
#if LV_USE_HASP_WIFI > 0
void webHandleWifiConfig()
{ // http://plate01/config/wifi
if(!httpIsAuthenticated(F("/config/wifi"))) return;
DynamicJsonDocument settings(256);
wifiGetConfig(settings.to<JsonObject>());
char buffer[64];
String nodename = haspGetNodename();
String httpMessage((char *)0);
httpMessage.reserve(1024);
httpMessage += String(F("<form method='POST' action='/config'>"));
httpMessage += String(F("<b>WiFi SSID</b> <i><small>(required)</small></i><input id='ssid' required "
"name='ssid' maxlength=32 placeholder='WiFi SSID' value='"));
httpMessage += settings[FPSTR(F_CONFIG_SSID)].as<String>();
httpMessage += String(F("'><br/><b>WiFi Password</b> <i><small>(required)</small></i><input id='pass' required "
"name='pass' type='password' maxlength=64 placeholder='WiFi Password' value='"));
if(settings[FPSTR(F_CONFIG_PASS)].as<String>() != "") {
httpMessage += F("********");
}
httpMessage += F("'><button type='submit' name='save' value='wifi'>Save Settings</button></form>");
httpMessage += F("<form method='get' action='/config'><button type='submit'>Configuration</button></form>");
webSendPage(nodename, httpMessage.length(), false);
@ -770,7 +758,6 @@ void webHandleMqttConfig()
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
#if LV_USE_HASP_MQTT > 0
void webHandleHaspConfig()
{ // http://plate01/config/http
if(!httpIsAuthenticated(F("/config/hasp"))) return;
@ -863,7 +850,7 @@ void webHandleHaspConfig()
webServer.sendContent(httpMessage); // len
webServer.sendContent_P(HTTP_END); // 20
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
void httpHandleNotFound()
{ // webServer 404
@ -922,26 +909,6 @@ void httpHandleEspFirmware()
// espStartOta(webServer.arg("espFirmware"));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void httpHandleReboot()
{ // http://plate01/reboot
if(!httpIsAuthenticated(F("/reboot"))) return;
String nodename = haspGetNodename();
String httpMessage = F("Rebooting Device");
webSendPage(nodename, httpMessage.length(), true);
webServer.sendContent(httpMessage); // len
webServer.sendContent_P(HTTP_END); // 20
delay(500);
debugPrintln(PSTR("HTTP: Reboot device"));
haspSetPage(0);
haspSetAttr(F("p[0].b[1].txt"), F("\"Rebooting...\""));
delay(500);
haspReset();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void httpHandleResetConfig()
{ // http://plate01/resetConfig
@ -1022,6 +989,9 @@ void httpSetup(const JsonObject & settings)
webServer.on(F("/config/hasp"), webHandleHaspConfig);
#if LV_USE_HASP_MQTT > 0
webServer.on(F("/config/mqtt"), webHandleMqttConfig);
#endif
#if LV_USE_HASP_WIFI > 0
webServer.on(F("/config/wifi"), webHandleWifiConfig);
#endif
webServer.on(F("/saveConfig"), webHandleSaveConfig);
webServer.on(F("/resetConfig"), httpHandleResetConfig);
@ -1036,9 +1006,16 @@ void httpReconnect()
{
if(!httpEnable) return;
if(WiFi.getMode() == WIFI_AP) {
webServer.on(F("/"), webHandleWifiConfig);
webServer.on(F("/config"), webHandleConfig);
webServer.onNotFound(webHandleWifiConfig);
}
webServer.stop();
webServer.begin();
debugPrintln(String(F("HTTP: Server started @ http://")) + WiFi.localIP().toString());
debugPrintln(String(F("HTTP: Server started @ http://")) +
(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString() : WiFi.localIP().toString()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1050,14 +1027,10 @@ void httpLoop(bool wifiIsConnected)
////////////////////////////////////////////////////////////////////////////////////////////////////
bool httpGetConfig(const JsonObject & settings)
{
if(!settings.isNull() && settings[F_HTTP_ENABLE] == httpEnable && settings[F_HTTP_PORT] == httpPort &&
settings[F_HTTP_USER] == httpUser && settings[F_HTTP_PASS] == httpPassword)
return false;
settings[F_HTTP_ENABLE] = httpEnable;
settings[F_HTTP_PORT] = httpPort;
settings[F_HTTP_USER] = httpUser;
settings[F_HTTP_PASS] = httpPassword;
settings[FPSTR(F_HTTP_ENABLE)] = httpEnable;
settings[FPSTR(F_HTTP_PORT)] = httpPort;
settings[FPSTR(F_HTTP_USER)] = httpUser;
settings[FPSTR(F_HTTP_PASS)] = httpPassword;
size_t size = serializeJson(settings, Serial);
Serial.println();

View File

@ -42,15 +42,31 @@ String mqttLightBrightStateTopic; // MQTT topic for outgoing panel backlight d
String mqttNodeTopic;
String mqttGroupTopic;
bool mqttEnabled;
////////////////////////////////////////////////////////////////////////////////////////////////////
// These defaults may be overwritten with values saved by the web interface
char mqttServer[64] = MQTT_HOST;
uint16_t mqttPort = MQTT_PORT;
char mqttUser[32] = MQTT_USER;
char mqttPassword[32] = MQTT_PASSW;
// char haspNode[16] = "plate01";
String mqttGroupName = "plates";
#ifdef MQTT_HOST
std::string mqttServer = MQTT_HOST;
#else
std::string mqttServer = "";
#endif
#ifdef MQTT_PORT
uint16_t mqttPort = MQTT_PORT;
#else
uint16_t mqttPort = 1883;
#endif
#ifdef MQTT_USER
std::string mqttUser = MQTT_USER;
#else
std::string mqttUser = "";
#endif
#ifdef MQTT_PASSW
std::string mqttPassword = MQTT_PASSW;
#else
std::string mqttPassword = "";
#endif
std::string mqttGroupName = "plates";
/*
const String mqttCommandSubscription = mqttCommandTopic + "/#";
@ -278,12 +294,13 @@ void mqttReconnect()
mqttGroupTopic = topicBuffer;
// haspSetPage(0);
debugPrintln(String(F("MQTT: Attempting connection to broker ")) + String(mqttServer) + String(F(" as clientID ")) +
mqttClientId);
debugPrintln(String(F("MQTT: Attempting connection to broker ")) + String(mqttServer.c_str()) +
String(F(" as clientID ")) + mqttClientId);
// Attempt to connect and set LWT and Clean Session
sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str());
if(!mqttClient.connect(mqttClientId.c_str(), mqttUser, mqttPassword, topicBuffer, 0, false, "OFF", true)) {
if(!mqttClient.connect(mqttClientId.c_str(), mqttUser.c_str(), mqttPassword.c_str(), topicBuffer, 0, false, "OFF",
true)) {
// Retry until we give up and restart after connectTimeout seconds
mqttReconnectCount++;
@ -353,28 +370,19 @@ void mqttReconnect()
void mqttSetup(const JsonObject & settings)
{
if(!settings[F_CONFIG_HOST].isNull()) {
strcpy(mqttServer, settings[F_CONFIG_HOST]);
}
if(!settings[F_CONFIG_PORT].isNull()) {
mqttPort = settings[F_CONFIG_PORT];
}
if(!settings[F_CONFIG_USER].isNull()) {
strcpy(mqttUser, settings[F_CONFIG_USER]);
}
if(!settings[F_CONFIG_PASS].isNull()) {
strcpy(mqttPassword, settings[F_CONFIG_PASS]);
}
if(!settings[F_CONFIG_GROUP].isNull()) {
mqttGroupName = settings[F_CONFIG_GROUP].as<String>();
}
mqttSetConfig(settings);
mqttClient.setServer(mqttServer, 1883);
mqttEnabled = mqttServer != "" && mqttPort > 0;
if(!mqttEnabled) return;
mqttClient.setServer(mqttServer.c_str(), 1883);
mqttClient.setCallback(mqttCallback);
}
void mqttLoop(bool wifiIsConnected)
{
if(!mqttEnabled) return;
if(wifiIsConnected && !mqttClient.connected())
mqttReconnect();
else
@ -404,19 +412,71 @@ void mqttStop()
bool mqttGetConfig(const JsonObject & settings)
{
if(!settings.isNull() && settings[F_CONFIG_HOST] == mqttServer && settings[F_CONFIG_PORT] == mqttPort &&
settings[F_CONFIG_USER] == mqttUser && settings[F_CONFIG_PASS] == mqttPassword &&
settings[F_CONFIG_GROUP] == mqttGroupName)
return false;
settings[F_CONFIG_GROUP] = mqttGroupName;
settings[F_CONFIG_HOST] = mqttServer;
settings[F_CONFIG_PORT] = mqttPort;
settings[F_CONFIG_USER] = mqttUser;
settings[F_CONFIG_PASS] = mqttPassword;
settings[FPSTR(F_CONFIG_GROUP)] = String(mqttGroupName.c_str());
settings[FPSTR(F_CONFIG_HOST)] = String(mqttServer.c_str());
settings[FPSTR(F_CONFIG_PORT)] = mqttPort;
settings[FPSTR(F_CONFIG_USER)] = String(mqttUser.c_str());
settings[FPSTR(F_CONFIG_PASS)] = String(mqttPassword.c_str());
size_t size = serializeJson(settings, Serial);
Serial.println();
return true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool mqttSetConfig(const JsonObject & settings)
{
bool changed = false;
if(!settings[FPSTR(F_CONFIG_GROUP)].isNull()) {
if(mqttGroupName != settings[FPSTR(F_CONFIG_GROUP)].as<String>().c_str()) {
debugPrintln(F("mqttGroupName changed"));
}
changed |= mqttGroupName != settings[FPSTR(F_CONFIG_GROUP)].as<String>().c_str();
mqttGroupName = settings[FPSTR(F_CONFIG_GROUP)].as<String>().c_str();
}
if(!settings[FPSTR(F_CONFIG_HOST)].isNull()) {
if(mqttServer != settings[FPSTR(F_CONFIG_HOST)].as<String>().c_str()) {
debugPrintln(F("mqttServer changed"));
}
changed |= mqttServer != settings[FPSTR(F_CONFIG_HOST)].as<String>().c_str();
mqttServer = settings[FPSTR(F_CONFIG_HOST)].as<String>().c_str();
}
if(!settings[FPSTR(F_CONFIG_PORT)].isNull()) {
if(mqttPort != settings[FPSTR(F_CONFIG_PORT)].as<uint16_t>()) {
debugPrintln(F("mqttPort changed"));
}
changed |= mqttPort != settings[FPSTR(F_CONFIG_PORT)].as<uint16_t>();
mqttPort = settings[FPSTR(F_CONFIG_PORT)].as<uint16_t>();
}
if(!settings[FPSTR(F_CONFIG_USER)].isNull()) {
if(mqttUser != settings[FPSTR(F_CONFIG_USER)].as<String>().c_str()) {
debugPrintln(F("mqttUser changed"));
}
changed |= mqttUser != settings[FPSTR(F_CONFIG_USER)].as<String>().c_str();
mqttUser = settings[FPSTR(F_CONFIG_USER)].as<String>().c_str();
}
if(!settings[FPSTR(F_CONFIG_PASS)].isNull()) {
if(mqttPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str()) {
debugPrintln(F("mqttPassword changed"));
}
changed |= mqttPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
mqttPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
}
size_t size = serializeJson(settings, Serial);
Serial.println();
return changed;
}

View File

@ -7,7 +7,6 @@ void mqttSetup(const JsonObject & settings);
void mqttLoop(bool wifiIsConnected);
void mqttStop();
void mqttReconnect();
bool mqttGetConfig(const JsonObject & settings);
void mqttSendNewEvent(uint8_t pageid, uint8_t btnid, int32_t val);
void mqttSendNewValue(uint8_t pageid, uint8_t btnid, int32_t val);
@ -16,4 +15,7 @@ void mqttHandlePage(String strPageid);
bool mqttIsConnected(void);
bool mqttGetConfig(const JsonObject & settings);
bool mqttSetConfig(const JsonObject & settings);
#endif

View File

@ -20,11 +20,22 @@
static WiFiEventHandler wifiEventHandler[3];
#endif
#include "DNSserver.h"
#include "user_config_override.h"
std::string wifiSsid = WIFI_SSID;
#ifdef WIFI_SSID
std::string wifiSsid = WIFI_SSID;
#else
std::string wifiSsid = "";
#endif
#ifdef WIFI_PASSW
std::string wifiPassword = WIFI_PASSW;
#else
std::string wifiPassword = "";
#endif
const byte DNS_PORT = 53;
DNSServer dnsServer;
// long wifiPrevMillis = 0;
// bool wifiWasConnected = false;
@ -101,33 +112,40 @@ void wifiSetup(JsonObject settings)
{
char buffer[64];
if(!settings[F_CONFIG_SSID].isNull()) {
wifiSsid = settings[F_CONFIG_SSID].as<String>().c_str();
wifiSetConfig(settings);
// sprintf_P(buffer, PSTR("Wifi Ssid: %s"), wifiSsid.c_str());
// debugPrintln(buffer);
}
if(!settings[F_CONFIG_PASS].isNull()) {
wifiPassword = settings[F_CONFIG_PASS].as<String>().c_str();
if(wifiSsid == "") {
WiFi.mode(WIFI_AP);
WiFi.softAP("HASP-test", "haspadmin");
IPAddress IP = WiFi.softAPIP();
// sprintf_P(buffer, PSTR("Wifi Password: %s"), wifiPassword.c_str());
// debugPrintln(buffer);
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", IP);
sprintf_P(buffer, PSTR("WIFI: Setting up temporary Access Point"));
debugPrintln(buffer);
sprintf_P(buffer, PSTR("WIFI: AP IP address : %s"), IP.toString().c_str());
debugPrintln(buffer);
httpReconnect();
return;
}
WiFi.mode(WIFI_STA);
sprintf_P(buffer, PSTR("WIFI: Connecting to : %s"), wifiSsid.c_str());
debugPrintln(buffer);
WiFi.mode(WIFI_STA);
WiFi.begin(wifiSsid.c_str(), wifiPassword.c_str());
#if defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback);
#endif
#if defined(ARDUINO_ARCH_ESP8266)
wifiEventHandler[0] = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client
wifiEventHandler[1] = WiFi.onStationModeDisconnected(wifiSTADisconnected);
wifiEventHandler[2] = WiFi.onStationModeConnected(wifiSTAConnected);
#endif
#if defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback);
#endif
WiFi.begin(wifiSsid.c_str(), wifiPassword.c_str());
}
bool wifiLoop()
@ -165,15 +183,53 @@ bool wifiLoop()
bool wifiGetConfig(const JsonObject & settings)
{
if(!settings.isNull() && settings[F_CONFIG_SSID] == String(wifiSsid.c_str()) &&
settings[F_CONFIG_PASS] == String(wifiPassword.c_str()))
return false;
settings[F_CONFIG_SSID] = String(wifiSsid.c_str());
settings[F_CONFIG_PASS] = String(wifiPassword.c_str());
settings[FPSTR(F_CONFIG_SSID)] = String(wifiSsid.c_str());
settings[FPSTR(F_CONFIG_PASS)] = String(wifiPassword.c_str());
size_t size = serializeJson(settings, Serial);
Serial.println();
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool wifiSetConfig(const JsonObject & settings)
{
/* if(!settings.isNull() && settings[FPSTR(F_CONFIG_STARTPAGE)] == haspStartPage &&
settings[FPSTR(F_CONFIG_THEME)] == haspThemeId && settings[FPSTR(F_CONFIG_HUE)] == haspThemeHue &&
settings[FPSTR(F_CONFIG_ZIFONT)] == haspZiFontPath && settings[FPSTR(F_CONFIG_PAGES)] == haspPagesPath)
return false;
*/
bool changed = false;
if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) {
if(wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str()) {
debugPrintln(F("wifiSsid changed"));
}
changed |= wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
wifiSsid = settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
}
if(!settings[FPSTR(F_CONFIG_PASS)].isNull() && settings[FPSTR(F_CONFIG_PASS)].as<String>() != F("********")) {
if(wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str()) {
debugPrintln(F("wifiPassword changed"));
}
changed |= wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
wifiPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
}
size_t size = serializeJson(settings, Serial);
Serial.println();
return changed;
}
void wifiStop()
{
debugPrintln(F("WIFI: Stopped"));
WiFi.mode(WIFI_OFF);
WiFi.disconnect();
}

View File

@ -5,6 +5,9 @@
void wifiSetup(JsonObject settings);
bool wifiLoop();
void wifiStop();
bool wifiGetConfig(const JsonObject & settings);
bool wifiSetConfig(const JsonObject & settings);
#endif