Add support for captive portal

Need to comment DEBUG and DEBUG_OUTPUT in DNSServer.cpp
This commit is contained in:
Luc 2015-09-07 21:26:31 +08:00
parent 9c04635e15
commit 283270f660
3 changed files with 32 additions and 20 deletions

View File

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

View File

@ -39,6 +39,11 @@
#ifdef MDNS_FEATURE
#include <ESP8266mDNS.h>
#endif
#ifdef CAPTIVE_PORTAL_FEATURE
#include <DNSServer.h>
const byte DNS_PORT = 53;
DNSServer dnsServer;
#endif
#ifdef SSDP_FEATURE
#include <ESP8266SSDP.h>
#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

View File

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