More functions
This commit is contained in:
		| @@ -36,18 +36,18 @@ void loop() { | |||||||
|     //Start Conection to wifi |     //Start Conection to wifi | ||||||
|     WiFi.begin(ssid, pasw); |     WiFi.begin(ssid, pasw); | ||||||
|     checkConnection(); |     checkConnection(); | ||||||
|      |  | ||||||
|     //HTTP CLIENT |     //HTTP CLIENT | ||||||
|     HTTPClient http; |     HTTPClient http; | ||||||
|     http.begin(url); //Begun HTTP Request |     http.begin(url); //Begun HTTP Request | ||||||
|     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; | ||||||
|      |  | ||||||
|     //Read and Handle DHT values |     //Read and Handle DHT values | ||||||
|     float tem = DHTs.readTemperature(); |     float tem = DHTs.readTemperature(); | ||||||
|     float hum = DHTs.readHumidity(); |     float hum = DHTs.readHumidity(); | ||||||
| @@ -60,65 +60,51 @@ void loop() { | |||||||
|         doc["values"]["humi"]["value"] = hum; |         doc["values"]["humi"]["value"] = hum; | ||||||
|         doc["values"]["humi"]["unit"] = "%"; |         doc["values"]["humi"]["unit"] = "%"; | ||||||
|     } |     } | ||||||
|      |  | ||||||
|     //Handle Photo Rezistor Values |     //Handle Photo Rezistor Values | ||||||
|     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); //Get Http response code |     int httpCode = http.POST(jsonPayload); //Get Http response code | ||||||
|     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); //Get deserialization Error if exists |     DeserializationError error = deserializeJson(doc, httpPayload); //Get deserialization Error if exists | ||||||
|      |  | ||||||
|     //configuration setup |     //configuration setup | ||||||
|     String hostName = doc["device"]["hostname"]; |     String hostName = doc["device"]["hostname"]; | ||||||
|     int sleepTime = doc["device"]["sleepTime"]; |     int sleepTime = doc["device"]["sleepTime"]; | ||||||
|     String ipAddress = doc["device"]["ipAddress"]; |     String ipAddress = doc["device"]["ipAddress"]; | ||||||
|     String state = doc["state"]; |     String state = doc["state"]; | ||||||
|      |  | ||||||
|     if (state != "succes") { |     if (state != "succes") { | ||||||
|         unsuccessfulRounds++; |         unsuccessfulRounds++; | ||||||
|         Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5"); |         Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5"); | ||||||
|     } else if (state == "succes") { |     } else if (state == "succes") { | ||||||
|         unsuccessfulRounds = 0; |         unsuccessfulRounds = 0; | ||||||
|     } |     } | ||||||
|      |  | ||||||
|     //Set static ip  |     //Set static ip | ||||||
|     IPAddress staticIpAddress; |     setStaticIp(ipAddress, "192.168.0.1", "255.255.255.0") | ||||||
|     IPAddress subnetIpAddress(192,168,0,1); |  | ||||||
|     IPAddress gatewayIpAddress(255, 255, 255, 0); |  | ||||||
|      |  | ||||||
|     if (staticIpAddress.fromString(ipAddress)) { |  | ||||||
|         WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress); |  | ||||||
|         Serial.print("STATIC IP address:\t"); |  | ||||||
|         Serial.println(WiFi.localIP());  |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     WiFi.hostname(hostName); //Set HostName |     WiFi.hostname(hostName); //Set HostName | ||||||
|      |  | ||||||
|     http.end();  //Close connection |     http.end();  //Close connection | ||||||
|     WiFi.disconnect(); //Disconect from WIFI |     WiFi.disconnect(); //Disconect from WIFI | ||||||
|     Serial.println("DISCONECTED FROM WIFI"); |     Serial.println("DISCONECTED FROM WIFI"); | ||||||
|      |  | ||||||
|     if(unsuccessfulRounds == 5) { //after 5 unsucessful request restart ESP |     if(unsuccessfulRounds == 5) { //after 5 unsucessful request restart ESP | ||||||
|         Serial.println("RESTARTING ESP"); |         Serial.println("RESTARTING ESP"); | ||||||
|         ESP.restart() |         ESP.restart() | ||||||
|     } |     } | ||||||
|      |  | ||||||
|     if (sleepTime > 0) { //if deep sleepTime > 0 use deep sleep |     sleep(); | ||||||
|         Serial.println("GOING TO SLEEP FOR " + String(sleepTime)); |  | ||||||
|         ESP.deepSleep((sleepTime * 60) * 1000000, RF_DEFAULT); |  | ||||||
|     } else { |  | ||||||
|         delay(5000); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| //checking if connection is working | //checking if connection is working | ||||||
| @@ -138,3 +124,27 @@ 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.config(staticIpAddress, subnetIpAddress, gatewayIpAddress); | ||||||
|  |       Serial.print("STATIC IP address:\t"); | ||||||
|  |       Serial.println(WiFi.localIP()); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | void sleep(int sleepTime) { | ||||||
|  |   if (sleepTime > 0) { //if deep sleepTime > 0 use deep sleep | ||||||
|  |       Serial.println("GOING TO SLEEP FOR " + String(sleepTime)); | ||||||
|  |       ESP.deepSleep((sleepTime * 60) * 1000000, RF_DEFAULT); | ||||||
|  |   } else { | ||||||
|  |       delay(5000); | ||||||
|  |   } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user