#include <WiFi.h>
#include <ThingSpeak.h>
#include <DHTesp.h>
WiFiClient client;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
unsigned long channelID = 2716175;
const char* writeAPIKey = "67DGUJBT27SKQC2T";
DHTesp banu;
TempAndHumidity data;
// Sensor Pins
#define LDR_PIN 34 // LDR sensor pin for light control
#define PIR_PIN 25 // PIR sensor for person detection
#define TRIG_PIN 5 // Ultrasonic Trigger pin for distance measurements
#define ECHO_PIN 18 // Ultrasonic Echo pin for distance measurements
#define LED_PIN 2 // Pin controlling the LED
#define DHT_PIN 4 // DHT sensor pin for temperature and humidity
#define IR_PIN 26 // IR sensor for person detection (AC control)
#define AC_PIN 27 // Pin controlling the AC relay
#define SOIL_MOISTURE_PIN 32 // Analog pin for soil moisture sensor
#define BIN_LEVEL_PIN 35 // Analog pin for bin level sensor
#define MOISTURE_PIN 36 // Analog pin for bin moisture sensor
#define LID_PIN 14 // Pin to control the bin lid (servo or motor)
#define MANUAL_BUTTON_PIN 19 // Pin for manual button to open the lid
#define PUMP_PIN 13 // Pin controlling the water pump relay
// Conditions
int lightThreshold = 600;
float distanceThreshold = 100.0;
float tempThreshold = 26.0; // Temperature above which AC turns on
float humidityThreshold = 60.0; // Humidity above which AC turns on
int moistureThreshold = 400; // Soil moisture threshold
int binLevelThreshold = 700; // Bin level threshold
float tankEmptyLevel = 10.0; // Water tank empty level in cm
// Variables to store sensor readings
String lightStatus = "OFF";
String acStatus = "OFF";
String buttonStatus = "RELEASED";
String motorStatus = "OFF";
int statusCode;
void setup() {
ThingSpeak.begin(client);
Serial.begin(115200);
banu.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LDR_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(IR_PIN, INPUT);
pinMode(AC_PIN, OUTPUT);
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(BIN_LEVEL_PIN, INPUT);
pinMode(MOISTURE_PIN, INPUT);
pinMode(LID_PIN, OUTPUT);
pinMode(MANUAL_BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(PUMP_PIN, OUTPUT);
}
void loop() {
// WiFi connection check
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Read DHT sensor for temperature and humidity
data = banu.getTempAndHumidity();
float temperature = data.temperature;
float humidity = data.humidity;
// Light Control Logic
int ldrValue = analogRead(LDR_PIN);
bool personDetected = digitalRead(PIR_PIN);
float distance = getDistance();
if (ldrValue < lightThreshold && personDetected && distance < distanceThreshold) {
digitalWrite(LED_PIN, HIGH);
lightStatus = "ON";
} else {
digitalWrite(LED_PIN, LOW);
lightStatus = "OFF";
}
// AC Control Logic
bool acPersonDetected = digitalRead(IR_PIN);
if (acPersonDetected && distance < distanceThreshold &&
(temperature > tempThreshold || humidity > humidityThreshold)) {
digitalWrite(AC_PIN, HIGH);
acStatus = "ON";
} else {
digitalWrite(AC_PIN, LOW);
acStatus = "OFF";
}
// Smart Wave Spin Logic
int binMoistureValue = analogRead(MOISTURE_PIN);
int binLevelValue = analogRead(BIN_LEVEL_PIN);
bool manualPress = !digitalRead(MANUAL_BUTTON_PIN); // Button pressed if LOW
if (personDetected && distance < distanceThreshold) {
if (binMoistureValue < moistureThreshold) {
if (binLevelValue < binLevelThreshold || manualPress) {
openLid();
motorStatus = "ON";
buttonStatus = manualPress ? "PRESSED" : "RELEASED";
} else {
closeLid();
motorStatus = "OFF";
}
} else {
closeLid();
motorStatus = "OFF";
}
} else if (manualPress) {
openLid();
motorStatus = "ON";
buttonStatus = "PRESSED";
} else {
closeLid();
motorStatus = "OFF";
}
// Smart Plant Monitoring Logic
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
float tankLevel = getTankLevel();
if (tankLevel > tankEmptyLevel) { // Check if the tank has water
if (soilMoistureValue > moistureThreshold) { // Check if the soil is dry
waterPlants();
motorStatus = "ON";
} else {
stopWatering();
motorStatus = "OFF";
}
} else {
stopWatering();
motorStatus = "OFF";
}
// Update ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
ThingSpeak.setField(1, lightStatus);
ThingSpeak.setField(2, String(temperature));
ThingSpeak.setField(3, String(humidity));
ThingSpeak.setField(4, String(binLevelValue));
ThingSpeak.setField(5, String(binMoistureValue));
ThingSpeak.setField(6, buttonStatus);
ThingSpeak.setField(7, String(soilMoistureValue));
ThingSpeak.setField(8, motorStatus);
statusCode = ThingSpeak.writeFields(channelID, writeAPIKey);
if (statusCode == 200) { //successful writing code
Serial.println("Channel update successful.");
} else {
Serial.println("Problem Writing data. HTTP error code :" + String(statusCode));
}
}
delay(1000);
}
// Function to get distance using ultrasonic sensor
float getDistance() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return (pulseIn(ECHO_PIN, HIGH) * 0.0343) / 2;
}
// Function to get tank water level using ultrasonic sensor
float getTankLevel() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return (pulseIn(ECHO_PIN, HIGH) * 0.0343) / 2;
}
// Functions to control the bin lid
void openLid() {
digitalWrite(LID_PIN, HIGH); // Adjust based on your actuator/servo setup
}
void closeLid() {
digitalWrite(LID_PIN, LOW);
}
// Functions to control the water pump
void waterPlants() {
digitalWrite(PUMP_PIN, HIGH); // Adjust based on your pump setup
}
void stopWatering() {
digitalWrite(PUMP_PIN, LOW);
}