PHP_SMART_HOME_V3/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2/Sonoff_S20_v2.ino

151 lines
4.2 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-11-30 17:35:45 +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";
2019-11-30 17:35:45 +00:00
int unsuccessfulRounds = 0; //Unsucesful atmpt counter
2019-08-23 20:17:42 +00:00
int lastState = 0;
2019-11-27 16:27:18 +00:00
//Pins
2019-08-23 20:17:42 +00:00
#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!");
2019-11-30 17:35:45 +00:00
Serial.print("IP address:");
Serial.println(WiFi.localIP());
Serial.print("MAC address:");
Serial.println(WiFi.macAddress());
2019-08-23 20:17:42 +00:00
}
void loop() {
2019-11-30 17:35:45 +00:00
StaticJsonDocument<250> jsonContent;
2019-08-23 20:17:42 +00:00
jsonContent["token"] = hwId;
2019-11-30 17:35:45 +00:00
if (!digitalRead(SONOFF_BUT)){
2019-08-23 20:17:42 +00:00
jsonContent["values"]["on/off"]["value"] = (int) !lastState;
2019-08-23 21:03:55 +00:00
if (!lastState == 1) {
2019-11-30 17:35:45 +00:00
digitalWrite(SONOFF, HIGH);
2019-08-23 21:03:55 +00:00
} else if (!lastState == 0){
2019-11-30 17:35:45 +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
2019-11-30 17:35:45 +00:00
DeserializationError error = deserializeJson(jsonContent, payload);
2019-11-27 16:19:22 +00:00
//configuration setup
2019-11-30 17:35:45 +00:00
String hostName = jsonContent["device"]["hostname"];
String ipAddress = jsonContent["device"]["ipAddress"];
String gateway = jsonContent["device"]["gateway"];
String subnet = jsonContent["device"]["subnet"];
String requestState = jsonContent["state"];
int state = jsonContent["value"];
2019-11-27 16:27:18 +00:00
2019-11-30 17:35:45 +00:00
if (requestState != "succes") {
2019-11-27 16:19:22 +00:00
unsuccessfulRounds++;
2019-11-30 17:35:45 +00:00
Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5");
} else if (requestState == "succes") {
2019-11-27 16:19:22 +00:00
unsuccessfulRounds = 0;
}
2019-11-30 17:35:45 +00:00
//Set static ip
setStaticIp(ipAddress, gateway, subnet);
2019-11-27 16:19:22 +00:00
WiFi.hostname(hostName);
if(unsuccessfulRounds == 5) {
Serial.println("RESTARTING ESP");
2019-11-30 17:35:45 +00:00
ESP.restart();
2019-11-27 16:19:22 +00:00
}
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);
}
2019-12-01 13:19:34 +00:00
digitalWrite(SONOFF, HIGH);
delay(250);
digitalWrite(SONOFF, HIGH);
delay(250);
2019-11-27 16:19:22 +00:00
Serial.print(".");
count++;
}
Serial.println("Timed out.");
return false;
}
2019-11-30 17:35:45 +00:00
void setStaticIp(String ipAddress, String subnet, String gateway){
//Set static ip
IPAddress staticIpAddress;
IPAddress subnetIpAddress;
IPAddress gatewayIpAddress;
if (
staticIpAddress.fromString(ipAddress) &&
subnetIpAddress.fromString(subnet) &&
gatewayIpAddress.fromString(gateway) &&
WiFi.localIP() != staticIpAddress
) {
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
Serial.print("STATIC IP address:");
Serial.println(WiFi.localIP());
}
}