JAlcocerTech E-books

Chapter 6 — The ESP32 in Practice: Solar, Valves, and Node-RED

A solar panel on the balcony. An ESP32 reading soil moisture. A solenoid valve that opens when the soil is dry. A Node-RED flow orchestrating the logic.

This is the project that made the ESP32 feel like a real tool rather than a development board.

Node-RED: Flow Programming for IoT

Node-RED is a visual programming tool for wiring together devices, APIs, and online services. It runs in Docker, has a browser-based editor, and uses a flow paradigm: data flows from left-to-right through connected nodes, each node transforms or routes the message.

For IoT, it fills the gap between raw MQTT data and useful decisions. Instead of writing Python scripts to subscribe to MQTT topics and trigger HTTP calls, you drag and drop:

[MQTT In: home/soil/moisture] → [Switch: value < 30] → [MQTT Out: home/valve/cmd]

Three nodes. Fifteen seconds to wire. The valve opens when moisture drops below 30%.

Node-RED connects natively to MQTT, HTTP, WebSocket, InfluxDB, PostgreSQL, and dozens of other systems via its node library. It’s not a replacement for code — it’s the glue between the parts that would otherwise require boilerplate.

MQTT Valve Control

Solenoid valves for irrigation are available for under €5. They operate at 12V DC or 24V AC. The ESP32 GPIO pin cannot drive them directly — it needs a relay or a MOSFET driver.

The control flow:

  1. ESP32 publishes moisture reading to home/soil/moisture every 10 minutes
  2. Node-RED subscribes to the topic, checks the value
  3. If moisture < threshold: Node-RED publishes "ON" to home/valve/cmd
  4. ESP32 subscribes to home/valve/cmd, triggers the relay, opens the valve for 60 seconds
  5. Node-RED publishes "OFF" after the delay

This pattern — sensor publishes data, Node-RED makes the decision, actuator subscribes to commands — is the correct separation of concerns for a maintainable IoT system. The decision logic lives in one place (Node-RED), not scattered across firmware.

sequenceDiagram
    participant Soil as ESP32 (Soil Sensor)
    participant Broker as MQTT Broker
    participant NR as Node-RED
    participant Valve as ESP32 (Valve)

    Soil->>Broker: publish home/soil/moisture (22%)
    Broker->>NR: deliver message
    NR->>NR: 22 < 30 threshold → trigger
    NR->>Broker: publish home/valve/cmd "ON"
    Broker->>Valve: deliver command
    Valve->>Valve: relay closes → valve opens
    Note over NR: wait 60 seconds
    NR->>Broker: publish home/valve/cmd "OFF"
    Broker->>Valve: deliver command
    Valve->>Valve: relay opens → valve closes

The Solar Reality

A 5W solar panel produces its rated power for 2–6 hours per day depending on location and season. The rest of the time, the system runs on the battery.

The practical implication: your ESP32 node needs to survive on battery for 18–22 hours per day. Deep sleep between readings is not optional — it’s the difference between a system that runs for months and one that dies overnight.

Monitor your battery voltage as part of your telemetry. Add a low-battery alert in Node-RED that publishes to Home Assistant before the node goes dark. Nothing is more frustrating than discovering your soil sensor died three days ago and your plants are dry.

Solar panel on the balcony powering outdoor ESP32 sensor nodes

Sensor readings landing in a local database — the result of a working MQTT → storage pipeline


Takeaway: Node-RED is the decision layer between sensors and actuators. Keep firmware simple — it reads and publishes. Let Node-RED handle the logic. Monitor battery voltage.