Getting Started with MQTT and Node-Red

Jon Stopple
February 17, 2024

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.

Install Node-RED

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.

Installation Options for Node-RED from nodered.org

It is an easy process to follow. On Windows it is two steps for the installation, and one step to begin running.

  1. Install Node.js
  2. Install Node-RED. Run the command from your command prompt.

npm install -g --unsafe-perm node-red

3. Run Node-RED. Run the command from your command prompt.

node-red

That’s it, you should see text similar to this once you’re running. Quick and simple.

Start of output after running Node-RED.

Next, navigate to http://127.0.0.1:1880 to see your flows.

Node-RED Flow.

That’s it, time to install Mosquitto locally and we’ll be ready to send and receive data!

Install Mosquitto

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.

  1. Go to the download page for Mosquitto. On Windows, you’ll download an .exe and run it to begin the installation.
  2. Once installed as a service, open up the directory where it was installed, typically C:\Program Files\mosquitto. You can also search your services for mosquitto and find the path to the executable if for some reason it is not in that location.
  3. Modify mosquitto.conf. This is the configuration for mosquitto that controls security settings, port access, and a range of other settings. By default, mosquitto.conf is a large file and contains mostly commented out lines. Just starting out locally, I’d recommend making sure the following two lines are not commented out.

listener 1883
allow_anonymous true

#This is a comment

4. Run the mosquitto service.

Test your MQTT skills!

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.

Node-RED MQTT nodes.

1. Setup the Inject node. We’ll use a simple “Test” topic and a payload of just a timestamp for the test.

Inject node settings.

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.

MQTT broker configuration.

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.

MQTT Out node configuration.

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!

Successfully connected MQTT 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.

MQTT client subscription.

MQTT In node configuration.

6. Inject and view your data.

Data published to our local MQTT broker via our Node-RED client.

Full setup with another subscription for the specific “Test” topic.

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.

Node-RED setup to control my Christmas lights automatically on a schedule via MQTT.