You are on page 1of 31

Workshop

Workshop
Author: Renata Frenken
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
Starting Up
• Short description Node-RED
• “Hello World” example
• Installation instructions
• Installation instructions for libraries necessary
for the next parts of the workshop
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.
“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
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 wire.
• 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
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.
CoAP

Mosquitto MQTT Broker

Dashboard
IoT Examples
• MQTT in Node-RED, how to connect to FHICT
MQTT Broker
• MQTT Mosca Broker
• Web Sockets with ESP8266/ESP32
• CoAP with ESP8266/ESP32
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 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.


MQTT Mosca Broker
If you’ve installed the MOSCA library in part 1 of this workshop, you can now use Mosquitto MQTT Broker instead of MQTT FHICT broker. In
that case you run your MQTT Broker locally.
I’ve created a small example on how to do it. Here is the flow (you can find it in mosca_flow file too):

[{"id":"978a7b90.f10518","type":"mosca
in","z":"d34b5bc8.913538","mqtt_port":"1884","mqtt_ws_port":8080,"name":"mymosca","username":"renata","password":"password","dbur
l":"","x":310,"y":220,"wires":[[]]},{"id":"6f0e210d.caebc","type":"mqtt
in","z":"d34b5bc8.913538","name":"","topic":"topic/mysensorvalue","qos":"2","broker":"6229742c.cc253c","x":340,"y":320,"wires":
[["f1adab22.3d1298"]]},
{"id":"f1adab22.3d1298","type":"debug","z":"d34b5bc8.913538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"co
mplete":"false","x":550,"y":320,"wires":[]},{"id":"8d3981a7.e51a7","type":"inject","z":"d34b5bc8.913538","name":"","topic":"topic/
mysensorvalue","payload":"35","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":440,"wires":
[["b938b25d.62b57"]]},{"id":"b938b25d.62b57","type":"mqtt
out","z":"d34b5bc8.913538","name":"","topic":"topic/mysensorvalue","qos":"","retain":"","broker":"6229742c.cc253c","x":670,"y":440,"wires
":[]},{"id":"2f1cef76.f689a","type":"comment","z":"d34b5bc8.913538","name":"Configure your onw mosca broker","info":"For network
address you can use \"localhost to run it locally. I've used port 1884 so I don't mess with my running MQTT FHICT
server\"","x":380,"y":160,"wires":[]},
{"id":"b1b01daa.8b026","type":"comment","z":"d34b5bc8.913538","name":"Subscribe","info":"","x":300,"y":280,"wires":[]},
{"id":"b792f8a.c4d1908","type":"comment","z":"d34b5bc8.913538","name":"publish","info":"I publish a number now instead of
string.","x":270,"y":380,"wires":[]},{"id":"6229742c.cc253c","type":"mqtt-
broker","z":"","name":"mymosca","broker":"localhost","port":"1884","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleanse
ssion":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willP
ayload":""}]
MQTT Mosca Broker(2)
Go inside the comment for short instructions

Mosca broker

Now I’ve published integer


MQTT Mosca Broker(3)
• The best way to set up your own Mosquitto
(Mosca) broker is to look at the example. Click
on the nodes to find out how they are
configured. The user name and password
(authentication) doesn’t matter, it’s not used
when you run it on your local machine.
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!

You might also like