You are on page 1of 47

Workshop

Workshop
Author: Renata Frenken
1. Starting Up
• Short description Node-RED
• “Hello World” example
• Installation instructions
• Installation instructions for libraries necessary
for the next parts of the workshop
Node-RED in Short
• developed for fast prototyping in IoT
• used also for wiring hardware, APIs and
services (web services, cloud, …)
• based on node.js
• programmed visually using flows
Node-RED Examples
• Data scraping
• Twitter Control
• Dashboards
• GUI’s
• Domotica
• Database Integration
• Web Sockets
• And Many Others
Example Project Fontys Students
(This project uses Node-RED for MQTT and GUI)
Install Node-RED
• Follow the official installation procedure on
Node-RED web. Before you install Node-RED,
you’ll have to install node.js too:
https://nodered.org/docs/getting-started/local#i
nstalling-with-npm

• Currently you can install it on Windows, Mac,


Linux, Raspberry PI or via docker.
How does Node-RED work?
• The visual blocks are communicating by sending messages
• Each message has an id and a payload, for more
explanation see:
https://nodered.org/docs/user-guide/messages
• Most of the blocks have input and output wires that
connect them together, some of them have only input or
only output.
• By clicking on the block you can see what you can do with it
or how you can configure it
• The blocks are represented as JSON texts
tabs
block library

comments

wires

blocks

flows
Useful plugins for installation
• Node-RED has a lot of nodes but as the online
support is really big, there are a lot of libraries
available too. By installing them you can get
extra functionality
• For the coming examples you should install at
least the libraries from the following page. You
can do that
by choosing “Manage Palette”
from the menu.
Mosquitto MQTT Broker

Dashboard
“Hello world” in Node-RED
Once you install Node-RED, you can run your first flow. Do the following steps:
1. Run Node-RED and connect to it via your browser by typing: localhost:1880
in your URL
2. Import the following flow (text) by going to menu->import->clipboard, see
the next picture:

[{"id":"ce74c126.c2771","type":"inject","z":"42d0aeef.c5aeb","name":"","topi
c":"Hello
World","payload":"","payloadType":"date","repeat":"","crontab":"","once":fals
e,"onceDelay":0.1,"x":200,"y":280,"wires":[["eadd0167.d32c4"]]},
{"id":"eadd0167.d32c4","type":"debug","z":"42d0aeef.c5aeb","name":"","acti
ve":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x
":540,"y":300,"wires":[]}]
Now you should see your first flow:
Input trigger block, click here to start the flow

blue stip means not deployed yet, see next sheet output block

wire that connects the blocks and creates flow


3. Try this flow by clicking on the input trigger
block and looking at the debug output.
4. You enable debug output here:
enabled debug output

disabled debug output

5. Anytime you change something, deploy!


Output of “Hello world”

Timestap filled in in the inpur trigger block


EXPLORE On Your Own
• Explore the NODE-RED blocks used in the
“Hello World” Example
• Try to change the input trigger in such a way,
that it will output any text you like (instead of
timestamp) and this output is generated every
3 seconds
• Try to Enable/Disable Output
2. MQTT
• MQTT protocol
• MQTT in Node-RED
• Installation instructions
• Installation instructions for libraries necessary
for the next parts of the workshop
MQTT
• Publish/Subscribe Client/Server Protocol
• Lightweight, simple, easy to implement
• Small code and network bandwith footprint
• Widely used
MQTT
MQTT example
topic MQTT Server Publ
re ish:
tu Tsen
era (Broker) sor/
1
mp /18
/Te
fig
on
r/C

Pu
o
iat

bl
ish
a d Temperature

Publish: Radiator/Config/21
R 1

:T
: 2
be g/ Sensor

se
ri fi

ns
c n
bs o

o
Su r/C

Pub

r/
o

2
t
dia

/1
lish
a

7
:R
Building sh

: Ts
bli
Pu

e ns
Automation

o r/
Server

3/2
Temperature

0
Sensor

Temperature
Sensor

Radiator
MQTT Topics
• Messages on MQTT are published on topics
• Topics don’t need to be configured, they have
to be published
• Topics are organized in hierarchical structure
• Example:

Radiator/Config/Temperature
MQTT in Node-RED
• MQTT is standard part of
Node-RED distribution
• There are 2 standard MQTT
nodes, 1 input and one output
node

The way to filter nodes


MQTT Broker
• There are plenty MQTT Brokers you can
connect to
• We are going to show AEDES (local MQTT
broker) and use free mosquitto test broker
AEDES MQTT Broker Example
Mosquitto Test MQTT Broker
• There are plenty MQTT Brokers you can connect to
• We are going to use free mosquitto test broker : test.mosquitto.org
Communication In Our Workshop
Student NR top
MQTT Client ic:
n r_ w
or k Node-RED Dashboard
sh in IBM Cloud

o p/g
ree
tin
gs

Student NR topic:nr
_worksh
op /greetin
gs
MQTT Client
MQTT Broker
test.mosquitto.org
te
Student’s s h o p /clima
rk
nr_wo te
HW MQTT Client topic: l i ma
c
(e.g.ESP32) o p/
h
orks
w
r_
… : n
p ic
to

Student’s
HW MQTT Client
(e.g.ESP32)
Try It out !
• Configure your MQTT block so that it can communicate with
“test.mosquitto.org” MQTT broker (we will use unsecure
connection and port 1883)
• Use inject block to define nr_workshop/greetings topic and a
string payload (string should be about 20 characters long, so
that it can fit in our dashboard)
Try it Out (part 2)
Go to the Node-RED dashboard in the IBM Watson Cloud :
https://node-red-workshop-int.eu-gb.mybluemix.net/ui/

You should be able to see your greetings to the other students


on this Node-RED dashboard (the last 10 messages can be seen)
Try it Out (in team)
Let one of the team members create an MQTT Server in Node-
RED and make agreement on a topic.

Let the other members of the team publish to this topic. If you
also subscribe to the topic, you should be able to see the
messages from the rest of the team
Node-RED with HW MQTT integration

• Use a microcontroller of your choice (e.g.


Raspberry Pi, Arduino, ESP32, ESP8266, …)
• Connect at least Temperature but preferrably
also Humidity sensor to it
Node-RED with HW MQTT integration (2)

• Create MQTT code that will send to the


nr_workshop/climate topic on test.mosquitto.org MQTT
broker a JSON object in the following format:

{“temperature”:value, “humidity”: value,


“location”:value, “hw”:value}

If you did well, your output should get visible on the


workshop dashboard:
https://node-red-workshop-int.eu-gb.mybluemix.net/ui/
Dashboard Output
That’s all for today!
• Questions?
• Want to know more? (look at the examples at
the end of presentation or find try any other
Node-RED functionality)
A Few More Examples
• Web Sockets
• CoAP Server
• FHICT MQTT Server
Web Sockets
• WebSockets are used for asynchronous web
communication. You can use them e.g. for the project
so that you can generate HTTP traffic
• Provided examples use ESP8266 nodeMcu or ESP32
code but of course you can try it with other MC
• Provided example gives an Arduino file for ESP8266 and
ESP32 where you have to fill in your own “ssid” and
“password”. When ESP8266/ESP32 starts up, it will give
you an IP address obtained from DHCP. This address
you can use to connect Node-RED to your board.
Web Sockets (2)
For the WebSockets you can use this example (also available in websocketWithESP8266NodeMCU):

[{"id":"189e7524.144a8b","type":"websocket in","z":"fa36cf76.b0724","name":"Web Socket


Client","server":"","client":"3456a8cb.a17378","x":230,"y":180,"wires":[["f12d5042.118e4"]]},
{"id":"a47c4ac7.e3ef48","type":"debug","z":"fa36cf76.b0724","name":"","active":true,"tosidebar":
true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":340,"wires":[]},
{"id":"f932c68b.a81b98","type":"inject","z":"fa36cf76.b0724","name":"","topic":"","payload":"\"H
allo WebSocket
Server\"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":42
0,"wires":[["85371db1.6e47a"]]},{"id":"85371db1.6e47a","type":"websocket
out","z":"fa36cf76.b0724","name":"","server":"","client":"3456a8cb.a17378","x":610,"y":440,"wir
es":[]},{"id":"f12d5042.118e4","type":"function","z":"fa36cf76.b0724","name":"","func":"let str =
msg.payload;\nif(str.substring(0,1)==\"O\")\nmsg.payload=\"LED_ON\";\nelse\
nmsg.payload=\"LED_OFF\";\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":320,"wires":
[["a47c4ac7.e3ef48"]]},{"id":"3456a8cb.a17378","type":"websocket-client","z":"","path":"ws://
192.168.2.106:81","tls":"","wholemsg":"false"}]
Web Sockets (3)
ESP8266 runs a “blink” code and sends output to Node-RED. It also accepts input from Node-RED (duplex communication)

Function block where you can do JavaScript processing on


a message.
I’ve added this one to change the message to “LED_ON”and
“LED_OFF” and also as an example of use.
Try to run the program without it to see the difference.
CoAP Server
• Here is a CoAP Server implementation provided in Node-RED

[{"id":"68da4f11.9bdf3","type":"coap request","z":"4ff0fc80.6b8b74","method":"PUT","observe":false,"url":"coap://192.168.2.143/lightled","content-format":"text/
plain","raw-buffer":false,"name":"PUT lightled","x":370,"y":180,"wires":[["ea9cbb87.4c9cd8"]]},
{"id":"f0a18ae1.f64608","type":"inject","z":"4ff0fc80.6b8b74","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,
"x":130,"y":200,"wires":[["68da4f11.9bdf3"]]},
{"id":"ea9cbb87.4c9cd8","type":"debug","z":"4ff0fc80.6b8b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":680,"y":20
0,"wires":[]},
{"id":"92f7cde6.decc5","type":"inject","z":"4ff0fc80.6b8b74","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"
x":140,"y":440,"wires":[["b8dc3d0a.3eadc"]]},{"id":"b8dc3d0a.3eadc","type":"coap request","z":"4ff0fc80.6b8b74","method":"GET","observe":false,"url":"coap://
192.168.2.143/lightled","content-format":"text/plain","raw-buffer":false,"name":"GET lightled","x":370,"y":500,"wires":[["8ecc3510.51e8b8"]]},
{"id":"8ecc3510.51e8b8","type":"debug","z":"4ff0fc80.6b8b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":670,"y":46
0,"wires":[]},
{"id":"7be20cd0.9205e4","type":"inject","z":"4ff0fc80.6b8b74","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1
,"x":110,"y":100,"wires":[["68da4f11.9bdf3"]]},
{"id":"65ab74d6.fe31fc","type":"inject","z":"4ff0fc80.6b8b74","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"
x":130,"y":300,"wires":[["955db9be.8c4ca8"]]},
{"id":"20c9b295.5af69e","type":"inject","z":"4ff0fc80.6b8b74","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,
"x":130,"y":360,"wires":[["955db9be.8c4ca8"]]},{"id":"955db9be.8c4ca8","type":"coap request","z":"4ff0fc80.6b8b74","method":"PUT","observe":false,"url":"coap://
192.168.2.143/light","content-format":"text/plain","raw-buffer":false,"name":"PUT light","x":400,"y":320,"wires":[["e661e2a8.0504e"]]},
{"id":"e661e2a8.0504e","type":"debug","z":"4ff0fc80.6b8b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":650,"y":320
,"wires":[]}]

• CoAP Client is implemented in ESP8266 (see coapserver-esp8266.ino)


CoAP Server (2)
CoAP Server (3)
• Study the mentioned material of CoAP. By
now, you should be a little bit expert
concerning Node-RED and you should be able
to understand what this code is doing and
make any changes you want.

Note: Only the ESP8266 CoAP code is working, the ESP32 code causes memory
crashes

Success!
MQTT Example with MQTT FHICT server
Use of FHICT MQTT Server
• Before you start using MQTT FHICT Server you
have to create an account, see next slide.
• Please, write down your credentials you’ll need
them for the configuration of the MQTT server
• You can also create your own topic where you
want to publish further, create it with your own
name and give it read/write access, so that you
can delete it in the future
https://selfservice.app.fhict.nl/Services/Mqtt

FHICT MQTT Portal


You should use here your own name and your own created topic,
this one is not visible as its access is only to me. For tests you
can also use public topics accessible to “Everyone”.
public/i871711_renata/mytopic

read/write

result
Configure MQTT Broker(Server)
Trigger Publish To Test
Try It out !
• Publish your name to the
public/i871711_renata topic
- we should be able to see the name strings
on the common screen
• More advanced : Create a string of the format:
<Name>!<timestamp> and publish it to the same
topic
Test with your own hardware
Now that you have an MQTT Broker, credentials and topic, you can start testing by publishing MQTT message by e.g. Arduino,
Raspberry PI, ESP8266. In this way you replace the MQTT publish from Node-RED by your own.
The used flow for MQTT FHICT :

[{"id":"a0b6f1e5.d295","type":"mqtt out","z":"650f08e9.b66298","name":"Publish
","topic":"public/i871711_renata/mytopic","qos":"","retain":"","broker":"985109ec.25b378","x":620,"y":460,"wires":[]},
{"id":"6d45b065.bf2b2","type":"inject","z":"650f08e9.b66298","name":"","topic":"","payload":"my first
test","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":460,"wires":[["a0b6f1e5.d295"]]},
{"id":"e3a9d45c.d23c98","type":"mqtt in","z":"650f08e9.b66298","name":"Subscribe to MQTT
broker","topic":"public/i871711_renata/mytopic","qos":"0","broker":"985109ec.25b378","x":230,"y":180,"wires":
[["37a28f73.65b57"]]},
{"id":"37a28f73.65b57","type":"debug","z":"650f08e9.b66298","name":"","active":true,"tosidebar":true,"console":false,"tostatus"
:false,"complete":"payload","x":600,"y":240,"wires":[]},
{"id":"e6f4f666.189988","type":"comment","z":"650f08e9.b66298","name":"Subscribe to FHICT MQTT server on topic
public/i871711_renata","info":"","x":270,"y":80,"wires":[]},
{"id":"6c2b457b.8e7eac","type":"comment","z":"650f08e9.b66298","name":"Publish timestamp on topic
public/i871711_renata","info":"","x":250,"y":380,"wires":[]},{"id":"985109ec.25b378","type":"mqtt-
broker","z":"","name":"mqtt.fhict.nl","broker":"mqtt.fhict.nl","port":"8883","tls":"","clientid":"","usetls":true,"compatmode":true,
"keepalive":"60","cleansession":true,"birthTopic":"public/i871711_renata/mytopic","birthQos":"2","birthPayload":"Subscribing to
FHICT MQTT Server","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Or you can find it in fhict_mqtt file.

You might also like