Reader small image

You're reading from  Raspberry Pi and MQTT Essentials

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803244488
Edition1st Edition
Right arrow
Author (1)
Dhairya Parikh
Dhairya Parikh
author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh

Right arrow

Node-RED on Raspberry Pi

This chapter will get you acquainted with very popular software for the Raspberry PiNode-RED. It is browser-based low-code programming software that allows beginners to create APIs and control the Pi hardware by creating flows, a connected component created by wiring several nodes together to perform a specific task. This chapter has four main sections:

  • Introduction to Node-RED
  • Node-RED first-time installation, setup, and demonstration
  • Node-RED MQTT components and dashboard setup
  • Mini project 2 – controlling a NodeMCU LED from the Node-RED dashboard

So, let's start with a basic introduction to what exactly Node-RED is.

Introduction to Node-RED

The website of Node-RED (https://nodered.org/) gives a perfect introduction to the purpose of using the software:

Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.

It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single click.

Hence, we can say that Node-RED is a UI-based programming tool that can be used to create various applications, which include hardware device control (Raspberry Pi GPIO access is also available), flow-based API development, and so on. We can run multiple flows at once and each of them will run independently.

Moreover, Node-RED allows the easy setup of several additional services, which would otherwise require a lot of work and experience. One such example is setting up a SQL database to store all the data arriving in Node-RED through any communication...

Node-RED first-time installation, setup, and demonstration

This section will cover in detail how to install and set up Node-RED on the Raspberry Pi. After that, two simple demonstration applications will be covered, which will show us the power of Node-RED and how it can be utilized to its full extent.

Node-RED installation

Installing and setting up Node-RED on your Raspberry Pi is a straightforward process. Please note that this section assumes that you have already set up your Raspberry Pi with the latest version of the Raspberry Pi OS with all the initial configurations. If that is not the case, please refer to Chapter 1, Introduction to Raspberry Pi and MQTT, for that.

First, access your Raspberry Pi as we will require the terminal window at the very least to install Node-RED on it. There are two ways to access your Pi:

  • Through the desktop interface: It requires a monitor, keyboard, and mouse connected to the Pi.
  • Through Secure Shell Protocol (SSH): SSH has...

Node-RED MQTT components and dashboard setup

This section will start with an introduction to the dashboard and MQTT functionalities of Node-RED. That will be followed by a simple project in which we will connect an LED to a NodeMCU development board and demonstrate control capabilities through the Node-RED dashboard. So, let’s get started.

Node-RED MQTT nodes

The MQTT communication protocol was discussed in detail in the previous chapters. In this section, we will be seeing how can we use MQTT nodes in Node-RED. In Node-RED, you can find two MQTT nodes in the network section:

  • The mqtt in node
  • The mqtt out node

Please refer to Figure 4.13 to see where you would find the MQTT nodes in the node palette:

Figure 4.13 – MQTT nodes under the network section in the node palette

As the names suggest, the mqtt in node is to capture data coming from various topics (basically for monitoring) and the mqtt out node is to publish data...

Mini project 2 – Controlling a NodeMCU LED from the Node-RED dashboard

The project’s aim is to control the onboard NodeMCU LED using a switch widget on our Node-RED dashboard, where we will be using MQTT as our primary communication protocol. We will proceed step by step, covering the hardware requirements, software setup, code explanation, and project demonstration.

Let's first start with the hardware requirements for this project.

Hardware requirements

This is a very simple project so the hardware requirements are quite straightforward:

  • A NodeMCU development board
  • A Raspberry Pi

Information about both these devices has been covered in detail in the previous chapters. In the later sections, device-specific instructions that need to be followed will be listed.

Software requirements

As for the software requirements, only the Raspberry Pi needs to be considered as the NodeMCU code explanation will come later.

The following should...

Summary

This chapter mainly focused on a single piece of software that we will be using a lot throughout this book – Node-RED. We started with an introduction to this software, followed by a tutorial on how to install/update it to the latest version on our Raspberry Pi. After that, we had a crash course on the basics of how to use it to its fullest extent. Next, we covered the MQTT components of Node-RED (the nodes specifically) and installed and got acquainted with the Node-RED dashboard. Finally, we wrapped up by creating a mini-project wherein we controlled the onboard NodeMCU board wirelessly through a simple dashboard hosted on the Raspberry Pi.

Now that we have covered all the building blocks for this book (Raspberry Pi, MQTT, and Node-RED), in the next chapter, we will be creating our first major project: a weather station based on NodeMCU. This project will have a far more complex and interactive dashboard and it will show us just how powerful and easy to use Node...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh