diff --git a/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino b/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino index 5035cbe..fad1c36 100644 --- a/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino +++ b/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino @@ -20,6 +20,10 @@ DHT DHTs(pinDHT, DHT11); void setup() { Serial.begin(9600); while (!Serial) continue; + delay(10); + Serial.println('\n'); + Serial.println("HW: " + String(hwId)); + pinMode(LIGHTPIN, INPUT); } diff --git a/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20.ino b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v1.ino similarity index 100% rename from _FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20.ino rename to _FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v1.ino diff --git a/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino new file mode 100644 index 0000000..1277235 --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino @@ -0,0 +1,126 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = " "; +const char* pasw = ""; +const char* hwId = ""; +const char* server = "http://dev.steelants.cz/vasek/home/api.php"; +int unsuccessfulRounds = 0; //time to wait before restart +int lastState = 0; + +//Constant +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + +void setup() { + Serial.begin(9600); + while (!Serial) continue; + delay(10); + Serial.println('\n'); + Serial.println("HW: " + String(hwId)); + + pinMode(SONOFF, OUTPUT); + pinMode(SONOFF_LED, OUTPUT); + pinMode(SONOFF_BUT, INPUT); + + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, pasw); + checkConnection(); + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); +} + +void loop() { + StaticJsonDocument<200> jsonContent; + jsonContent["token"] = hwId; + + if (!digitalRead(SONOFF_BUT)){ + jsonContent["values"]["on/off"]["value"] = (int) !lastState; + if (!lastState == 1) { + digitalWrite(SONOFF, HIGH) + } else if (!lastState == 0){ + digitalWrite(SONOFF, LOW) + } + while(!digitalRead(SONOFF_BUT)) { + delay(100); + } + } + + String requestJson = ""; + serializeJson(jsonContent, requestJson); + Serial.println("JSON: " + requestJson); + + //HTTP CLIENT + HTTPClient http; + http.begin(server); + http.addHeader("Content-Type", "text/plain"); //Specify content-type header + + 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 + + deserializeJson(jsonContent, payload); + String hostname = jsonContent["device"]["hostname"]; + int state = jsonContent["value"]; + + + DeserializationError error = deserializeJson(doc, httpPayload); + + //configuration setup + String hostName = doc["device"]["hostname"]; + String state = doc["state"]; + + if (state != "succes") { + unsuccessfulRounds++; + Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5"); + } else if (state == "succes") { + unsuccessfulRounds = 0; + } + + WiFi.hostname(hostName); + + if(unsuccessfulRounds == 5) { + Serial.println("RESTARTING ESP"); + ESP.restart() + } + + if (state != lastState){ + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + } else { + Serial.println("OFF"); + digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW + } + } + lastState = state; +} + +bool checkConnection() { + int count = 0; + Serial.print("Waiting for Wi-Fi connection"); + while ( count < 30 ) { + if (WiFi.status() == WL_CONNECTED) { + Serial.println(); + Serial.println("Connected!"); + return (true); + } + delay(500); + Serial.print("."); + count++; + } + Serial.println("Timed out."); + return false; +}