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-08-23 20:17:42 +00:00
|
|
|
const char* server = "http://dev.steelants.cz/vasek/home/api.php";
|
2019-09-19 12:50:42 +00:00
|
|
|
const char* hwId = "";
|
2019-08-23 20:17:42 +00:00
|
|
|
int lastState = 0;
|
2019-09-19 12:50:42 +00:00
|
|
|
int reconectAtemptsMax = 10; //time to wait before restart
|
2019-08-23 20:17:42 +00:00
|
|
|
|
|
|
|
//Constant
|
|
|
|
#define SONOFF 12
|
|
|
|
#define SONOFF_LED 13
|
|
|
|
#define SONOFF_BUT 0
|
|
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
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);
|
|
|
|
Serial.print("Connecting to ");
|
2019-08-23 21:03:55 +00:00
|
|
|
Serial.print(ssid); Serial.println(" ...");
|
|
|
|
|
|
|
|
int i = 0;
|
2019-09-19 12:50:42 +00:00
|
|
|
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
|
2019-09-09 18:46:49 +00:00
|
|
|
delay(1000);
|
2019-09-19 12:50:42 +00:00
|
|
|
Serial.print(++i); Serial.print(' ');
|
2019-08-23 21:03:55 +00:00
|
|
|
}
|
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-08-23 20:17:42 +00:00
|
|
|
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"];
|
2019-08-23 21:03:55 +00:00
|
|
|
int state = jsonContent["value"];
|
2019-08-23 20:17:42 +00:00
|
|
|
WiFi.hostname(hostname);
|
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
|
|
|
}
|