#define BLYNK_TEMPLATE_ID "TMPL54AvTM1jD"
#define BLYNK_TEMPLATE_NAME "SMART IRRIGATION SYSTEM VIRTUAL SIMULATION"
#define BLYNK_AUTH_TOKEN "scYy9loB8-4jf-nKTKj8j4Y8mQTfziIb"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ThingSpeak.h>
// Pin Definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_MOISTURE_PIN 34
#define WATER_LEVEL_PIN 35
#define RELAY_PIN 5
#define DHT_LED_PIN 18
#define PUMP_LED_PIN 19
// OLED display width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define the display address for I2C
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// WiFi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak Settings
const int myChannelNumber = 2561337;
const char* apiKey = "PDSQXYC7E0QF2BVU";
const char* server = "api.thingspeak.com";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(115200);
dht.begin();
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Initialize relay to off state
pinMode(DHT_LED_PIN, OUTPUT);
digitalWrite(DHT_LED_PIN, LOW); // Turn off DHT LED initially
pinMode(PUMP_LED_PIN, OUTPUT);
digitalWrite(PUMP_LED_PIN, LOW); // Initialize the LED to off state
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
// Blynk setup
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
}
void loop() {
Blynk.run();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
int waterLevelValue = analogRead(WATER_LEVEL_PIN);
// Convert analog readings to percentage
int soilMoisturePercentage = map(soilMoistureValue, 0, 4095, 0, 100);
int waterLevelPercentage = map(waterLevelValue, 0, 4095, 0, 100);
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Update ThingSpeak fields
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, soilMoisturePercentage);
ThingSpeak.setField(4, waterLevelPercentage);
// Control DHT LED based on temperature and humidity thresholds
if (temperature > 35 || temperature < 12 || humidity > 70 || humidity < 40) {
digitalWrite(DHT_LED_PIN, HIGH);
Serial.println("Temperature or Humidity out of range!");
} else {
digitalWrite(DHT_LED_PIN, LOW);
}
// Define a threshold value for soil moisture
const int SOIL_MOISTURE_THRESHOLD = 40;
// Control relay based on soil moisture threshold
if (soilMoisturePercentage < SOIL_MOISTURE_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH); // Turn on relay (water pump)
digitalWrite(PUMP_LED_PIN, HIGH); // Turn on the LED
Blynk.virtualWrite(V5, 1); // Update Blynk virtual pin for Pump LED
Serial.println("Soil moisture is low. Pump is turned on.");
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off relay (water pump)
digitalWrite(PUMP_LED_PIN, LOW); // Turn off the LED
Blynk.virtualWrite(V5, 0); // Update Blynk virtual pin for Pump LED
}
// Display data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Soil Moisture: ");
display.print(soilMoisturePercentage);
display.println("%");
display.print("Water Level: ");
display.print(waterLevelPercentage);
display.println("%");
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.display();
// Output data to Serial monitor
Serial.println("Temp: " + String(temperature, 2) + "°C");
Serial.println("Humidity: " + String(humidity, 1) + "%");
Serial.println("Soil Moisture: " + String(soilMoisturePercentage) + "%");
Serial.println("Water Level: " + String(waterLevelPercentage) + "%");
// Write to ThingSpeak channel
int response = ThingSpeak.writeFields(myChannelNumber, apiKey);
if (response == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(response));
}
Serial.println("---");
// Send data to Blynk app
Blynk.virtualWrite(V1, temperature);
Blynk.virtualWrite(V2, humidity);
Blynk.virtualWrite(V3, soilMoisturePercentage);
Blynk.virtualWrite(V4, waterLevelPercentage);
// Wait before next loop
delay(15000);
}