• 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 , https://thecustomizewindows com/2019/09/esp32-arduino-multiple-mqtt-topic/
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

  • 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.

  • Control Multiple AC Appliances With One ESP32 Arduino

    Here is how to use ESP32 and IBM Watson IoT to control multiple relays (i.e. multiple AC appliances) by pushbutton and over the internet.

  • WROOM ESP32 Example Codes For IBM Watson IoT Platform

    Here Are Few WROOM ESP32 Example Codes For IBM Watson IoT Platform So That Anyone Can Get Started With Both of Them Without Huge Experience.

  • Connecting ESP32 Arduino with DHT11 with IBM Watson IoT

    Earlier, we described how to create graph on IBM Watson IoT dashboard by using the default widgets. In previous guide, we described how to use ESP32 Arduino with DHT11 sensor. Here is the Code and Diagram to Connect ESP32 Arduino with DHT11 with IBM Watson IoT and Get Odometer Like Gauges on Dashboard. For this […]

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 (24.3K 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

  • How to Restrict Certain Posts or Categories In WordPress by CountryJune 6, 2023
  • Influence of Digitization on Procurement : Part 1June 6, 2023
  • How We Can Show Apple Icon, Windows Logo on Website TextJune 5, 2023
  • What is Google Bard? How it WorksJune 5, 2023
  • How to Create a PHP Installer Script to Create Tables in MySQL DatabaseJune 4, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

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

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT