#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Firebase Realtime Database URL --usi leak hii stuff msee anaeza connect to the backend.
const char* firebaseURL = "https://sgc-esp32-default-rtdb.firebaseio.com";
// 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 to Firebase (update "latest" node)
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Fixed path "sensors/latest" hio ndio tag but i'm not using it
String url = String(firebaseURL) + "/sensors/latest.json";
// Create JSON payload data yenye natuma kwa backend ya firebase
String payload = "{";
payload += "\"temperature\":" + String(temperature, 2) + ",";
payload += "\"tempStatus\":\"" + tempStatus + "\",";
payload += "\"soilMoisture\":" + String(soilMoisturePercent, 2) + ",";
payload += "\"moistureStatus\":\"" + moistureStatus + "\",";
payload += "\"soilPH\":" + String(soilPH, 2) + ",";
payload += "\"phStatus\":\"" + phStatus + "\",";
payload += "\"timestamp\":" + String(millis());
payload += "}";
Serial.println("Sending to Firebase:");
Serial.println(payload);
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.PUT(payload); // use PUT instead of POST juu hii inareplace badala ya kutengeneza records mingi
if (httpResponseCode > 0) {
Serial.print("Server Response: ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
} else {
Serial.print("Error sending: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(10000); // Send every 10 seconds venye ile code ya kwanza ilikua
}