From db938a571cd3e5be023714562a185230ee2a31ec Mon Sep 17 00:00:00 2001 From: Paul Kendall Date: Tue, 1 Mar 2022 17:15:52 +1300 Subject: [PATCH] Better handling of flashed home-wifi credentials (#61) * Better handling of flashed home-wifi credentials * Fix whitespace --- lib/WIFI/devWIFI.cpp | 68 ++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/lib/WIFI/devWIFI.cpp b/lib/WIFI/devWIFI.cpp index e283136..e3dafcf 100644 --- a/lib/WIFI/devWIFI.cpp +++ b/lib/WIFI/devWIFI.cpp @@ -41,14 +41,14 @@ extern unsigned long rebootTime; #if defined(TARGET_VRX_BACKPACK) static const char *myHostname = "elrs_vrx"; -static const char *ssid = "ExpressLRS VRx Backpack"; +static const char *wifi_ap_ssid = "ExpressLRS VRx Backpack"; #elif defined(TARGET_TX_BACKPACK) static const char *myHostname = "elrs_txbp"; -static const char *ssid = "ExpressLRS TX Backpack"; +static const char *wifi_ap_ssid = "ExpressLRS TX Backpack"; #else #error Unknown target #endif -static const char *password = "expresslrs"; +static const char *wifi_ap_password = "expresslrs"; static const char *home_wifi_ssid = "" #ifdef HOME_WIFI_SSID @@ -61,6 +61,9 @@ STR(HOME_WIFI_PASSWORD) #endif ; +static char station_ssid[33]; +static char station_password[65]; + static bool wifiStarted = false; bool webserverPreventAutoStart = false; extern bool InBindingMode; @@ -177,9 +180,9 @@ static void WebUpdateSendMode(AsyncWebServerRequest *request) { String s; if (wifiMode == WIFI_STA) { - s = String("{\"mode\":\"STA\",\"ssid\":\"") + config.GetSSID(); + s = String("{\"mode\":\"STA\",\"ssid\":\"") + station_ssid; } else { - s = String("{\"mode\":\"AP\",\"ssid\":\"") + config.GetSSID(); + s = String("{\"mode\":\"AP\",\"ssid\":\"") + station_ssid; } #if defined(NAMIMNO_TX_BACKPACK) s += "\",\"stm32\":\"yes"; @@ -232,14 +235,14 @@ static void sendResponse(AsyncWebServerRequest *request, const String &msg, WiFi static void WebUpdateAccessPoint(AsyncWebServerRequest *request) { DBGLN("Starting Access Point"); - String msg = String("Access Point starting, please connect to access point '") + ssid + "' with password '" + password + "'"; + String msg = String("Access Point starting, please connect to access point '") + wifi_ap_ssid + "' with password '" + wifi_ap_password + "'"; sendResponse(request, msg, WIFI_AP); } static void WebUpdateConnect(AsyncWebServerRequest *request) { DBGLN("Connecting to home network"); - String msg = String("Connecting to network '") + config.GetSSID() + "', connect to http://" + + String msg = String("Connecting to network '") + station_ssid + "', connect to http://" + myHostname + ".local from a browser on that network"; sendResponse(request, msg, WIFI_STA); } @@ -250,9 +253,14 @@ static void WebUpdateSetHome(AsyncWebServerRequest *request) String password = request->arg("password"); DBGLN("Setting home network %s", ssid.c_str()); - config.SetSSID(ssid.c_str()); - config.SetPassword(password.c_str()); - config.Commit(); + strcpy(station_ssid, ssid.c_str()); + strcpy(station_password, password.c_str()); + // Only save to config if we don't have a flashed wifi network + if (home_wifi_ssid[0] == 0) { + config.SetSSID(ssid.c_str()); + config.SetPassword(password.c_str()); + config.Commit(); + } WebUpdateConnect(request); } @@ -262,8 +270,19 @@ static void WebUpdateForget(AsyncWebServerRequest *request) config.SetSSID(""); config.SetPassword(""); config.Commit(); - String msg = String("Home network forgotten, please connect to access point '") + ssid + "' with password '" + password + "'"; - sendResponse(request, msg, WIFI_AP); + // If we have a flashed wifi network then let's try reconnecting to that otherwise start an access point + if (home_wifi_ssid[0] != 0) { + strcpy(station_ssid, home_wifi_ssid); + strcpy(station_password, home_wifi_password); + String msg = String("Temporary network forgotten, attempting to connect to network '") + station_ssid + "'"; + sendResponse(request, msg, WIFI_STA); + } + else { + station_ssid[0] = 0; + station_password[0] = 0; + String msg = String("Home network forgotten, please connect to access point '") + wifi_ap_ssid + "' with password '" + wifi_ap_password + "'"; + sendResponse(request, msg, WIFI_AP); + } } static void WebUpdateHandleNotFound(AsyncWebServerRequest *request) @@ -394,7 +413,8 @@ static void WebUploadForceUpdateHandler(AsyncWebServerRequest *request) { target_seen = true; if (request->arg("action").equals("confirm")) { WebUploadResponseHandler(request); - } else { + } + else { #if defined(PLATFORM_ESP32) updater.abort(); #endif @@ -432,15 +452,19 @@ static void startWiFi(unsigned long now) #elif defined(PLATFORM_ESP32) WiFi.setTxPower(WIFI_POWER_13dBm); #endif - if (config.GetSSID()[0]==0 && home_wifi_ssid[0]!=0) { - config.SetSSID(home_wifi_ssid); - config.SetPassword(home_wifi_password); - config.Commit(); + if (home_wifi_ssid[0] != 0) { + strcpy(station_ssid, home_wifi_ssid); + strcpy(station_password, home_wifi_password); } - if (config.GetSSID()[0]==0) { + else { + strcpy(station_ssid, config.GetSSID()); + strcpy(station_password, config.GetPassword()); + } + if (station_ssid[0] == 0) { changeTime = now; changeMode = WIFI_AP; - } else { + } + else { changeTime = now; changeMode = WIFI_STA; } @@ -587,17 +611,17 @@ static void HandleWebUpdate() #endif changeTime = now; WiFi.softAPConfig(apIP, apIP, netMsk); - WiFi.softAP(ssid, password); + WiFi.softAP(wifi_ap_ssid, wifi_ap_password); WiFi.scanNetworks(true); startServices(); break; case WIFI_STA: - DBGLN("Connecting to home network '%s' '%s'", config.GetSSID(), config.GetPassword()); + DBGLN("Connecting to home network '%s' '%s'", station_ssid, station_password); wifiMode = WIFI_STA; WiFi.mode(wifiMode); WiFi.setHostname(myHostname); // hostname must be set after the mode is set to STA changeTime = now; - WiFi.begin(config.GetSSID(), config.GetPassword()); + WiFi.begin(station_ssid, station_password); startServices(); default: break;