Improved firmware

This commit is contained in:
JonatanRek 2019-09-09 20:46:49 +02:00
parent b303a49445
commit b1ad8b227b
1 changed files with 39 additions and 19 deletions

View File

@ -4,12 +4,12 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
//Variables //Variables
const char* ssid = "SteelAntsNET"; const char* ssid = "";
const char* pasw = "tgr786hgtp3CZ"; const char* pasw = "";
const char* server = "http://dev.steelants.cz/vasek/home/api.php"; const char* server = "http://dev.steelants.cz/vasek/home/api.php";
const char* hwId = "tatsad5"; const char* hwId = "tatsad5";
int lastState = 0; int lastState = 0;
int reconectAtemptsMax = 10; //time to wait before restart int reconectAtemptsMax = 0; //time to wait before restart
//Constant //Constant
#define SONOFF 12 #define SONOFF 12
@ -35,27 +35,45 @@ void setup() {
Serial.print(ssid); Serial.println(" ..."); Serial.print(ssid); Serial.println(" ...");
int i = 0; int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' '); Serial.print(++i); Serial.print(' ');
digitalWrite(SONOFF_LED, HIGH);
delay(1000);
digitalWrite(SONOFF_LED, LOW);
delay(1000);
} }
digitalWrite(SONOFF_LED, HIGH);
Serial.println('\n'); Serial.println('\n');
Serial.println("Connection established!"); Serial.println("Connection established!");
Serial.print("IP address:\t"); Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
void loop() { void loop() {
if (WiFi.status() != WL_CONNECTED){
digitalWrite(SONOFF_LED, HIGH);
delay(1000);
digitalWrite(SONOFF_LED, LOW);
delay(1000);
reconectAtemptsMax++;
Serial.println("Reconect Attempt " + String(reconectAtemptsMax) + " from 10");
if (reconectAtemptsMax == 10) {
ESP.restart();
}
return;
}
StaticJsonDocument<200> jsonContent; StaticJsonDocument<200> jsonContent;
jsonContent["token"] = hwId; jsonContent["token"] = hwId;
if (!digitalRead(SONOFF_BUT)){ if (!digitalRead(SONOFF_BUT)){
jsonContent["values"]["on/off"]["value"] = (int) !lastState; jsonContent["values"]["on/off"]["value"] = (int) !lastState;
if (!lastState == 1) { if (!lastState == 1) {
digitalWrite(SONOFF, HIGH) digitalWrite(SONOFF, HIGH);
} else if (!lastState == 0){ } else if (!lastState == 0){
digitalWrite(SONOFF, LOW) digitalWrite(SONOFF, LOW);
} }
while(!digitalRead(SONOFF_BUT)) { while(!digitalRead(SONOFF_BUT)) {
delay(100); delay(100);
@ -80,15 +98,17 @@ void loop() {
String hostname = jsonContent["device"]["hostname"]; String hostname = jsonContent["device"]["hostname"];
int state = jsonContent["value"]; int state = jsonContent["value"];
WiFi.hostname(hostname); WiFi.hostname(hostname);
if (state != lastState){ if (httpCode == 200){
if (state == 1 && lastState == 0) { if (state != lastState){
Serial.println("ON"); if (state == 1 && lastState == 0) {
digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW Serial.println("ON");
} else { digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW
Serial.println("OFF"); } else {
digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW Serial.println("OFF");
} digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW
} }
lastState = state; }
}
lastState = digitalRead(SONOFF);
} }