Node-RED usage tutorial 2

Overzicht

This tutorial builds on the first tutorial to create a process that starts bringing in data from external sources to do something useful locally.

The process will:

Periodically retrieve information from the website

Convert this information into useful form

Show results in Debug sidebar

1. Add an Inject node

In the previous tutorial, the Inject node was used to trigger the process when its button was clicked. For this tutorial, the Inject node will be configured to trigger the process periodically.

Drag the Inject node from the panel to the workspace.

Double-click the node to bring up the editing dialog box. Set the repeat interval to every 5 minutes.

Click Finish to close the dialog box.

2. Add HTTP request node

HTTP request nodes can be used to retrieve web pages when triggered.

After adding one to the workspace, edit it to set the URL property to:

https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv

Then click Finish to close the dialog box.

This URL is a feed of last month’s major earthquakes from the USGS website. The site offers many other options that you may want to try after completing this tutorial.

3. Add CSV node

Add a CSV node and edit its properties. Enable the “First row contains column names” option.

Then click Finish to close.

4. Add Debug node

Add a Debug node to the output.

5. Connect them together

Add wire connections:

Inject node output into HTTP request node input.

HTTP request node output to CSV node input.

The CSV node output goes to the Debug node input.

6. Add a Switch node

Add the Switch node to the workspace. Edit its properties and configure it as msg.payload.mag Check properties with test, change >= it to test anumber and value 7. Click Finish to close.

Add a second connection from the CSV node to this Switch node.

7. Add change node

Add a Change node and connect it to the output of the Switch node. Configure it to set msg.payload to the string PANIC!.

8. Add debugging node

Connect the new Debug node to the output of the Change node

9. Deployment

Deploy the flow to the runtime by clicking the Deploy button.

Open the Debug sidebar tab and click the Inject button. You should see a list of entries that looks like this:

msg.payload: Object

{“time”:”2017-11-19T15:09:03.120Z”,”latitude”:-21.5167,”longitude”:168.5426,”depth”:14.19,”mag”:6.6,”magType”:”mww” ,”gap”:21,”dmin”:0.478,”rms”:0.86,”net”:”us”,”id”:”us2000brgk”,”updated”:”2017-11-19T17:10:58.449Z “,”place”:”68km E of Tadine, New Caledonia”,”type”:”earthquake”,”horizontalError”:6.2,”depthError”:2.8,”magError”:0.037,”magNst”:72,”status “:”reviewed”,”locationSource”:”us”,”magSource”:”us”}

You can now click the small arrow to the left of each property to expand them and inspect the contents

If there are any earthquakes with a magnitude greater than 7, you will also see a debug message like this:

msg.payload: string(6)

“PANIC!”

You can change the switch value 7 to a smaller value to test your program. Remember to click Deploy after making changes. ***

generalize

This process automatically triggers every 5 minutes and retrieves data from the url. It parses the data and displays it in the debug sidebar. It also checks the data for amplitude values ​​and branches the stream for any message with an amplitude greater than or equal to 7. The payload of such messages is modified and displayed in the Debug sidebar.

resource

The process created in this tutorial is represented by the following json. To import it into the editor, copy it to the clipboard and paste it into the Import dialog box.

[{“id”:”e36406f2.8ef798″,”type”:”inject”,”z”:”f03b57d5.e525f8″,”name”:””,”topic”:””,”payload”:”” ,”payloadType”:”str”,”repeat”:”300″,”crontab”:””,”once”:false,”x”:130,”y”:900,”wires”:[[“c3c50023 .3bbed”]]},{“id”:”c3c50023.3bbed”,”type”:”http request”,”z”:”f03b57d5.e525f8″,”name”:”Recent Quakes”,”method”: “GET”,”url”:”https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv”,”tls”:””,”x”:300,”y” :900,”wires”:[[“8afc6cac.e0812”]]},{“id”:”8afc6cac.e0812″,”type”:”csv”,”z”:”f03b57d5.e525f8″,”name” :””,”sep”:”,”,”hdrin”:true,”hdrout”:””,”multi”:”one”,”ret”:”\n”,”temp”:””, “x”:470,”y”:900,”wires”:[[“44779781.4190f8″,”6f0eb546.9e208c”]]},{“id”:”44779781.4190f8″,”type”:”debug”, “z”:”f03b57d5.e525f8″,”name”:””,”active”:true,”complete”:false,”x”:630,”y”:900,”wires”:[]},{ “id”:”6f0eb546.9e208c”,”type”:”switch”,”z”:”f03b57d5.e525f8″,”name”:””,”property”:”payload.mag”,”propertyType”:” msg”,”rules”:[{“t”:”gte”,”v”:”7″,”vt”:”num”}],”checkall”:”true”,”outputs”:1,” x”:510,”y”:960,”wires”:[[“d78d4aa8.8c8208”]]},{“id”:”d78d4aa8.8c8208″,”type”:”change”,”z”:” f03b57d5.e525f8″,”name”:””,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”PANIC!” ,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:650 ,”y”:1020,”wires”:[[“72fddece.fac0d”]]},{“id”:”72fddece.fac0d”,”type”:”debug”,”z”:”f03b57d5.e525f8″ ,”name”:””,”active”:true,”complete”:false,”x”:750,”y”:960,”wires”:[]}]

Neem contact met ons op