0% found this document useful (0 votes)
31 views80 pages

Working With Node Red (LAB-V)

Working with Node Red

Uploaded by

saijenivarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views80 pages

Working With Node Red (LAB-V)

Working with Node Red

Uploaded by

saijenivarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 80

Working with Node Red

MQTT & CoAP


LAB-V
Run Node-RED
Run the browser
• http://127.0.0.1:1880
Adding an inject node
• To inject a message into the flow
• From the node palette on the left side of the Node-RED editor, select an Inject
node and drag it onto the flow
• Double-click the node to open the “Edit inject node” view
• For the Payload field, select string and enter Hello, world! in the text field
• Click Done.
Adding a Debug node
• print out our message to the debug console window.
• From the node palette, select a Debug node and drag it onto the flow, and then
place it to the right-hand side of the Inject node.
• Wire the nodes together. Place the mouse cursor over the Inject node’s output
port (a small gray square on the right-hand side of the node), then left-click and
drag a wire over to the input port of the Debug node. A gray wire should now
be connecting the output of the Inject node to the input of the Debug node.
Deploy and Run
• to deploy it to the server and run it
• Click the Deploy button.
• Click the Debug tab in the right-hand side of the editor window.
• Click the Inject node’s button, which is the blue square coming out from the left-hand
side of the Inject node. Clicking the button is what will inject a message into the flow.
• A “Hello, world!” message should appear in the debug window.
• Click the Inject node again to send another message.
Practice –I (Using Function)
• Step 1 - Add a Function node
• Step 2 - Add Code and Run
• Step 3 - Add Two-Second Interval
Adding a Function node
• the Function block, which allows you to enter JavaScript code to manipulate
the msg object.
• Select a Function node and drag it out onto the flow.
• Carefully place it over the existing wire between the existing Inject and Debug
nodes. Node-RED will insert the new node between the two existing nodes, and
rewire the nodes together.
Add Code and Run
• Double-click on the Function node to open the “Edit function node” view.
• Copy and paste the following JavaScript code into the Function field:
• var dateNow = new Date(); var timeAsString =
dateNow.toLocaleTimeString(); msg.payload = msg.payload + '
The time is ' + timeAsString + '.'; return msg
• In the Name field, enter “Add time to msg”.
• Click Done.
• Click Deploy.
• Click the Inject node’s button.
• In the Debug tab, you should see the new output. For example, “Hello, world!
The time is 4:25:29 PM”
List of Exercises –II
• Write a program for String Count : counting number of words and
number of characters in the following format:
• The entered string : Hello World
• Number of characters in the string : 10
• Number of Words in the string:2
• Write a Program to convert the temperature given as Degrees to
Fahernheit using the formula in Node-RED
• (T°C × 9/5) + 32 = X°F
Exploring Node Red
Practice-I
Split the topic into different nodes
Setting Inject Node
Setting more outputs
Output
Try this
• Simulate the inject and debug node for three inputs and three
outputs.
Exercise-II
• Simulate there are two sensors controlling the operation of two
objects in two different rooms. the first sensor will operate the
operation of AC and second sensor will control the operation of Light.
Simulate the Node-Red Code for injecting two sensors that will display
four different outputs
• Sensor 1 = 1, the output as “AC is ON”
• Sensor 1 = 0, the output as “AC is OFF”.
• Sensor 2 = 1, the output as “LIGHT is ON”
• Sensor 2 = 0, the output as “LIGHT is OFF”
Finally the output as : “AC is ON and LIGHT is ON”
Node-RED set of nodes
• Default nodes:
• input, output,
function and
social media
nodes
Change node
used to modify a message’s properties and set context properties without having to resort to a
Function node.
Set - set a property. The value can be a variety of different types, or can be taken from an
existing message or context property.
Change - search and replace parts of a message property.
Move - move or rename a property.
Delete - delete a property.
Switch

• allows messages to be routed to different branches of a flow by


evaluating a set of rules against each message.
Practice-II
• Node RED example using SWITCH & CHANGE NODES
Join Node
• Joins sequences of messages into a single message.
• There are three modes available:
• Automatic
• When paired with the split node, it will automatically join the messages to
reverse the split that was performed.
• Manual
• Join sequences of messages in a variety of ways.
• reduce sequence
• Apply an expression against all messages in a sequence to reduce it to a single
message.
Practice-III
• Node RED example using JOIN Nodes.
Addition of two numbers (Exercise-III
& IV)

1. Make a calculator program by injecting the two numbers and the


operation to be performed.
2. Make a calculator with only two inputs and provide output to all
the arithmetic operations as a single output
References
• https://nodered.org/docs/user-guide/nodes
• https://www.youtube.com/watch?v=oRTb6bgGN_I
• https://www.youtube.com/watch?v=GJS_qgVEjQ0
• https://www.youtube.com/watch?v=LIEKDOzeEdI
• https://nodered.org/docs/user-guide/
Message Queue
Telemetry Transport
LAB-IX
By
Dr Shola Usharani
IoT Protocols
Introduction
• Invented by IBM for used for connecting Oil pipeline telemetry system
through Satellite communications.
• It is an open standard.
• Used in machine to machine
• One of the main protocol used for IoT networks.
Message Queue Telemetry Transport
(MQTT)
• It is based on Publish- subscribe model.
• It is light weight model and is used for small devices connection.
• It is more scalable.
• It uses less power.
Working Principle
Contd.,
• Many to many protocol
• It has
• Central MQTT broker
• Publishers
• Consumers or subscribers
QOS levels for MQTT
Three QoS levels 0 (min),1,2(max):
• QoS_0 (fire and forget): a single effort, or transmit burst, with no guarantee of message
arrival.
• QoS_1:attempts to guarantee the message is received, at least once
• QoS_2:
• the slowest and safest level .
• guarantees the message is received, only once, and is also decoded
• Higher QoS levels more overhead with the messages.
Requirement to connect MQTT
Broker or server
• Broker IP address or name (default port as 1883).
• Unique client name
• Client connection to publish/subscribe.
• Username/password authentication if required to connect the client.
• Setting various flags
• Clean Session Flag
• Time to Live
• Last Will and Testament
Clean Session Flag
• It is about remembering status of the client session when disconnects
• Flag = False : broker will not remember the client.
• Flag = True : Broker will remember the client .
• Default it is set to “False”.
Time to Live
• How much the connection can be live
• Default value is 60 seconds
Last Will and Testament
• Notification message
• Its about status of Publisher topic connection to subscribers.
• Set by the publisher client.
• SET : sent to subscribers as the publisher disconnected abnormally.
• NOT SET : publisher disconnects normally.
Publishing a Message
• Client publish a message to a broker.
• the message sent:
• Message topic
• Message QOS
• Retain flag : Whether the message should be retained.
• False broker doesn’t keep the message.
• True the last message received by the broker with the retained flag set will be kept.
Installing MQTT Nodes
Type
broker
Available local broker
• Masca
• Aedes
MQTT Scenario (Practice-I)

Aedes broker
Error with Mosca Broker
• Schema error
• https://stackoverflow.com/questions/64189045/node-js-mosca-broker-error-
expected-schema-to-be-an-object-or-boolean
• Solution
• Use aedes broker
• Install the above broker
Broker install
Publish Node (MQTT Out)
• To publish a Message to an MQTT broker use the
MQTT publish node in the workspace.

Configuring the screen


MQTT Out (To edit server IP)

server IP = client ID and Broker name or IP address.


MQTT Out (Connection Tab)

edit icon to the right of the


server you can edit here
MQTT Out(Security Tab)

To set the username/password for the connection


determined by the MQTT broker.
MQTT Out(Birth Message)

The birth message is published by the client


when the MQTT node starts.
MQTT Out(Will Message)

stored on the broker and sent in the


advent of a failed client connection.
Configuring the Subscribe Node
(MQTT In )
MQTT Scenario (Practice-I)

Aedes broker
Configuration of Nodes in Node
Red
Inject Node
Aedes Broker
Publisher
Subscriber

Automatically set
once the
publisher is
connected
MQTT Scenario (Practice-II)
Multiple Topics using Single
Publisher
Function 37,38 & 39
• msg.topic="a";
• return msg;
Function node (40)
if(msg.topic=="a")
msg.payload=msg.payload+ " Topic A";
if (msg.topic == "b")
msg.payload = msg.payload + " Topic B";
if (msg.topic == "c")
msg.payload = msg.payload + " Topic C";
return msg;
Practice-III
• Multiple Subscribers with single topic
• Multiple topics and multiple subscribers
Exercise to submit
• Multiple topics with single subscriber
• Addition of two numbers
• String Upper
• Finding number of words
References
1. Arshdeep Bahga, Vijay Madisetti, “Internet of Things A Hands-On
Approach”, (Chapter 1), 2015.
2. Greg Dunko, Joydeep Misra,Josh Robertson, Tom Snyder,”A
reference Guide to The Internet of Things”, 2017 Bridgera LLC,RIoT.
3. Link used :
1. http://www.steves-internet-guide.com/configuring-the-mqtt-publish-node/.
2. https://www.youtube.com/watch?v=C7v8TQLIsE8: local broker
CoAP
• It allows simple electronic devices to communicate over the Internet.
• It is designed for small devices with low-power sensors and actuators that
need to be controlled or supervised remotely, through standard Internet
networks.
• It is an efficient protocol that connects gateways with sensor nodes and
with servers or cloud platforms for Web integration and global access.
• HTTP is expensive, but CoAP is a simple, less expensive protocol that meets
all affordable needs with resource constraints.
• This protocol is designed for environments like low-end microcontrollers
and constrained networks with critical bandwidth and a high error rate
such as 6LowPANs.
• It is defined by the IETF.
• CoAP is based on the request-response communication model.
• CoAP resembles HTTP in terms of the REST model with GET, POST,
PUT and DELETE methods, URIs, response codes, MIME types, etc
Topics covered
• CoAP node installation
• Editing CoAP nodes
• Sample Example
• Practice Examples
node-red-contrib-coap

• Functionality
• "coap request" and "coap in" nodes
• Install
• cd $NODE_RED_HOME
• npm install node-red-contrib-coap
• or
• Manage palette
COAP Nodes
• COAP Request node
• COAP In node.
Practice -1

Server
Client COAP
Practice-I
COAP Server

On cl
icking
edit o
pti on
Function Node
COAP Request Node
Practice-II
Client 1

Server
COAP
Client 2

• Connect client 2 based on the example 1 and if the


value of temperature is > 25 display in debug window
as “Weather is HOT” otherwise “Weather is COOL”.
• Output:
• Client 1 : the temperature is 32
• Client 2 : Weather is HOT
Practice-III
Server 1
Client 1

COAP
Client 2
Server 2

• Server 1weather report server


• Server 2-Home automation server
• Client 1 Requests the weather report of your location and
displays the status
• Client 2 requests for temperature and humidity values of your
house and displays the status
Example
• Connect to the openweather API to process the weather conditions of
your local area. Analyse the report and display the report data on
dashboard with various interactive widgets.
References
• https://www.youtube.com/watch?v=dvdwFE7ikMc

You might also like