From 283270f660c6f9250f470624fe0e3d00b30b6d25 Mon Sep 17 00:00:00 2001 From: Luc Date: Mon, 7 Sep 2015 21:26:31 +0800 Subject: [PATCH] Add support for captive portal Need to comment DEBUG and DEBUG_OUTPUT in DNSServer.cpp --- esp8266/config.h | 6 ++++-- esp8266/esp8266.ino | 20 ++++++++++++++++++++ esp8266/webinterface.cpp | 26 ++++++++------------------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/esp8266/config.h b/esp8266/config.h index bc27ce5f..637b41cd 100644 --- a/esp8266/config.h +++ b/esp8266/config.h @@ -19,14 +19,16 @@ */ //comment to disable -//MDNS_FEATURE: this feature allow in Station mode to enter the name defined +//MDNS_FEATURE: this feature allow type the name defined //in web browser by default : http:\\esp8266.local and connect -//this feature does not work in AP mode #define MDNS_FEATURE //SSDD_FEATURE: this feature is a discovery protocol, supported on Windows out of the box #define SSDP_FEATURE +//CAPTIVE_PORTAL_FEATURE: In SoftAP redirect all unknow call to main page +#define CAPTIVE_PORTAL_FEATURE + //USE_CSS_FEATURE: this feature allow to have fancy UI by adding css in page #define USE_CSS_FEATURE diff --git a/esp8266/esp8266.ino b/esp8266/esp8266.ino index 83fb1597..0a2110da 100644 --- a/esp8266/esp8266.ino +++ b/esp8266/esp8266.ino @@ -39,6 +39,11 @@ #ifdef MDNS_FEATURE #include #endif +#ifdef CAPTIVE_PORTAL_FEATURE +#include +const byte DNS_PORT = 53; +DNSServer dnsServer; +#endif #ifdef SSDP_FEATURE #include #endif @@ -101,6 +106,15 @@ void setup() { wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port); #endif +#ifdef CAPTIVE_PORTAL_FEATURE + if (wifi_get_opmode()!=WIFI_STA ) + { + // if DNSServer is started with "*" for domain name, it will reply with + // provided IP to all DNS request + dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); + } + #endif + #ifdef SSDP_FEATURE SSDP.setSchemaURL("description.xml"); SSDP.setHTTPPort( wifi_config.iweb_port); @@ -119,6 +133,12 @@ void setup() { //main loop void loop() { +#ifdef CAPTIVE_PORTAL_FEATURE + if (wifi_get_opmode()!=WIFI_STA ) + { + dnsServer.processNextRequest(); + } +#endif //web requests web_interface->WebServer.handleClient(); //TODO use a method to handle serial also in class and call it instead of this one diff --git a/esp8266/webinterface.cpp b/esp8266/webinterface.cpp index e40ae6ee..73b0dd71 100644 --- a/esp8266/webinterface.cpp +++ b/esp8266/webinterface.cpp @@ -173,6 +173,7 @@ const char HTTP_START[] PROGMEM = "http://"; const char HTTP_MDNS_NAME[] PROGMEM = "mDNS name: "; const char HTTP_END[] PROGMEM = ".local"; const char SSDP_PROTOCOL_NAME[] PROGMEM = "SSDP Protocol: "; +const char CAPTIVE_PORTAL_LABEL[] PROGMEM = "Captive Portal: "; const char VALUE_11B[] PROGMEM = "11b"; const char VALUE_11N[] PROGMEM = "11n"; const char VALUE_11G[] PROGMEM = "11g"; @@ -528,6 +529,12 @@ void handle_web_interface_root() sstatus+=FPSTR(LOCAL_NAME); LABEL_UNITS(FPSTR(HTTP_MDNS_NAME),sstatus.c_str(),FPSTR(HTTP_END)) #endif + #ifdef CAPTIVE_PORTAL_FEATURE + if (wifi_get_opmode()!=WIFI_STA ) + { + LABEL(FPSTR(CAPTIVE_PORTAL_LABEL),FPSTR(VALUE_YES)) + } + #endif #ifdef SSDP_FEATURE LABEL(FPSTR(SSDP_PROTOCOL_NAME),FPSTR(VALUE_YES)) #endif @@ -1568,23 +1575,6 @@ void handle_web_interface_configSTA() } -void handle_not_found() -{ - String IP; - String buffer2send =""; - if (wifi_get_opmode()==WIFI_STA ) IP=wifi_config.ip2str(WiFi.localIP()); - else IP=wifi_config.ip2str(WiFi.softAPIP()); - if (wifi_config.iweb_port!=80) - { - IP+=":"; - IP+=String(wifi_config.iweb_port); - } - buffer2send+=(FPSTR(T404_PAGE)); - buffer2send+=(IP.c_str()); - buffer2send+=(FPSTR(T404_PAGE_2)); - web_interface->WebServer.send(200, "text/html", buffer2send); -} - void handle_web_interface_printer() { String IP; @@ -1876,7 +1866,7 @@ WEBINTERFACE_CLASS::WEBINTERFACE_CLASS (int port):WebServer(port) #ifdef SSDP_FEATURE WebServer.on("/description.xml", HTTP_GET, handle_SSDP); #endif - WebServer.onNotFound( handle_not_found); + WebServer.onNotFound( handle_web_interface_root); answer4M105="T:0 /0 "; answer4M114="X:0.0 Y:0.0 Z:0.000"; answer4M220="100";