#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// ====== WiFi Configuration ======
const char* ssid = "Wokwi-GUEST"; // Simulated WiFi SSID
const char* password = ""; // Simulated WiFi password
const char* auth_token = "BHvR9FAxZW-JJnJDpnai5bbBod5qesUm"; // Your Blynk authentication token
// ====== Weather API Configuration ======
const char* apiKey = "72921b57d598a73428417b4ddb8904e9"; // OpenWeather API Key
const char* city = "Surakarta"; // City
const char* country = "ID"; // Country code (Indonesia)
// ====== Pin Configuration ======
#define SOIL_SENSOR_PIN 34 // Soil moisture sensor pin
#define RELAY_PIN 26 // Relay pin for water pump
// ====== Global Variables ======
int soilMoisture = 0; // Variable to store soil moisture value
bool isRaining = false; // Weather status from API
bool manualPump = false; // Manual control status for the pump
// ====== Function to send data to Blynk (POST) ======
void sendToBlynk(String virtualPin, String value) {
HTTPClient http;
// Create URL for Blynk API
String url = "https://blynk.cloud/external/api/update?token=" + String(auth_token) + "&V" + String(virtualPin) + "=" + String(value);
// Begin HTTP request
http.begin(url);
// Set headers to match curl
http.addHeader("User-Agent", "curl/7.64.1");
http.addHeader("Accept", "application/json"); // Expect JSON response
http.addHeader("Connection", "keep-alive"); // Maintain connection
// Send GET request
int httpResponseCode = http.GET();
// Check response code s
if (httpResponseCode == 200) {
Serial.println("Data sent to Blynk successfully");
} else {
Serial.println("Failed to send data to Blynk");
}
http.end(); // Close the connection
}
// ====== Function to read soil moisture sensor ======
void readSoilSensor() {
soilMoisture = analogRead(SOIL_SENSOR_PIN);
Serial.print("Soil Moisture: ");
Serial.println(soilMoisture);
sendToBlynk("V8", String(soilMoisture)); // Send data to Virtual Pin V4
}
// ====== Function to get weather data from API ======
void getWeatherData() {
HTTPClient http;
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "," + String(country) + "&appid=" + String(apiKey) + "&units=metric";
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Weather Data: " + payload);
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
// Extract rain volume
float rainVolume = doc["rain"]["1h"].as<float>(); // Directly cast rain volume as float
isRaining = (rainVolume > 0); // If rain volume is greater than 0, it's raining
Serial.print("Rain Volume: ");
Serial.println(rainVolume);
Serial.print("Is it raining? ");
Serial.println(isRaining ? "Yes" : "No");
sendToBlynk("V5", String(rainVolume)); // Send rain data to Virtual Pin V5
} else {
Serial.println("Failed to get weather data!");
}
http.end();
}
// ====== Function to check watering logic ======
void checkWatering() {
if (manualPump) {
digitalWrite(RELAY_PIN, HIGH);
} else {
if (isRaining) {
digitalWrite(RELAY_PIN, LOW);
Serial.println("Rain detected, pump off.");
} else if (soilMoisture > 2500) {
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Soil is dry, pump on.");
} else {
digitalWrite(RELAY_PIN, LOW);
Serial.println("Soil is moist, pump off.");
}
}
sendToBlynk("V6", String(digitalRead(RELAY_PIN))); // Send pump status to Virtual Pin V6
}
// ====== Function to check Blynk Online Status ======
void checkBlynkStatus() {
HTTPClient http;
//String url = "http://sgp1.blynk.cloud/external/api/isHardwareConnected?token=" + String(auth_token);
String url = "https://blynk.cloud/external/api/get?token="+ String(auth_token);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("✅ Blynk is ONLINE");
} else {
Serial.println("❌ Blynk is OFFLINE");
}
http.end();
}
// ====== Setup ======
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
pinMode(SOIL_SENSOR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ Connected to WiFi!");
}
// ====== Loop ======
void loop() {
checkBlynkStatus(); // Check if Blynk is online
readSoilSensor(); // Read soil moisture
getWeatherData(); // Get weather data from API
checkWatering(); // Check if watering is needed
delay(30000); // Update every 30 seconds
}