Created
June 17, 2016 04:25
-
-
Save igrr/424ce9d67029b9c654e509d29de68719 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <FS.h> | |
#include <ESP8266WebServer.h> | |
void startSoftAP() { | |
Serial.println("startSoftAP"); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP("ESPAP"); | |
ESP8266WebServer server; | |
bool configDone = false; | |
String ssid; | |
String pass; | |
server.serveStatic("/", SPIFFS, "/index.html"); | |
server.on("/config", [&]() { | |
Serial.println("config"); | |
ssid = server.arg("ssid"); | |
pass = server.arg("pass"); | |
Serial.print("SSID: "); | |
Serial.print(ssid); | |
Serial.print("pass: "); | |
Serial.println(pass); | |
configDone = true; | |
server.send(200, "text/plain", "ok"); | |
}); | |
server.begin(); | |
while (!configDone) { | |
server.handleClient(); | |
} | |
Serial.println("Config is done, trying to connect:"); | |
tryToConnect(ssid, pass); | |
} | |
void tryToConnect(String ssid, String pass) { | |
int start = millis(); | |
const int timeout = 10000; | |
Serial.println(); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid.c_str(), pass.c_str()); | |
while (WiFi.status() != WL_CONNECTED && millis() - start < timeout) { | |
delay(500); | |
Serial.print('.'); | |
} | |
Serial.println(); | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("Connection failed"); | |
} | |
else { | |
Serial.println("Connected!"); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.setDebugOutput(true); | |
delay(1000); | |
Serial.println(); | |
Serial.println("Starting...."); | |
if (!SPIFFS.begin()) { | |
Serial.println("Failed to mount filesystem"); | |
} | |
Dir root = SPIFFS.openDir("/"); | |
while (root.next()) { | |
Serial.println(root.fileName()); | |
} | |
if (WiFi.SSID().length() > 0) { | |
Serial.print("We have SSID saved: "); | |
Serial.println(WiFi.SSID()); | |
tryToConnect(WiFi.SSID(), WiFi.psk()); | |
} | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.println("Not connected, starting SOFTAP"); | |
startSoftAP(); | |
} | |
Serial.println("Finally connected"); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment