In this guide I’m going to walk you through deploying a powerful open-source MQTT broker called EMQX on a Raspberry Pi single board linux computer. I’ll also show you how to connect the MQTTX test client to the EMQX broker.
EMQX is the most feature rich, secure, and reliable MQTT broker available as both an open-source project and as a managed cloud service.
If you’re unfamiliar, MQTT stands for Message Queuing Telemetry Transport. It’s a robust messaging protocol with a publish and subscriber model - using topics to define where messages are coming from or going to.
You can use MQTT to make devices like Arduino compatible dev boards talk to each-other or applications in various languages.
The EMQX open-source MQTT broker is light weight and can be run on most platforms.
For a home-automation or industrial application you may want to run your broker on a stand alone device that runs on low power.
This is where the Raspberry Pi comes in. EMQX has build released for ARM 64 Bit, which is compatible with newer models of the Raspberry Pi, such as the Pi 3 and Pi 4.
Preparing the Raspberry PiEMQX has an ARM64 debian build that we need to remotely install on the Raspberry Pi. To do so you need to write the Raspberry Pi OS to an SD with pre-configured settings to connect to your wireless network and enable SSH.
- Insert an SD card with at least 8gb of storage into your host machine.
- Download and open the Raspberry Pi Imager.
- Select Raspberry Pi OS (64 bit) from the Operating System list.
- Select your SD card from the Storage list.
- Click on the Settings icon and configure an easily remembered hostname, such as emqx.local, enable SSH, set a username/password and add your WiFi credentials.
- Click Save
- Click Write, after a few minutes a pop-up will indicate success.
- Insert the SD card into the Raspberry Pi and connect it to a USB C Power supply.
Mac OS has SSH built into the default terminal application, for Windows you will want to use an application like PuTTY. Detailed instructions can be found in the Raspberry Pi Documentation.
- Open a new terminal window.
- Type the following and press enter.
ssh username@hostname.local
- If prompted to add an SSH key to the list of known hosts, type yes and press enter.
Upon successful login, you now have shell access to your Raspberry Pi device.
In the terminal, with an active SSH session on the Pi, enter the following commands.
curl -s https://assets.emqx.com/scripts/install-emqx-deb.sh | sudo bash
sudo apt-get install emqx
sudo systemctl start emqx
Upon completion, the EMQX open-source MQTT broker should be running on your Raspberry Pi as a service. Meaning, the broker will start on boot.
Accessing the EMQX DashboardEMQX serves a dashboard and mangement portal over the web. It can be accessed by navigating to hostname.local:18083
from a browser. Replacing hostname with the one you've set for your Pi.
The default username/password is admin/public
Before any client can connect to the newly deployed EMQX MQTT Broker, an authenticator needs to be created. Password-Based Authentication using the Built-in Database can quickly be created using the management portal.
- From the dashboard navigate to the Authentication page.
- Click on + Create.
- Select Password-Based and click Next.
- Select Built-in Database and click Next.
- Leave all the default settings and click Create.
Now that we've created a Password-Based authenticator using the Built-in database, we need to add users to the database.
- From the Authentication page, under the Actions column for the Password-based authenticator, click Users.
- Click the plus icon to create a new user with a username/password.
Repeat this process as necessary to create users for clients to connect as.
Connecting a Client to EMQX using MQTTXMQTTX is a free tool that helps you test client connections to an MQTT broker. It also lets you publish and subscribe to topics. We'll use it to connect to the EMQX broker running on the Pi using the credentials created in the last step.
- Download, install, and open the MQTTX application.
- Click on the plus icon to create a New Connection.
- Name your connection, supply the username/password, and set the hostname to match the one configured on your Pi.
- Click Connect.
Topics are named routes for knowing where a message is going to or coming from. They use a slash notation. For example, sensors/temp or sensors/humidity.
To publish messages to a topic, connect a device, construct a payload in the visible text box, and use the send button to publish it. The following image shows how to send a simple JSON message to testtopic/1.
To subscribe to a message, click on New subscription and supply the topic you'd like to subscribe to. Any message published to that topic will appear in the window for that connection. Try this out by creating a new user, a new connection, and subscribe to a topic that the other connection is publishing to.
You can also subscribe to all sub-topics under a topic using the # pound sign. For example, the topic sensors/# would yield any message published under the sensors/ topic.
Viewing Topic Metrics in the EMQX DashboardClick the Magnifying Lens icon to open the Topic Metrics page. Here you can add Topics to monitor. Any subsequent messages to that topic will be tracked here and you can get an idea of how many messages are going through.
Now that you've learned how to connect a test client from MQTTX, you'll want to connect your applications or devices to the broker.
We've published a series of blogs on how to connect using a variety of languages. MQTT is also a long-standing protocol in the IoT industry and there are numerous libraries for embedded devices, such as Arduino, available.
We have an amazing community of open-source contributors and users of the EMQX project and cloud service. Join in on the discussions, share your projects, or feel free to ask any questions! We'd love to hear from you!
Comments