Build Prototype IoT System With Node-Red, MQTT, and NodeMCU.

Thushara Sampath
5 min readJun 29, 2021

--

In this blog, we build a complete IoT system using NodeMCU, Node-red, MQTT.

To demonstrate the ecosystem I’m going to use a simple IoT system that has capable of send sensor data and visualize in an online dashboard. Also, let’s add a LED on NodeMCU that can control from anywhere in the world.

Prerequisites 🔌

Hardware : NodeMCU / USB cable

Software : Arduino IDE/ Web Browser (😂)

You can buy hardware from any online store such as aliexpress, eBay, etc. And download Arduino IDE from here.

Step 1: Configure Arduino IDE to program NodeMCU 🛠

go through the following steps. (You need to have an Internet connection.)

  1. Install Arduino IDE’s recent version get it from Arduino website.
  2. Start Arduino and open the Preferences window. (File>Preferences )
  3. Enter https://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  4. Open Boards Manager from Tools > Board>Boards Manager menu and find esp8266 platform.
  5. Search for ESP8266 and Select the version you need from a drop-down box. (Can select the latest version)
  6. Click the install button.

Step 2 : Test whether everything is configured. 💡

  1. Open Arduino IDE, Go to Tools > Board >ESP8266 Boards, and select your board in my case it’s NodeMCU 1.0.
  2. Select Port from Tools > Ports. In my case it’s COM8. This port is like a USB port to your board is connected.

3. Go to File>Examples>Basics>Blink. Then a new window will appear with some codes. Upload that to your board. if everything is configured correctly after uploading LED on the board will blink continuously. 🎉🥳 yee!!

Step 3 : Connect NodeMCU to MQTT 🎆

MQTT is a publication and Subscribe protocol that is used widely in IoT applications. Visit MQTT — The Standard for IoT Messaging for more information. For MQTT communication we need a broker service. my preferred broker is Eclipse Mosquitto.

  1. We have to install an additional third-party library. For that go Tools>Manage Libraries… search for PubSubClient and install it.

2. Open a new Arduino file and add the following codes. Code is explained in the comments. Here we followings,

  • Connect NodeMCU to Wi-Fi.
  • Connect NodeMCU to MQTT broker.
  • Subscribe to the “LED” topic. When the message equal to 1, turn on the LED and when the message is equal to 0, turn off the LED.

here we use the LED on the NodeMCU board itself.

  • Read Analog Pin (A0) values (so that we can mimic a sensor inputs😁, just touch the A0 pin with your finger, readings will change).
  • Publish Analog Pin readings to the “LDR” topic once every 2 seconds.

Step 4 : Setup Node-Red 🖇

  1. go to FRED: Front End for Node-RED (sensetecnic.com) and sign up for a free account.
  2. Log in to your Node-Fred account and start an instance. (Read official documentation if you structured FRED Docs: Intro (sensetecnic.com)).
If everything okay, you should be able to this kind of screen

3. press copy following JSON code, go to Node-Fred then press crtl+i, paste copied JSON there, and press import.

Brief Explanation of Nodes I used,

  • MQTT in: This node subscribes to a topic. In our case is subscribe to “LDR”.
  • Chart: This node collects passed payload data. In our case A0 pin readings. and plot them in a chart in the dashboard.
  • Switch: This node adds a toggle switch to the dashboard. we can configure the payload. In our case, I configure it to pass string 0 when turning off and pass string 1 when turning on.
  • MQTT out: this node publishes to an MQTT topic. In our case, the topic is “LED” and we send 0 or 1 as the message.

you can use debug node to check outputs of other nodes. I recommend reading about The Core Nodes : Node-RED (nodered.org).

4. Deploy the flow by clicking the Deploy button. ( it is at the top right corner in the Node-Fred window.
5. Then go to the dashboard. (do the following steps, the dashboard will open in a new window.)

6. Then your dashboard should be looked like this. 🤩🤩(Please note that your NodeMCU should be turned on and with the above MQTT code.)

Node-Red Dashboard

In this dashboard,

  • You can turn on/off LED in your NodeMCU by toggling the switch.
  • This chart shows readings of the analog pin of your NodeMCU. Touch you will be able to see value variation in the chart.

Conclusion

  • We use MQTT as the communication protocol for our IoT system. With MQTT we can send data from NodeMCU to NodeRed and vice versa.
  • MQTT brokers act as middle man.
  • This is a really basic system but you can extend this idea to build a cool and useful IoT system.
  • As example,

you can connect a light to the NodeMCU and Connect android app via MQTT(there are a lot of apps in stores). Then you can turn on/off light (water motor/Fan/heater etc..) from anywhere in the world.

You can connect the humidity sensor and water motor with NodeMCU, check your greenhouse humidity, and turn on/off the watering using Smart Phone.

  • I hope to continue this tutorial future 😊
  • So, Have Fun😋😋

--

--

Thushara Sampath

Undergraduate | Electronic and Telecommunication Engineering at University of Moratuwa , Sri Lanka