24.02.2017ESP8266 - Uložení nastavení WIFI
Demo využití filesystému
Demo programu který uloží nastavení wifi do souboru na filesystému a pak ho použije k připojení na síť.
Demo program
// use command: <SSID;PASSWORD> #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include "FS.h" const char* ssid = "defaultssid"; const char* password = "defaultpwd"; const char* authFile = "/wifi.auth"; ESP8266WebServer server(80); const int led = 13; void handleRoot() { digitalWrite(led, 1); server.send(200, "text/plain", "hello from esp8266!"); digitalWrite(led, 0); } void handleNotFound(){ digitalWrite(led, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(led, 0); } bool readSSID = false; bool readPwd = false; bool readDone = false; String rSSID = ""; String rPWD = ""; void readAuthFromSerial(){ if (Serial.available() >0) { while(Serial.available() >0) { char c = Serial.read(); //gets one byte from serial buffer if(c == '<'){ readSSID = true; } else if(c == ';'){ readPwd = true; } else if(c == '>'){ readSSID = false; readDone = true; } else if(readPwd){ rPWD += c; } else if(readSSID){ rSSID += c; } } if(readDone){ Serial.println(); Serial.println("Received from serial:"); Serial.println("---------------------"); Serial.print("SSID:"); Serial.println(rSSID); Serial.print("Password:"); Serial.println(rPWD); Serial.println("- Save to file"); saveAuth(rSSID, rPWD); Serial.println("Read from file:"); Serial.println("---------------------"); checkAuth(); Serial.println("---------------------"); Serial.println("Reboot your device"); readDone = false; readSSID = false; readPwd = false; rSSID = ""; rPWD = ""; } } } void saveAuth(String ssid, String password) { File f = SPIFFS.open(authFile, "w"); if (!f) { Serial.println("file creation failed"); return; } f.print(ssid); f.print(";"); f.print(password); f.print(";"); f.close(); } void checkAuth() { File f = SPIFFS.open(authFile, "r"); if (!f) { Serial.println("File not found. Set auth usingo command <SSID;PASSWORD>"); return; } Serial.print("SSID: "); Serial.println(getAuthSSID()); Serial.print("Password: "); Serial.println(getAuthPassword()); f.close(); } String getAuthSSID() { File f = SPIFFS.open(authFile, "r"); if (!f) { Serial.println("File not found. Set auth usingo command <SSID;PASSWORD>"); return ""; } int i = 0; while(f.available()) { String line = f.readStringUntil(';'); if(i == 0) { f.close(); return line; } i++; } f.close(); return ""; } String getAuthPassword() { File f = SPIFFS.open(authFile, "r"); if (!f) { Serial.println("File not found. Set auth usingo command <SSID;PASSWORD>"); return ""; } int i = 0; while(f.available()) { String line = f.readStringUntil(';'); if(i == 1) { f.close(); return line; } i++; } f.close(); return ""; } void setup(void){ const char* _ssid = ""; const char* _password = ""; pinMode(led, OUTPUT); digitalWrite(led, 0); bool result = SPIFFS.begin(); // always use this to "mount" the filesystem Serial.begin(115200); _ssid = strdup(getAuthSSID().c_str()); _password = strdup(getAuthPassword().c_str()); Serial.println(); Serial.println(); Serial.println("Connect to:"); Serial.print("SSID: "); Serial.println(_ssid); Serial.print("PWD: "); Serial.println(_password); Serial.println(); WiFi.begin(_ssid, _password); Serial.println(""); // Wait for connection int counter = 0; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); readAuthFromSerial(); counter++; if(counter > 40){ Serial.println(); counter = 0; } } Serial.println(""); Serial.print("Connected to "); Serial.println(_ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { 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"); } void loop(void){ readAuthFromSerial(); server.handleClient(); }
comments powered by Disqus