void WiFiManager::startWebConfig() { DEBUG_PRINT(""); display.print("WiFi connected"); DEBUG_PRINT("WiFi connected"); DEBUG_PRINT(WiFi.localIP()); DEBUG_PRINT(WiFi.softAPIP()); if (!mdns.begin(_apName, WiFi.localIP())) { DEBUG_PRINT("Error setting up MDNS responder!"); display.print("mDNS error"); while(1) { delay(1000); } } DEBUG_PRINT("mDNS responder started"); // Start the server server_s.begin(); display.print("Server started"); DEBUG_PRINT("Server started"); while(serverLoop() == WM_WAIT) { //looping } display.print("All done. Bye."); DEBUG_PRINT("Setup done"); delay(10000); ESP.reset(); }
void WiFiManager::startWebConfig() { DEBUG_PRINT(""); DEBUG_PRINT("WiFi connected"); DEBUG_PRINT(WiFi.localIP()); DEBUG_PRINT(WiFi.softAPIP()); if (!mdns.begin(_apName, WiFi.localIP())) { DEBUG_PRINT("Error setting up MDNS responder!"); while (1) { delay(1000); } } DEBUG_PRINT("mDNS responder started"); // Start the server server.begin(); DEBUG_PRINT("Server started"); while (serverLoop() == WM_WAIT) { //looping if(timeout > 0 && start + timeout < millis()) { //we passed timeout value, release DEBUG_PRINT("timeout reached"); return; } } DEBUG_PRINT("Setup done"); delay(5000); ESP.reset(); }
void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT); if (digitalRead(BUTTON_PIN) == 0) { Serial.println("Factory Reset"); } WiFiManager wifi; wifi.autoConnect(DEVICE_NAME); if (!mdns.begin(DEVICE_NAME, WiFi.localIP())) { Serial.println("Error setting up MDNS responder!"); } else { Serial.println("mDNS responder started"); } // Start TCP (HTTP) server server.begin(); Serial.println("TCP server started"); Serial.println("Listening on port 8266"); Serial.print("Sketch size: "); Serial.println(ESP.getSketchSize()); Serial.print("Free size: "); Serial.println(ESP.getFreeSketchSpace()); server.on("/", handle_root); server.on("/on", handle_on); server.on("/off", handle_off); httpUpdater.setup(&server); state = OFF; Serial.println("State: OFF"); pinMode(LED_PIN, OUTPUT); }
int WiFiManager::serverLoop() { // Check for any mDNS queries and send responses mdns.update(); String s; WiFiClient client = server_s.available(); if (!client) { return(WM_WAIT); } DEBUG_PRINT("New client"); // Wait for data from client to become available while(client.connected() && !client.available()){ delay(1); } // Read the first line of HTTP request String req = client.readStringUntil('\r'); // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { DEBUG_PRINT("Invalid request: "); DEBUG_PRINT(req); return(WM_WAIT); } req = req.substring(addr_start + 1, addr_end); DEBUG_PRINT("Request: "); DEBUG_PRINT(req); client.flush(); if (req == "/") { s = HTTP_200; String head = HTTP_HEAD; head.replace("{v}", "Config ESP"); s += head; s += HTTP_SCRIPT; s += HTTP_STYLE; s += HTTP_HEAD_END; int n = WiFi.scanNetworks(); DEBUG_PRINT("scan done"); if (n == 0) { DEBUG_PRINT("no networks found"); s += "<div>No networks found. Refresh to scan again.</div>"; } else { for (int i = 0; i < n; ++i) { DEBUG_PRINT(WiFi.SSID(i)); DEBUG_PRINT(WiFi.RSSI(i)); String item = HTTP_ITEM; item.replace("{v}", WiFi.SSID(i)); s += item; delay(10); } } s += HTTP_FORM; s += HTTP_END; DEBUG_PRINT("Sending config page"); } else if ( req.startsWith("/s") ) { String qssid; qssid = urldecode(req.substring(8,req.indexOf('&')).c_str()); DEBUG_PRINT(qssid); DEBUG_PRINT(""); req = req.substring( req.indexOf('&') + 1); String qpass; qpass = urldecode(req.substring(req.lastIndexOf('=')+1).c_str()); setEEPROMString(0, 32, qssid); setEEPROMString(32, 64, qpass); EEPROM.commit(); s = HTTP_200; String head = HTTP_HEAD; head.replace("{v}", "Saved config"); s += HTTP_STYLE; s += HTTP_HEAD_END; s += "saved to eeprom...<br/>resetting in 10 seconds"; s += HTTP_END; client.print(s); client.flush(); DEBUG_PRINT("Saved WiFiConfig...restarting."); return WM_DONE; } else { s = HTTP_404; DEBUG_PRINT("Sending 404"); } client.print(s); DEBUG_PRINT("Done with client"); return(WM_WAIT); }
void setup(void){ pinMode(led, OUTPUT); digitalWrite(led, LED_OFF); // Note the serial rate - if you don't match it when connecting you'll get garbage. Serial.begin(115200); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // This tells the network that you can access the ip address of the esp8266 via 'esp8266.local' // The first arg is the hostname - '.local' will be appended to it, the second arg is the ip address if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/inline", [](){ server.send(200, "text/plain", "this works as well"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); }