Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b1063f

Browse files
Merge pull request #266 from arduino/sync/liamAljundi/nodered-tutorial
Liam aljundi/nodered tutorial
2 parents 83531be + 9b0df1c commit 8b1063f

File tree

10 files changed

+256
-0
lines changed

10 files changed

+256
-0
lines changed
308 KB
Loading[フレーム]
1.29 MB
Loading[フレーム]
2.11 MB
Loading[フレーム]
1.27 MB
Loading[フレーム]
2.39 MB
Loading[フレーム]
1.2 MB
Loading[フレーム]
1.4 MB
Loading[フレーム]
538 KB
Loading[フレーム]
67.7 KB
Loading[フレーム]
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
---
2+
title: 'Node-RED with Arduino IoT Cloud'
3+
compatible-products: [mkr-1000-wifi, mkr-wifi-1010, nano-33-iot, nano-rp2040-connect]
4+
difficulty: advanced
5+
description: 'Learn how to use Node-RED together with the Arduino IoT Cloud to create advanced automation systems.'
6+
tags:
7+
- Node-RED
8+
- IoT Cloud
9+
- Automation
10+
- IoT
11+
author: 'Liam Aljundi'
12+
---
13+
14+
## Introduction
15+
16+
Node-RED is a programming tool for connecting hardware devices such as Arduino with other hardware devices, APIs, and online services easily using a web-based flow editor. It allows you to connect those different devices and services by connecting a combination of nodes that create your desired flow.
17+
18+
In this tutorial, we will look into the applications of using Node-RED together with the Arduino IoT Cloud. The integration of the two platforms allows us to facilitate communications between the Arduino IoT Cloud and home automation devices, send and receive data from online services such as Email and SMS, and write JavaScript code to manipulate the data.
19+
20+
***Note: this tutorial requires a [subscription to the Arduino IoT Cloud](https://cloud.arduino.cc/plans), all three subscriptions (Entry, Maker & Maker Plus) are valid.***
21+
22+
## Goals
23+
24+
- Set up Node-RED with the Arduino IoT Cloud.
25+
- Use simple Flows with Node-RED.
26+
- Build a Node-RED Flow that pulls data from Arduino IoT Cloud.
27+
- Manipulate data received from the Arduino IoT Cloud using JavaScript code.
28+
- Send email notifications with data from the Arduino IoT Cloud.
29+
30+
## Hardware & Software Requirements
31+
32+
- [Arduino Create Agent](https://github.com/arduino/arduino-create-agent)
33+
- [Node.js](https://nodejs.org/en/) and [Node-RED installed]()
34+
- An [Arduino account](http://create.arduino.cc/iot).
35+
36+
For this tutorial, you will need a cloud compatible board. You will find all compatible boards in the link below:
37+
38+
- [Arduino IoT Cloud compatible hardware.](/cloud/iot-cloud/tutorials/iot-cloud-getting-started#compatible-hardware)
39+
40+
41+
## Node-RED Setup
42+
43+
Setting up Node-RED is simple, we will run it locally following the steps below:
44+
45+
**1.** Install [Node.js](https://nodejs.org/en/). To check if Node.js is installed, open a terminal and enter the following command:
46+
47+
```shell
48+
node --version
49+
```
50+
51+
If correctly installed, you will see the version number printed in the terminal.
52+
53+
**2.** Install Node-RED from the command line using the command `sudo npm install -g --unsafe-perm node-red`, delete `sudo` if you are using a Windows machine.
54+
55+
**3.** After installation, start it by running the command `node-red` in the terminal, you should see a similar output to this:
56+
57+
```shell
58+
$ node-red
59+
60+
Welcome to Node-RED
61+
===================
62+
63+
30 Jun 23:43:39 - [info] Node-RED version: v1.3.5
64+
30 Jun 23:43:39 - [info] Node.js version: v14.7.2
65+
30 Jun 23:43:39 - [info] Darwin 19.6.0 x64 LE
66+
30 Jun 23:43:39 - [info] Loading palette nodes
67+
30 Jun 23:43:44 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
68+
30 Jun 23:43:44 - [info] Settings file : /Users/nol/.node-red/settings.js
69+
30 Jun 23:43:44 - [info] HTTP Static : /Users/nol/node-red/web
70+
30 Jun 23:43:44 - [info] Context store : 'default' [module=localfilesystem]
71+
30 Jun 23:43:44 - [info] User directory : /Users/nol/.node-red
72+
30 Jun 23:43:44 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable
73+
30 Jun 23:43:44 - [info] Creating new flows file : flows_noltop.json
74+
30 Jun 23:43:44 - [info] Starting flows
75+
30 Jun 23:43:44 - [info] Started flows
76+
30 Jun 23:43:44 - [info] Server now running at http://127.0.0.1:1880/red/
77+
78+
```
79+
80+
**4.** Open the Node-RED editor by going to your browser and entering `http://localhost:1880`.
81+
82+
***For more details on installing Node-RED, you can check [their installation page](https://nodered.org/docs/getting-started/local).***
83+
84+
## The Node-RED Editor
85+
86+
The Node-RED editor consists of four main parts:
87+
88+
- A **header on the top** containing the Deploy button, main menu, and the user menu (only visible if user authentication is enabled).
89+
- The **palette on the left side**, containing the available nodes.
90+
- A **workspace in the middle**, where flows can be created.
91+
- The **sidebar on the right**, containing editing tools such as a node configuration tool and a debugger.
92+
93+
![The Node-RED editor](assets/nodered-01.png)
94+
95+
You can run the simple flow shown below using Node-RED's default nodes:
96+
97+
- drag the **"inject"** node from the palette on the left side into the workspace in the middle
98+
- double-click on the node to edit it
99+
- assign a name and topic to it
100+
- click on the dropdown menu next to *msg.payload* and choose **"string"**, then enter a random message
101+
- click **Done**
102+
- drag the **"debug"** node into the workspace
103+
- connect the two nodes by dragging a wire from the message node to the debug node
104+
- click on the debug menu from the sidebar on the right
105+
- press **Depoly** from the header on the top
106+
- finally, press on the checkbox of the message node
107+
108+
![Creating a simple flow](assets/nodered-02.gif)
109+
110+
Your message should be printed to the console on the right side.
111+
112+
In addition to the default nodes installed in node-RED, you can use the palette manager to install additional nodes that can be useful to creating more advanced flows. Follow the steps below to install the Arduino IoT Cloud nodes using the palette manager:
113+
114+
**1.** Click on the menu in the header bar in the top right corner.
115+
116+
**2.** Select **"Manage palette"**.
117+
118+
**3.** Go to the **"Install"** tab.
119+
120+
**4.** Search for **"Arduino"**.
121+
122+
**5.** From the results, install **"@arduino/node-red-contrib-arduino-iot-cloud"**.
123+
124+
![Installing the Arduino IoT Cloud Palette](assets/nodered-03.gif)
125+
126+
Now, you should be able to use the Arduino IoT Cloud nodes from the palette on the left side of the editor. There are five IoT Cloud nodes: property in, property out, historic, periodic, and inject. Each of the nodes has a description of its function, which appears when you hover over the node.
127+
128+
## Setting Up a Thing
129+
130+
The Thing we will create for this example is quite simple. It is a sketch to read humidity values from a **DHT11 humidity sensor**, any other values can be used alternatively, such as temperature, soil moisture, light etc. Keep in mind that the code and example below are specific to using the DHT11 humidity sensor.
131+
132+
Follow the steps below to set up the Thing:
133+
134+
- Go to the Arduino IoT Cloud -> Thing -> Create a Thing
135+
- Add your Device and your Network credentials
136+
- Add an integer Variable, with the name humidity, and set permissions to Read Only
137+
- Go to the **"Sketch"** tab and replace the code with the code below:
138+
139+
```arduino
140+
141+
#include "thingProperties.h"
142+
#include <Arduino_MKRIoTCarrier.h>
143+
MKRIoTCarrier carrier;
144+
145+
#include <dht.h> // include this library, named: "dht library"
146+
dht DHT;
147+
#define DHT11_PIN 7 // define DHT pin
148+
149+
void setup() {
150+
// Initialize serial and wait for port to open:
151+
Serial.begin(9600);
152+
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
153+
delay(1500);
154+
155+
// Defined in thingProperties.h
156+
initProperties();
157+
158+
// Connect to Arduino IoT Cloud
159+
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
160+
161+
setDebugMessageLevel(2);
162+
ArduinoCloud.printDebugInfo();
163+
164+
carrier.begin();
165+
}
166+
167+
void loop() {
168+
ArduinoCloud.update();
169+
170+
int readValue = DHT.read11(DHT11_PIN); // check the data coming from the DHT pin
171+
humidity = DHT.humidity; // assign the humidity variable to data from sensor
172+
}
173+
174+
```
175+
176+
### Circuit
177+
178+
The final step is connecting the **DHT11 humidity sensor** to our Arduino MKR Wi-Fi 1010 board, as shown in the image below. You can also freely use any other [IoT Cloud compatible board](/cloud/iot-cloud/tutorials/iot-cloud-getting-started#compatible-hardware) and a different sensor.
179+
180+
![Connecting the DHT11 Sensor.](assets/rednode-09.png)
181+
182+
## Communicating with Node-RED
183+
184+
Use the steps below to use Node-RED with the Arduino IoT Cloud:
185+
186+
![Obtaining API Key](assets/nodered-04.gif)
187+
188+
**1.** Go to the Arduino IoT Cloud -> Integrations -> Create API Key
189+
190+
**2.** Save the Client ID and Client Secret in a safe document
191+
192+
**3.** Go to Node-RED web page at [http://localhost:1880](http://localhost:1880)
193+
194+
**4.** Add the Arduino IoT Cloud "**property out**" node from the palette menu on the left side
195+
196+
**5.** Double click on the node -> select "Add new Arduino-connection"
197+
198+
**6.** Click on the pen icon to enter the API Key details -> enter the Client ID and Client Secret saved in the previous steps -> click on the ADD button
199+
200+
**7.** You should now be able to select any of your Things and properties (Variables). In our case, we select the **Humidity Thing**, and **humidity property** (Variable) to retrieve data values from.
201+
202+
![Communicating with the Cloud.](assets/nodered-05.gif)
203+
204+
![Values in console.](assets/nodered-06.gif)
205+
206+
### Creating a Flow
207+
208+
Now that you have a node pulling data from the Arduino IoT Cloud, you need to use the *Inject* node in order to manipulate and send the data. All you need to do is dragging the *Inject* node and connect it to the property out node, then double-clicking on the inject node to ensure that the right Thing and property are selected.
209+
210+
Finally, we will add a debug node from the top of the palette menu, to print the values coming from the Arduino IoT Cloud into the console. You will now be able to see the values from the humidity sensor printed into the Node-RED console.
211+
212+
The next step is adding a **function node** that processes the data coming from the Arduino IoT Cloud. This node is important when working with a constant flow of data, as it allows you to use JavaScript code to create customized triggers from the received values. After dragging the function node into the editor, double-click on the node and select **"on message"** tab, then insert the code below:
213+
214+
```js
215+
let data = msg.payload;
216+
let trigger = context.get("status");
217+
218+
if(typeof trigger == "undefined")
219+
trigger = false;
220+
221+
if(data>60 && !trigger){
222+
trigger = true;
223+
context.set("status",trigger);
224+
return msg;
225+
}
226+
227+
if(data<60 && trigger){
228+
trigger = false;
229+
context.set("status",trigger);
230+
return msg;
231+
}
232+
```
233+
234+
This code will only send values when they are above 60, or when they change back to below 60. You can connect it to the flow, in between the humidity inject node, and the debug node. Try it out by blowing air on the humidity sensors to increase the values.
235+
236+
![Filtering values with a function.](./assets/nodered-07.gif)
237+
238+
### Sending Email Notifications
239+
240+
Now that you are able to pull and manipulate data from the Arduino IoT Cloud, it's time to use this data. In this example, we will show how to send alarm notifications to your email, using the Arduino IoT Cloud. To achieve this, we need to add an email nodes following the steps below:
241+
242+
- Click on the menu in the header bar in the top right corner.
243+
- Select **"Manage palette"**.
244+
- Go to the "install" tab.
245+
- Search for "Email".
246+
- From the shown results, install **"node-red-node-email"**.
247+
248+
Next, drag the email node from the palette menu on the left side into the editor. Double-click on the node, then enter an email to send the data to, as well as your email information to send the data from.
249+
250+
![Email node](./assets/nodered-08.png)
251+
252+
Now, all you need to do is replace the debug node with the email node to receive email notifications with the humidity data received from the Arduino IoT Cloud. You can customize the emails using the function node if you wish.
253+
254+
## Further Applications
255+
256+
In this tutorial we demonstrated how you can use the [Arduino IoT Cloud](https://create.arduino.cc/iot/) together with [Node-RED](https://nodered.org/) to build a more complex automation applications. The email notification is only one of the many different use cases, other applications can be creating communication between the Arduino IoT Cloud to other home automation devices, sending customized SMS and WhatsApp messages, and many more.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /