PHP_SMART_HOME_V3/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino

127 lines
3.3 KiB
Arduino
Raw Normal View History

2019-08-23 20:17:42 +00:00
//Includes
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
//Variables
2019-09-19 12:56:44 +00:00
const char* ssid = " ";
2019-09-09 18:46:49 +00:00
const char* pasw = "";
2019-09-19 12:50:42 +00:00
const char* hwId = "";
2019-11-27 16:19:22 +00:00
const char* server = "http://dev.steelants.cz/vasek/home/api.php";
int unsuccessfulRounds = 0; //time to wait before restart
2019-08-23 20:17:42 +00:00
int lastState = 0;
//Constant
#define SONOFF 12
#define SONOFF_LED 13
#define SONOFF_BUT 0
void setup() {
Serial.begin(9600);
2019-11-27 16:19:22 +00:00
while (!Serial) continue;
2019-08-23 20:17:42 +00:00
delay(10);
2019-08-23 21:03:55 +00:00
Serial.println('\n');
2019-08-23 20:17:42 +00:00
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);
2019-11-27 16:19:22 +00:00
checkConnection();
2019-09-19 12:50:42 +00:00
2019-08-23 21:03:55 +00:00
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
2019-08-23 20:17:42 +00:00
}
void loop() {
StaticJsonDocument<200> jsonContent;
jsonContent["token"] = hwId;
if (!digitalRead(SONOFF_BUT)){
jsonContent["values"]["on/off"]["value"] = (int) !lastState;
2019-08-23 21:03:55 +00:00
if (!lastState == 1) {
2019-09-19 12:50:42 +00:00
digitalWrite(SONOFF, HIGH)
2019-08-23 21:03:55 +00:00
} else if (!lastState == 0){
2019-09-19 12:50:42 +00:00
digitalWrite(SONOFF, LOW)
2019-08-23 21:03:55 +00:00
}
2019-08-23 20:17:42 +00:00
while(!digitalRead(SONOFF_BUT)) {
delay(100);
}
}
String requestJson = "";
serializeJson(jsonContent, requestJson);
Serial.println("JSON: " + requestJson);
2019-08-23 21:03:55 +00:00
2019-11-27 16:19:22 +00:00
//HTTP CLIENT
2019-08-23 20:17:42 +00:00
HTTPClient http;
http.begin(server);
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
2019-11-27 16:19:22 +00:00
2019-08-23 20:17:42 +00:00
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"];
2019-08-23 21:03:55 +00:00
int state = jsonContent["value"];
2019-11-27 16:19:22 +00:00
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()
}
2019-09-19 12:50:42 +00:00
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;
2019-08-23 20:17:42 +00:00
}
2019-11-27 16:19:22 +00:00
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;
}