#include <WiFi.h>
#include <DHT.h>
#define DHTPIN 4 // Pin where DHT11 is connected
#define DHTTYPE DHT11 // DHT 11
#define LED_PIN 2 // Pin where LED is connected
#define RELAY_PIN 5 // Pin where Relay is connected
#define LDR_PIN 34 // Pin where LDR is connected
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Your_SSID"; // Wi-Fi SSID
const char* password = "Your_PASSWORD"; // Wi-Fi password
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(LDR_PIN, INPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read LDR (Light)
int lightLevel = analogRead(LDR_PIN);
// Print values to Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C\t");
Serial.print("Light: ");
Serial.println(lightLevel);
// Control LED based on Light level (example)
if (lightLevel < 1000) {
digitalWrite(LED_PIN, HIGH); // Turn on LED if light level is low
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED if light level is high
}
// Control Relay based on Temperature (example)
if (t > 30) {
digitalWrite(RELAY_PIN, HIGH); // Turn on Relay if temperature is high
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off Relay if temperature is normal
}
delay(2000); // Delay for 2 seconds
}