diff --git a/_FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino b/_FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino index b2d5156..7ca1b3b 100644 --- a/_FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino +++ b/_FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino @@ -6,94 +6,206 @@ #include //Variables -const char* ssidServer = "ESPFilip"; -const char* paswServer = "Sapatr6"; +const char* ssidServer = ""; +const char* paswServer = ""; String ssid = ""; String pasw = ""; String apiToken = ""; const char* host = "http://dev.steelants.cz"; const char* url = "/vasek/home/api.php"; -//NetworkData -// IPAddress staticIpAddress = ""; -// IPAddress subnetIpAddress = ""; -// IPAddress gatewayIpAddress = ""; +IPAddress staticIpAddress; +IPAddress gatewayIpAddress; +IPAddress subnetIpAddress; String content; bool conf = false; +bool buttonActive = false; +int state = 0; +int realState = 0; +String requestJson = ""; +int unsuccessfulRounds = 0; //Unsucesful atmpt counter ESP8266WebServer server(80); StaticJsonDocument<250> jsonContent; +DeserializationError error; //Pins #define RELAY 4 //12 #define SWITCH 5 //0 void setup() { - Serial.begin(115200); - EEPROM.begin(100); - while (!Serial) continue; - delay(10); - //read saved data - ssid = ReadEeprom(1,33); - pasw = ReadEeprom(33,65); - apiToken = ReadEeprom(65,97); - - //set pins - pinMode(SWITCH, INPUT); - pinMode(RELAY, OUTPUT); - - //wifi - if (ssid != "") { - WiFi.persistent(false); - WiFi.mode(WIFI_STA); - #if defined(staticIpAddress) && defined(subnetIpAddress) && defined(gatewayIpAddress) - WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress); - #endif - WiFi.begin(ssid, pasw); - conf = wifiVerify(20); - if (conf) { - return; - } + Serial.begin(115200); + EEPROM.begin(100); + while (!Serial) continue; + delay(10); + //read saved data + ssid = ReadEeprom(1, 33); + pasw = ReadEeprom(33, 65); + apiToken = ReadEeprom(65, 97); + //set pins + pinMode(SWITCH, INPUT); + pinMode(RELAY, OUTPUT); + state = EEPROM.read(0); + digitalWrite(RELAY, state); + realState = state; + attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE); + //wifi + if (ssid != "") { + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + //Serial.println(WiFi.localIP()); + //Serial.println("IP nastaveny z hodnot."); + //WiFi.config(staticIpAddress, gatewayIpAddress, subnetIpAddress); + WiFi.begin(ssid, pasw); + conf = wifiVerify(20); + if (conf) { + Serial.println(WiFi.localIP()); + jsonContent = {}; + jsonContent["token"] = apiToken; + jsonContent["values"]["on/off"]["value"] = (String)realState; + sendDataToWeb(); + return; } - setupAP(); + } + setupAP(); } void loop() { - if (!conf) { + if (conf) { + if (unsuccessfulRounds >= 5) { + Serial.println("RESTARTING ESP"); + ESP.restart(); + } + if (buttonActive) { + delay (500); + jsonContent = {}; + jsonContent["token"] = apiToken; + requestJson = ""; + jsonContent["values"]["on/off"]["value"] = (String)realState; + digitalWrite(RELAY, realState); + EEPROM.write(0, realState); + EEPROM.commit(); + sendDataToWeb(); + buttonActive = false; + } + loadDataFromWeb(); + } else { server.handleClient(); } } -bool wifiVerify(int t){ +void handleInterrupt() { + buttonActive = true; + realState = !state; + digitalWrite(RELAY, realState); +} + +bool wifiVerify(int t) { int c = 0; - Serial.println("Waiting for Wifi to connect to Shelly1"); + Serial.println("Waiting for Wifi to connect to Shelly1"); while (c < t) { - if (WiFi.status() == WL_CONNECTED) { return true; } + if (WiFi.status() == WL_CONNECTED) { + c = t; + return true; + } delay(500); - Serial.print(WiFi.status()); + Serial.print(WiFi.status()); c++; } return false; } -void CleanEeprom(){ - for (int i = 1; i < 100; ++i) { - EEPROM.write(i, 0); +void loadDataFromWeb() { + jsonContent = {}; + jsonContent["token"] = apiToken; + requestJson = ""; + sendDataToWeb(); + + if (error.code() != DeserializationError::Ok) { + Serial.println(error.c_str()); + unsuccessfulRounds++; + Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5"); + error = DeserializationError::Ok; + return; + } + + //configuration setup + String hostName = jsonContent["device"]["hostname"]; + String requestState = jsonContent["state"]; + JsonObject object = jsonContent.as(); + if (!object["value"].isNull()) { + state = (int)jsonContent["value"]; + } + + if (requestState != "succes") { + unsuccessfulRounds++; + Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5"); + } else if (requestState == "succes") { + unsuccessfulRounds = 0; + } + + WiFi.hostname(hostName); + Serial.println("state: " + (String)state + ", realState: " + (String)realState); + if (state != realState && !buttonActive) { + if (state == 1 && realState == 0) { + Serial.println("ON state: " + (String)state + ", realState: " + (String)realState); + realState = 1; + } else { + Serial.println("OFF"); + realState = 0; + } + digitalWrite(RELAY, realState); + EEPROM.write(0, realState); + EEPROM.commit(); + } +} + +void sendDataToWeb() { + serializeJson(jsonContent, requestJson); + Serial.println("JSON: " + requestJson); + error = deserializeJson(jsonContent, sendHttpRequest()); +} + +String sendHttpRequest () { + HTTPClient http; + http.setReuse(true); + Serial.println("HTTP url: " + String(host) + String(url) + ""); //Print HTTP return code + http.begin(String(host) + String(url)); + http.addHeader("Content-Type", "text/plain"); //Specify content-type header + Serial.println("HTTP request: " + String(requestJson) + ""); //Print HTTP return code + int httpCode = http.POST(requestJson); + String payload = http.getString(); //Get the response payload + http.end(); + + Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code + Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload + + if (httpCode == -1) { + unsuccessfulRounds++; + Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5"); + return ""; + } + return payload; +} + +void CleanEeprom() { + for (int i = 1; i < 100; ++i) { + EEPROM.write(i, 0); } } void WriteEeprom (String data, int start = 1) { for (int i = 0; i < data.length(); ++i) { - EEPROM.write(start + i, data[i]); + EEPROM.write(start + i, data[i]); } EEPROM.commit(); } -String ReadEeprom(int min, int max){ +String ReadEeprom(int min, int max) { String localString; - for(int i = min; i < max; ++i) { + for (int i = min; i < max; ++i) { localString += char(EEPROM.read(i)); } return localString; @@ -102,7 +214,7 @@ String ReadEeprom(int min, int max){ void createWeb() { server.on("/", []() { - if (server.args() == 3){ + if (server.args() == 3) { ssid = server.arg("wifi-ssid"); pasw = server.arg("wifi-pasw"); apiToken = server.arg("apiToken"); @@ -125,20 +237,38 @@ void createWeb() content += ""; content += "

WIFI Configuration

"; content += "Refresh"; + content += "
"; + int n = WiFi.scanNetworks(); + if (n == 0) + content += ""; + else + { + for (int i = 0; i < n; ++i) + { + content += "" + WiFi.SSID(i) + "
"; + } + } + content += "
"; content += "
"; - content += "
"; - content += "
"; - content += "
"; + content += "
"; + content += "
"; + content += "
"; content += ""; content += "
"; + content += ""; content += ""; - server.send(200, "text/html", content); + server.send(200, "text/html", content); }); } void setupAP(void) { WiFi.mode(WIFI_STA); WiFi.disconnect(); + WiFi.softAPdisconnect(true); delay(100); int n = WiFi.scanNetworks(); Serial.println("scan done"); @@ -149,7 +279,7 @@ void setupAP(void) { 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(": "); @@ -157,12 +287,12 @@ void setupAP(void) { Serial.print(" ("); Serial.print(WiFi.RSSI(i)); Serial.print(")"); - Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*"); + Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*"); delay(10); - } + } } delay(100); - WiFi.softAP(ssidServer); + WiFi.softAP(ssidServer, paswServer); Serial.println("softap"); Serial.println(""); Serial.println("WiFi connected");