The Agri-Tech Builder

Coding smart solutions for off-grid farming

Designing a Smart Soil Moisture Dashboard with ESP32

Author: Marcial Rey (In Between Bamboos Farm)


Overwatering washes away nutrients, and underwatering stunts crop growth. To take the guesswork out of my vegetable production, I am bringing IoT (Internet of Things) to the soil. By placing a cheap ESP32 microcontroller and an analog soil moisture sensor directly into the garden beds, the farm can now literally tell me when it is thirsty.

Bridging Hardware and Software

The ESP32 reads the moisture resistance in the soil every hour. As long as it has a Wi-Fi connection, it wraps that data into an HTTP POST request and fires it directly to my PHP web server, updating my dashboard in real-time.

The Code: The C++ ESP32 Sender

Here is the C++ Arduino code running on the microcontroller out in the dirt. It reads the sensor and transmits it to my PHP Agri-Tech server.

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "FARM_WIFI";
const char* password = "securepassword";
const char* serverName = "http://offgridcoding.com/api/soil-logger.php";

int moisturePin = 34; // Analog pin connected to the sensor

void setup() {
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  if(WiFi.status() == WL_CONNECTED){
    HTTPClient http;
    http.begin(serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // Read the soil moisture
    int moistureValue = analogRead(moisturePin);
    String httpRequestData = "sensor_id=BED_01&moisture=" + String(moistureValue);
    
    // Send to PHP server
    int httpResponseCode = http.POST(httpRequestData);
    http.end();
  }
  
  // Go to sleep for 1 hour to save solar battery
  delay(3600000); 
}