#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials for Wokwi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Webhook URL (use HTTP for Wokwi simulation)
const char* serverURL = "http://webhook.site/e5ff4158-492b-487a-ad12-023cf99fcb15";
// Potentiometer pins
#define TEMP_PIN 32
#define SOIL_PIN 34
#define PH_PIN 35
// Function to map ADC to percentage
float adcToPercent(int adcValue) {
return map(adcValue, 0, 4095, 0, 100);
}
// Function to map ADC to pH scale
float adcToPH(int adcValue) {
return (adcValue / 4095.0) * 14.0;
}
// Function to map ADC to temperature range (10°C - 40°C)
float adcToTemp(int adcValue) {
return map(adcValue, 0, 4095, 10, 40);
}
// Thresholds
const float MOISTURE_LOW = 30;
const float MOISTURE_HIGH = 70;
const float PH_LOW = 5.5;
const float PH_HIGH = 7.5;
const float TEMP_LOW = 15;
const float TEMP_HIGH = 35;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
}
void loop() {
// Read sensors (potentiometers)
int tempADC = analogRead(TEMP_PIN);
int soilADC = analogRead(SOIL_PIN);
int phADC = analogRead(PH_PIN);
float temperature = adcToTemp(tempADC);
float soilMoisturePercent = adcToPercent(soilADC);
float soilPH = adcToPH(phADC);
// Farmer-friendly short messages
String moistureStatus = "";
if (soilMoisturePercent < MOISTURE_LOW) moistureStatus = "Dry";
else if (soilMoisturePercent <= MOISTURE_HIGH) moistureStatus = "Perfect";
else moistureStatus = "Too Wet";
String phStatus = "";
if (soilPH < PH_LOW) phStatus = "Acidic";
else if (soilPH <= PH_HIGH) phStatus = "Perfect";
else phStatus = "Basic";
String tempStatus = "";
if (temperature < TEMP_LOW) tempStatus = "Cold";
else if (temperature <= TEMP_HIGH) tempStatus = "Perfect";
else tempStatus = "Hot";
// Print values and status
Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C | ");
Serial.print(tempStatus); Serial.print(" | ");
Serial.print("Soil Moisture: "); Serial.print(soilMoisturePercent); Serial.print(" % | ");
Serial.print(moistureStatus); Serial.print(" | ");
Serial.print("Soil pH: "); Serial.print(soilPH, 2);
Serial.print(" | "); Serial.println(phStatus);
// Send data and status to webhook
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(serverURL) + "?temp=" + String(temperature) +
"&tempStatus=" + tempStatus +
"&soil=" + String(soilMoisturePercent) +
"&soilStatus=" + moistureStatus +
"&ph=" + String(soilPH, 2) +
"&phStatus=" + phStatus;
Serial.println("Sending to webhook:");
Serial.println(url);
http.begin(url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("Server Response: "); Serial.println(httpResponseCode);
} else {
Serial.print("Error sending: "); Serial.println(httpResponseCode);
}
http.end();
}
delay(10000); // Send every 10 seconds
}