From 01ecabd2f81a0711c63cb39c371e311eb15f4cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Nidecki?= Date: Mon, 1 Mar 2021 13:09:57 +0100 Subject: [PATCH] https://github.com/SQ9MDD/TTGO-T-Beam-LoRa-APRS/issues/19 Allow connecting to WiFi networks without password --- include/taskWebServer.h | 2 ++ src/taskWebServer.cpp | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/taskWebServer.h b/include/taskWebServer.h index 43a6092..097a8e5 100644 --- a/include/taskWebServer.h +++ b/include/taskWebServer.h @@ -16,6 +16,8 @@ extern Preferences preferences; extern WiFiServer tncServer; #endif // MAX 15 chars for preferenece key!!! +static const char *const PREF_WIFI_SSID = "wifi_ssid"; +static const char *const PREF_WIFI_PASSWORD = "wifi_password"; static const char *const PREF_APRS_CALLSIGN = "aprs_callsign"; static const char *const PREF_APRS_RELAY_PATH = "aprs_relay_path"; static const char *const PREF_APRS_RELAY_PATH_INIT = "aprs_relay_init"; diff --git a/src/taskWebServer.cpp b/src/taskWebServer.cpp index c2667c2..4e84cc8 100644 --- a/src/taskWebServer.cpp +++ b/src/taskWebServer.cpp @@ -75,15 +75,15 @@ void handle_ScanWifi() { } void handle_SaveWifiCfg() { - if (!server.hasArg("wifi_ssid") || !server.hasArg("wifi_password")){ + if (!server.hasArg(PREF_WIFI_SSID) || !server.hasArg(PREF_WIFI_PASSWORD)){ server.send(500, "text/plain", "Invalid request"); } - if (!server.arg("wifi_ssid").length() || !server.arg("wifi_password").length()){ + if (!server.arg(PREF_WIFI_SSID).length()){ server.send(403, "text/plain", "Empty SSID or Password"); } - preferences.putString("wifi_ssid", server.arg("wifi_ssid")); - preferences.putString("wifi_password", server.arg("wifi_password")); + preferences.putString(PREF_WIFI_SSID, server.arg(PREF_WIFI_SSID)); + preferences.putString(PREF_WIFI_PASSWORD, server.arg(PREF_WIFI_PASSWORD)); server.sendHeader("Location", "/"); server.send(302,"text/html", ""); @@ -105,8 +105,8 @@ void handle_Restore() { void handle_Cfg() { String jsonData = "{"; - jsonData += R"("wifi_ssid":")" + jsonEscape(preferences.getString("wifi_ssid")) + R"(",)"; - jsonData += R"("wifi_password":")" + jsonEscape((preferences.getString("wifi_password").isEmpty() ? String("") : "*")) + R"(",)"; + jsonData += R"(PREF_WIFI_SSID:")" + jsonEscape(preferences.getString(PREF_WIFI_SSID)) + R"(",)"; + jsonData += R"(PREF_WIFI_PASSWORD:")" + jsonEscape((preferences.getString(PREF_WIFI_PASSWORD).isEmpty() ? String("") : "*")) + R"(",)"; jsonData += jsonLineFromPreferenceString(PREF_APRS_CALLSIGN); jsonData += jsonLineFromPreferenceString(PREF_APRS_RELAY_PATH); jsonData += jsonLineFromPreferenceString(PREF_APRS_SYMBOL_TABLE); @@ -177,12 +177,12 @@ void handle_SaveAPRSCfg() { server.on("/restore", handle_Restore); server.onNotFound(handle_NotFound); - String wifi_password = preferences.getString("wifi_password"); - String wifi_ssid = preferences.getString("wifi_ssid"); - if (!wifi_password.length() || !wifi_ssid.length()){ + String wifi_password = preferences.getString(PREF_WIFI_PASSWORD); + String wifi_ssid = preferences.getString(PREF_WIFI_SSID); + if (!wifi_ssid.length()){ WiFi.softAP(apSSID.c_str(), apPassword.c_str()); } else { - WiFi.begin(wifi_ssid.c_str(), wifi_password.c_str()); + WiFi.begin(wifi_ssid.c_str(), wifi_password.length() ? wifi_password.c_str() : nullptr); Serial.println("Connecting to " + wifi_ssid); while (WiFi.status() != WL_CONNECTED) { Serial.print("Not connected: ");