From bc8119accec4c5ee3cdb519f44a5f3ab90f2c388 Mon Sep 17 00:00:00 2001 From: Visualapproach Date: Thu, 18 Jul 2024 15:54:32 +0200 Subject: [PATCH] WiFi connect to last known if not saved + clarification on network config page --- Code/data/upload.html | 1 - Code/data/wifi.html | 14 ++-- Code/lib/BWC_unified/FW_VERSION.h | 2 +- Code/lib/BWC_unified/enums.h | 4 +- Code/src/main.cpp | 126 ++++++++++++++++-------------- Code/src/main.h | 2 +- 6 files changed, 78 insertions(+), 71 deletions(-) diff --git a/Code/data/upload.html b/Code/data/upload.html index 49faa32a..f82081b9 100644 --- a/Code/data/upload.html +++ b/Code/data/upload.html @@ -48,7 +48,6 @@
-
diff --git a/Code/data/wifi.html b/Code/data/wifi.html index 261ef614..e59020ad 100644 --- a/Code/data/wifi.html +++ b/Code/data/wifi.html @@ -43,7 +43,8 @@

Access Point:

- + @@ -62,7 +63,9 @@

Access Point:

Soft Access Point:

??
- +
??
@@ -133,10 +136,9 @@

NTP server:

Reset WiFi Config:

- This button resets the access point settings.
- The ESP will restart and start the "WiFi Configuration Manager". - Connect to it's access point and configure your WiFi (just like the first configuration). - Don't forget to disable WiFi Manager AP again if you want it off. + This button deletes the WiFi credentials.
+ The ESP will restart and enable its own AP (if you have it checked above). + Connect to WiFi AP with SSID "layzspa_module######" and go to address http://192.168.4.2/wifi.html

diff --git a/Code/lib/BWC_unified/FW_VERSION.h b/Code/lib/BWC_unified/FW_VERSION.h index c21dc988..da930d01 100644 --- a/Code/lib/BWC_unified/FW_VERSION.h +++ b/Code/lib/BWC_unified/FW_VERSION.h @@ -1 +1 @@ -#define FW_VERSION "2024-07-18-0900" +#define FW_VERSION "2024-07-18-1553" diff --git a/Code/lib/BWC_unified/enums.h b/Code/lib/BWC_unified/enums.h index 5d714fe1..d4e75bb6 100644 --- a/Code/lib/BWC_unified/enums.h +++ b/Code/lib/BWC_unified/enums.h @@ -255,6 +255,6 @@ struct sWifi_info String ip4DnsSecondary_str; String ip4NTP_str; bool enableAp; - bool enableWmApFallback = true; - bool enableStaticIp4 = false; + bool enableWmApFallback; + bool enableStaticIp4; }; \ No newline at end of file diff --git a/Code/src/main.cpp b/Code/src/main.cpp index a1142617..2a464610 100644 --- a/Code/src/main.cpp +++ b/Code/src/main.cpp @@ -78,6 +78,7 @@ void setup() mqtt_info->mqttTelemetryInterval = 600; mqtt_info->mqttUsername = MQTT_USER_F; mqtt_info->useMqtt = true; + wifi_info = new sWifi_info; } bwc->setup(); bwc->loop(); @@ -88,7 +89,7 @@ void setup() updateWSTimer->attach(WS_PERIOD, []{ sendWSFlag = true; }); loadWebConfig(); startWiFi(); - if(wifi_info.enableWmApFallback) startSoftAp(); // not blocking anymore so no use case should exist for this to be turned off. + if(wifi_info->enableWmApFallback) startSoftAp(); // not blocking anymore so no use case should exist for this to be turned off. startHttpServer(); startWebSocket(); startOTA(); @@ -329,7 +330,7 @@ void startWiFi() WiFi.mode(WIFI_STA); loadWifi(); - if (wifi_info.enableStaticIp4) + if (wifi_info->enableStaticIp4) { BWC_LOG_P(PSTR("Setting static IP\n"),0); IPAddress ip4Address; @@ -337,23 +338,28 @@ void startWiFi() IPAddress ip4Subnet; IPAddress ip4DnsPrimary; IPAddress ip4DnsSecondary; - ip4Address.fromString(wifi_info.ip4Address_str); - ip4Gateway.fromString(wifi_info.ip4Gateway_str); - ip4Subnet.fromString(wifi_info.ip4Subnet_str); - ip4DnsPrimary.fromString(wifi_info.ip4DnsPrimary_str); - ip4DnsSecondary.fromString(wifi_info.ip4DnsSecondary_str); + ip4Address.fromString(wifi_info->ip4Address_str); + ip4Gateway.fromString(wifi_info->ip4Gateway_str); + ip4Subnet.fromString(wifi_info->ip4Subnet_str); + ip4DnsPrimary.fromString(wifi_info->ip4DnsPrimary_str); + ip4DnsSecondary.fromString(wifi_info->ip4DnsSecondary_str); BWC_LOG_P(PSTR("WiFi > using static IP %s on gateway %s\n"),ip4Address.toString().c_str(), ip4Gateway.toString().c_str()); WiFi.config(ip4Address, ip4Gateway, ip4Subnet, ip4DnsPrimary, ip4DnsSecondary); } /* Connect in station mode to the AP given (your router/ap) */ - if (wifi_info.enableAp) + if (wifi_info->enableAp) { - BWC_LOG_P(PSTR("WiFi > using WiFi configuration with SSID %s\n"), wifi_info.apSsid.c_str()); + BWC_LOG_P(PSTR("WiFi > using WiFi configuration with SSID %s\n"), wifi_info->apSsid.c_str()); - WiFi.begin(wifi_info.apSsid.c_str(), wifi_info.apPwd.c_str()); + WiFi.begin(wifi_info->apSsid.c_str(), wifi_info->apPwd.c_str()); // checkWifi_ticker->attach(2.0, checkWiFi_ISR); - BWC_LOG_P(PSTR("WiFi > Trying to connect ..."),0); + BWC_LOG_P(PSTR("WiFi > AP info loaded. Waiting for connection ...\n"),0); + } + else + { + BWC_LOG_P(PSTR("WiFi > AP info not found. Using last known AP ...\n"),0); + WiFi.begin(); } BWC_YIELD; } @@ -421,7 +427,7 @@ void checkNTP() void startNTP() { BWC_LOG_P(PSTR("NTP > start\n"),0); - configTime(0,0,wifi_info.ip4NTP_str, F("pool.ntp.org"), F("time.nist.gov")); + configTime(0,0,wifi_info->ip4NTP_str, F("pool.ntp.org"), F("time.nist.gov")); ntpCheck_ticker->attach(3.0, checkNTP_ISR); } @@ -1283,19 +1289,19 @@ void loadWifi() return; } - wifi_info.enableAp = doc[F("enableAp")]; - if(doc.containsKey(F("enableWM"))) wifi_info.enableWmApFallback = doc[F("enableWM")]; - wifi_info.apSsid = doc[F("apSsid")].as(); - wifi_info.apPwd = doc[F("apPwd")].as(); + wifi_info->enableAp = doc[F("enableAp")]; + if(doc.containsKey(F("enableWM"))) wifi_info->enableWmApFallback = doc[F("enableWM")]; + wifi_info->apSsid = doc[F("apSsid")].as(); + wifi_info->apPwd = doc[F("apPwd")].as(); - wifi_info.enableStaticIp4 = doc[F("enableStaticIp4")]; + wifi_info->enableStaticIp4 = doc[F("enableStaticIp4")]; String s(30); - wifi_info.ip4Address_str = doc[F("ip4Address")].as(); - wifi_info.ip4Gateway_str = doc[F("ip4Gateway")].as(); - wifi_info.ip4Subnet_str = doc[F("ip4Subnet")].as(); - wifi_info.ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as(); - wifi_info.ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as(); - wifi_info.ip4NTP_str = doc[F("ip4NTP")].as(); + wifi_info->ip4Address_str = doc[F("ip4Address")].as(); + wifi_info->ip4Gateway_str = doc[F("ip4Gateway")].as(); + wifi_info->ip4Subnet_str = doc[F("ip4Subnet")].as(); + wifi_info->ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as(); + wifi_info->ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as(); + wifi_info->ip4NTP_str = doc[F("ip4NTP")].as(); BWC_YIELD; } @@ -1314,17 +1320,17 @@ void saveWifi() DynamicJsonDocument doc(1024); - doc[F("enableAp")] = wifi_info.enableAp; - doc[F("enableWM")] = wifi_info.enableWmApFallback; - doc[F("apSsid")] = wifi_info.apSsid; - doc[F("apPwd")] = wifi_info.apPwd; - doc[F("enableStaticIp4")] = wifi_info.enableStaticIp4; - doc[F("ip4Address")] = wifi_info.ip4Address_str; - doc[F("ip4Gateway")] = wifi_info.ip4Gateway_str; - doc[F("ip4Subnet")] = wifi_info.ip4Subnet_str; - doc[F("ip4DnsPrimary")] = wifi_info.ip4DnsPrimary_str; - doc[F("ip4DnsSecondary")] = wifi_info.ip4DnsSecondary_str; - doc[F("ip4NTP")] = wifi_info.ip4NTP_str; + doc[F("enableAp")] = wifi_info->enableAp; + doc[F("enableWM")] = wifi_info->enableWmApFallback; + doc[F("apSsid")] = wifi_info->apSsid; + doc[F("apPwd")] = wifi_info->apPwd; + doc[F("enableStaticIp4")] = wifi_info->enableStaticIp4; + doc[F("ip4Address")] = wifi_info->ip4Address_str; + doc[F("ip4Gateway")] = wifi_info->ip4Gateway_str; + doc[F("ip4Subnet")] = wifi_info->ip4Subnet_str; + doc[F("ip4DnsPrimary")] = wifi_info->ip4DnsPrimary_str; + doc[F("ip4DnsSecondary")] = wifi_info->ip4DnsSecondary_str; + doc[F("ip4NTP")] = wifi_info->ip4NTP_str; if (serializeJson(doc, file) == 0) { @@ -1344,22 +1350,22 @@ void handleGetWifi() DynamicJsonDocument doc(1024); - doc[F("enableAp")] = wifi_info.enableAp; - doc[F("enableWM")] = wifi_info.enableWmApFallback; - doc[F("apSsid")] = wifi_info.apSsid; + doc[F("enableAp")] = wifi_info->enableAp; + doc[F("enableWM")] = wifi_info->enableWmApFallback; + doc[F("apSsid")] = wifi_info->apSsid; doc[F("apPwd")] = F(""); if (!hidePasswords) { - doc[F("apPwd")] = wifi_info.apPwd; + doc[F("apPwd")] = wifi_info->apPwd; } - doc[F("enableStaticIp4")] = wifi_info.enableStaticIp4; - doc[F("ip4Address")] = wifi_info.ip4Address_str; - doc[F("ip4Gateway")] = wifi_info.ip4Gateway_str; - doc[F("ip4Subnet")] = wifi_info.ip4Subnet_str; - doc[F("ip4DnsPrimary")] = wifi_info.ip4DnsPrimary_str; - doc[F("ip4DnsSecondary")] = wifi_info.ip4DnsSecondary_str; - doc[F("ip4NTP")] = wifi_info.ip4NTP_str; + doc[F("enableStaticIp4")] = wifi_info->enableStaticIp4; + doc[F("ip4Address")] = wifi_info->ip4Address_str; + doc[F("ip4Gateway")] = wifi_info->ip4Gateway_str; + doc[F("ip4Subnet")] = wifi_info->ip4Subnet_str; + doc[F("ip4DnsPrimary")] = wifi_info->ip4DnsPrimary_str; + doc[F("ip4DnsSecondary")] = wifi_info->ip4DnsSecondary_str; + doc[F("ip4NTP")] = wifi_info->ip4NTP_str; String json; json.reserve(200); if (serializeJson(doc, json) == 0) @@ -1387,18 +1393,18 @@ void handleSetWifi() return; } - wifi_info.enableAp = doc[F("enableAp")]; - if(doc.containsKey("enableWM")) wifi_info.enableWmApFallback = doc[F("enableWM")]; - wifi_info.apSsid = doc[F("apSsid")].as(); - wifi_info.apPwd = doc[F("apPwd")].as(); + wifi_info->enableAp = doc[F("enableAp")]; + if(doc.containsKey("enableWM")) wifi_info->enableWmApFallback = doc[F("enableWM")]; + wifi_info->apSsid = doc[F("apSsid")].as(); + wifi_info->apPwd = doc[F("apPwd")].as(); - wifi_info.enableStaticIp4 = doc[F("enableStaticIp4")]; - wifi_info.ip4Address_str = doc[F("ip4Address")].as(); - wifi_info.ip4Gateway_str = doc[F("ip4Gateway")].as(); - wifi_info.ip4Subnet_str = doc[F("ip4Subnet")].as(); - wifi_info.ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as(); - wifi_info.ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as(); - wifi_info.ip4NTP_str = doc[F("ip4NTP")].as(); + wifi_info->enableStaticIp4 = doc[F("enableStaticIp4")]; + wifi_info->ip4Address_str = doc[F("ip4Address")].as(); + wifi_info->ip4Gateway_str = doc[F("ip4Gateway")].as(); + wifi_info->ip4Subnet_str = doc[F("ip4Subnet")].as(); + wifi_info->ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as(); + wifi_info->ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as(); + wifi_info->ip4NTP_str = doc[F("ip4NTP")].as(); saveWifi(); @@ -1428,10 +1434,10 @@ void handleResetWifi() void resetWiFi() { - wifi_info.enableAp = false; - wifi_info.enableWmApFallback = true; - wifi_info.apSsid = F("empty"); - wifi_info.apPwd = F("empty"); + wifi_info->enableAp = false; + wifi_info->enableWmApFallback = true; + wifi_info->apSsid = F("empty"); + wifi_info->apPwd = F("empty"); saveWifi(); delay(3000); periodicTimer->detach(); diff --git a/Code/src/main.h b/Code/src/main.h index 17fa2cce..fb2adaba 100644 --- a/Code/src/main.h +++ b/Code/src/main.h @@ -49,7 +49,7 @@ bool gotIP_flag = false; bool disconnected_flag = false; int periodicTimerInterval = 60; -sWifi_info wifi_info; +sWifi_info* wifi_info; /** A file to store the uploads */ File fsUploadFile;