Homelab: ESP32 MQTT Relay Control for Timed Fish Tank Aeration


5V Relay

Relay wiring can be complex. I purchased a single-channel 5V relay switch.

An ESP32 is connected externally as a controller, and a battery serves as the power source (the ESP32 requires a separate power supply).

A USB female connector is used to connect the USB devices to be controlled.

The ESP32 uses MicroPython for coding, and micropython-mqtt is used for MQTT.

Fish Tank Aeration

The first smart home scenario is for fish tank aeration. MQTT messages are used to send the aeration duration, turning on the relay, for example, to turn off after 30 seconds.

The red indicator light on the relay signifies that the current state is normal but the circuit is off, while the green light indicates that power is currently flowing normally.

If it’s a multi-channel relay, theoretically, multiple USB devices can be controlled. This allows for controlling automatic feeding, automatic lighting, automatic aeration, automatic watering, etc., based on different pins.

Home Assistant

Connect Home Assistant to the same MQTT broker, then set up automation tasks, such as sending MQTT messages at timed intervals of several hours.

After the ESP32 receives an MQTT message, it starts powering on, and the power-on duration is determined by the sent parameter, such as 30 seconds.

I use a self-deployed EMQX MQTT broker on a cloud server, which is generally uninterrupted. Of course, a better approach would be to use an internal network MQTT broker, which would ensure automation isn’t affected by internet outages and would eliminate many configurations related to external proxies and SSL certificates.