// channel id :2440972
//channel api key :TAYG4N9B393ONJM5
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
// Pin configuration
const int DHT_PIN = 15; // Pin connected to the DHT sensor
const int LED_PIN = 13; // Pin connected to the LED
// WiFi credentials
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// ThingSpeak channel details
const int myChannelNumber =2440972;
const char* myApiKey = "TAYG4N9B393ONJM5";
const char* server = "api.thingspeak.com";
// DHT sensor and WiFi client
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Set up LED pin
pinMode(LED_PIN, OUTPUT);
// Connect to WiFi
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()));
// Set WiFi mode to STA (station) mode
WiFi.mode(WIFI_STA);
// Initialize ThingSpeak client
ThingSpeak.begin(client);
}
void loop() {
// Read temperature and humidity from DHT sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Update ThingSpeak fields
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
// Check temperature and humidity conditions
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40) {
digitalWrite(LED_PIN, HIGH); // Turn on LED if conditions are met
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED if conditions are not met
}
// Send data to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print temperature and humidity to serial monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
// Check if data was successfully pushed to ThingSpeak
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
// Delay for 10 seconds before the next iteration
delay(10000);
}