/**
based on
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
This example is about home automation. We have a room with a light and a fan.
2 LEDS depicting two outputs.
Light - Red LED
Fan - Green LED
Our DHT22 is there to take temperature and humidity reading of the room. Our user
may connect to thingspeak cloud and view the data directly.
Our user may send https commands to http or mqtt server directly to switch on/off
the light or fan.
Our user may set up any http command sending app for this automation.
*/
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const int DHT_PIN = 21;
const int Light_PIN = 32;
const int Fan_PIN = 33;
const int LED_PIN = 33;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2557311;
const char* myApiKey = "5X41H36VVQQFPJGA";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
ThingSpeak.setField(1,data.temperature);
ThingSpeak.setField(2,data.humidity);
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40) {
digitalWrite(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
}