Blog Startseite AI generated Robot Image

Building an Automated Irrigation System for Your Garden with ESP32 and MQTT

10.12.2024 - Engine: Gemini

Building an Automated Irrigation System for Your Garden with ESP32 and MQTT

Building a Garden Irrigation System with ESP32 and MQTT

Components Required

  • ESP32 microcontroller
  • Soil Moisture Sensor
  • Relay board
  • Solenoid Valves
  • Water Pump
  • MQTT Server

Steps

1. Connect the Components

  • Connect the Soil Moisture Sensor to an analog input pin on the ESP32.
  • Connect the Relay board to a digital output pin on the ESP32.
  • Connect the Solenoid Valves to the Relay board.
  • Connect the Water Pump to the Solenoid Valves.

2. Install Software

  • Install Arduino IDE.
  • Download the ESP32 board definition to Arduino IDE.
  • Install the following libraries:
    • PubSubClient (for MQTT communication)
    • Adafruit Unified Sensor (for Soil Moisture Sensor)

3. Write the Code

  • Create a new Arduino project.
  • Write the following code:
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_DHT.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* topic = "/garden/irrigation";

DHT dht(SOIL_MOISTURE_SENSOR_PIN, DHT11);

// Setup MQTT Client
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(9600);
  dht.begin();

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // Connect to MQTT server
  client.setServer(mqttServer, mqttPort);
  client.connect("garden_irrigation");

  // Publish MQTT message
  client.publish(topic, "online");
}

void loop() {
  // Read Soil Moisture Sensor
  float humidity = dht.getHumidity();

  // Check if Irrigation is needed
  if (humidity < 30) {
    // Open Solenoid Valve
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    // Close Solenoid Valve
    digitalWrite(RELAY_PIN, LOW);
  }

  // Send data to MQTT server
  client.publish(topic, String(humidity).c_str());
  delay(5000);
}

4. Upload Code and Configure MQTT Server

  • Upload the code to the ESP32 board.
  • Configure your MQTT server to subscribe to the topic /garden/irrigation.

5. Testing the System

  • Start the system and wait for the ESP32 to connect to WiFi and establish connection to MQTT server.
  • Check your MQTT server for messages with moisture level and irrigation status.

Notes

  • Make sure your Soil Moisture Sensor is rated for outdoor use.
  • Use a relay that has enough switching capacity for your Solenoid Valves.
  • The moisture and irrigation interval settings can be adjusted based on plant type and environment.

Note:

All texts on this blog are generated using Artificial Intelligence (AI). The purpose of this blog is to test the generated content in the context of SEO and analyze its rankings. Please be aware that I cannot take responsibility for the accuracy or completeness of the texts published here.


© 2024 ivosys.de - Made with iMicroBlog by ivosys.de - Code CSS3 HTML5 Javascript PHP