Minore FW Tweeks

This commit is contained in:
JonatanRek 2019-11-30 18:35:45 +01:00
parent 563999e92d
commit 3bca87a4b3
2 changed files with 45 additions and 33 deletions

View File

@ -133,7 +133,8 @@ void setStaticIp(String IpAddress, String subnet, String gateway){
if ( if (
staticIpAddress.fromString(ipAddress) && staticIpAddress.fromString(ipAddress) &&
subnetIpAddress.fromString(subnet) && subnetIpAddress.fromString(subnet) &&
gatewayIpAddress.fromString(gateway) gatewayIpAddress.fromString(gateway) &&
WiFi.localIP() != staticIpAddress
) { ) {
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress); WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
Serial.print("STATIC IP address:\t"); Serial.print("STATIC IP address:\t");

View File

@ -4,11 +4,11 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
//Variables //Variables
const char* ssid = " "; const char* ssid = "";
const char* pasw = ""; const char* pasw = "";
const char* hwId = ""; const char* hwId = "";
const char* server = "http://dev.steelants.cz/vasek/home/api.php"; const char* server = "http://dev.steelants.cz/vasek/home/api.php";
int unsuccessfulRounds = 0; //time to wait before restart int unsuccessfulRounds = 0; //Unsucesful atmpt counter
int lastState = 0; int lastState = 0;
//Pins //Pins
@ -35,20 +35,22 @@ void setup() {
Serial.println('\n'); Serial.println('\n');
Serial.println("Connection established!"); Serial.println("Connection established!");
Serial.print("IP address:\t"); Serial.print("IP address:");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.print("MAC address:");
Serial.println(WiFi.macAddress());
} }
void loop() { void loop() {
StaticJsonDocument<200> jsonContent; StaticJsonDocument<250> 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);
@ -71,39 +73,30 @@ void loop() {
Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code
Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload
deserializeJson(jsonContent, payload); DeserializationError error = deserializeJson(jsonContent, payload);
String hostname = jsonContent["device"]["hostname"];
//configuration setup
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"]; int state = jsonContent["value"];
if (requestState != "succes") {
DeserializationError error = deserializeJson(doc, httpPayload);
//configuration setup
String hostName = doc["device"]["hostname"];
String ipAddress = doc["device"]["ipAddress"];
String state = doc["state"];
if (state != "succes") {
unsuccessfulRounds++; unsuccessfulRounds++;
Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5"); Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5");
} else if (state == "succes") { } else if (requestState == "succes") {
unsuccessfulRounds = 0; unsuccessfulRounds = 0;
} }
//Set static ip //Set static ip
IPAddress addr; setStaticIp(ipAddress, gateway, subnet);
if (addr.fromString(ipAddress)) {
IPAddress ip(addr);
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
}
WiFi.hostname(hostName); WiFi.hostname(hostName);
if(unsuccessfulRounds == 5) { if(unsuccessfulRounds == 5) {
Serial.println("RESTARTING ESP"); Serial.println("RESTARTING ESP");
ESP.restart() ESP.restart();
} }
if (state != lastState){ if (state != lastState){
@ -134,3 +127,21 @@ bool checkConnection() {
Serial.println("Timed out."); Serial.println("Timed out.");
return false; return false;
} }
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());
}
}