#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
// OLED Display Settings
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// Wi-Fi Settings
const char* ssid = "Wokwi-GUEST"; // Wi-Fi SSID
const char* password = ""; // Wi-Fi password (empty for Wokwi)
// ThingSpeak Settings
const char* server = "http://api.thingspeak.com/update"; // ThingSpeak server
String apiKey = "6ELCHRTEBEDGUCQB"; // Your ThingSpeak API Key
// Pin Definitions
const int soilPin = 34; // Pin for potentiometer (simulates soil moisture sensor)
const int buzzerPin = 25; // Pin for buzzer
const int ledPin = 26; // Pin for LED
const int DHT_PIN = 15; // Pin for DHT22 data
// Variables for dynamic temperature and humidity
float simulatedTemperature = 25.0;
float simulatedHumidity = 50.0;
int soilValue = 0;
// Function to simulate dynamic temperature and humidity
void updateSimulatedValues() {
// Change temperature and humidity dynamically
simulatedTemperature += random(-10, 11) / 10.0; // Change by -1.0 to +1.0
simulatedHumidity += random(-10, 11) / 10.0; // Change by -1.0 to +1.0
// Keep values within realistic limits
if (simulatedTemperature < 15.0) simulatedTemperature = 15.0;
if (simulatedTemperature > 35.0) simulatedTemperature = 35.0;
if (simulatedHumidity < 20.0) simulatedHumidity = 20.0;
if (simulatedHumidity > 80.0) simulatedHumidity = 80.0;
}
void setup() {
Serial.begin(115200);
// Pin Modes
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
// Initialize OLED Display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED display failed to initialize!");
while (1);
}
display.clearDisplay();
// Wi-Fi Connection
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected!");
}
void sendDataToThingSpeak(int soilValue, float temperature, float humidity) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Create the URL string
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(soilValue) +
"&field2=" + String(temperature) +
"&field3=" + String(humidity);
// Send HTTP GET request
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak! Response code: " + String(httpResponseCode));
} else {
Serial.println("Error sending data to ThingSpeak. Response code: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Wi-Fi not connected. Data not sent.");
}
}
void loop() {
// Update simulated temperature and humidity
updateSimulatedValues();
// Read soil moisture
soilValue = analogRead(soilPin);
// Display data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Soil Moisture: " + String(soilValue));
display.println("Temperature: " + String(simulatedTemperature) + " C");
display.println("Humidity: " + String(simulatedHumidity) + " %");
display.display();
// Send data to ThingSpeak
sendDataToThingSpeak(soilValue, simulatedTemperature, simulatedHumidity);
// LED and Buzzer Control
if (soilValue < 1500) { // Critical threshold value
digitalWrite(ledPin, HIGH); // Turn ON LED
digitalWrite(buzzerPin, HIGH); // Activate Buzzer
Serial.println("Soil is dry! Irrigation started.");
} else {
digitalWrite(ledPin, LOW); // Turn OFF LED
digitalWrite(buzzerPin, LOW); // Deactivate Buzzer
Serial.println("Soil is moist. No irrigation needed.");
}
delay(20000); // Wait for 20 seconds before next iteration
}