/* HandlePortal.ino, Example for the AutoConnect library. Copyright (c) 2018, Hieromon Ikasamo https://github.com/Hieromon/AutoConnect This software is released under the MIT License. https://opensource.org/licenses/MIT */ /* This is a way of not explicitly declaring ESP8266WebServer. It uses the ESP8266WebServer function without its declaration. I recommend that you consider this example compared to HandlePortalEX.ino. https://github.com/Hieromon/AutoConnect/blob/master/examples/HandlePortalEX/HandlePortalEX.ino It will help you understand AutoConnect usage. */ #if defined(ARDUINO_ARCH_ESP8266) #include #include #elif defined(ARDUINO_ARCH_ESP32) #include #include #endif #include #ifndef BUILTIN_LED #define BUILTIN_LED 2 // backward compatibility #endif AutoConnect portal; void handleRoot() { String page = PSTR( "" "" "" "" "" "" "
" AUTOCONNECT_LINK(BAR_32) "
" "BUILT-IN LED
" "GPIO("); page += String(BUILTIN_LED); page += String(F(") : HIGH") : String("SlateBlue\">LOW"); page += String(F("")); page += String(F("

lowhigh

")); page += String(F("")); portal.host().send(200, "text/html", page); } void handleGPIO() { WebServerClass& server = portal.host(); if (server.arg("v") == "low") digitalWrite(BUILTIN_LED, LOW); else if (server.arg("v") == "high") digitalWrite(BUILTIN_LED, HIGH); sendRedirect("/"); } void sendRedirect(String uri) { WebServerClass& server = portal.host(); server.sendHeader("Location", uri, true); server.send(302, "text/plain", ""); server.client().stop(); } bool atDetect(IPAddress& softapIP) { Serial.println("Captive portal started, SoftAP IP:" + softapIP.toString()); return true; } void setup() { delay(1000); Serial.begin(115200); Serial.println(); pinMode(BUILTIN_LED, OUTPUT); // Put the home location of the web site. // But in usually, setting the home uri is not needed cause default location is "/". //portal.home("/"); // Starts user web site included the AutoConnect portal. portal.onDetect(atDetect); if (portal.begin()) { WebServerClass& server = portal.host(); server.on("/", handleRoot); server.on("/io", handleGPIO); Serial.println("Started, IP:" + WiFi.localIP().toString()); } else { Serial.println("Connection failed."); while (true) { yield(); } } } void loop() { portal.handleClient(); if (WiFi.status() == WL_IDLE_STATUS) { #if defined(ARDUINO_ARCH_ESP8266) ESP.reset(); #elif defined(ARDUINO_ARCH_ESP32) ESP.restart(); #endif delay(1000); } }