This is only a preview of the October 2021 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
IoT
Cricket
Khairul Alam – making the Internet of Things accessible for everyone
Part 2: Working with Node-RED and Raspberry Pi
I
n this issue, we will show how
you can easily start to build the first
parts of a local home automation
system. We will make a battery-powered
Wi-Fi sensor with an IoT Cricket module
and integrate it with Node-RED – without
writing code or programming. We will
use the popular low-price Raspberry
Pi (RPi) to host Node-RED in our local
network. If you don’t have an RPi, you
can use FRED (https://fred.sensetecnic.
com/) a cloud-based Node-RED server
without subscription costs.
Last time, in Part 1 (PE, June 2021) we
showed how to use Cricket to make an IoT
sensor node without any programming
and integrate it with ThingSpeak for
real-time visualisation of sensor data.
ThingSpeak is a powerful tool for showing
and analysing data, but for automation
systems we need a more generic core
system for data processing and integration
to other systems. This is where NodeRED comes into play.
Node-RED
The aim this month is to expand our
capabilities by setting up the chassis of
a local home automation system using
Node-RED, explaining how Node-RED
is used as we progress. We are going
to replicate what we achieved with
ThingSpeak in Part 1, which will set
us up for more advanced projects in the
future. This project can be realised by
makers at all skill levels. It may require
some basic soldering, but it doesn’t
require any coding or programming. By
the end of this project you’ll have a solid
understanding of how you can build your
own local system and extend it by adding
your own IoT end nodes.
Our system is going to be based on
the MQTT communication protocol,
which has been widely adopted in most
Md. Khairul Alam is an Assistant Professor
at the University of Asia Pacific, Dhaka,
Bangladesh. He is an independent Cricket
developer and does not work for Things
On Edge.
58
IoT sensor nodes and home automation
systems. We discussed MQTT protocol in
detail in Part 1, so please refer to the June
issue if you missed it. We will set up our
own local MQTT broker for this project
by installing an open-source Mosquitto
MQTT broker on our RPi. Using a local
broker gives us full control of the system
without relying on the Internet.
Prerequisites
You will need the following hardware:
RPi with Raspbian OS installed (we
used ver. 3 but others should work)
IoT Cricket Wi-Fi module
2 × AAA battery holder
2 × AAA batteries.
What is Node-RED?
Node-RED is a free and open-source
graphical programming tool, primarily
designed for the Internet of Things.
It allows programmers of any level to
interconnect physical things, cloud-based
systems, databases and APIs to build
applications using a web interface. It’s
great advantage is that it requires very
little, if any, programming knowledge.
With Node-RED we can easily design
our own automation system(s) by dragging
and dropping nodes and connecting them.
As the data flows through the system of
connected nodes it triggers and performs
actions defined by each node. One can
compare Node-RED to a water system
with various type of nodes, such as valves,
power generators, heaters/coolers, pumps,
tanks, outlets and so on. As the water flows
it activates system nodes to use/modify
(decision making) the water and generate
the desired output(s) of the system.
For Node-RED, the ‘water’ is your data.
As the data flows through the system, it
passes in sequence from one connected
node to another. Each node acts on the
data and performs some actions and
then passes it on. There are many types
of nodes available which, for example,
allow data to be:
Filtered
Visualised on custom dashboards
Stored in database/filesystem
Sent as notifications – eg, email or Twitter.
We also have many choices around
integrating the data flow with other
software and Internet services. Plus, a big
advantage of Node-RED is that we can set
up a system in our local network without
relying on Internet service providers.
Node-RED allows developers to
connect nodes (ie, predefined code
blocks) to perform a task. The connected
nodes – usually a combination of input
nodes, processing nodes and output
nodes – are ‘wired’ together to make
up an application program called a
‘flow’. There are lots of nodes available
to perform simple and complex tasks,
including web access, Twitter posting,
email, HTTP, UDP, MQTT, control of
GPIO ports, displaying a graphical
dashboard and more. Also, many builtin nodes allow you to install third-party
nodes, just like we install third-party
libraries in Arduino.
Note that although we will focus on
the graphical nature of Node-RED, it
allows you a lot of functional control
(similar to custom functions in a text
language) through JavaScript. JavaScript
is the programming language that is
underlying Node-RED.
That’s enough background to NodeRED. Now let’s learn how we can use it to
make our own system; and the best way
to learn is by doing it ourselves. There
are plenty of resources available on the
Internet (Google, of course, is your friend
here). However, this month’s project will
provide a basic introduction of how to
set it up on an RPi and integrate devices
built with the Cricket IoT Wi-Fi module
into your system.
Installing Node-RED on
Raspberry Pi
Before starting, I am assuming you have
access to an RPi with the Raspbian
OS installed and already have some
experience working with it. If you have
yet to install the OS then you can find
Practical Electronics | October | 2021
Now, you can open the Node-RED web editor by
going to the following URL in your favourite web
browser from any computer/phone connected to the
same network as the RPi:
http://{your_pi_ip-address}:1880
Fig.1. Installing ‘build-essential’ before installing Node-RED on the RPi.
Fig.2. Part of the terminal output when Node-RED starts in RPi.
detailed instructions on installing the operating system image
on RPi here: https://bit.ly/pe-oct21-iot1
You can find the most up-to-date official
instructions on installing Node-RED on RPi
here: https://bit.ly/pe-oct21-iot2
Note that if you downloaded and
installed the latest RPi OS then it’s
possible Node-RED is already preinstalled.
You can check this at the terminal with
the command:
node-red --help
Before we start installing, it’s good
practice first to update the system
components using the following terminal
commands:
sudo apt-get update
sudo apt-get upgrade
To obtain the IP address of your RPi use this command:
hostname -I
If everything works correctly, you should now be greeted by
the screen shown in Fig.3.
It would be convenient to have Node-RED running permanently
as a service in the background so that whenever we start our
RPi the Node-RED system automatically launches at boot time.
All we need to do this is execute the following command:
node-red-start
The above command starts the Node-RED service and displays
its log output. Pressing Ctrl-C or closing the window does
not stop the service; it keeps running in the background. Now
enable the service to autostart by running the command:
sudo s0ystemctl enable nodered.service
Then, run the following command to install
‘build-essential’:
sudo apt install build-essential
Fig.3. View of the Node-RED workspace.
Note that you may get the result shown in
That’s it. Now, whenever we boot RPi the Node-RED service
Fig.1 if it’s already the newest version. After that, install Nodewill automatically start.
RED by running the following command in the terminal. This
command will download and run a script provided by Node-RED
authority to install Node.js, npm and Node-RED onto an RPi.
Getting started with Node-RED
bash <(curl -sL https://raw.githubusercontent.
Now that we have Node-RED up and running
com/node-red/linux-installers/master/deb/
on our RPi, let’s start with a very simple
update-nodejs-and-nodered)
example before going on to our main project.
As we’ve already discussed, a program
The ‘npm’ stand for ‘node package manager’ and is useful for
created in Node-RED is called a ‘flow’,
the JavaScript programming language that Node-RED is built
and it’s created by wiring multiple nodes
on. npm will enable you to install additional modules for use
together. There are three main types of nodes:
with Node-RED. Before the script begins to run you will be
1. Input nodes (eg, inject)
Fig.4. The three
asked two questions. Just type ‘y’ and press ENTER for both
2. Processing nodes (eg, function)
basic nodes.
to continue.
3. Output nodes (eg, debug).
After installing Node-RED you need to start it. There are
different ways to do this, but the easiest is to run the following
Three example nodes are shown in Fig.4. Input nodes allow
command, which will produce the terminal output shown in
you to input data into a Node-RED program or flow. They have
Fig.2: node-red
at least one output endpoint represented by a small grey square
This works fine on an RPi with at least 2GB of RAM. For
on their right side. Output nodes are used to send data outside
earlier RPi models with limited RAM, it’s better to specify the
and have a single endpoint on their right side. Processing nodes
amount of RAM, as follows:
allow us to process data. They have an input endpoint and one
node-red-pi --max-old-space-size=256
or more output endpoints. In a Node-RED flow, messages pass
between nodes, moving from input nodes, through processing
Regardless of which method you choose, the Node-RED service
nodes to output nodes.
will begin within a few seconds, and indicate that it is running
Let’s build a ‘Hello World’ type of flow to see how simple it is
on ‘localhost’ with the default port 1880.
to use the Node-RED UI to build and deploy (run) flows. (Note
Practical Electronics | October | 2021
59
Fig.5. Three parts of Node-RED environment.
Fig.6. The Hello
World flow.
that in this case we won’t actually print out the words ‘Hello World’ – we
will say ‘hello’ by printing a timestamp.) To do this we will use an inject
node to input a timestamp into the flow, which is consecutively wired to a
debug node for printing out the timestamp on the screen.
Since this is our first flow build, we’ll explain each step with a screenshot.
Our first node will be the inject node. This is used to generate input for
Fig.7. Deploying the flow.
our flow. Drag and drop an inject node (from
the node palette on the left) into the flow
workspace (centre), and then look at the
info tab (on the right) where you’ll see the
attributes of the node. Fig.5 shows the node
palette, workspace with added inject node
and debug palette. In the workspace inject
node you can see a timestamp because
by default a timestamp input is set –
the current time in milliseconds since 1
January 1970.
You will see a small grey square on the
right side of the node where we attach a
wire that routes an output message to the
next node in the flow. To sense the output
from the inject node let’s add a debug node
by dragging one from the node palette to
the workspace. There are now two nodes in the
workspace and we need to wire these together. To
do that, click on the grey output point for the inject
node, holding the mouse button down, drag towards
the debug node. An orange wire appears, which
you can then attach to the grey input point on the
debug node. The complete flow will look like Fig.6.
After building the ‘Hello World’ flow we need to
‘deploy’ it to see the output. Click the Deploy button
(top right) and you will see a pop-up saying the flow
has been successfully deployed (see Fig.7).
Now click on the left tab on the inject node and
you will see the timestamp (a long number) appear
in the debug pane on the right-hand side (see Fig.8).
Congratulation, you created and deployed your first
flow, and we are ready to move on to the next step.
Our goal is to receive the temperature and battery
level from the Cricket using MQTT communication
protocol and display the data on a graphical web
dashboard. Node-RED will be used to receive MQTT
data, make a graphical dashboard and display the
result in real time. Let’s take this in stages.
MQTT in brief
Fig.8. Output of the flow.
MQTT is a lightweight messaging protocol designed
for resource-constrained devices with low bandwidth.
MQTT consists of a message broker and a number
of clients. The broker is a server that receives all
messages from the clients and then routes those
messages to relevant destination clients. A client is
anything that can interact with the broker to send
and receive messages using a specific topic. Every
MQTT broker (local or cloud) has a host address
and a port number, which is 1883 for non-encrypted
communication. Clients connected to the same broker
can communicate with each other. To know more
about MQTT please refer to page 44 of the June issue.
In this project we are going to set up our own
local MQTT broker on our RPi running Node-RED.
Installing an MQTT broker on Raspberry Pi
First, with Node-RED running, we will install the
open-source Mosquitto MQTT broker on our RPi.
Mosquitto is lightweight and is suitable for use on
all devices from low-power, single-board computers
to full servers. To Install Mosquitto broker, open a
terminal and type the following command:
sudo apt-get install mosquitto -y
Fig.9. Editing the RPi config file.
60
To configure Mosquitto broker open the config file
using a text editor (eg, nano or vi):
Practical Electronics | October | 2021
Finally, restart RPi to
apply changes:
sudo reboot
That’s it! Our MQTT
broker is up and
running now. You can
test the installation
with the command:
mosquitto -v
Fig.10. Configuration of the Cricket.
sudo nano /etc/mosquitto/
mosquitto.conf
Add the following lines at the top and save:
port 1883
allow_anonymous true
Fig.9 should make this clear. Now enable
the broker and allow it to autostart on
boot up using the following command:
sudo systemctl enable mosquitto.
service
Fig.11. mqtt in node topic configuration.
Practical Electronics | October | 2021
This will return the
Mosquitto version that
is running on your RPi.
For more information
on how to install
M o s q u i t t o M Q T T Fig.12. Configuring mqtt server.
broker on an RPi, visit:
https://bit.ly/pe-oct21-iot3
In order to configure Cricket we need
Soon, we will start building a flow in
to attach the batteries and press the builtNode-RED to receive MQTT messages
in button for five seconds until the LED
using an MQTT node. But before that
starts flashing fast. Then connect to the
we need to configure a Cricket to send
‘toe_device’ hotspot from any device with
data over MQTT to our system.
web browser capabilities. Set your Wi-Fi
credential and note down the SN (serial)
from the INFO panel. The setting should
Configuring Cricket to send
be as shown in Fig.10. (Note: you need
sensor data via MQTT
to adjust the IP address in the ‘url’ box
The Cricket IoT module transmits the
to match your RPi.)
temperature and battery voltage level to
We set the Cricket to wake up every 30
the /SERIAL/temp and /SERIAL/batt
seconds because that is a good interval
topics respectively, where SERIAL is
for experimenting with data and setting
the unique serial number of the Cricket
up this project. However, once you are
module. Before starting the flow, let’s find
done with this project, please change it
the serial number of the Cricket module
to an interval that makes more sense, for
and configure it to transmit temperature
example, once an hour or more. Your
(using the built-in temperature sensor) and
batteries will thank you!
battery level through MQTT. (We won’t
Now you can exit from the configuration
discuss the whole configuration process
mode and the device is ready. Cricket is
here. For more configuration details refer
sending data to our local MQTT broker
to Part 1 in the June 2021 issue.)
every 30 seconds, so next we will make
Cricket comes with a built-in
a flow to receive the data from the broker
temperature sensor (or, if you want, you
and show it in a graphical dashboard.
can add any other external analogue sensor
to its IO2 pin). We need to configure it to
send this temperature value via MQTT.
Receiving an MQTT message
We’ll configure it to send the temperature
in Node-RED
every 30 seconds to our system. Since it’s
To receive an MQTT message, drag and
running on battery power we can place
drop an ‘mqtt in’ node into the workspace.
and monitor the temperature anywhere
Then double click to configure it for Cricket
at home or even outside in the garden.
and our local MQTT broker. Enter your
Fig.13. mqtt server and topic added.
61
Fig.16. Installing node-red-dashboard.
Fig.14. MQTT messages received in Node-RED.
Go to the install tab, type for node-red-dashboard and
click the install button, as shown in Fig.16. Close the window
when done.
Now we are ready to create a dashboard. We already know
our device and the topics it is sending out. Therefore, we can
precisely set the topics for the data that interest us. In our
case, the device is reporting temperature and battery level in
the following topics:
/YOUR_CRICKET_SERIAL_NUMBER/temp
/YOUR_CRICKET_SERIAL_NUMBER/batt
Therefore, your topic should look something like this:
/887C3E0039/temp
Now let’s create the whole diagram from scratch. In the
workspace, add:
Two ‘mqtt in’ nodes and assign the above topics
A chart node (for temperature) from the dashboard palette
and connect it with the temp mqtt input
A ‘gauge’ node (for battery) and connect it with the batt
mqtt input
A ‘function’ node between the batt mqtt input and the gauge
node to calculate the percentage power of the battery
Fig.15. Opening Node-RED Manage palette.
topic in the topic field (eg, /SERIAL/# for receiving all
messages from Cricket; /SERIAL/temp for temperature; and
/SERIAL/batt for battery voltage). I set the topic to receive
all the messages and thus observe all the MQTT data sent
by Cricket (see Fig.11).
We also need to tell Node-RED to connect to our local
MQTT broker from where the messages are expected to
be coming. To do this we need to add mqtt-broker by
clicking on the pen icon. A new window will open as
shown in Fig.12. In the connection tab enter the server
name (eg, the IP address of the RPi). Click Add and then
Done. See Fig.13.
To watch the received message from the mqtt broker we
also need a debug node. Add a debug node and wire it
with the mqtt in node. Now deploy the flow and you will
receive the MQTT messages from Cricket every 30 seconds,
as shown in Fig.14.
You need to assign a group for the chart and gauge nodes. Double
click on the nodes and assign ‘Home’ group with ‘Reading
Room’ Tab for both nodes. Optionally you can set label for the
chart and gauge nodes. See Fig.17 for details.
Creating a Node-RED dashboard
It would be useful to have a nice user interface (UI) instead
of just a raw data print out. The good news is Node-RED
enables us to build a good UI, which we will use to build
a basic dashboard for our device to plot temperature data
and show the battery status.
First, we need to install one extra component called ‘nodered-dashboard’ for Node-RED. To do that, click on the account
icon from top right and select ‘Manage palette’ – see Fig.15.
62
Fig.17. Editing chart node.
Practical Electronics | October | 2021
Fig.18. Complete Node-RED flow for receiving
sensor data and visualising in graphical dashboard.
The complete flow will look
like Fig.18. Now we are ready to
deploy and open our dashboard
by using the same link address as
we used to open the Node-RED
editor. We only need to add a /ui
suffix at the end of the address. In
our case the link looks as follow:
http://192.168.1.103:1880/ui
After going to the address, you
will find the elegant dashboard
shown in Fig.19.
Summary
Cricket reports a battery voltage with a
number from 0-255. We will convert the
value to a percentage using the function
node. Double click the function node and
paste the following lines into the editor
and click done:
msg.payload = parseInt((msg.
payload / 255) * 100)
return msg;
We can now enjoy our own live dashboard
of our Wi-Fi automation system on any
computer or phone. Of course, this
was a very basic project – just putting
together a temperature sensor and the
basics of creating a simple dashboard.
Nevertheless, it’s laid the foundations
of what we’ll need to build on for much
more advanced systems in the future.
In this project
we have shown the
essential hardware
and software needed
to start building
low-power, energyefficient automation
systems. Node-RED
and Cricket enable
Fig.19. Dashboard created by Node-RED.
us to build advanced systems without
writing any code. This results in a rapid
deployment of powerful IoT solutions
and automation.
In upcoming articles we will add all
sorts of environmental and security
sensors and support systems – great
things to look forward to!
ESR Electronic Components Ltd
Fig.20. Complete system with Raspberry Pi, live Cricket and the
Node-RED dashboard on a smartphone web browser.
Cricket special offer!
PE / OKdo.com
We hope this and June’s Cricket articles have inspired
you to experiment with IoT and build your own lowcost, low-power Internet of Things projects.
All of our stock is RoHS compliant and CE
approved. Visit our well stocked shop for
all of your requirements or order on-line.
We can help and advise with your enquiry,
from design to construction.
3D Printing • Cable • CCTV • Connectors • Components •
Enclosures • Fans • Fuses • Hardware • Lamps • LED’s •
Leads • Loudspeakers • Panel Meters • PCB Production •
Power Supplies • Relays • Resistors • Semiconductors •
Soldering Irons • Switches • Test Equipment • Transformers
and so much more…
Monday to Friday 08:30 - 17.00, Saturday 08:30 - 15:30
We have agreed a fantastic deal with OKdo.com
– a unique global seller of SBC and IoT-related
components and systems. Until the end of October
you can receive a 20% discount on Cricket (while
stocks last, limit of two transactions per person),
plus free delivery on orders over £25!
To get your Cricket discount, just enter promo code
CRICKETPE when you check out at the OKdo.com
online shop:
www.okdo.com/p/things-on-edge-cricket-module
Practical Electronics | October | 2021
Station Road
Cullercoats
North Shields
Tyne & Wear
NE30 4PQ
Tel: 0191 2514363 sales<at>esr.co.uk www.esr.co.uk
63
![]() ![]() |