#include <DHTesp.h>
#include <WiFi.h>
#include <ThingSpeak.h> // Library for ThingSpeak integration
#include <ESP32Servo.h> // Include ESP32Servo library (assuming ESP32 board)
#include <LiquidCrystal.h>
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi password
const int DHT_PIN = 12;
DHTesp dhtSensor;
const int servoPin = 2; // Pin connected to the servo motor
Servo servo;
// ThingSpeak settings
unsigned long channelID = 2476530; // Replace with your ThingSpeak channel ID
const char* writeAPIKey = "LRZGANM3C55FH260"; // Replace with your ThingSpeak write API key
WiFiClient client; // Declare the client object
float highTempThreshold = 30; // Temperature above which pump turns on (in deg C)
float lowTempThreshold = 25; // Temperature below which pump turns off (in deg C)
float lowMoistureThreshold = 30; // Moisture below which pump turns on (in %)
// Optional: Very high moisture threshold (if needed)
float veryHighMoistureThreshold = 80; // Moisture above which pump might need additional logic (adjust as needed)
const int pirPin = 27; // PIR sensor digital output pin
const int ledPin = 14; // LED pin
const int rs = 5;
const int en = 17;
const int d4 = 16;
const int d5 = 4;
const int d6 = 0;
const int d7 = 15;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int pirState = LOW;
int val;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
servo.attach(servoPin); // Attach the servo to the specified pin
lcd.begin(16, 2);
// Set PIR sensor pin as input
pinMode(pirPin, INPUT);
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi!");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperature = data.temperature;
float humidity = data.humidity;
val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
// Print data to serial monitor
Serial.println("Temp: " + String(temperature, 2) + "deg C");
Serial.println("Humidity: " + String(humidity, 1) + "%");
// Control servo based on temperature and moisture
if (temperature > highTempThreshold && humidity < lowMoistureThreshold) {
servo.write(180); // Set servo to position mimicking pump on (adjust angle as needed)
} else if (humidity > veryHighMoistureThreshold) {
// Optional logic for very high moisture (e.g., turn on additional pump or alarm)
Serial.println("Very high moisture detected!");
// Add your desired actions here (servo control, notifications, etc.)
} else {
servo.write(0); // Set servo to position mimicking pump off (adjust angle as needed)
}
// Display data and servo state on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(String(temperature, 1));
lcd.print("degC ");
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(String(humidity, 1));
lcd.print("% ");
lcd.print(servo.read() == 180 ? "Pump ON" : "Pump OFF");
// Send data to ThingSpeak (including moisture if possible)
ThingSpeak.setField(1, temperature); // Field 1 for temperature
ThingSpeak.setField(2, humidity); // Field 2 for humidity
// Add a field for moisture if ThingSpeak supports it (check ThingSpeak documentation)
// ThingSpeak.setField(3, moisture); // Example (assuming ThingSpeak supports field 3)
int response = ThingSpeak.writeFields(channelID, writeAPIKey);
if (response == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.println("Error sending data to ThingSpeak. HTTP error code: " + String(response));
}
Serial.println("---");
delay(2000);
}