#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /** * Example code for running a ESP8266 thermostat using an IR LED * and a DHT22 sensor. Accepts commands via HTTP and uses JSON * to transmit values between the device and the server. * * Be sure that the selected board is the ESP8266 NodeMCU v1.0. * Otherwise, the core libraries will not be loaded in and the * compilation will fail. * * Required libraries * IRremoteESP8266 @ v2.6.4 * DHT sensor library @v1.3.0 * ArduinoJson @ v6.11.5 */ #include "DHT.h" #define DHTPIN 4 // what digital pin the DHT22 is conected to #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors DHT dht(DHTPIN, DHTTYPE); const char *ssid = "Banana"; // replace with your wifi ssid and wpa2 key const char *pass = "fruit4336"; float h = 0; float t = 0; float f = 0; float tempSet = 19.0; int period = 2000; unsigned long time_now = 0; #define IR_LED D1 // ESP8266 GPIO pin to use. Recommended: 4 (D2). IRTcl112Ac toshibair(IR_LED); // Replace IRTc1112Ac with what ever class is used for your device ESP8266WebServer server ( 80 ); //the port to run the HTTP server IPAddress ip(192, 168, 3, 3); //the IP address of the device IPAddress gateway(192, 168, 1, 1); //the gateway IPAddress subnet(255, 255, 0, 0); //the subnet boolean state = false; int tcMode = 0; //0 = cool, 1 = dh void setup() { Serial.begin(9600); Serial.setTimeout(2000); Serial.println("Connecting to "); Serial.println(ssid); WiFi.config(ip, gateway, subnet); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("Device Started"); Serial.println("-------------------------------------"); Serial.println("Running DHT!"); Serial.println("-------------------------------------"); ArduinoOTA.setHostname("Thermomitor"); ArduinoOTA.begin(); // Start the server server.on("/set/temp", handleBody); server.on("/set/off", handleOff); server.on("/set/on", handleOn); server.on("/value", handleValue); server.on("/state", HTTPMethod::HTTP_POST, handleSetState); server.on("/state", HTTPMethod::HTTP_GET, handleGetState); server.on("/update",handleUpdateState); server.onNotFound(handleNotFound); server.begin(); toshibair.begin(); } //every loop we fetch sensor values if it has been long enough //and every loop we will run the HTTP server and the OTA server void loop() { // Report every 2 seconds. if(millis() > time_now + period) { // Readings the values can take up to 250ms // Sensor readings could also be 2 seconds old, as it only reports every 2 seconds h = dht.readHumidity(); // Read temperature as Celsius (the default) t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } time_now = millis(); } //run the HTTP server server.handleClient(); //allow the ESP to do its thing yield(); //run the OTA (Over the Air programming) server ArduinoOTA.handle(); } //the /set/on endpoint void handleOn() { toshibair.on(); toshibair.setTemp(tempSet); toshibair.send(); state = true; server.send(200, "text/html", "ON" ); } //the /set/off endpoint void handleOff() { toshibair.off(); toshibair.send(); state = false; server.send(200, "text/html", "OFF" ); } //the /state GET endpoint void handleGetState() { StaticJsonDocument<200> root; root["state"] = state; if(tcMode == 0) { root["mode"] = "cool"; } else if(tcMode == 1) { root["mode"] = "dh"; } String output; serializeJson(root, output); server.send(200, "text/html", output ); } //the /value GET endpoint void handleValue() { StaticJsonDocument<200> root; root["humidity"] = h; root["temperature"] = t; String output; serializeJson(root, output); server.send(200, "text/html", output ); } //the /update endpoint void handleUpdateState() { StaticJsonDocument<200> root; root["state"] = state; String output; serializeJson(root, output); if(state) { toshibair.on(); toshibair.setTemp(tempSet); toshibair.send(); } else { toshibair.off(); toshibair.send(); } server.send(200, "text/html", output ); } //the /state POST endpoint void handleSetState() { if (server.hasArg("plain")== false){ //Check if body received server.send(200, "text/plain", "Body not received"); return; } String message = "Body received:\n"; message += server.arg("plain"); message += "\n"; String payload = server.arg("plain"); const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60; DynamicJsonDocument root(capacity); // Parse JSON object auto error = deserializeJson(root, payload); if (error) { Serial.print(F("deserializeJson() failed with code ")); Serial.println(error.c_str()); return; } state = root["state"]; String tmp = root["mode"]; if(tmp == "cool") { tcMode = 0; if(state) { toshibair.on(); toshibair.setMode(kTcl112AcCool); toshibair.setTemp(tempSet); toshibair.send(); } else { toshibair.off(); toshibair.send(); } } else if(tmp == "dh"){ tcMode = 1; if(state) { toshibair.on(); toshibair.setMode(kTcl112AcDry); toshibair.send(); } else { toshibair.off(); toshibair.send(); } } server.send(200, "text/plain", message); Serial.println(message); } //the not found endpoint void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for ( uint8_t i = 0; i < server.args(); i++ ) { message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; } server.send ( 404, "text/plain", message ); } //the /set/temp endpoint void handleBody() { //Handler for the body path if (server.hasArg("plain")== false){ //Check if body received server.send(200, "text/plain", "Body not received"); return; } String message = "Body received:\n"; message += server.arg("plain"); message += "\n"; String payload = server.arg("plain"); const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60; DynamicJsonDocument root(capacity); // Parse JSON object auto error = deserializeJson(root, payload); if (error) { Serial.print(F("deserializeJson() failed with code ")); Serial.println(error.c_str()); return; } tempSet = root["temperature"]; server.send(200, "text/plain", message); Serial.println(message); }