Remote Control ESP32 Lights - MQTT and MicroPython

by bhaveshtk in Circuits > Wireless

1216 Views, 2 Favorites, 0 Comments

Remote Control ESP32 Lights - MQTT and MicroPython

both-buttons-leds.gif

What if you want to remote control a drone, or create a smart home lighting system, or collect sensor data from many devices? Enter MQTT: a robust and flexible IoT protocol that lets us do all that and more!

In this project we'll connect two ESP32 boards to each other wirelessly using the MQTT protocol. We will take advantage of the power and flexibility of MicroPython to get the project up and running quickly!

Supplies

  1. Two generic ESP32 boards, for e.g. https://www.amazon.ca/gp/product/B07QCP2451/
  2. Two micro-USB cables

Watch the Video

MicroPython - MQTT tutorial on ESP32

The tutorial video goes through step-by-step instructions of how to setup the hardware, programs and the code to write. So please have a look at that before attempting it yourself!

Alternatively, if you prefer reading than watching video, I have a written step-by-step tutorial here.

Connect Both ESP32s to Your Computer Via USB

image-6.png

Setup/Configure Mosquitto Broker

Add the lines:

listener 1883
allow_anonymous true

To the bottom of your mosquitto config file. Save the file.

Start Mosquitto Broker

Use this command in terminal (config file location is shown for macOS, it will differ on Windows & Linux) to start the mosquitto broker:

mosquitto -c /opt/homebrew/etc/mosquitto/mosquitto.conf

Save Bootup Code and Application Code to First ESP32

You will need to use the Thonny editor to upload these files to your microcontroller.

  1. The bootup code connects to WiFi and is found in boot.py.
  2. Make sure to set the WIFI_NAME and WIFI_PASS to your own WiFi network's username and password.
  3. The application code is in main.py and it does all the MQTT work of connecting, publishing and subscribing.
  4. Make sure to replace the broker IP address with your computer's IP address.
  5. Make sure to replace pin numbers (0 and 2) with the correct pin number for the button and LED on your ESP32 board's pinout diagram / datasheet. It's not always the same.

Downloads

Save Bootup Code and Application to Second ESP32

The files are the same as above, with one major exception: change all references to "blue" to "green", and "green" to "blue" in the main.py code. This is so the MQTT broker can differentiate each microcontroller from each other, by device name and MQTT topic names.

Restart Both ESP32s and Try It Out!

FXZ6G4CL3YGIYZO.gif

Each button press should light the LED on the other microcontroller! Yay, a remote controlled light :D

Further Resources

broker-diagram.jpg
sequence-diagram.png

Attached here are diagrams (also shown in the tutorial video) that will help you understand the network architecture of this project, and the sequence of how MQTT subscription and message publishing happens.


If you liked this project and would like more MicroPython tutorials, tips and projects, just follow me on my website and I'll email them to you 1-2 times a month. If you prefer video tutorials, you can follow me on youtube here 😃


Lastly, if you have questions, feedback or just wanna have a chat, hit me up on twitter 😃