Building a Smart Farm with Arduino and ESP32: A Comprehensive Guide from Temperature and Humidity Monitoring to Automated Irrigation

🛠️ Low-Cost Agricultural IoT Solution | 💡 A Greenhouse Control System for Beginners

🌟 I. Project Background and System Value

🌱 Pain Points in Traditional Agriculture:

  • Manual inspections are time-consuming and labor-intensive
  • Difficulty in responding to fluctuations in environmental parameters
  • Severe waste of water resources

🚀 Core Functions of the Smart System:

  1. Real-time monitoring of temperature, humidity, light intensity, and soil moisture
  2. Automatic adjustment of shading curtains and irrigation systems
  3. Remote control and alerts via mobile APP

🔧 II. Hardware List and Cost Budget

📦 Essential Materials (Total Budget Approximately 300 Yuan)

Module Type Recommended Model Unit Price Function Description
Main Control ESP32 NodeMCU 50 Yuan Integrated Wi-Fi / Bluetooth
Sensor DHT11 (Temperature and Humidity) 10 Yuan Detects air temperature and humidity
BH1750 (Light) 20 Yuan Measures light intensity
FC-28 (Soil Moisture) 30 Yuan Monitors soil moisture
Actuator Relay Module 15 Yuan Controls water pump / shading curtain
Display OLED Screen 25 Yuan Local data display
Power Supply Solar Panel + Lithium Battery 150 Yuan Outdoor power supply solution

🚀 III. Comprehensive Development Process

📝 Four Key Steps from Hardware to Software

  1. Circuit Construction (Breadboard Connection)

  • Wiring Diagram

    ESP32

    ├── GPIO4 → DHT11 Data  
    ├── GPIO5 → Relay Control  
    ├── SDA → OLED Data  
    ├── SCL → OLED Clock  
    ├── A0 → BH1750 Analog Output  
    └── A1 → FC-28 Analog Output  
    
  • Code Writing (Arduino IDE)

    • Core Code for Temperature and Humidity Monitoring

      #include <WiFi.h>

      #include &lt;PubSubClient.h&gt;  
      #include &lt;DHT.h&gt;  
      #define DHT_PIN 4  
      DHT dht(DHT_PIN, DHT11);
      
      const char* ssid ="YourWiFi";
      const char* password ="YourPassword";
      const char* mqtt_server ="mqtt.eclipseprojects.io";
      
      void setup(){
        dht.begin();
        WiFi.begin(ssid, password);
        while(WiFi.status()!= WL_CONNECTED) delay(1000);
      }
      
      void loop(){
        float h = dht.readHumidity();
        float t = dht.readTemperature();
        client.publish("farm/temp", String(t).c_str());
        delay(5000);
      }  
      
  • Mobile APP Configuration (Blynk Platform)

    • Step-by-Step Guide
    1. Register a Blynk account and create a new project
    2. Configure virtual pins V0 to display temperature and V1 to control irrigation
    3. Insert the authorization code into the Arduino code

    🎯 IV. Core Function Implementation

    1. Real-time Monitoring of Environmental Parameters

    • Data Collection Logic

      int light = analogRead(A0); // Read light intensity

      int soil = analogRead(A1);   // Read soil moisture  
      
    • Threshold Alarmif (t > 30) client.publish(“alert”, “High Temperature Warning!”);

    2. Automation Control

    • Automatic Irrigation System

      if (soil < 300 && h < 60) {

        digitalWrite(5, HIGH); // Turn on water pump  
        client.publish("irrigation", "Automatic watering in progress");  
      }  
      
    • Smart Shading Curtainif (light > 800) digitalWrite(6, HIGH); // Pull up shading curtain

    3. Local Data Visualization

    • OLED Display Code

      display.clearDisplay();

      display.setTextSize(1);  
      display.setCursor(0,0);  
      display.print("T: "); display.print(t);  
      display.print(" H: "); display.print(h);  
      display.display();  
      

    🔐 V. Advanced Function Development

    🚀 Three Directions to Enhance System Intelligence

    1. Low Power Design

    • Use <span>ESP.deepSleep()</span> to enter sleep mode, waking up every 30 minutes
    • Turn off unused peripherals (e.g., Bluetooth, ADC)
  • Cloud Data Analysis

    • Upload data to the ThingSpeak platform to generate historical trend charts
    • Implementation Code:

      client.publish(“channels/123456/feeds/field1”, String(t).c_str());

  • Video Surveillance Integration

    • Connect the ESP32-CAM module for real-time transmission of greenhouse images
    • Example command: “View real-time greenhouse images”

    ⚠️ VI. Pitfall Guide

    🔥 Common Problem Solutions During Development

    1. Abnormal Sensor Data

    • Check for loose connections and avoid strong electrical interference (e.g., from water pump motors)
  • Automatic Irrigation False Trigger

    • Add debounce logic:

      if (soil < 300 && lastSoil > 300) digitalWrite(5, HIGH);

  • Unstable Outdoor Power Supply

    • Use a solar charging controller to ensure the lithium battery voltage remains stable at 5V

    📢 ConclusionThe combination of Arduino and ESP32 provides a low-cost, easily expandable solution for smart agriculture. From greenhouses to fields, you can easily achieve intelligent environmental control. Start building your smart farm now!

    Leave a Comment