Marcio Cunha

How to automate plant irrigation with ESP32 and MQTT

Learn how to build an automated watering system using ESP32 microcontrollers, soil moisture sensors, and the MQTT protocol for smart home IoT integration.

Marcio Cunha•4 min
Also available in:EspañolPortuguês
Summary
  • The ESP32 microcontroller connects physical devices to the internet via low-cost Wi-Fi.
  • The MQTT protocol enables lightweight communication between sensors and smart home platforms.
  • Capacitive sensors measure soil moisture without suffering rapid corrosion from electrolysis.
  • Electromechanical relays act as safe switches to trigger 12V water pumps.
  • Local integration ensures continuous operation even during external internet connection drops.

The challenge of keeping plants watered in a busy routine

Anyone who grows plants at home knows that watering them just right is the secret to preventing root rot from overwatering and severe dehydration. In practice, this means constantly monitoring soil moisture, a tough task for anyone who spends the day away or travels frequently. The ideal solution is delegating this vigilance to a small embedded computer capable of making local decisions and talking to our smart home. If you enjoy integrating systems, you can see how to structure robust routines in other scenarios by checking our guide on how to build microservices resilient to network failures, whose fault-tolerance principles apply perfectly to hardware.

Understanding the ESP32 and its versatility in IoT projects

The core of our project is the ESP32, a tiny and extremely cheap chip that comes with built-in Wi-Fi and Bluetooth connections. In practice, it works like a headless mini computer that we can program to read physical sensors and send that data over the wireless network. Compared to traditional Arduino models, the ESP32 offers far more processing power and memory, allowing it to run simultaneous tasks without freezing. This ability to manage multiple data streams resembles the logic we discussed when analyzing how to handle heavy concurrency in Go queues without blocking, where the system must respond rapidly to intense external stimuli.

The role of the MQTT protocol in residential communication

To make the ESP32 talk to the rest of our home, we use MQTT, a lightweight messaging protocol designed specifically for the Internet of Things. In practice, it works like an internal postal system where devices publish information to specific 'topics' or subscribe to them to receive commands. A central server, called an MQTT broker (like the popular Mosquitto), manages the traffic of these messages instantly and with minimal battery and network bandwidth consumption. This avoids the need to create complex web APIs, making data exchange extremely agile between the garden and the living room control panel.

Choosing the ideal moisture sensor and preventing corrosion

There are two main types of moisture sensors on the market: resistive ones, which use two exposed metal rods, and capacitive ones, which measure electrical field variation without direct metal contact with the soil. In practice, resistive sensors oxidize and rust within weeks due to electrolysis caused by continuous electrical current in damp soil. Therefore, we choose the capacitive sensor, which features a protective varnish layer and guarantees years of durability. It converts soil moisture into a voltage signal that the ESP32 easily reads through its analog ports.

Assembly scheme and basic reading code

Physical assembly requires connecting the sensor power to the 3.3V and GND pins of the ESP32, as well as wiring the signal pin to an available analog input. To trigger the water pump, we use a relay module, which is an electrical switch controlled by a 3.3V signal from the microcontroller. Below, we present a snippet of C++ code using the Arduino IDE to read moisture and publish the data via MQTT:

#include <WiFi.h>\n#include <PubSubClient.h>\n\nconst char* ssid = 'YourWiFiNetwork';\nconst char* password = 'YourWiFiPassword';\nconst char* mqtt_server = '192.168.1.100';\n\nWiFiClient espClient;\nPubSubClient client(espClient);\nconst int sensorPin = 34;\n\nvoid setup() {\n  Serial.begin(115200);\n  WiFi.begin(ssid, password);\n  client.setServer(mqtt_server, 1883);\n}\n\nvoid loop() {\n  if (!client.connected()) {\n    // MQTT reconnection routine\n  }\n  client.loop();\n  \n  int moisture = analogRead(sensorPin);\n  client.publish('home/garden/moisture', String(moisture).c_str());\n  delay(5000);\n}

Handling network failures and ensuring autonomous operation

In smart home automation projects, the Wi-Fi network might drop or the router might reboot, and the system cannot stop working because of that. In practice, we program the ESP32 with automatic reconnection logic and keep local safety rules that prevent the pump from running forever if the signal drops. If the sensor reading indicates dry soil and the MQTT connection fails, the microcontroller should still have autonomy to decide whether to run an emergency watering based on internal timers. This defensive approach ensures your plants stay alive even during a blackout in your local network.

Final thoughts on automation and intelligence in IoT

Automating irrigation with ESP32 and MQTT transforms a repetitive manual task into an intelligent, reliable ecosystem fully integrated into your home. Besides saving water and ensuring plant health, the project serves as a solid foundation to expand your sensor network to other rooms and devices. With proper hardware planning and resilient code, you build a truly autonomous and lasting automation infrastructure.