From 0575a97fb3d73c2a4ad88c1913d066c9e6453e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 23 Apr 2018 22:46:58 +0200 Subject: [PATCH] Added captive portal when in AP mode --- code/espurna/config/general.h | 8 +++++++- code/espurna/wifi.ino | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index c26533b6..8bebbe4b 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -78,7 +78,7 @@ #define DEBUG_UDP_PORT 514 #endif -// If DEBUG_UDP_PORT is set to 514 syslog format is assumed +// If DEBUG_UDP_PORT is set to 514 syslog format is assumed // (https://tools.ietf.org/html/rfc3164) // DEBUG_UDP_FAC_PRI is the facility+priority #define DEBUG_UDP_FAC_PRI (SYSLOG_LOCAL0 | SYSLOG_DEBUG) @@ -268,7 +268,13 @@ #define WIFI_RECONNECT_INTERVAL 180000 // If could not connect to WIFI, retry after this time in ms #endif +#ifndef WIFI_MAX_NETWORKS #define WIFI_MAX_NETWORKS 5 // Max number of WIFI connection configurations +#endif + +#ifndef WIFI_AP_CAPTIVE +#define WIFI_AP_CAPTIVE 1 // Captive portal enabled when in AP mode +#endif #ifndef WIFI_AP_MODE #define WIFI_AP_MODE AP_MODE_ALONE diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 3b781f7f..554730a7 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -196,6 +196,27 @@ void _wifiInject() { } } +#if WIFI_AP_CAPTIVE + +DNSServer _wifi_dnsServer; + +void _wifiCaptivePortal(justwifi_messages_t code, char * parameter) { + + if (MESSAGE_ACCESSPOINT_CREATED == code) { + _wifi_dnsServer.setErrorReplyCode(DNSReplyCode::NoError); + _wifi_dnsServer.start(53, "*", WiFi.softAPIP()); + DEBUG_MSG_P(PSTR("[WIFI] Captive portal enabled\n")); + } + + if (MESSAGE_CONNECTED == code) { + _wifi_dnsServer.stop(); + DEBUG_MSG_P(PSTR("[WIFI] Captive portal disabled\n")); + } + +} + +#endif // WIFI_AP_CAPTIVE + #if DEBUG_SUPPORT void _wifiDebug(justwifi_messages_t code, char * parameter) { @@ -422,6 +443,9 @@ void wifiSetup() { _wifiConfigure(); // Message callbacks + #if WIFI_AP_CAPTIVE + wifiRegister(_wifiCaptivePortal); + #endif #if DEBUG_SUPPORT wifiRegister(_wifiDebug); #endif @@ -446,6 +470,12 @@ void wifiLoop() { jw.loop(); + #if WIFI_AP_CAPTIVE + if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) { + _wifi_dnsServer.processNextRequest(); + } + #endif + if (_wifi_scan_client_id > 0) { _wifiScan(_wifi_scan_client_id); _wifi_scan_client_id = 0;