From 1264ab938789fd7c7418bed4a5b445ec46bc8486 Mon Sep 17 00:00:00 2001 From: Thomas Ballmann Date: Sat, 6 Feb 2021 22:03:31 +0100 Subject: [PATCH] #49 captive portal added --- lib/app/app.cpp | 8 +++++++- lib/wlan/wlan.cpp | 46 +++++++++++++++++++++++++++++++++++++--------- lib/wlan/wlan.h | 1 + src/main.cpp | 2 ++ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/app/app.cpp b/lib/app/app.cpp index d4f8056..2632e27 100644 --- a/lib/app/app.cpp +++ b/lib/app/app.cpp @@ -97,7 +97,13 @@ void setupApp() setupOTA(); server.onNotFound([](AsyncWebServerRequest *request) { - request->send(404); + Serial.printf("not found: http://%s%s\n", request->host().c_str(), request->url().c_str()); + + if (ON_STA_FILTER(request)) { + request->send(404); + } else { + request->send(SPIFFS, "/dist/index.html"); + } }); server.on("/stats", HTTP_GET, [](AsyncWebServerRequest *request) { diff --git a/lib/wlan/wlan.cpp b/lib/wlan/wlan.cpp index 19e23fe..fac231f 100644 --- a/lib/wlan/wlan.cpp +++ b/lib/wlan/wlan.cpp @@ -1,10 +1,20 @@ #include "wlan.h" #include "settings.h" +#include const char *deviceName = "paperdash-epd"; RTC_DATA_ATTR int wifiFailedCount = 0; -void initClientMode(const char *ssid, const char *password); +// DNS server +const byte DNS_PORT = 53; +DNSServer dnsServer; + +/* Soft AP network parameters */ +IPAddress apIP(192,178,4,1); + +#include + +bool initClientMode(const char *ssid, const char *password); void initAPMode(); void setupWlan() @@ -15,12 +25,14 @@ void setupWlan() String ssid = NVS.getString("wifi.ssid"); String password = NVS.getString("wifi.password"); + bool clientMode = false; if (!ssid.isEmpty() && !password.isEmpty() && wifiFailedCount <=3) { - // client mode - initClientMode(ssid.c_str(), password.c_str()); + // try client mode + clientMode = initClientMode(ssid.c_str(), password.c_str()); } - else + + if (!clientMode) { // ap mode initAPMode(); @@ -31,7 +43,14 @@ void setupWlan() Serial.println("setup Wlan - done"); } -void initClientMode(const char *ssid, const char *password) + +void loopWlan() +{ + // DNS + dnsServer.processNextRequest(); +} + +bool initClientMode(const char *ssid, const char *password) { uint8_t tryCount = 5; long startMills = millis(); @@ -52,15 +71,13 @@ void initClientMode(const char *ssid, const char *password) if (!tryCount--) { // TODO is this correct? + WiFi.disconnect(); wifiFailedCount++; if (wifiFailedCount > 3) { Serial.println(" wifi is not reachable..."); - WiFi.disconnect(); - initAPMode(); - return; + return false; } else { tryCount = 5; - WiFi.disconnect(); Serial.println(" wifi reset..."); delay(500); WiFi.begin(ssid, password); @@ -82,14 +99,25 @@ void initClientMode(const char *ssid, const char *password) Serial.print(" connected in: "); Serial.println(millis() - startMills); + + return true; } void initAPMode() { Serial.println(" init AP (Access Point)"); + WiFi.mode(WIFI_AP); WiFi.softAP("paperdash"); + // prevent esp from crashing + delay(2000); + + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); + + // redirect all to local ap + dnsServer.start(DNS_PORT, "*", apIP); + IPAddress IP = WiFi.softAPIP(); Serial.print(" AP IP address: "); Serial.println(IP); diff --git a/lib/wlan/wlan.h b/lib/wlan/wlan.h index c8993f4..547d563 100644 --- a/lib/wlan/wlan.h +++ b/lib/wlan/wlan.h @@ -5,5 +5,6 @@ #include void setupWlan(); +void loopWlan(); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3fbee71..7e1adb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,8 @@ void setup() void loop() { + loopWlan(); + if (WiFi.isConnected()) { loopDateTime();