Configure Node-RED & Wemos D1 Mini

Disclosure: Some of the links in this post are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Table of Contents

I had this neat idea to control a mobile robot using Node-RED. Before creating the Node-RED dashboard for a robot, I wanted to create something simple as a proof of concept. And, in this article, I’m going to share with you how to configure Node-RED with the Wemos D1 Mini using MQTT.

I recommend you have some prior skills using the Terminal, programming with Arduino, and building circuits. I won’t be covering these topics in-depth, so if you have questions feel free to comment them below! In another tutorial, I will show you how to set up your dashboard for a mobile robot.

Don’t forget to subscribe to receive updates when that tutorial launches.

[et_bloom_inline optin_id=optin_12]

How to configure Node-RED and Wemos D1 Mini using MQTT

There are a lot of moving pieces to get everything to communicate properly. Essentially, we’re going to wire up a circuit to the Wemos D1 Mini. Then, we will connect the Wemos to our router and send information to our computer using MQTT. The computer will also be connected to the same router and have both MQTT and Node-RED installed. Finally, we can create a Node-RED flow to capture and display sensor data in a dashboard. When we visit the dashboard in our browser, we’ll be able to see values from the Potentiometer (in real-time) and control all of our LED’s.

A picture is worth a thousand words, so here’s a quick overview of what we’re going to build.

Node-RED and Wemos D1 Mini diagram

When we’re done, we should have a prototype that looks something like this.

 

Technical Definitions before we start

I also want to define a couple of terms before we begin.

MQTT (Message Queuing Telemetry Transport) is a messaging protocol that simplifies the way we communicate (publish and subscribe) to messages between machines.

In this project, we will use an open-sourced MQTT broker, called Mosquitto. I will use MQTT and Mosquitto interchangeably throughout this tutorial.

Node-RED is a flow-based development tool commonly used for Internet of Things (IoT).

We’re also going to be subscribing and publishing to topics. You can think of a topic as a message. Topics contain information that we want to send or receive. Subscribing to a topic means we’ll receive the information in. Publishing to a topic means we’re sending data out. This is always in reference to the device that’s communicating. The topic name is pretty important because it contains the data from our device. Therefore, I will be italicizing all topic names to make them easier to find.

Controller and Circuit Components

This tutorial is pretty simple, so you won’t need many components to get started. Here’s the list of materials in case you don’t have them already.

One of the biggest things I can share with you, is that you don’t need a Raspberry Pi to complete this project. I’m using my computer to run MQTT and Node-RED. If you want to use the Raspberry Pi to host your communications, that’s fine as well, but it’s not necessary.

Now that we have a general understanding of the system, I’m going to split the tutorial into three parts.

1. Configure your computer with Node-RED and MQTT

2. Wire up the Wemos D1 mini Circuit

3. Create a Node-RED dashboard

Part 1: Setup your computer with Node-RED and MQTT

I’m going to use my MacBook Pro for this, but you could also get setup running a Raspberry Pi. I haven’t tested this on Windows, but I imagine the process is similar once you have Node-RED and Mosquitto installed.

First, install Node-RED for your operating system.

The instructions for installing Node-RED on Linux/Mac OS are here. The instructions for Node-RED on Windows can be found here.

Then install Mosquitto (MQTT broker)

Here’s a link to the Mosquitto download page. Once you have mosquitto, you can launch and interact with MQTT using these terminal commands. Write them down or copy/paste them somewhere, because they’re really helpful!

Start Mosquitto from Terminal

> brew services start mosquitto

Stop Mosquitto from Terminal

> brew services stop mosquitto

Subscribe to topic/hello

> mosquitto_sub -t topic/hello

Publish “your message” to topic/hello

> mosquito_pub -t topic/hello -m “your message”

You can test to see if mosquitto is working properly by opening up two different terminal windows and subscribing to a topic in one and publishing to the same topic in another. You should have 3 separate terminal windows open.

This is what it should look like:

publish and subscribe with MQTT

Launch Node-RED & Create a Flow

Now, let’s open up Node-RED. Open up a terminal and type

> node-red

Node-RED will launch typically on https://127.0.0.1:1880 Open up this address in your browser. I recommend using Chrome. If you’ve never used Node-RED before, welcome! Watch this quick little getting started video before continuing.



Subscribe on YouTube

While this is a video using an industrial controller that has Node-RED, it does provide a good overview of how to create a flow and get started within the software.

You can also read the Getting Started guide to create your first flow. While this may seem foreign at first, once you create a few flows, it’ll start to make sense.

Verify Node-RED can Receive and Send MQTT messages

Subscribe to a Message using Mosquitto

Now that we have Node-RED running, we’re going to subscribe to a message.This is eventually how we’ll receive messages from the Wemos D1 Mini.

First select the MQTT and a debug node from the menu on the left. Connect them together with a wire. Then, open up the MQTT block and type “localhost” as the hostname. Next, Enter topic/hello as the topic name. This is what your node should look like.

MQTT properties

You’ll need to add a Server. Click on the pencil icon and use the screenshot below to fill out the settings.

Node-RED MQTT server settings

Connect the right side of the MQTT block to the left side of the debug block.

Finally, deploy the flow by clicking the Deploy button in the top right.

Now open up the debug window. Also, open up a terminal window and publish a message to the same we defined above (topic/hello).
> mosquitto_pub -t topic/hello -m "hello world"
When you press enter, you should see this message transfer over to your debug window.

Publish a Message using Mosquitto

Let’s publish a message to ensure we have bidirectional communication.

Bring an Inject Node and an MQTT output node into your flow. Connect them together using a wire. Then open up the Inject node and change the payload to String. I’m going to send over the String, “hello world (node-red)” to show you that we’re sending data from Node-RED on the topic, topic/hello.

Next, open up the MQTT output node and make sure your server is set to localhost:1883 and the topic name is topic/hello.

Finally, click the deploy button in the top right corner. Inject the input node, and you should see “hello world (node-red)” in your debug tab.

If this happens, you’re ready to move to the next step! We have successfully connected mosquitto to Node-RED!

If not, go ahead and try the steps above again until it works!

Part 2: Wire & Program your Controller

I’m using a Wemos D1 Mini for this. You could also use a NodeMCU or any other WiFi-enabled controller.

Here’s the circuit diagram.

Wemos D1 Mini wiring diagram

We’re keeping it really simple with 4 LEDs and a Potentiometer.

Once your circuit is wired up, you’ll need to write some code to Subscribe and Publish sensor data to Mosquitto. We’ll use the PubSubClient Library for this.

PubSubClient Library using Arduino IDE

If you’re using the Arduino IDE, you can install this library by going to Sketch > Include Library > Manage Libraries. Type in “PubSubClient” in the search menu. Choose the library by Nick O’Leary. Click Install. You may have to restart Arduino if prompted.

configure Node-RED and Wemos D1 Mini using MQTT

PubSubClient Library using Platformio

On Atom.io/Platformio, go to the Platformio Home. Then click Libraries on the left. Search for PubSubClient. Click the Install button and restart Atom.

configure Node-RED and Wemos D1 Mini using MQTT

Program the Wemos D1 Mini

Set up your program like you typically would control the LED’s and potentiometer. Define variables for your pins, configure the setup() and loop() methods. Verify that your LED’s and potentiometer are working before continuing.

Now, let’s write a quick sketch to turn on the red LED when a ‘1’ is received and off when a ‘0’ is received.

We’ll use this modified template originally created by the Bald Engineer as a starting point.

Then, we’ll add additional functionally to control all 4 of our LED’s. You can test this out by opening up a Terminal window and sending a publishing command.

For example, to turn the red LED on, use this command:

> mosquitto_pub -t ledStatus/red -m "1"

To turn off the red LED, use this command:

> mosquitto_pub -t ledStatus/red -m "0"

If everything is configured properly, you should see the red LED turn on and off, respectively. Repeat this process for the remaining 3 LED’s. The code template should provide enough of a start to get all of your LED’s working. But if you need some inspiration, click here for a hint.

Publish Potentiometer Data to an MQTT Topic

Lastly, we’ll need to publish data from the potentiometer to another topic. I’m going to call this topic sensor/potentiometer.

To do this, we’ll modify our loop method. I’m going to send a new reading every 20 seconds.

Refer to the following code:

Part 3: Create a Dashboard in Node-RED

You’ll need to install the Dashboard library using NPM. Use the README page on the GitHub repository to get this configured.

Once installed, you’ll have access to all of the nodes used to make dashboard widgets.

For this project, we’ll use 4 switches to publish messages to each LED topic. This will allow us to click the button on the dashboard and turn the LED on and off. We’ll also periodically send data from the potentiometer to the dashboard to display on a gauge.

Configure a Switch Node to Control an LED

Drag a switch node and an MQTT node into the flow. Connect them together with a wire.

Next, open up the switch node (double click on it). Fill out the properties similar to the following screenshot. Make sure your “On Payload” is 1 and your “Off Payload” is 0. The topic name should match the name of the topic your LED is subscribed to. For the red LED, I used the topic name ledStatus/red.

configure Node-RED and Wemos D1 Mini using MQTT

Click Done. Then deploy this flow. Open up the Dashboard by clicking the box with the arrow in the dashboard tab.

Click the LED switch on your dashboard. You should see the LED turn on and off if it’s configured properly!

Add a Gauge to show Potentiometer Readings

We will show the potentiometer readings in our dashboard using a gauge widget.

Drag over an MQTT node and a gauge node. Connect them together with a wire. Next, open up the. MQTT node and verify that the server is localhost:1883 and the topic is sensor/potentiometer. Make sure you use the same name from the Wemos code.

Open up the gauge node and copy the settings shown below. This will change how the gauge appears on the dashboard. Feel free to use your own version of these settings to better match your data.

configure Node-RED and Wemos D1 Mini using MQTT

Once you’ve added in the gauge, deploy the flow and open up your dashboard. You should see something like this.

configure Node-RED and Wemos D1 Mini using MQTT

Interact with the switches and the potentiometer. Verify that you can turn the LED’s on and off. Make sure you’re getting readings from the potentiometer on your dashboard.

You can add additional widgets and functionality as you feel, but this will be a good starting point as we work towards controlling a mobile robot with Node-RED.

Wrap Up

We covered a lot of topics in this tutorial. Everything from configuring an MQTT broker, to using Node-RED to create dashboards, to wiring up a circuit and writing code for our Wemos board.

I know this tutorial was rather lengthy, but I wanted to provide some fundamental information before moving on to more complicated applications. In the next tutorial, we’re going to wire up a DC motor and control it using Node-RED. This will get us one step closer to controlling our Mobile Robot!

If you have any questions, feel free to post them below! Node-RED has a lot of features, so it’d be good to explore different ways of collecting, publishing, and processing data.

Try this project?

Share your experience in the comments section below! If you have any questions, I’m here to help!

Related Articles

6 Responses

  1. Hi Liz, great tutorial and very well illustrated, it’s easy to follow and learn.
    Before I found this website
    I had already concocted a simple node-red project with a raspberry pi and some sensors but not using MQTT as it’s just for local tests.
    My question is related to persistent states, i.e. how could I recall every time I start node-red, that any switch/led was on or off in the previous session? I have not found a good solution yet only using node-red tools (guess something like “context data” json files but didn’t get it to work as expected). Maybe using some small database to store/load those states? Maybe MQTT could deal with it?

    Thank you and greetings from Spain!

    1. Blogless, thank you for your kind words!

      To answer your question, there are probably a number of ways you could do this, but here are a couple thoughts that initially come to mind:

      1. As you mentioned – Store previous iterations of important variables into a database, then read from the database on startup
      2. More classically, I’ve only seen Node-RED used in a ‘continuously running’ state – i.e. once your system is operational, Node-RED stays running and isn’t terminated intentionally
      3. Use a “Change” node to set the value of data into “global variables” – I’ve played with this briefly, so I haven’t done enough testing to know if these vars are overwritten on initialization. I tend to think they’re not overwritten unless they’re set by a sensor reading.
      4. Use a Function node and set your own variables within a script. I think Node-RED supports Python and Javascript. Their Template node also looks interesting.

      Hope this helps! Please keep me posted on how your project goes!

    1. Alfredo,

      Looks like your #include statements are empty. I recommend going back to the article and reading through it again. The best way to test your code is to run it on the board. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Invest in Your Future Tech Career & Get Matched with an Expert Robotics Mentor

Connect with your Advisor and select the right Learn Robotics Program to boost your Tech Career

Wait,

Learn Robotics Online
— Get 2 months free!

Exclusive Limited Offer for Serious Beginners Ready to take their Hobby to Engineering Internships, $100k+ Careers, and Beyond!

Enroll now, and earn your first robotics certificate in the next 7 days.

👇 Click below to claim this deal.