#define BLYNK_TEMPLATE_ID "TMPL3eHmROYVB"
#define BLYNK_TEMPLATE_NAME "soil monitoring"
#define BLYNK_AUTH_TOKEN "d4fzAT_GSea7Tb7picudqyNr2ZLrXtLP"
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_MOISTURE_PIN 34
#define PH_PIN 35
#define BUZZER_PIN 18
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Use the Wokwi Guest network
char pass[] = "";
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C LCD(0x27, 16, 2);
// Function to map analog readings to pH values
float mapToPH(int sensorValue) {
// Map the analog sensor value (0-4095) to the pH range (1-14)
return 1 + (sensorValue / 4095.0) * 13.0;
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize DHT sensor
dht.begin();
// Initialize buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off initially
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
Blynk.begin(auth, ssid, pass);
configTime(0, 0, "pool.ntp.org");
}
void loop() {
// Ensure WiFi is connected
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconnecting to WiFi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(" Connected!");
}
// Read sensor data
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
int phSensorValue = analogRead(PH_PIN);
// Check if any readings failed and exit early (to try again in the next loop)
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Map the pH sensor analog reading to pH value
float pHValue = mapToPH(phSensorValue);
// Print the sensor data
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Soil Moisture: ");
Serial.print(soilMoistureValue);
Serial.print(", pH: ");
Serial.println(pHValue);
// Send data to Blynk
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V2, soilMoistureValue);
Blynk.virtualWrite(V3, pHValue);
// Display the sensor data on the LCD
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("T: ");
LCD.print(temperature);
LCD.print(" C");
LCD.setCursor(0, 1);
LCD.print("H: ");
LCD.print(humidity);
LCD.print(" %");
// Activate buzzer if conditions are met
if (temperature > 30.0 || temperature < 15.0 || humidity < 20.0 || humidity > 80.0 || pHValue < 5.5 || pHValue > 7.5) {
digitalWrite(BUZZER_PIN, HIGH); // Turn on buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
}
delay(2000);
}