FW v3 TWEEKS

This commit is contained in:
JonatanRek 2019-11-26 13:56:06 +01:00
parent 3e71be821c
commit 9e6968bf6c

View File

@ -8,6 +8,7 @@ const char* ssid = "";
const char* pasw = ""; const char* pasw = "";
const char* hwId = ""; const char* hwId = "";
const char* url = "http://dev.steelants.cz/vasek/home/api.php"; const char* url = "http://dev.steelants.cz/vasek/home/api.php";
int unsuccessfulRounds = 0;
//Pins //Pins
#define pinDHT 4 #define pinDHT 4
@ -17,84 +18,98 @@ const char* url = "http://dev.steelants.cz/vasek/home/api.php";
DHT DHTs(pinDHT, DHT11); DHT DHTs(pinDHT, DHT11);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
while (!Serial) continue; while (!Serial) continue;
pinMode(LIGHTPIN, INPUT); pinMode(LIGHTPIN, INPUT);
} }
void loop() { void loop() {
WiFi.begin(ssid, pasw); WiFi.begin(ssid, pasw);
checkConnection(); checkConnection();
//HTTP CLIENT //HTTP CLIENT
HTTPClient http; HTTPClient http;
http.begin(url); http.begin(url);
http.addHeader("Content-Type", "text/plain"); //Specify content-type header http.addHeader("Content-Type", "text/plain"); //Specify content-type header
DHTs.begin(); DHTs.begin();
//JsonDocsDefinition //JsonDocsDefinition
StaticJsonDocument<265> doc; StaticJsonDocument<265> doc;
doc["token"] = hwId; doc["token"] = hwId;
float tem = DHTs.readTemperature(); float tem = DHTs.readTemperature();
float hum = DHTs.readHumidity(); float hum = DHTs.readHumidity();
Serial.println("TEMP" + String(tem) + ";HUMI" + String(hum)); Serial.println("TEMP" + String(tem) + ";HUMI" + String(hum));
if (isnan(tem) || isnan(hum)) { if (isnan(tem) || isnan(hum)) {
Serial.println("Unable to read DHT"); Serial.println("Unable to read DHT");
} else { } else {
doc["values"]["temp"]["value"] = tem; doc["values"]["temp"]["value"] = tem;
doc["values"]["temp"]["unit"] = "C"; doc["values"]["temp"]["unit"] = "C";
doc["values"]["humi"]["value"] = hum; doc["values"]["humi"]["value"] = hum;
doc["values"]["humi"]["unit"] = "%"; doc["values"]["humi"]["unit"] = "%";
} }
doc["values"]["light"]["value"] = analogRead(LIGHTPIN); doc["values"]["light"]["value"] = analogRead(LIGHTPIN);
doc["values"]["light"]["unit"] = ""; doc["values"]["light"]["unit"] = "";
/*More Senzores to come*/ /*More Senzores to come*/
String jsonPayload = ""; String jsonPayload = "";
serializeJson(doc, jsonPayload); serializeJson(doc, jsonPayload);
Serial.print("JSON: "); Serial.print("JSON: ");
Serial.println(jsonPayload); Serial.println(jsonPayload);
int httpCode = http.POST(jsonPayload); int httpCode = http.POST(jsonPayload);
String httpPayload = http.getString(); //Get the response payload String httpPayload = http.getString(); //Get the response payload
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(httpPayload) + ""); //Print request response payload Serial.println("HTTP BODY: " + String(httpPayload) + ""); //Print request response payload
DeserializationError error = deserializeJson(doc, httpPayload); DeserializationError error = deserializeJson(doc, httpPayload);
//configuration setup //configuration setup
String hostName = doc["device"]["hostname"]; String hostName = doc["device"]["hostname"];
int sleepTime = doc["device"]["sleepTime"]; int sleepTime = doc["device"]["sleepTime"];
WiFi.hostname(hostName); String state = doc["state"];
http.end(); //Close connection if (state != "succes") {
Serial.println("DISCONECTED FROM WIFI"); unsuccessfulRounds++;
WiFi.disconnect(); Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5");
} else if (state == "succes") {
Serial.println("GOING TO SLEEP FOR " + String(sleepTime)); unsuccessfulRounds = 0;
if (sleepTime > 0) { }
ESP.deepSleep((sleepTime * 60) * 1000000, RF_DEFAULT);
} else { WiFi.hostname(hostName);
delay(5000);
} http.end(); //Close connection
Serial.println("DISCONECTED FROM WIFI");
WiFi.disconnect();
if(unsuccessfulRounds == 5) {
Serial.println("RESTARTING ESP");
ESP.restart()
}
Serial.println("GOING TO SLEEP FOR " + String(sleepTime));
if (sleepTime > 0) {
ESP.deepSleep((sleepTime * 60) * 1000000, RF_DEFAULT);
} else {
delay(5000);
}
} }
bool checkConnection() { bool checkConnection() {
int count = 0; int count = 0;
Serial.print("Waiting for Wi-Fi connection"); Serial.print("Waiting for Wi-Fi connection");
while ( count < 30 ) { while ( count < 30 ) {
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
Serial.println(); Serial.println();
Serial.println("Connected!"); Serial.println("Connected!");
return (true); return (true);
}
delay(500);
Serial.print(".");
count++;
} }
delay(500); Serial.println("Timed out.");
Serial.print("."); return false;
count++;
}
Serial.println("Timed out.");
return false;
} }