MQTT, or Message Queuing Telemetry Protocol, is an open standard for allowing devices or applications to publish and subscribe to messages using named topics as routes (i.e. /sensors/temperature). It has built-in features like QoS (Quality of Service) that let you specify how you want to prioritize or guarantee message delivery. Its a favorite of IoT hobbyists and industrial automation alike.
EMQX is an open-source MQTT broker (the server that devices publish and subscribe through) with well-rounded security, efficiency, and scale. It can be run locally, in the cloud, and even on a Raspberry Pi. Its also offered as a cloud service, with a serverless free-tier version available.
The purpose of this guide is to walk you through using EMQX with Node-Red, an open-source flow-based programming environment packed with community sourced integrations. In short, you'll be able to visually create programs that consume or publish data through MQTT.
Running EMQX Locally and Creating a Client UserEMQX can be installed on a local machine or cloud instance in a number of ways. The easiest is to follow the instructions for your operating system here https://www.emqx.io/downloads
All these instructions are for running in the terminal of your operating system. You can also build the project from source via GitHub. https://github.com/emqx/emqx
Once installed and running, the EMQX admin dashboard can be found at http://127.0.0.1:18083/
You'll need to create user credentials for a client, in this case Node-Red, to connect as. Use the following instructions.
- Open the EMQX Dashboard and login with the default username "admin" and password "public".
- 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.
- 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.
A free serverless instance of EMQX can be deployed by navigating to https://www.emqx.io/ and clicking on "Try EMQX Cloud". While limited in some ways, it still provides a powerful MQTT broker with 1 million session/minutes per month, forever.
You'll need to create user credentials for a client, in this case Node-Red, to connect as. Use the following instructions.
- Navigate to "Authentication & ACL" from the project page for your serverless instance of EMQX.
- Click on the plus sign to You'll need to create user credentials for a client, in this case Node-Red, to connect as.
- Create an arbitrary and secure username and password.
- Click Save.
Node-Red has instructions for installing and running the project in a variety of ways. For this example I'm running it locally, but you could easily choose to run it on a Raspberry Pi or by using a cloud service like FlowForge.
Once running, navigate to the IP address or hostname where its hosted to begin creating a new flow. This guide will ultimately lead you to building this flow.
Node-Red has a built in node for Publishing and Subscribing to topics using MQTT. In order for it to connect to an MQTT broker like EMQX, credentials and server information must be configured. The following will walk you through connecting using EMQX open-source or EMQX cloud and how to publish and subscribe to topics.
- In a new flow, drag an "mqtt in" to the flow grid.
- Double click on the node to open the editor.
- Click the edit button next to "Add a new mqtt-broker"
- Enter "hello/world" in the Topic field.
- In the "Server" field enter the server address and port for your EMQX broker. If running EMQX open-source locally, enter in localhost and port 1883. For EMQX Cloud, the server and port for your broker can be found on the project page for your serverless instance.
- On the "Security" tab, enter in the username and password for the user created earlier.
- If using EMQX Cloud, you'll need to enable TLS and upload the CA Certificate file, which can also be found on the project page for the serverless instance.
- Finish and click update.
Next lets test this works by publishing and subscribing to the "hello/world" topic and creating a way to debug output.
- Drag in a debug node and connect the mqtt-in node to it. The topic that the mqtt-in node is subscribed to, hello/world, will send all messages received to the debug node. Those results can be seen on the right side of the screen.
- Drag in a mqtt-out node, double click it to verify that it is set to publish to the hello/world topic.
- Drag in an inject node and connect it to the mqtt-out node.
- Click Deploy in the top right corner of the page.
- Click the button next to the inject node. If you see a timestamp in the debug tab, then congratulations! You've successfully subscribed and published to a topic using an EMQX broker.
A common use case for MQTT is connecting sensor devices to ingest, process, and display sensor data. In the event that you don't have such a device to test with, I've provided a simple flow that simulates a device sending sensor data every few seconds. I've also included a sample dashboard for displaying that data.
Before importing the flow, you'll need to install the node-red-dashboard module. Instructions can be found here https://flows.nodered.org/node/node-red-dashboard
You can also install it from the node palette from the top right menu icon.
To import the flow:
- Copy the JSON of the flow from this gist
- In the top right click on the menu icon and select "Import"
- Paste in the JSON and click Import.
- Double click on the MQTT nodes and make sure they are configured for your EMQX instance.
- Click Deploy.
- Click on the inject node button. In the right hand debug tab you will see sample output.
- On the right hand side click the carrot icon to reveal a drop-down, select dashboard, then click the icon that resembles a sideway arrow to open the dashboard in a new tab.
This guide demonstrated how to install Node-Red, EMQX Open-Source, and how to create an EMQX cloud instance. It also showed you how to create credentials for a user on EMQX and configure an MQTT node in Node-Red. Finally it walked through creating flows that can both publish and subscribe to MQTT topics and display that data in real time.
Node-Red has many powerful features and integrations for building IoT automation and processing data. With EMQX you now have a reliable and scalable way to incorporate realtime data from anywhere into these applications. Try connecting an IoT device or another application to your EMQX broker and see what you can build with that data using Node-Red.
Join Our Community!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