diff --git a/README.md b/README.md index 3fec868..339a9b4 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,15 @@ Wifi Host for the Arduino-Laser-Spirograph HTTP GET endpoints: /motor/{motorNr}/{value} /laser/{value} - /ssid/{ssidname} - /pwd/{password} - /apmode/1 -> SofAP (default), 0 -> client /saveconf -> write wificonfig to file /resetwifi -> reconnect wifi with the new settings - /readwifi -> returns wifi settings + /wificonfig -> GET returns wifi settings + POST sets new wifi settings + apMode=0: will try to connect to SSID first, 1: will directly start the AP + SSID=ssid + password=password + save=true will save config to spiffs + apply=true will reset wifi and try to connect with new param (buggy atm) /readvalues -> return laser and motor values (all 0 at startup, ram only) # Install @@ -25,7 +28,7 @@ HTTP GET endpoints: https://github.com/bblanchon/ArduinoJson/ - follow this instruction: - set wifi parameters in data/config.json before upload + set wifi parameters in data/wifi.json before upload http://esp8266.github.io/Arduino/versions/2.0.0/doc/filesystem.html#uploading-files-to-file-system You may also need to: diff --git a/esp8266-laser.ino b/esp8266-laser.ino index 4fcefb6..93b2771 100644 --- a/esp8266-laser.ino +++ b/esp8266-laser.ino @@ -11,36 +11,47 @@ #include #include -SoftwareSerial laserSerial(14, 12, false, 256); +/* + some hard coded config, change if needed +*/ -String ssid = ""; -String password = ""; +// Set how long the esp should try to connect to a network before starting it's own AP +int wifiTimeout = 15000; //ms -int apMode = 1; // 0: client, 1: AP (default) // will be overwriten -int apModeRuntime = 0; -int wifiTimeout = 10000; //ms - - -// [0] laser[1] m1 [2] m2 [3] m3 -byte lmValues[4] = {0, 0, 0, 0}; - - -/* Soft AP network parameters */ +// Hardcoded Soft AP network parameters char *ssidAP = "laserAp"; -IPAddress apIP(192, 168, 4, 1); // note: update metaRefreshStr string if ip change! +IPAddress apIP(192, 168, 4, 1); IPAddress netMsk(255, 255, 255, 0); +// this is sent to server if invalid request +const char *metaRefreshStr = "

redirecting...

"; + /* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */ const char *myHostname = "esplaser"; -const char *metaRefreshStr = "

redirecting...

"; +// espsoftwareserial +// SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic = false, unsigned int buffSize = 64); +SoftwareSerial laserSerial(14, 12, false, 256); +/* + End of hard coded config +*/ + +String ssid = ""; +String password = ""; + +int apMode = 1; // default value, will be overwriten with config from file +int _apMode = 0; // runtime apMode + +// [0] laser[1] m1 [2] m2 [3] m3 +byte lmValues[4] = {0, 0, 0, 0}; unsigned long previousMillis = 0; // Web server ESP8266WebServer server(80); + //format bytes String formatBytes(size_t bytes) { if (bytes < 1024) { @@ -182,43 +193,70 @@ void handleReadValues() json = String(); } -void handleReadWifi() +void hReadWifi() { - String json = "{ \"handledReadWifi\":"; - json += "{\"apMode\":\"" + (String)apMode + "\","; - json += "\"ssid\":\"" + (String)ssid + "\","; - json += "\"password\":\"***\"}}"; //(String)password - server.send(200, "text/json", json); - json = String(); + // Serial.println("hReadWifi"); + String responseBuffer = String(); + StaticJsonBuffer<200> jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + JsonObject& data = root.createNestedObject("wificonfig"); + data["apMode"] = apMode; + data["SSID"] = ssid; + data["Password"] = "***"; //password; + //data["save"] = "false"; + //data["apply"] = "false"; + root.prettyPrintTo(responseBuffer); + //root.prettyPrintTo(Serial); + server.send(200, "text/json", responseBuffer); + jsonBuffer.clear(); + responseBuffer = String(); } -void handleSsid(String uri) +void hWriteWifi() { - ssid = uri.substring(6); - - String json = "{ \"handledSsid\":"; - json += "{\"ssid\":\"" + (String)ssid + "\"}}"; - server.send(200, "text/json", json); - json = String(); + // Serial.println("hWriteWifi"); + String responseBuffer = String(); + boolean save = false; + boolean apply = false; + StaticJsonBuffer<200> jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + JsonObject& data = root.createNestedObject("wificonfig"); + if (server.hasArg("apMode") ) { + apMode = server.arg("apMode").toInt(); + data["apMode"] = apMode; + } + if (server.hasArg("SSID") ) { + ssid = server.arg("SSID"); + data["SSID"] = ssid; + } + if (server.hasArg("password") ) { + password = server.arg("password"); + data["Password"] = "***"; //password; + } + if (server.arg("save") == "true") + { + save = true; + data["save"] = "true"; + } + if (server.arg("apply") == "true") + { + apply = true; + data["apply"] = "true"; + } + root.prettyPrintTo(responseBuffer); + //root.prettyPrintTo(Serial); + server.send(200, "text/json", responseBuffer); + if (save) { + saveConfigFile(); + } + if (apply) { + setupWifi(); + } + jsonBuffer.clear(); + responseBuffer = String(); } -void handlePwd(String uri) -{ - password = uri.substring(5); - String json = "{ \"handledPwd\":"; - json += "{\"password\":\"" + (String)password + "\"}}"; - server.send(200, "text/json", json); - json = String(); -} -void handleApmode(String uri) -{ - apMode = uri.substring(8, 9).toInt(); - apModeRuntime = apMode; - String json = "{ \"handledApmode\":"; - json += "{\"apMode\":\"" + (String)apMode + "\"}}"; - server.send(200, "text/json", json); - json = String(); -} + bool saveConfigFile() { StaticJsonBuffer<200> jsonBuffer; @@ -227,7 +265,7 @@ bool saveConfigFile() { json["SSID"] = ssid; json["Password"] = password; - File configFile = SPIFFS.open("/config.json", "w"); + File configFile = SPIFFS.open("/wifi.json", "w"); if (!configFile) { Serial.println("Failed to open config file for writing"); return false; @@ -249,8 +287,8 @@ void handleSaveConf() { } boolean readConfigFile() { - Serial.println("Reading config.json"); - File configFile = SPIFFS.open("/config.json", "r"); + Serial.println("Reading wifi.json"); + File configFile = SPIFFS.open("/wifi.json", "r"); if (!configFile) { Serial.println("Failed to open config file"); return false; @@ -273,7 +311,7 @@ boolean readConfigFile() { return false; } apMode = json["apMode"]; - apModeRuntime = apMode; + apMode = apMode; const char* tSsid = json["SSID"]; const char* tPwd = json["Password"]; ssid = tSsid; @@ -287,8 +325,8 @@ boolean readConfigFile() { boolean setupWifi() { Serial.print("setupWifi apMode="); - Serial.println(apModeRuntime); - if (apModeRuntime == 0) + Serial.println(apMode); + if (_apMode == 0) { Serial.print("Connecting to "); Serial.println(ssid); @@ -345,14 +383,14 @@ void setup(void) { if (!readConfigFile()) { Serial.println("reading wifi config failed, start SoftAP"); - apModeRuntime = 1; + _apMode = 1; } delay(100); if (!setupWifi()) { Serial.println("connect failed, start SoftAP"); - apModeRuntime = 1; // fallback to AP mode + _apMode = 1; // fallback to AP mode setupWifi(); } @@ -371,7 +409,13 @@ void setup(void) { server.on("/listFiles", HTTP_GET, handleFileList); server.on("/saveconf", HTTP_GET, handleSaveConf); server.on("/resetwifi", HTTP_GET, setupWifi); - server.on("/readwifi", HTTP_GET, handleReadWifi); + + server.on("/wificonfig", HTTP_GET, hReadWifi); + server.on("/wificonfig", HTTP_POST, hWriteWifi); + // server.on("/wificonfig", HTTP_POST, []() { + // hWriteWifi(); + // }); + server.on("/readvalues", HTTP_GET, handleReadValues); server.on("/heap", HTTP_GET, []() { String json = "{"; @@ -399,58 +443,20 @@ void handleNotFound() { handleLaser(uri); return; } - if (uri.substring(0, 6) == "/ssid/") - { - handleSsid(uri); - return; - } - if (uri.substring(0, 5) == "/pwd/") - { - handlePwd(uri); - return; - } - if (uri.substring(0, 8) == "/apmode/") - { - handleApmode(uri); - return; - } - if (uri.substring(0, 12) == "/config.json") + if (uri.substring(0, 12) == "/wifi.json") { server.send(403, "text/plain", "Forbidden"); return; } if (!handleFileRead(server.uri())) { - server.send(302, "text/html", metaRefreshStr); + server.send(404, "text/html", metaRefreshStr); // server.send(302, "text/plain", "blub"); } } void loop(void) { server.handleClient(); - - - int i = 0; - char commandbuffer[100]; - String sBuffer; - - if (laserSerial.available()) { - delay(100); - - while ( laserSerial.available() && i < 99) { - commandbuffer[i] = laserSerial.read(); - i++; - } - commandbuffer[i++] = '\0'; - } - - if (i > 0) - { - delay(200); - Serial.print("received from laser: "); - Serial.println((char*)commandbuffer); -// Serial.println(()sBuffer); - } }