// Define Blynk details
#define BLYNK_TEMPLATE_ID "TMPL6I5pKHSyT"
#define BLYNK_TEMPLATE_NAME "Smart Plant Care And Watering System"
#define BLYNK_AUTH_TOKEN "h3RgVnW-JYt7ijtboJaUiNR-a7y2-HxO"
// Include libraries
#include <WiFi.h>
#include <BlynkSimpleEsp32.h> // Blynk library
#include <HTTPClient.h>
#include "ThingSpeak.h"
// Define pins
#define SOIL_MOISTURE_PIN 34 // Soil moisture sensor pin
#define BUZZER_PIN 5 // Buzzer pin
#define RELAY_PIN 12 // Relay pin for pump
// WiFi credentials
const char* SSID = "Wokwi-GUEST"; // WiFi SSID
const char* PASS = ""; // WiFi password (empty for Wokwi guest network)
// ThingSpeak details
const int MyChannelID = 2827198;
const char* API_KEY = "PSS20CYIZPIDALRC";
WiFiClient client;
bool systemOn = false;
int buzzerVolume = 255; // Buzzer volume (0-255)
void setup() {
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure pump is off initially
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi.");
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Connect to Blynk
Blynk.config(BLYNK_AUTH_TOKEN);
Blynk.connect();
}
void loop() {
Blynk.run(); // Handle Blynk communication
if (!systemOn) {
digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
Blynk.virtualWrite(V1, 0); // Update pump button (OFF)
Blynk.virtualWrite(V2, 0); // Update buzzer button (OFF)
Serial.println("System OFF: Pump and Buzzer are OFF");
return;
}
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
int moisturePercentage = map(soilMoisture, 0, 4095, 0, 100); // Convert to percentage
Serial.print("Soil Moisture Level: ");
Serial.println(moisturePercentage);
// Send soil moisture data to Blynk (using virtual pin V0)
Blynk.virtualWrite(V0, moisturePercentage);
// Check soil moisture level and control buzzer/pump
if (moisturePercentage < 30) { // If soil is dry (threshold can be adjusted)
tone(BUZZER_PIN, 262, buzzerVolume);
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Watering the plant...");
Blynk.virtualWrite(V1, 1); // Update pump button (ON)
Blynk.virtualWrite(V2, 1); // Update buzzer button (ON)
Blynk.logEvent("low_moisture", "Soil is too dry! Watering the plant...");
}
else if (moisturePercentage > 70) { // If soil moisture is too high (overwatered)
tone(BUZZER_PIN, 523, buzzerVolume);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Warning: Overwatering! Soil moisture is too high.");
Blynk.virtualWrite(V1, 0); // Update pump button (OFF)
Blynk.virtualWrite(V2, 0); // Update buzzer button (OFF)
Blynk.logEvent("high_moisture", "Soil is overwatered! Pump stopped.");
}
else {
noTone(BUZZER_PIN);
digitalWrite(RELAY_PIN, LOW);
Blynk.virtualWrite(V1, 0); // Update pump button (OFF)
Blynk.virtualWrite(V2, 0); // Update buzzer button (OFF)
Serial.println("Soil moisture is ideal. No action needed.");
}
// Send soil moisture data to ThingSpeak
ThingSpeak.setField(1, moisturePercentage);
int statusCode = ThingSpeak.writeFields(MyChannelID, API_KEY);
if (statusCode == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Failed to send data to ThingSpeak. Error code: ");
Serial.println(statusCode);
}
delay(2000);
}
// Blynk virtual pin handlers
BLYNK_WRITE(V3) { // System On/Off button
systemOn = param.asInt(); // Get the value of the system on/off switch (1 = ON, 0 = OFF)
if (systemOn) {
Serial.println("System ON");
} else {
Serial.println("System OFF");
}
}
BLYNK_WRITE(V4) { // Buzzer Volume Control (Slider)
buzzerVolume = param.asInt(); // Get the buzzer volume from the slider (0-255)
Serial.print("Buzzer volume set to: ");
Serial.println(buzzerVolume);
}