MQTT and IoT go hand in hand. MQTT is a publish subscribe model for transmitting data packets and is widely used in IoT (Internet of Things). This guide will review how to setup Node-RED and Mosquitto, an MQTT broker, to publish data and subscribe to topics with a Node-RED client. This is a practical, how-to guide on using a local Mosquitto broker and Node-RED instance that takes less than 20 minutes to complete.
For specific details about MQTT, check out my previous post.
First step, install Node-RED. Head over to nodered.org and pick your installation of choice. I’m running locally, so I pick that option. Please note if you’re installing on Windows, you’ll need to select that option from the yellow box at the beginning of the page.
It is an easy process to follow. On Windows it is two steps for the installation, and one step to begin running.
3. Run Node-RED. Run the command from your command prompt.
That’s it, you should see text similar to this once you’re running. Quick and simple.
Next, navigate to http://127.0.0.1:1880 to see your flows.
That’s it, time to install Mosquitto locally and we’ll be ready to send and receive data!
Mosquitto is only slightly more difficult to install than Node-RED. If you only want to experiment with MQTT, you can skip this step and use the public broker at test.mosquitto.org.
4. Run the mosquitto service.
Now, we’re ready to test sending messages!
Let’s setup the flow in Node-RED. We’ll use the following MQTT nodes and the tried and true Inject and Debug nodes.
1. Setup the Inject node. We’ll use a simple “Test” topic and a payload of just a timestamp for the test.
2. Connect the Inject to an MQTT Out node. First, one intermediate step. We need to setup the MQTT broker configuration. This configuration can be used by any MQTT node. For now, we’re connecting on the default unsecured MQTT port of 1883 and on the local machine at 127.0.0.1 without a user login. You can also use the public broker from Mosquitto at test.mosquitto.org on port 1883 if you did not install Mosquitto earlier.
3. Back to the MQTT Out node settings. We’ll leave the Topic blank because we specified msg.topic in our Inject node. This lets you use one MQTT out node and dynamically send a variety of messages through it with varying payloads and topics. Then, select the broker address you just added for your server connection.
4. Ok, if you deploy your setup, you should see a green connected box! You’re ready to publish MQTT data with your new client!
5. Insert a MQTT In node with a Debug node. Before we publish data, let’s setup a subscription so we can verify the data actually went somewhere. We’ll tie into the same server we configured earlier and use the default topic of # to subscribe to all messages on the broker.
6. Inject and view your data.
That’s it! You’re successfully publishing data and subscribing with your Node-RED client through the Mosquitto broker. This application is obviously not useful, but the skills learned here are useful for extending to a variety of IoT projects. I used MQTT to turn my Christmas lights on and off on a custom schedule with a smartplug so I could save time plugging in my lights and money on electricity. And it was all done with 3 nodes! That was also a simple example, but you can see how this can be handy for a variety of projects.