#include <WiFi.h>
#include "ThingSpeak.h"
#include <DHT.h>
#include <LiquidCrystal.h>
// --- Network Settings ---
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
WiFiClient client;
// --- ThingSpeak Settings ---
unsigned long myChannelNumber = 3238516; // Put your Channel ID here
const char * myWriteAPIKey = "EM5TJ1UYA9NQVALU";
// Put your Write API Key here
// --- Hardware Pins ---
#define DHTPIN 21
#define DHTTYPE DHT22
#define RELAY_PIN 26
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
lcd.begin(16, 2);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected!");
ThingSpeak.begin(client);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
int relayStatus = (t > 30.0) ? 1 : 0;
digitalWrite(RELAY_PIN, relayStatus);
// Update LCD
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(t, 1);
lcd.print(" H:"); lcd.print(h, 0);
// Send to ThingSpeak
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, relayStatus);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // ThingSpeak free tier requires 15+ seconds between updates
}