changes for soft ap + captive portal

1.2-legacy
Jm Casler 2020-09-18 15:33:03 -07:00
rodzic 48dd6d388d
commit b203c95dd1
7 zmienionych plików z 69 dodań i 12 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -423,6 +423,7 @@ void loop()
// TODO: This should go into a thread handled by FreeRTOS.
handleWebResponse();
handleDNSResponse();
delay(msecstosleep);
}

Wyświetl plik

@ -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 += "<meta http-equiv=\"refresh\" content=\"0;url=http://meshtastic.org/\" />\n";
webserver.send ( 200, "text/html", out );
return;
}
void notifyWebUI() {
DEBUG_MSG("************ Got a message! ************\n");

Wyświetl plik

@ -14,3 +14,4 @@ void handleJSONChatHistory();
void notifyWebUI();
void handleHotspot();

Wyświetl plik

@ -1,10 +1,13 @@
#include "meshwifi.h"
#include <WiFi.h>
#include <DNSServer.h>
#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();
}
}

Wyświetl plik

@ -3,6 +3,7 @@
#include <Arduino.h>
#include <functional>
#include <WiFi.h>
#include <DNSServer.h>
void initWifi();
@ -10,4 +11,6 @@ void deinitWifi();
void WiFiEvent(WiFiEvent_t event);
bool isWifiAvailable();
bool isWifiAvailable();
void handleDNSResponse();

Wyświetl plik

@ -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());