#include <WiFi.h>
#include <HTTPClient.h>
// Pin assignments
const int moistureSensorPin = 34; // Analog pin for soil moisture sensor
const int greenLEDPin = 25; // Digital pin for the green LED indicating watering
const int redLEDPin = 26; // Digital pin for the red LED indicating no watering
const int waterPumpPin = 2; // Digital pin for the relay controlling the water pump
// Sensor reading storage
int moistureLevel = 0; // To store moisture level from the sensor
// WiFi settings
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud rate
pinMode(moistureSensorPin, INPUT); // Configure sensor pin as input
pinMode(greenLEDPin, OUTPUT); // Configure green LED pin as output
pinMode(redLEDPin, OUTPUT); // Configure red LED pin as output
pinMode(waterPumpPin, OUTPUT); // Configure relay pin as output
setup_wifi(); // Connect to WiFi
}
void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void sendWebhookRequest(String data) {
HTTPClient http;
http.begin("https://webhookwizard.com/api/webhook/in?key=sk_4738b49d-a08e-433e-bb52-c5673372dd60");
http.addHeader("Content-Type", "application/json");
// Add your data to the request body
String requestBody = "{\"data\": \"" + data + "\"}";
// Send the POST request
int httpResponseCode = http.POST(requestBody);
// Free resources
http.end();
}
void loop() {
// Read moisture from sensor
moistureLevel = analogRead(moistureSensorPin);
// Watering logic based on moisture threshold
if (moistureLevel < 2000) {
// Low moisture: Activate water pump and green LED, turn off red LED
digitalWrite(redLEDPin, LOW);
digitalWrite(waterPumpPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
} else {
// Adequate moisture: Deactivate water pump and green LED, turn on red LED
digitalWrite(greenLEDPin, LOW);
digitalWrite(waterPumpPin, LOW);
digitalWrite(redLEDPin, HIGH);
}
// Debugging output
Serial.print("Soil Moisture: ");
Serial.println(moistureLevel); // Display moisture level
String level = String(moistureLevel);
// Call the webhook function with the moisture level
sendWebhookRequest(level);
delay(1000); // Wait for 1 second
}