• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » ESP32 Arduino : Multiple MQTT Topic

By Abhishek Ghosh September 10, 2019 5:53 pm Updated on September 10, 2019

ESP32 Arduino : Multiple MQTT Topic

Advertisement

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 :

Advertisement

---

Vim
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 :

Vim
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”:

Vim
1
2
3
4
5
6
…
void callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic,"led1status")==0)
}  
 
…

ESP32 Arduino Multiple MQTT Topic

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 :

Vim
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

This Article Has Been Shared 841 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to ESP32 Arduino : Multiple MQTT Topic

  • ESP32 Arduino : Create a Webpage to Control a Relay Module

    Here is How to Create a Webpage to Control a Relay Module Using ESP32 Arduino. This is a basic example which provides the base of advanced projects.

  • ESP32 Arduino and TM1637 Seven Segment LED Display

    TM1637 Seven Segment LED Display is Popular Thing in Arduino Prototyping World. Here is Some Words About Using TM1637 With ESP32 Arduino.

  • ESP32 Arduino Built-in Hall Sensor Code & Theory

    ESP32 has a Hall Effect Sensor near pin 22. Here is ESP32 Arduino Built-in Hall Sensor Code & Theory. Here is something about Hall Effect Sensor as well.

  • ESP32 MicroPython Setup with Adafruit Ampy

    Here is ESP32 MicroPython Setup with Adafruit Ampy for Windows, MacOS X and Linux. You need Python needs to be running and install few tools.

  • How to Control Multiple Relays With Single Arduino ESP32?

    Before How to Control Multiple Relays With Single Arduino ESP32 Testing, You Need to Learn How to Create Multiple MQTT Channels & Fetch Data.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (22.1K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • The Cost of Doing Business as a Handyman July 1, 2022
  • Samsung Galaxy S22 Ultra: Long Term Review June 30, 2022
  • How to Make the Most of Your S Pen (S22 Ultra) June 29, 2022
  • Safe Chargers for Samsung Galaxy S22 Ultra June 27, 2022
  • How Telecoms Can Use The Cloud To Power Their 5G Network June 24, 2022

About This Article

Cite this article as: Abhishek Ghosh, "ESP32 Arduino : Multiple MQTT Topic," in The Customize Windows, September 10, 2019, July 2, 2022, https://thecustomizewindows.com/2019/09/esp32-arduino-multiple-mqtt-topic/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2022 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy