diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 29637934..1acffc4d 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -852,16 +852,22 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i // The coordinates define the left starting point of the text display->setTextAlignment(TEXT_ALIGN_LEFT); - if ( WiFi.status() != WL_CONNECTED ) { + if (radioConfig.preferences.wifi_ap_mode) { + display->drawString(x, y, String("WiFi - Software AP")); + } else if ( WiFi.status() != WL_CONNECTED ) { display->drawString(x, y, String("WiFi - Not Connected")); } else { display->drawString(x, y, String("WiFi - Connected")); } - display->drawString(x, y + FONT_HEIGHT * 1, WiFi.localIP().toString().c_str()); + if (radioConfig.preferences.wifi_ap_mode) { + display->drawString(x, y + FONT_HEIGHT * 1, "IP " + String(WiFi.softAPIP().toString().c_str())); + } else { + display->drawString(x, y + FONT_HEIGHT * 1, "IP " + String(WiFi.localIP().toString().c_str())); + } - display->drawString(x, y + FONT_HEIGHT * 2, wifiName); - display->drawString(x, y + FONT_HEIGHT * 3, wifiPsw); + display->drawString(x, y + FONT_HEIGHT * 2, "SSID " + String(wifiName)); + display->drawString(x, y + FONT_HEIGHT * 3, "PWD " + String(wifiPsw)); /* Display a heartbeat pixel that blinks every time the frame is redrawn */ #ifdef SHOW_REDRAWS diff --git a/src/main.cpp b/src/main.cpp index b0d133f2..65881b9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -423,6 +423,7 @@ void loop() // TODO: This should go into a thread handled by FreeRTOS. handleWebResponse(); + handleDNSResponse(); delay(msecstosleep); } diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index f52cade5..09219602 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -25,6 +25,7 @@ void initWebServer() { webserver.onNotFound(handleNotFound); //webserver.on("/", handleJSONChatHistory); //webserver.on("/json/chat/history", handleJSONChatHistory); + webserver.on("/hotspot-detect.html", handleHotspot); webserver.on("/", []() { webserver.send(200, "text/plain", "Everything is awesome!"); }); @@ -69,13 +70,24 @@ void handleNotFound() { for (uint8_t i = 0; i < webserver.args(); i++) { message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n"; } - + Serial.println(message); webserver.send(404, "text/plain", message); /* */ } +/* + This supports the Apple Captive Network Assistant (CNA) Portal +*/ +void handleHotspot() { + DEBUG_MSG("Hotspot Request\n"); + String out = ""; + //out += "Success\n"; + out += "\n"; + webserver.send ( 200, "text/html", out ); + return; +} void notifyWebUI() { DEBUG_MSG("************ Got a message! ************\n"); diff --git a/src/meshwifi/meshhttp.h b/src/meshwifi/meshhttp.h index cbff51db..04d1db0f 100644 --- a/src/meshwifi/meshhttp.h +++ b/src/meshwifi/meshhttp.h @@ -14,3 +14,4 @@ void handleJSONChatHistory(); void notifyWebUI(); +void handleHotspot(); diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 14ac8a2a..3ff8ed12 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -1,10 +1,13 @@ #include "meshwifi.h" #include +#include #include "configuration.h" #include "main.h" #include "NodeDB.h" #include "meshwifi/meshhttp.h" +DNSServer dnsServer; + bool isWifiAvailable() { const char *wifiName = radioConfig.preferences.wifi_ssid; @@ -45,21 +48,39 @@ void initWifi() return; } - //strcpy(radioConfig.preferences.wifi_ssid, WiFi_SSID_NAME); - //strcpy(radioConfig.preferences.wifi_password, WiFi_SSID_PASSWORD); if (radioConfig.has_preferences) { const char *wifiName = radioConfig.preferences.wifi_ssid; + const char *wifiPsw = radioConfig.preferences.wifi_password; - if (*wifiName) { - const char *wifiPsw = radioConfig.preferences.wifi_password; + if (1) { + radioConfig.preferences.wifi_ap_mode = 1; + strcpy(radioConfig.preferences.wifi_ssid, "MeshTest2"); + strcpy(radioConfig.preferences.wifi_password, "12345678"); + } else { + radioConfig.preferences.wifi_ap_mode = 0; + strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); + strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); + } + + + if (*wifiName && *wifiPsw) { if (radioConfig.preferences.wifi_ap_mode) { + + 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", wifiName, WiFi.softAP(wifiName, wifiPsw)); + DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.softAPIP().toString().c_str()); + + dnsServer.start(53, "*", apIP); + + } else { WiFi.mode(WIFI_MODE_STA); WiFi.onEvent(WiFiEvent); //esp_wifi_set_ps(WIFI_PS_NONE); // Disable power saving - DEBUG_MSG("JOINING WIFI: ssid=%s\n", wifiName); if (WiFi.begin(wifiName, wifiPsw) == WL_CONNECTED) { DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.localIP().toString().c_str()); @@ -127,6 +148,11 @@ void WiFiEvent(WiFiEvent_t event) break; case SYSTEM_EVENT_AP_START: DEBUG_MSG("WiFi access point started\n"); + Serial.println(WiFi.softAPIP()); + + // Start web server + initWebServer(); + break; case SYSTEM_EVENT_AP_STOP: DEBUG_MSG("WiFi access point stopped\n"); @@ -163,4 +189,10 @@ void WiFiEvent(WiFiEvent_t event) break; default: break; } +} + +void handleDNSResponse() { + if (radioConfig.preferences.wifi_ap_mode) { + dnsServer.processNextRequest(); + } } \ No newline at end of file diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index 044839ab..970cc447 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -3,6 +3,7 @@ #include #include #include +#include void initWifi(); @@ -10,4 +11,6 @@ void deinitWifi(); void WiFiEvent(WiFiEvent_t event); -bool isWifiAvailable(); \ No newline at end of file +bool isWifiAvailable(); + +void handleDNSResponse(); diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index a78b0098..de9f0c61 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -526,8 +526,10 @@ void setBluetoothEnable(bool on) initWifi(); } } else { + // shutdown wifi + deinitWifi(); + // We have to totally teardown our bluetooth objects to prevent leaks - deinitWifi(); // shutdown wifi deinitBLE(); Serial.printf("Shutdown BT: %u heap size\n", ESP.getFreeHeap());