2019-11-27 16:27:18 +00:00
|
|
|
//Includes
|
2019-11-04 14:51:30 +00:00
|
|
|
#include <DHT.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266HTTPClient.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
//Variables
|
2020-02-06 16:57:11 +00:00
|
|
|
const char* ssid = "Smart-Home";
|
|
|
|
const char* pasw = "S1pjg3684dcCPTUQ";
|
|
|
|
const char* hwId = "45235sadad";
|
2019-11-04 14:51:30 +00:00
|
|
|
const char* url = "http://dev.steelants.cz/vasek/home/api.php";
|
2019-11-26 12:56:06 +00:00
|
|
|
int unsuccessfulRounds = 0;
|
2019-11-04 14:51:30 +00:00
|
|
|
|
|
|
|
//Pins
|
|
|
|
#define pinDHT 4
|
2019-12-19 22:15:44 +00:00
|
|
|
#define LIGHTPIN 13
|
2019-11-04 14:51:30 +00:00
|
|
|
|
|
|
|
//Inicializations
|
|
|
|
DHT DHTs(pinDHT, DHT11);
|
|
|
|
|
|
|
|
void setup() {
|
2019-11-26 12:56:06 +00:00
|
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial) continue;
|
2019-11-27 16:19:22 +00:00
|
|
|
delay(10);
|
|
|
|
Serial.println('\n');
|
2019-11-27 18:48:41 +00:00
|
|
|
//Show start up Configuration
|
2019-11-27 16:19:22 +00:00
|
|
|
Serial.println("HW: " + String(hwId));
|
2019-11-27 18:15:13 +00:00
|
|
|
Serial.print("IP address:\t");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
Serial.print("MAC address:\t");
|
2019-12-19 22:15:44 +00:00
|
|
|
Serial.println(WiFi.macAddress());
|
2019-11-26 12:56:06 +00:00
|
|
|
pinMode(LIGHTPIN, INPUT);
|
2019-11-04 14:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2019-11-27 18:48:41 +00:00
|
|
|
//Start Conection to wifi
|
2019-11-26 12:56:06 +00:00
|
|
|
WiFi.begin(ssid, pasw);
|
|
|
|
checkConnection();
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
//HTTP CLIENT
|
|
|
|
HTTPClient http;
|
2019-11-27 18:48:41 +00:00
|
|
|
http.begin(url); //Begun HTTP Request
|
2019-11-26 12:56:06 +00:00
|
|
|
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
DHTs.begin();
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
//JsonDocsDefinition
|
|
|
|
StaticJsonDocument<265> doc;
|
|
|
|
doc["token"] = hwId;
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
//Read and Handle DHT values
|
2019-11-26 12:56:06 +00:00
|
|
|
float tem = DHTs.readTemperature();
|
|
|
|
float hum = DHTs.readHumidity();
|
|
|
|
Serial.println("TEMP" + String(tem) + ";HUMI" + String(hum));
|
|
|
|
if (isnan(tem) || isnan(hum)) {
|
|
|
|
Serial.println("Unable to read DHT");
|
|
|
|
} else {
|
|
|
|
doc["values"]["temp"]["value"] = tem;
|
|
|
|
doc["values"]["temp"]["unit"] = "C";
|
|
|
|
doc["values"]["humi"]["value"] = hum;
|
|
|
|
doc["values"]["humi"]["unit"] = "%";
|
|
|
|
}
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
//Handle Photo Rezistor Values
|
2019-12-19 22:15:44 +00:00
|
|
|
doc["values"]["light"]["value"] = (String)!digitalRead(LIGHTPIN);
|
2019-11-26 12:56:06 +00:00
|
|
|
doc["values"]["light"]["unit"] = "";
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
/*More Senzores to come*/
|
|
|
|
String jsonPayload = "";
|
|
|
|
serializeJson(doc, jsonPayload);
|
|
|
|
Serial.print("JSON: ");
|
|
|
|
Serial.println(jsonPayload);
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
int httpCode = http.POST(jsonPayload); //Get Http response code
|
2019-11-26 12:56:06 +00:00
|
|
|
String httpPayload = http.getString(); //Get the response payload
|
|
|
|
Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code
|
|
|
|
Serial.println("HTTP BODY: " + String(httpPayload) + ""); //Print request response payload
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
DeserializationError error = deserializeJson(doc, httpPayload); //Get deserialization Error if exists
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
//configuration setup
|
|
|
|
int sleepTime = doc["device"]["sleepTime"];
|
2019-12-19 22:15:44 +00:00
|
|
|
String hostName = doc["device"]["hostname"];
|
|
|
|
String ipAddress = doc["device"]["ipAddress"];
|
|
|
|
String gateway = doc["device"]["gateway"];
|
|
|
|
String subnet = doc["device"]["subnet"];
|
|
|
|
String state = doc["state"];
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
if (state != "succes") {
|
|
|
|
unsuccessfulRounds++;
|
2019-12-19 22:15:44 +00:00
|
|
|
Serial.println("UNSUCCESSFUL ROUND NUMBER " + (String)unsuccessfulRounds + "FROM 5");
|
2019-11-26 12:56:06 +00:00
|
|
|
} else if (state == "succes") {
|
|
|
|
unsuccessfulRounds = 0;
|
|
|
|
}
|
2019-11-28 07:45:51 +00:00
|
|
|
|
|
|
|
//Set static ip
|
2019-12-19 22:15:44 +00:00
|
|
|
setStaticIp(ipAddress, gateway, subnet);
|
2019-11-27 18:48:41 +00:00
|
|
|
WiFi.hostname(hostName); //Set HostName
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-26 12:56:06 +00:00
|
|
|
http.end(); //Close connection
|
2019-11-27 18:48:41 +00:00
|
|
|
WiFi.disconnect(); //Disconect from WIFI
|
2019-11-26 12:56:06 +00:00
|
|
|
Serial.println("DISCONECTED FROM WIFI");
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
if(unsuccessfulRounds == 5) { //after 5 unsucessful request restart ESP
|
2019-11-26 12:56:06 +00:00
|
|
|
Serial.println("RESTARTING ESP");
|
2019-12-19 22:15:44 +00:00
|
|
|
ESP.restart();
|
2019-11-26 12:56:06 +00:00
|
|
|
}
|
2019-11-28 07:45:51 +00:00
|
|
|
|
2019-12-19 22:15:44 +00:00
|
|
|
sleep(sleepTime);
|
2019-11-04 14:51:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:48:41 +00:00
|
|
|
//checking if connection is working
|
2019-11-04 14:51:30 +00:00
|
|
|
bool checkConnection() {
|
2019-11-26 12:56:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
count++;
|
2019-11-04 14:51:30 +00:00
|
|
|
}
|
2019-11-26 12:56:06 +00:00
|
|
|
Serial.println("Timed out.");
|
|
|
|
return false;
|
2019-11-04 14:51:30 +00:00
|
|
|
}
|
2019-11-28 07:45:51 +00:00
|
|
|
void setStaticIp(String IpAddress, String subnet, String gateway){
|
|
|
|
//Set static ip
|
|
|
|
IPAddress staticIpAddress;
|
|
|
|
IPAddress subnetIpAddress;
|
|
|
|
IPAddress gatewayIpAddress;
|
|
|
|
|
|
|
|
if (
|
2019-12-19 22:15:44 +00:00
|
|
|
staticIpAddress.fromString(IpAddress) &&
|
2019-11-28 07:45:51 +00:00
|
|
|
subnetIpAddress.fromString(subnet) &&
|
2019-11-30 17:35:45 +00:00
|
|
|
gatewayIpAddress.fromString(gateway) &&
|
|
|
|
WiFi.localIP() != staticIpAddress
|
2019-11-28 07:45:51 +00:00
|
|
|
) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|