0% found this document useful (0 votes)
204 views22 pages

Node Red

Node-RED is a programming tool that allows users to visually connect hardware devices, APIs, and online services using a flow-based programming model. It provides a browser-based editor to create JavaScript functions and wire together nodes representing different inputs and outputs. The lightweight runtime is built on Node.js and is ideal for running on low-cost hardware like Raspberry Pi or in the cloud. Example flows demonstrate connecting an inject node to send messages to a debug node, adding timestamps to messages using a function node, and connecting to GPIO pins on a Raspberry Pi to control an LED.

Uploaded by

akah
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)
204 views22 pages

Node Red

Node-RED is a programming tool that allows users to visually connect hardware devices, APIs, and online services using a flow-based programming model. It provides a browser-based editor to create JavaScript functions and wire together nodes representing different inputs and outputs. The lightweight runtime is built on Node.js and is ideal for running on low-cost hardware like Raspberry Pi or in the cloud. Example flows demonstrate connecting an inject node to send messages to a debug node, adding timestamps to messages using a function node, and connecting to GPIO pins on a Raspberry Pi to control an LED.

Uploaded by

akah
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/ 22

NODE-RED

NODE-RED
 Node-RED is a flow based development tool for visual
programming.
 It is developed originally by IBM for wiring together

hardware devices, APIs and online services as part of the


Internet of Things.
 It provides a browser-based editor, which can be used to

create JavaScript functions , 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.
 JavaScript functions can be created within the editor using a

rich text editor.


 A built-in library allows you to save useful functions,

templates or flows for re-use.


Node.js
 The light-weight runtime is built on Node.js,
taking full advantage of its event-driven, non-
blocking model. This makes it ideal to run at the
edge of the network on low-cost hardware such as
the Raspberry Pi as well as in the cloud.
 With over 225,000 modules in Node's package

repository, it is easy to extend the range of palette


nodes to add new capabilities.
Example#1
 Step1- Add an Inject node
1. If using Node-RED running on your computer, you can
use http://127.0.0.1:1880 or whichever address or
hostname you’re using.
2. If you haven’t used Node-RED before, there should be one
empty flow named “Flow 1”.
3. From the node palette on the left side of the Node-RED
editor, select an Inject node and drag it onto the flow.
4. Double-click the node to open the “Edit inject node” view.
5. For the Payload field, select string and enter Hello,
world! in the text field.
6. Click Done.
Step2-Add a Debug node

 We need a destination for the Inject node’s message. We’ll use the
Debug node, which will print out our message to the debug
console window.
1. 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.
2. 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.
3. The Debug node will automatically print
the msg.payload property to the console window, which we’ll
see in the next step.
Step3- Deploy and Run
1. Click the Deploy button.
2. Click the Debug tab in the right-hand side of the
editor window.
3. 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.
4. A “Hello, world!” message should appear in the
debug window.
5. Click the Inject node again to send another
message.
Hello World Example Part#2
 Step1-Add a Function Node
 First off, let’s add the current time to the message.

To do that, we’ll use the Function block, which


allows you to enter JavaScript code to manipulate
the msg object.
1. Select a Function node and drag it out onto the
flow.
2. 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.
Step 2 - Add Code and Run

1. Double-click on the Function node to open the


“Edit function node” view.
2. Copy and paste the following JavaScript code
into the Function field:
3. In the Name field, enter “Add time to msg”.
4. Click Done.
5. Click Deploy.
6. Click the Inject node’s button.
7. In the Debug tab, you should see the new output.
Step 3 - Add Two-Second Interval

 Now let’s adjust the flow to automatically inject a new


message every two seconds. The existing Inject node
can already do that, we just need to adjust its settings.
1. Double-click on the existing Inject node to open the
“Edit inject node” view.
2. For the Repeat field, select interval and enter 2
seconds for the period.
3. Click Done.

4. Click Deploy.

5. In the Debug tab, you should now see your output


every two seconds.
Example#3
 Let’s start with comment node
 Drag and drop a comment node from node palette.
 Double click on the comment node and you will see the
configuration window.
 Write the name of comment node
 Add the detailed text in body part as you like.
 Click on ok
 Add the inject node in flow.
 Double click on inject node , change it to timestamp.
 Default behaviour for the node is to inject a timestamp –
the current time in milliseconds since January 1, 1970.
 You’ll also notice that the inject node now named
timestamp.
 Now add debug node.
 wire them together and then deploy the flow and

test it.
 This is the simplest flow possible and will send the

current timestamp to the debug node for display


in the debug pane. 
 Now we’ll edit the inject node to deliver a text

message rather than a timestamp.


 Double click on inject node, in the payload field,
select string instead of timestamp and then type any
string you like into the blank field below the payload
field.
 Click Ok to save changes.
 Click the deploy button again to resolve that and

then go ahead and click the tab on the inject node.


 If you look at the debug output you’ll see that

instead of a timestamp, your text has been delivered


as a message to the debug node.
 The debug node can be used to examine them in more detail.
 All messages in Node-RED have three default properties: the
payload, which we are seeing above, a message topic, which is a
user-defined string describing what the message is about (its
topic, if you will) and an internal identifier.
 You can actually see this information if you edit the debug node
configuration.
 Select the debug node, double click and change the node output
field to “Complete msg object”.
 Then save this, deploy again and click the inject node, you will
see in the debug pane a JSON structure that contains 3 fields: a
“topic” which is currently blank, a “payload” which contains the
string you set in the inject node and an internal ID field
“_msgid”. 
Example#4
 This example builds on the MQTT node, which provides a
convenient way to take input from an MQTT broker.
 First, drag and drop an mqtt input node and configure it
for the broker. 
 Don’t forget to configure the topic to something unique, in
the case of this example we are using
noderedlecture/sensor but you should use your own
unique topic, i.e. <your name here>/sensor
 Server is broker.mqtt-dashboard.com.
 You can use their websockets client showcase on
http://www.hivemq.com/demos/websocket-client/
 
 You’ll be using their websocket client in this example, so
navigate to that page and connect to the broker.
 You will publish a JSON encoded string to the topic you
configured to see both the use of the mqtt node and the
json node.
 Since you are sending a JSON string, you will need to parse
the message that the mqtt node generates when it receives
the MQTT message. To do that, you’ll need to drag and
drop a json node and connect it to the mqtt node’s output.
 Node-RED’s json node is a sort of convenience function, in
that it parses the incoming message and tries to convert it
to/from JSON. So if you send it a JSON string, it will convert
it to a JavaScript object, and vice versa.
 Then drag the debug node put into workspace.
 Then wire up the usual debug node to the json

node and deploy, then use the HiveMQ dashboard


to send the JSON string {“analyze”:false,
“value”:10}
 If you look closely at the output, you can see that

the msg.payload contains an object, which itself


has two fields, analyze and value, each with their
own values.
Example#5
 Connecting to a GPIO pin
 First install the raspberry pi node
 Go to the menu in right side of node red panel
 Then go to manage palette.
 Then go to install.
 Search raspberry pi module, type node-red-node-pi-

gpio
 Then install it
 Now you can see in node palette,and find the

raspberry pi nodes.
Connecting to a GPIO pin

 Drag and drop the raspberry output in blank page.


 Double-click on the node and a box will appear to

let you configure the node.


 Change the GPIO pin to be GPIO17 and

tick Initialise pin state?.


 Leave the setting for Initial level of pin on low.
 Give the node a name - we called it Red LED

because the LED we used was red, feel free to


change the name. When you are finished,
click Done.
Injecting Messages
 Now scroll back up to the list of nodes. To turn the LED on
and off, we need an input.
 In Node-RED we can inject messages into the flow and

cause things to happen as a result. Drag an inject node onto


the flow.
 Double-click on the inject node. Use the drop down next

to Payload to change the data type to string and type 1 in


the Payload box - this will be the message. Type On in
the Name box. Press Done.
 Repeat the previous steps to create another inject node,

except this time add 0 as the payload message, and call this
node Off.
Deploying the Flow
 Wiring up the input and output nodes.
 Our flow is finished, so we can deploy it. Click on the
Deploy button on screen.
 A message should pop up at the top saying
“Successfully deployed”.
 Now click on the blue square on the left of
the On node to inject the message 1. The Red
LED node receives the message and your LED should
light up.
 You should be able to turn the LED off by clicking the
blue square on the Off node, which injects the
message 0
Debugging the flow
 If your LED doesn’t turn on and off, firstly check that you
have wired the components correctly on the breadboard.
Make sure you have wired your LED to
both ground and pin 17 on your Raspberry Pi.
 You can also display Node-RED debugging information

by wiring up your nodes to a Debug node, which can be


found under output.
 Drag in a debug node and wire your two inject nodes to

it, then click Deploy.


 When you click the buttons to inject the message, Node-

RED will show you what was injected in the Debug tab


on the right side of the screen.
THANK YOU!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy