HTTP Response Flow

Overview

This tutorial makes a flow that starts to bring in data from external sources to do something useful locally.

The flow will:

  • Retrieve information from a website at a regular interval
  • Convert that information into a useful form
  • Display the result in the Debug sidebar

1. Add an Inject node

In previous tutorials, the Inject node was used to trigger the flow when its button was clicked. For this tutorial, the Inject node will be configured to trigger the flow at a regular interval.

Drag an Inject node onto the workspace from the palette.

Double click the node to bring up the edit dialog. Set the repeat interval to every 4 seconds.

Click Done to close the dialog.

2. Add an HTTP Request node

The HTTP Request node can be used to retrieve a web-page 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_week.csv

Then click Done to close the dialog.

This URL is a feed of significant earthquakes in the last 7 days from the US Geological Survey web site. The site offers a number of other options that you may want to play around with after completing this tutorial.

3. Add a CSV node

Add a CSV node and edit its properties. Enable option for ‘First row contains column names’.

Then click Done to close.

4. Add a Debug node

Add a Debug node to the output.

5. Wire them all together

Add wires connecting:

  • The Inject node output to the HTTP Request node input.
  • The HTTP Request node output to the CSV node input.
  • The CSV node output to the Debug node input.

7. Add a Switch node

Add a Switch node to the workspace. Edit its properties and configure it to check the property msg.payload.mag with a test of > change it to test on a number and the value 6. Click Done to close.

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

8. Add a Change node

Add a Change node, wired to the output of the Switch node. Configure it to set msg.payload to the string BIG EARTHQUAKE FOUND!.

9. Add a Debug node

Wire a new Debug node to the output of the Change node

10. Deploy

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

With the Debug sidebar tab open click the Inject button. You should see a list of entries with some contents that look like:

If there were any quakes with a magnitude greater than 7 you will also see debug messages like:

msg.payload : string(6)
"BIG EARTHQUAKE FOUND!!"

Summary

This flow is automatically triggered every 5 minutes and retrieves data from a URL. It parses the data and displays in the Debug sidebar. It also checks the magnitude value in the data and branches the flow for any messages with a magnitude greater than 6. The payloads of such messages are modified and displayed in the Debug sidebar.

Source

The flow created in this tutorial is represented by the following JSON. To import it into the editor, copy it to your clipboard and then paste it into the Import dialog.

[{“id”:”9dd59961.ceb908″,”type”:”tab”,”label”:”Flow 1″,”disabled”:false,”info”:””},{“id”:”ea7c9d52.b2556″,”type”:”inject”,”z”:”9dd59961.ceb908″,”name”:””,”topic”:””,”payload”:””,”payloadType”:”date”,”repeat”:”5″,”crontab”:””,”once”:false,”onceDelay”:0.1,”x”:110,”y”:100,”wires”:[[“5972b6d6.6385f8”]]},{“id”:”5972b6d6.6385f8″,”type”:”http request”,”z”:”9dd59961.ceb908″,”name”:””,”method”:”GET”,”ret”:”txt”,”url”:”https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_week.csv”,”tls”:””,”x”:220,”y”:180,”wires”:[[“7743de81.bb9c6”]]},{“id”:”7743de81.bb9c6″,”type”:”csv”,”z”:”9dd59961.ceb908″,”name”:””,”sep”:”,”,”hdrin”:true,”hdrout”:””,”multi”:”one”,”ret”:”\n”,”temp”:””,”skip”:”0″,”x”:340,”y”:240,”wires”:[[“9b80fad0.312d58″,”81232228.f4bad”]]},{“id”:”81232228.f4bad”,”type”:”console”,”z”:”9dd59961.ceb908″,”name”:””,”active”:true,”tosidebar”:true,”console”:false,”tostatus”:false,”complete”:”payload”,”x”:530,”y”:180,”wires”:[]},{“id”:”9b80fad0.312d58″,”type”:”switch”,”z”:”9dd59961.ceb908″,”name”:””,”property”:”payload.mag”,”propertyType”:”msg”,”rules”:[{“t”:”gt”,”v”:”6″,”vt”:”num”}],”checkall”:”true”,”outputs”:1,”x”:450,”y”:300,”wires”:[[“6a1e053d.c4a96c”]]},{“id”:”6a1e053d.c4a96c”,”type”:”change”,”z”:”9dd59961.ceb908″,”name”:””,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”BIG EARTHQUAKE FOUND!”,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:560,”y”:380,”wires”:[[“b227b4f1.83e928”]]},{“id”:”b227b4f1.83e928″,”type”:”console”,”z”:”9dd59961.ceb908″,”name”:””,”active”:true,”tosidebar”:true,”console”:false,”tostatus”:false,”complete”:”payload”,”x”:710,”y”:420,”wires”:[]}]

Was this article helpful?

Related Articles

Leave A Comment?