Working With URL API in Node-RED
by noderedhelper in Circuits > Remote Control
11542 Views, 1 Favorites, 0 Comments
Working With URL API in Node-RED
This instructable will teach you how to use URL API (http get) in node-RED. It is intentionally simple. And if you are relatively new to node-RED then this example is right for you. I will teach you how to use node-RED environment and what is, and how to use URL API.
For teaching purposes i will be using NETIO 4All smart power socket, but dont worry NETIO has nice online demo which we will be using so you dont have to buy anything.
URL API
*For teaching purposes I will explain with smart powersocket NETIO 4All
The string of characters received by the NETIO device in the URL is split into individual commands and the device then sets its outputs to the desired states according to the action number.
Actions with the output:
- 0 = Output switched off (Off)
- 1 = Output switched on (On)
- 2 = Output switched off for short time (short Off)
- 3 = Output switched on for short time (short On)
- 4 = Output switched from one state to the other (toggle)
- 5 = Output state unchanged (no change)
URL API example(toggles the state of output 1):
http://netio-4all.netio-products.com:8080/netio.cgi?pass=netio-psw&output1=4
Node-RED
But what is node-RED?
Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. Node-RED provides a browser-based flow editor and a wide choice of nodes and options.
It is built on Node.JS, making it ideal to run on low-cost hardware such as the Raspberry Pi as well as in the cloud.
Node-RED is a popular platform for IoT (Internet of Things) applications. A flow (that is, a script or a project) can be easily transferred as a configuration json file.
Flow
And now the main part. This is how node-RED environment looks like. In the next step i will show you how to import flows.
As you can see the Node-RED provides a browser-based flow editor that makes it easy to wire together flows using the wide range of nodes in the palette. Flows can be then deployed to the 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.
How to Import Flow - Step 1
In the menu, select Import -> Clipboard.
How to Import Flow - Step 2
Then, copy the text below to the indicated field and click Import.
[{"id":"53632275.7d628c","type":"tab","label":" NETIO AN29 (URL API)","disabled":false,"info":""},{"id":"bf404b4d.c9abf8","type":"http request","z":"53632275.7d628c","name":"HTTP Request (GET)","method":"GET","ret":"txt","url":"http://netio-4all.netio-products.com:8080/netio.cgi?pass=&{{msg.payload}}","tls":"","x":600,"y":160,"wires":[["2277782e.54d148","3c4f1015.9d8298"]]},{"id":"8a190541.5e1018","type":"function","z":"53632275.7d628c","name":"Action Selector","func":"switch(msg.payload)\n{\n case 0:\n return {payload : \"output1=0\"};\n case 1: \n return {payload : \"output1=1\"};\n case 2: \n return {payload : \"output1=2\"};\n case 3: \n return {payload : \"output1=3\"};\n case 4:\n return {payload : \"output1=4\"};\n}","outputs":1,"noerr":0,"x":400,"y":160,"wires":[["bf404b4d.c9abf8"]]},{"id":"2277782e.54d148","type":"debug","z":"53632275.7d628c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":810,"y":120,"wires":[]},{"id":"3c4f1015.9d8298","type":"ui_text","z":"53632275.7d628c","group":"e4da58a6.64a3d8","order":5,"width":"6","height":"3","name":"","label":"Response from server: ","format":"{{msg.payload}}","layout":"col-center","x":840,"y":200,"wires":[]},{"id":"24ae69d.9d70496","type":"ui_button","z":"53632275.7d628c","name":"Output 1 = ON","group":"a380c23b.f3499","order":1,"width":0,"height":0,"passthru":false,"label":"Output 1 = ON","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"num","topic":"","x":120,"y":80,"wires":[["8a190541.5e1018"]]},{"id":"bbd4e78b.886a18","type":"ui_button","z":"53632275.7d628c","name":"Output 1 = OFF","group":"a380c23b.f3499","order":2,"width":0,"height":0,"passthru":false,"label":"Output 1 = OFF","tooltip":"","color":"","bgcolor":"","icon":"","payload":"0","payloadType":"num","topic":"","x":120,"y":120,"wires":[["8a190541.5e1018"]]},{"id":"e75f5457.45d01","type":"ui_button","z":"53632275.7d628c","name":"Output 1 = Short ON","group":"a380c23b.f3499","order":3,"width":0,"height":0,"passthru":false,"label":"Output 1 = Short ON","tooltip":"","color":"","bgcolor":"","icon":"","payload":"2","payloadType":"num","topic":"","x":140,"y":160,"wires":[["8a190541.5e1018"]]},{"id":"77f6af6b.abc61","type":"ui_button","z":"53632275.7d628c","name":"Output 1 = Short OFF","group":"a380c23b.f3499","order":4,"width":0,"height":0,"passthru":false,"label":"Output 1 = Short OFF (Restart)","tooltip":"","color":"","bgcolor":"","icon":"","payload":"3","payloadType":"num","topic":"","x":140,"y":200,"wires":[["8a190541.5e1018"]]},{"id":"645a9bee.f96f5c","type":"ui_button","z":"53632275.7d628c","name":"Output 1 = Toggle","group":"a380c23b.f3499","order":5,"width":0,"height":0,"passthru":false,"label":"Output 1 = Toggle","tooltip":"","color":"","bgcolor":"","icon":"","payload":"4","payloadType":"num","topic":"","x":130,"y":240,"wires":[["8a190541.5e1018"]]},{"id":"e4da58a6.64a3d8","type":"ui_group","z":"","name":"Response from server","tab":"1bb571ba.89b2ce","order":2,"disp":true,"width":"6","collapse":false},{"id":"a380c23b.f3499","type":"ui_group","z":"","name":"Control Output 1","tab":"1bb571ba.89b2ce","order":1,"disp":true,"width":"6","collapse":false},{"id":"1bb571ba.89b2ce","type":"ui_tab","z":"","name":" NETIO AN29 (URL API)","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
Installing Missing Nodes
The nodes are loaded into the selected flow. It is possible that an error message is displayed with a list of blocks that are being imported but are not installed in Node-RED yet. In this case, the missing blocks need to be installed. If there are nodes missing, select Manage palette in the menu.
Then select Install and find and install the nodes you are missing.
Dashboard
Dashboard is Graphical interface by which you can operate your program if you want.
The Dashboard can be opened by clicking the indicated symbol, or at the address of your Node-RED server with ui appended, e.g.: 127.0.0.1:1880/ui
How It Actually Works
- Five buttons created in the Flow are displayed in the Dashboard.
- After clicking the Output 1 = ON button in the Dashboard, the payload is set to 1(defined in each of the buttons in order to specify the action).
- The Action Selector node selects the string that corresponds to the action(input payload) and creates the resulting URL string for controlling the smart power socket
- The HTTP Request (GET) node concatenates the IP address + the action from the input payload value, and sends the resulting string using HTTP GET Request. The server response(status) is returned as the output.
- msg.payload node displays the response from the HTTP server running in the netio 4All device.
- And Response from server node displays the server response in the Dashboard
Nodes Mentioned Above and Their Settings
More Information
Thank you for reading my instructable. I hope you enjoyed it and learned something new.
This instructable was quick guide on How to use URL API with node-RED
For detailed guide click on the link below.
Also there are similiar guides about various uses of node-RED so if you are interested feel free to explore:
Working with REST JSON in node-RED
Working with REST XML in node-RED
Working with TCP/Modbus in node-RED
Coming soon.