Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Cloud Calling System with MQTT
Message Queuing Telemetry Transport (MQTT) is a straightforward messaging protocol. It is also the most widely used messaging protocol in the realm of the Internet of Things (IoT).
MQTT protocols define how IoT devices exchange data. They operate in an event-driven manner and are interconnected using the Publish/Subscribe model. The sender (Publisher) and the receiver (Subscriber) communicate through Topics. A device publishes a message on a specific topic, and all devices subscribed to that topic receive the message.
In this section, we’ll create a service bell system using UNO R4, HiveMQ (a free public MQTT broker service), and four buttons. Each of the four buttons corresponds to a restaurant table, and when a customer presses a button, you’ll be able to see which table needs service on HiveMQ.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring
Schematic
How to play?
HiveMQ is an MQTT broker and client-based messaging platform that facilitates fast, efficient, and reliable data transfer to IoT devices.
Open HiveMQ Web Client in your web browser.
Connect the client to the default public proxy.
Click on Add New Topic Subscription.
Enter the topics you wish to follow and click Subscribe. Make sure the topics you set here are unique to avoid receiving messages from other users, and pay attention to case sensitivity.
In this example code, we set the topic as
SunFounder MQTT Test
. If you have made any changes, ensure that the topic in the code matches the subscribed topic on the webpage.
Install the Library
To install the library, use the Arduino Library Manager and search for “ArduinoMqttClient” and install it.
ArduinoMqttClient.h
: Used for MQTT communication.
Run the Code
Note
You can open the file
04_mqtt_button.ino
under the path ofelite-explorer-kit-main\iot_project\04_mqtt_button
directly.Or copy this code into Arduino IDE.
Note
In the code, SSID and password are stored in arduino_secrets.h
. Before uploading this example, you need to modify them with your own WiFi credentials. Additionally, for security purposes, ensure that this information is kept confidential when sharing or storing the code.
After running the code, go back to HiveMQ Web Client, and when you press one of the buttons on the breadboard, you will see the Messages prompt on HiveMQ.
How it works?
This code is for an Arduino-based project that connects to Wi-Fi and communicates with an MQTT broker using the MQTT protocol. Additionally, it can detect whether four buttons are pressed and send the corresponding messages to the MQTT broker.
Here is a detailed explanation of the code:
Include Relevant Libraries:
#include <WiFiS3.h> #include <ArduinoMqttClient.h>
Include Sensitive Information:
The
arduino_secrets.h
file contains the SSID and password for the Wi-Fi network.
#include "arduino_secrets.h" char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS;
Initialize Variables:
Variables for managing Wi-Fi and MQTT connections.
Initialize button pins and button states.
setup()
:Initialize serial communication.
Check for the presence of the Wi-Fi module and attempt to connect to Wi-Fi.
Print network data.
Attempt to connect to the MQTT broker.
Subscribe to MQTT topics.
Set buttons to input mode.
loop()
:Keep the MQTT connection active.
Check if each button is pressed, and if so, send MQTT messages.
Other Utility Functions:
printWifiData()
: Prints information about the currently connected Wi-Fi network.printCurrentNet()
: Prints relevant data about the current network.printMacAddress(byte mac[])
: Prints the MAC address.onMqttMessage(int messageSize)
: Callback function triggered when a message is received from the MQTT broker. It prints the received message topic and content.sendButtonMessage(int buttonNumber)
: Use this function to send MQTT messages when a button is pressed.