#define BLYNK_TEMPLATE_ID "TMPL3kJCwuEoB"
#define BLYNK_TEMPLATE_NAME "smart soil nutrition monitoring system"
#define BLYNK_AUTH_TOKEN "NhdFWHg8xudMydNPjYzf6s5zMkDkk4I5"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Replace with your Blynk auth token
char auth[] = "NhdFWHg8xudMydNPjYzf6s5zMkDkk4I5";
// Replace with your Wi-Fi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT Sensor settings
#define DHTPIN 4 // Pin where the DHT sensor is connected
#define DHTTYPE DHT22 // DHT 22 (you can also use DHT11)
DHT dht(DHTPIN, DHTTYPE);
// Soil moisture sensor pin (simulated by potentiometer)
int soilMoisturePin = 34;
// Variables to store sensor values
double temperature;
double humidity;
int soilMoistureValue;
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
pinMode(soilMoisturePin, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(2000);
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
Blynk.run();
// Reading temperature and humidity
temperature = (double)dht.readTemperature();
humidity = (double)dht.readHumidity();
// Reading soil moisture (simulated by potentiometer)
int sensorValue = analogRead(soilMoisturePin);
Serial.print("Raw Soil Moisture Value: ");
Serial.println(sensorValue);
// Map raw sensor value to percentage (0-100%)
soilMoistureValue = map(sensorValue, 0, 4095, 0, 100);
// Ensure the mapping is correct, adjust if necessary
soilMoistureValue = constrain(soilMoistureValue, 0, 100); // Ensure value is within 0-100%
// Sending sensor data to Blynk
Blynk.virtualWrite(V1, temperature);
Blynk.virtualWrite(V2, humidity);
Blynk.virtualWrite(V3, soilMoistureValue);
// Printing values to Serial Monitor for debugging
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Soil Moisture: ");
Serial.print(soilMoistureValue);
Serial.println(" %");
// Display sensor data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Temperature:");
display.print(temperature);
display.println(" C");
display.println("Humidity:");
display.print(humidity);
display.println(" %");
display.println("Soil Moisture:");
display.print(soilMoistureValue);
display.println(" %");
display.display();
delay(2000); // Wait for 2 seconds before the next loop
}