#include <WiFi.h>
#include <ThingSpeak.h> // Library for ThingSpeak integration
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi password
// ThingSpeak settings
unsigned long channelID = 2715922; // Replace with your ThingSpeak channel ID
const char* writeAPIKey = "99TP6NOLCKYAPFES"; // Replace with your ThingSpeak write API key
WiFiClient client; // Declare the client object
#include "DHTesp.h"
const int DHT_PIN = 15;
const int buz_PIN = 12;
DHTesp dhtSensor;
// #include <Wire.h>
// #include <LiquidCrystal_I2C.h>
// LiquidCrystal_I2C lcd(0x27,16,2);
#include <LiquidCrystal.h>
const int rs = 23;
const int en = 22;
const int d4 = 21;
const int d5 = 19;
const int d6 = 18;
const int d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define relay 2
const int pirPin = 13; // PIR sensor digital output pin
void setup()
{
// Set PIR sensor pin as input
pinMode(pirPin, INPUT);
pinMode(relay, OUTPUT);
pinMode(buz_PIN, OUTPUT);
//Wire.begin(23, 22);// lcd pins
Serial.begin(115200);
lcd.begin(16, 2);
// lcd.init();//
// lcd.backlight();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// 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 DHT_sensor()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
ThingSpeak.setField(1, data.temperature); // Field 1 for temperature
ThingSpeak.setField(2, data.humidity); // Field 2 for humidity
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));
}
lcd.setCursor ( 0, 0 );
lcd.print ( "Temp: " );
lcd.print ( String(data.temperature, 2)+ "°C");
lcd.setCursor ( 0, 1 );
lcd.print ( "Humidity:" );
lcd.print ( String(data.humidity, 1) + "%");
if(data.temperature>30)
{
digitalWrite(relay, HIGH);
digitalWrite(buz_PIN, HIGH);
}
else
{
digitalWrite(relay, LOW);
digitalWrite(buz_PIN, LOW);
}
delay(1000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}
void loop()
{
int16_t i = analogRead(34);
ThingSpeak.setField(3, i); // Field 3 for soil irrigation
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
delay(500);
lcd.clear();
DHT_sensor();
motionSensor();
}
int pirState = LOW;
void motionSensor()
{
int val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(relay, HIGH); // turn LED ON
digitalWrite(buz_PIN, HIGH);
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(relay, LOW); // turn LED OFF
digitalWrite(buz_PIN, LOW);
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;
}
}
}