#include <WiFi.h>
#include <ThingSpeak.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSPEAK_API_KEY "QVQH7FTOG7G8LQGD"
unsigned long THINGSPEAK_CHANNEL_ID = 2278259;
#define LDRPIN 35
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LDRPIN, INPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
}
void loop() {
float Humidity = dht.readHumidity();
float Temperature = dht.readTemperature();
int Light = analogRead(LDRPIN);
if (isnan(Humidity) || isnan(Temperature)) {
Serial.println("Failed to read from DHT sensor!");
delay(1000);
return;
}
ThingSpeak.begin(client);
ThingSpeak.setField(1,Light);
ThingSpeak.setField(2,Temperature);
ThingSpeak.setField(3,Humidity);
Serial.print("Light up:"); Serial.println(Light);
Serial.print("Temp up"); Serial.println(Temperature);
Serial.print("Hum up"); Serial.println(Humidity);
int x = ThingSpeak.writeFields(THINGSPEAK_CHANNEL_ID,THINGSPEAK_API_KEY);
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(3000);
}