#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* apiEndpoint = "https://api.mocki.io/v1/your-endpoint"; // Mock API
const int potentiometerPin = 34; // Potentiometer pin
const int redPin = 25; // Red LED pin
const int greenPin = 26; // Green LED pin
const int bluePin = 27; // Blue LED pin
int moistureValue = 0;
bool rainForecasted = false;
// Setup
void setup() {
Serial.begin(115200);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
// Fetch mock weather API
bool getRainForecast() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiEndpoint);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
Serial.println("API Response: " + payload);
return payload.indexOf("rain:true") >= 0;
}
http.end();
}
return false;
}
void setLEDColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop() {
moistureValue = analogRead(potentiometerPin); // Read potentiometer value
rainForecasted = getRainForecast();
Serial.print("Moisture Level: ");
Serial.println(moistureValue);
Serial.print("Rain Forecasted: ");
Serial.println(rainForecasted ? "YES" : "NO");
// RGB LED Logic
if (rainForecasted) {
setLEDColor(0, 0, 255); // Blue: Rain forecasted, no irrigation
Serial.println("Pump: OFF (Rain forecasted)");
} else if (moistureValue < 800) {
setLEDColor(255, 0, 0); // Red: Very dry, full power irrigation
Serial.println("Pump: ON (Full power)");
} else if (moistureValue < 1200) {
setLEDColor(255, 255, 0); // Yellow: Borderline dry, moderate output
Serial.println("Pump: ON (Moderate power)");
} else {
setLEDColor(0, 255, 0); // Green: Moist, no irrigation needed
Serial.println("Pump: OFF (Soil is moist)");
}
delay(5000);
}