From d82aaaa806f7e8814db7928f97007792553ac39f Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 12 Dec 2020 18:33:52 -0800 Subject: [PATCH] #560 - Partial work for Charles. --- src/main.cpp | 10 ++------ src/meshwifi/meshwifi.cpp | 44 +++++++++++++++++++++++++++++++----- src/meshwifi/meshwifi.h | 2 +- src/nimble/BluetoothUtil.cpp | 2 +- src/nrf52/wifi-stubs.cpp | 2 +- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f23d79b6a..3f028613b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -308,6 +308,7 @@ void setup() // BUTTON_PIN is pulled high by a 12k resistor. if (!digitalRead(BUTTON_PIN)) { forceSoftAP = 1; + DEBUG_MSG("-------------------- Setting forceSoftAP = 1\n"); } #endif @@ -481,15 +482,8 @@ void setup() } #endif - if (forceSoftAP) { - strcpy(radioConfig.preferences.wifi_ssid, "meshtasticAdmin"); - strcpy(radioConfig.preferences.wifi_password, "12345678"); - radioConfig.preferences.wifi_ap_mode = true; - DEBUG_MSG("Forcing SoftAP\n"); - } - // Initialize Wifi - initWifi(); + initWifi(forceSoftAP); if (!rIf) recordCriticalError(ErrNoRadio); diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 71c4d2277..4b267399d 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -21,8 +21,16 @@ uint8_t wifiDisconnectReason = 0; // Stores our hostname char ourHost[16]; +bool forcedSoftAP = 0; + + bool isWifiAvailable() { + // If wifi status is connected, return true regardless of the radio configuration. + if (forcedSoftAP) { + return 1; + } + const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; @@ -58,20 +66,44 @@ void deinitWifi() } // Startup WiFi -void initWifi() +void initWifi(bool forceSoftAP) { - if (isWifiAvailable() == 0) { - return; + + if (forceSoftAP) { + // do nothing + DEBUG_MSG("----- Forcing SoftAP\n"); + } else { + if (isWifiAvailable() == 0) { + return; + } } + forcedSoftAP = forceSoftAP; + createSSLCert(); - if (radioConfig.has_preferences) { + if (radioConfig.has_preferences || forceSoftAP) { const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; - if (*wifiName && *wifiPsw) { - if (radioConfig.preferences.wifi_ap_mode) { + if ((*wifiName && *wifiPsw) || forceSoftAP) { + if (forceSoftAP) { + + DEBUG_MSG("----- Forcing SoftAP\n"); + + const char *softAPssid = ""; + const char *softAPpasswd = ""; + + IPAddress apIP(192, 168, 42, 1); + WiFi.onEvent(WiFiEvent); + + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); + DEBUG_MSG("STARTING WIFI AP: ssid=%s, ok=%d\n", softAPssid, WiFi.softAP(softAPssid, softAPpasswd)); + DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.softAPIP().toString().c_str()); + + dnsServer.start(53, "*", apIP); + + } else if (radioConfig.preferences.wifi_ap_mode) { IPAddress apIP(192, 168, 42, 1); WiFi.onEvent(WiFiEvent); diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index ac03e29f5..d56a44943 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -9,7 +9,7 @@ #include #endif -void initWifi(); +void initWifi(bool forceSoftAP); void deinitWifi(); bool isWifiAvailable(); diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 8a0dd779b..daba69372 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -545,7 +545,7 @@ void setBluetoothEnable(bool on) if (firstTime) { firstTime = 0; } else { - initWifi(); + initWifi(0); } } else { diff --git a/src/nrf52/wifi-stubs.cpp b/src/nrf52/wifi-stubs.cpp index 30321e5e4..1c1009862 100644 --- a/src/nrf52/wifi-stubs.cpp +++ b/src/nrf52/wifi-stubs.cpp @@ -1,7 +1,7 @@ #include "meshwifi/meshhttp.h" #include "meshwifi/meshwifi.h" -void initWifi() {} +void initWifi(bool forceSoftAP) {} void deinitWifi() {}