It is possible to use a single ESP32 to handle multiple relays in your IoT project. We can use MQTT to subscribe or publish multiple topics. A good real-life deployable example of IoT controlled AC appliances is our guide on . In that project, we have two status indicator LEDs and one relay connect to the ESP32. We could control the AC appliance connected with the relay either by pressing a pushbutton or with cURL command (i.e. command over HTTP). We also shown how to use an Android application to get the control job done.
There are many use cases of handling multiple MQTT topics. However, most common usages are with multiple relays. Many of the users want to use their 4 channel relays with their ESP32 Arduino to build DIY home automation.
Most of the readers complaining that our code on GitHub is for single relay. Publishing and subscribing to multiple topic possible to achieve in various ways. For a single relay or device, we use String
with the callback()
function and end it :
---
1 2 3 | … void callback(char* topic, byte* payload, unsigned int length); … |
Also, we subscribing to one MQTT topic. For our previous code, we used these lines for the single device :
1 2 3 4 5 6 7 8 9 10 11 | … const char commandTopic[] = "iot-2/cmd/+/fmt/+"; void gotMsg(char* topic, byte* payload, unsigned int payloadLength); … void gotMsg(char* topic, byte* payload, unsigned int payloadLength) { … subscribeTo(commandTopic); … |
Our previous example with IBM IoT is quite difficult as there is complexity of button press, debouncing, publishing and fetching data from correct URI over HTTP. Using multiple relays with MQTT not so difficult in any basic example.
strcmp
is a function of Arduino PubSub client which does most of the job to “compare”:
1 2 3 4 5 6 | … void callback(char* topic, byte* payload, unsigned int length) { if (strcmp(topic,"led1status")==0) } … |
strcmp()
is part of libc. It is purely of C, C++. strcmp
is an in-built function, which is used for the comparison of 2 strings such as string 1, string 2. If you want to learn in-details about strcmp
then you can check the websites on C, C++. We want to avoid going into too many details.
Problem with strcmp
is that it may return a negative number or something more than zero. That is often unexpected to the newbie! We are almost whipping the function to use in MQTT! Originally it was not designed for these situations. Our example with IBM Watson IoT was built thinking to avoid future problems. For that code, you’ll not need strcmp
(we will provide that solution sooner). You can try simple examples with just control of multiple relays over the internet. There are a lot of Q&A on useful sites with working (or half-working) code, such as :
1 2 3 4 | https://forum.arduino.cc/index.php?topic=618748.0 https://github.com/bvansambeek/domotica/blob/master/Nodes/Wemos_MQQT_4btns_3relays_temp/Wemos_MQQT_4btns_3relays_temp.ino |
We hope, we have covered the topic required for our future guides!
Tagged With adrdunio mqtt mehtere topics , arduino mqtt subscribe to multiple topics , https://thecustomizewindows com/2019/09/esp32-arduino-multiple-mqtt-topic/