From f089205dc058617ba8cf92604b85d7825c395b51 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Fri, 23 Aug 2019 22:17:42 +0200 Subject: [PATCH] beta test --- .../firmwares/Sonoff_S20/ESP_END_POINT.ino | 94 ---------------- .../ESP_END_POINT/ESP_END_POINT.ino | 100 ++++++++++++++++++ 2 files changed, 100 insertions(+), 94 deletions(-) delete mode 100644 _FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT.ino create mode 100644 _FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT/ESP_END_POINT.ino diff --git a/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT.ino b/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT.ino deleted file mode 100644 index 582df12..0000000 --- a/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT.ino +++ /dev/null @@ -1,94 +0,0 @@ -//Includes -#include -#include -#include - -//Variables -const char* ssid = ""; -const char* pasw = ""; -const char* server = "httpa://dev.steelants.cz/vasek/home/api.php"; -const char* hwId = ""; -int lastState = 0; -int reconectAtemptsMax = 10; //time to wait before restart - -//Constant -#define SONOFF 12 -#define SONOFF_LED 13 -#define SONOFF_BUT 0 -HTTPClient http; - -void setup() { - Serial.begin(9600); - Serial.println("HW: " + String(hwId)); - pinMode(SONOFF, OUTPUT); - pinMode(SONOFF_LED, OUTPUT); - pinMode(SONOFF_BUT, OUTPUT); - pinMode(SONOFF_BUT, INPUT); - - WiFi.persistent(false); - WiFi.mode(WIFI_STA); - - WiFi.setAutoConnect (true); - WiFi.setAutoReconnect (true); - - WiFi.begin(ssid, pasw); - http.begin(server); -} - -void loop() { - int reconectAtempts = 0; - while(WiFi.status() != WL_CONNECTED){ - if (reconectAtemptsMax == reconectAtempts) { - ESP.restart(); - } - WiFi.begin(ssid, pasw); - delay(1000); - } - - bool buttonState = digitalRead(SONOFF_BUT); - - http.addHeader("Content-Type", "text/plain"); //Specify content-type header - - StaticJsonBuffer<1024> jsonContent; - jsonContent["token"] = hwId; - - if (buttonState){ - jsonContent["values"]["on/off"]["value"] = lastState; - jsonContent["values"]["on/off"]["unit"] = ''; - while(buttonState) { - delay(100); - } - } - - String requestJson = ""; - serializeJson(jsonContent, requestJson); - jsonContent.clean(); - Serial.println("JSON: " + requestJson); - - int httpCode = http.POST(jsonContent); - String payload = http.getString(); //Get the response payload - - Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code - Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload - - deserializeJson(doc, payload); - - string hostname = doc["device"]["hostname"]; - - WiFi.hostname(hostname); - - int state = doc["state"]; - - if (state == 1 && lastState == 0) { - Serial.println("ON"); - digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW - digitalWrite(SONOFF_LED, LOW); // 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 - digitalWrite(SONOFF_LED, HIGH); // Turn the LED on by making the voltage LOW - } - - lastState = state; - delay(1000); -} \ No newline at end of file diff --git a/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT/ESP_END_POINT.ino b/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT/ESP_END_POINT.ino new file mode 100644 index 0000000..3d5321b --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_S20/ESP_END_POINT/ESP_END_POINT.ino @@ -0,0 +1,100 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = "SteelAntsNET"; +const char* pasw = "tgr786hgtp3CZ"; +const char* server = "http://dev.steelants.cz/vasek/home/api.php"; +const char* hwId = "tatsad5"; +int lastState = 0; +int reconectAtemptsMax = 10; //time to wait before restart + +//Constant +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + + +void setup() { + Serial.begin(9600); + 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); + Serial.print("Connecting to "); + Serial.print(ssid); Serial.println(" ..."); + + int i = 0; + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + delay(1000); + Serial.print(++i); Serial.print(' '); + } + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); +} + +void loop() { + + Serial.println("CONECED"); + bool buttonState = !digitalRead(SONOFF_BUT); + Serial.println("CONECED" + String(buttonState)); + + StaticJsonDocument<200> jsonContent; + jsonContent["token"] = hwId; + + if (!digitalRead(SONOFF_BUT)){ + jsonContent["values"]["on/off"]["value"] = (int) !lastState; + while(!digitalRead(SONOFF_BUT)) { + delay(100); + } + } + + String requestJson = ""; + serializeJson(jsonContent, requestJson); + Serial.println("JSON: " + requestJson); + + 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"]; + + WiFi.hostname(hostname); + + int state = jsonContent["value"]; + if (state != lastState){ + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + digitalWrite(SONOFF_LED, LOW); // 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 + digitalWrite(SONOFF_LED, HIGH); // Turn the LED on by making the voltage LOW + } + } + + lastState = state; + delay(1000); +}