Repository: chriscook8/esp-arduino-apboot Branch: master Commit: 6fcd5a3ac0f4 Files: 2 Total size: 6.8 KB Directory structure: gitextract_5pjj65ay/ ├── ESP-wifiboot.ino └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: ESP-wifiboot.ino ================================================ #include "ESP8266WiFi.h" #include #include #include MDNSResponder mdns; WiFiServer server(80); const char* ssid = "BUBBLES"; String st; void setup() { Serial.begin(115200); EEPROM.begin(512); delay(10); Serial.println(); Serial.println(); Serial.println("Startup"); // read eeprom for ssid and pass Serial.println("Reading EEPROM ssid"); String esid; for (int i = 0; i < 32; ++i) { esid += char(EEPROM.read(i)); } Serial.print("SSID: "); Serial.println(esid); Serial.println("Reading EEPROM pass"); String epass = ""; for (int i = 32; i < 96; ++i) { epass += char(EEPROM.read(i)); } Serial.print("PASS: "); Serial.println(epass); if ( esid.length() > 1 ) { // test esid WiFi.begin(esid.c_str(), epass.c_str()); if ( testWifi() == 20 ) { launchWeb(0); return; } } setupAP(); } int testWifi(void) { int c = 0; Serial.println("Waiting for Wifi to connect"); while ( c < 20 ) { if (WiFi.status() == WL_CONNECTED) { return(20); } delay(500); Serial.print(WiFi.status()); c++; } Serial.println("Connect timed out, opening AP"); return(10); } void launchWeb(int webtype) { Serial.println(""); Serial.println("WiFi connected"); Serial.println(WiFi.localIP()); Serial.println(WiFi.softAPIP()); if (!mdns.begin("esp8266", WiFi.localIP())) { Serial.println("Error setting up MDNS responder!"); while(1) { delay(1000); } } Serial.println("mDNS responder started"); // Start the server server.begin(); Serial.println("Server started"); int b = 20; int c = 0; while(b == 20) { b = mdns1(webtype); } } void setupAP(void) { WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); int n = WiFi.scanNetworks(); Serial.println("scan done"); if (n == 0) Serial.println("no networks found"); else { Serial.print(n); Serial.println(" networks found"); for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found Serial.print(i + 1); Serial.print(": "); Serial.print(WiFi.SSID(i)); Serial.print(" ("); Serial.print(WiFi.RSSI(i)); Serial.print(")"); Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*"); delay(10); } } Serial.println(""); st = "
    "; for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found st += "
  • "; st +=i + 1; st += ": "; st += WiFi.SSID(i); st += " ("; st += WiFi.RSSI(i); st += ")"; st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*"; st += "
  • "; } st += "
"; delay(100); WiFi.softAP(ssid); Serial.println("softap"); Serial.println(""); launchWeb(1); Serial.println("over"); } int mdns1(int webtype) { // Check for any mDNS queries and send responses mdns.update(); // Check if a client has connected WiFiClient client = server.available(); if (!client) { return(20); } Serial.println(""); Serial.println("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) { Serial.print("Invalid request: "); Serial.println(req); return(20); } req = req.substring(addr_start + 1, addr_end); Serial.print("Request: "); Serial.println(req); client.flush(); String s; if ( webtype == 1 ) { if (req == "/") { IPAddress ip = WiFi.softAPIP(); String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello from ESP8266 at "; s += ipStr; s += "

"; s += st; s += "

"; s += "\r\n\r\n"; Serial.println("Sending 200"); } else if ( req.startsWith("/a?ssid=") ) { // /a?ssid=blahhhh&pass=poooo Serial.println("clearing eeprom"); for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); } String qsid; qsid = req.substring(8,req.indexOf('&')); Serial.println(qsid); Serial.println(""); String qpass; qpass = req.substring(req.lastIndexOf('=')+1); Serial.println(qpass); Serial.println(""); Serial.println("writing eeprom ssid:"); for (int i = 0; i < qsid.length(); ++i) { EEPROM.write(i, qsid[i]); Serial.print("Wrote: "); Serial.println(qsid[i]); } Serial.println("writing eeprom pass:"); for (int i = 0; i < qpass.length(); ++i) { EEPROM.write(32+i, qpass[i]); Serial.print("Wrote: "); Serial.println(qpass[i]); } EEPROM.commit(); s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello from ESP8266 "; s += "Found "; s += req; s += "

saved to eeprom... reset to boot into new wifi\r\n\r\n"; } else { s = "HTTP/1.1 404 Not Found\r\n\r\n"; Serial.println("Sending 404"); } } else { if (req == "/") { s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello from ESP8266"; s += "

"; s += "\r\n\r\n"; Serial.println("Sending 200"); } else if ( req.startsWith("/cleareeprom") ) { s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello from ESP8266"; s += "

Clearing the EEPROM

"; s += "\r\n\r\n"; Serial.println("Sending 200"); Serial.println("clearing eeprom"); for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); } EEPROM.commit(); } else { s = "HTTP/1.1 404 Not Found\r\n\r\n"; Serial.println("Sending 404"); } } client.print(s); Serial.println("Done with client"); return(20); } void loop() { // put your main code here, to run repeatedly: } ================================================ FILE: README.md ================================================ # esp-arduino-apboot ESP8266 wifi configurator in Arduino lang.. uses eeprom for configs, boots to AP mode if no working config found