#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#define DHTPIN 4 // GPIO pin for DHT22
#define DHTTYPE DHT22
#define LED_PIN 13 // GPIO pin for LED
#define BLUE_LED_PIN 12 // GPIO pin for blue LED
#define SERVO_PIN 14 // GPIO pin for Servo
#define SWITCH_PIN 2 // GPIO pin for switch
const char *ssid = "Wokwi-GUEST"; // Change to your WiFi SSID
const char *password = ""; // Change to your WiFi password
const char *thingSpeakApiKey = "0XI2EECT69IIDIGJ"; // Change to your ThingSpeak API Key
const long channelId = 2391788; // Change to your ThingSpeak channel ID
const char* server = "api.thingspeak.com";
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C LCD(0x27, 16, 2); // Adjust the LCD I2C address if needed
Servo servo; // Use ESP32Servo instead of Servo
float humidity, temperature; // Declare variables outside the if condition
void setup() {
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
WiFiClient client; // Declare WiFiClient object
ThingSpeak.begin(client); // Initialize ThingSpeak with WiFiClient object
LCD.init();
delay(1000);
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("ESP32 Project");
delay(2000);
LCD.clear();
dht.begin();
pinMode(LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
servo.attach(SERVO_PIN);
}
void loop() {
Serial.println("Entering loop");
if (digitalRead(SWITCH_PIN) == LOW) {
Serial.println("Switch is LOW");
delay(2000);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Error: Cannot read sensor data!");
Serial.println("Error: Cannot read sensor data!");
return;
}
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Temp: ");
LCD.print(temperature, 1);
LCD.print("C");
LCD.setCursor(0, 1);
LCD.print("Humidity: ");
LCD.print(humidity, 1);
LCD.print("%");
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print("C Humidity: ");
Serial.print(humidity);
Serial.println("%");
if ((humidity < 55 || humidity > 60) || (temperature < 37.8 || temperature > 41)) {
digitalWrite(LED_PIN, HIGH);
if (humidity < 55 || temperature < 37.8) {
digitalWrite(BLUE_LED_PIN, HIGH);
} else {
digitalWrite(BLUE_LED_PIN, LOW);
}
// Commented out the buzzer-related code
// digitalWrite(BUZZER_PIN, HIGH);
if (humidity > 60 || temperature > 41) {
servo.write(180);
} else {
servo.write(90);
}
// Send data to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
int writeResult = ThingSpeak.writeFields(channelId, thingSpeakApiKey);
Serial.print("ThingSpeak write result: ");
Serial.println(writeResult);
if (writeResult != 200) {
Serial.println("Error writing to ThingSpeak");
}
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
// Commented out the buzzer-related code
// digitalWrite(BUZZER_PIN, LOW);
servo.write(90);
}
} else {
Serial.println("Switch is HIGH");
digitalWrite(LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
// Commented out the buzzer-related code
// digitalWrite(BUZZER_PIN, LOW);
servo.write(90);
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Switch is HIGH");
LCD.setCursor(0, 1);
LCD.print("Press the switch");
Serial.println("");
}
}