-
Notifications
You must be signed in to change notification settings - Fork 9
1. Receive with HTTPS nodes
KPN LoRa requires a server to receive HTTPS messages. In this exercise you will build a basic HTTPS receiver in Node-Red.
- In Node Red you can add a HTTPS node by selecting the node under input (left hand-side) and dragging it to your flow editor.
- Double click on the node to edit the parameters:
- Method We should listen to a POST command
- URL is the destination where the POST messages should be sent to. For instance “/lorapost”
- Name is the name of the Node, so whatever you like, here I chose “LoRa Post Catcher”
- From the HTTPS Node, the post should go somewhere. Select and drag a debug Node from the output nodes. Select the dot on your HTTP node and drag a line towards your debug node to connect the 2.
- Note: NodeRed passes around J-SON objects called a 'msg'. Every Node receives a msg and does something with it. Usually the msg.payload attribut contains the important data. In case of the HTTPS node it creates a msg object with the msg.payload containing the payload of the post message. The debug tab should therefore show the payload here. It does so automatically, but you can click on the Node to change the parameter you want to see.
- On the top right corner, click on the button deploy. You're changes are now saved and deployed on the server. You need to deploy each time you have changed something in your NodeRed and want to save it.
- In principle you are ready to do your first postmessage. The link to post to is "https://YourNodeRedServer.com/lorapost". If you're using bluemix you can get your NodeRed server adres by copying your browser adress (e.g. **http://yourname.eu-gb.mybluemix.net**/red/#) and changing it to https://yourname.eu-gb.mybluemix.net/lorapost. Important to at the s to the http, this is necessary for KPN LoRa messages.
If you try your url directly in your browser you should get Cannot GET /lorapost
. This is because a browser actually uses a GET method and we are using POST. We need to sent a proper POST message. There are several ways to do this, but the one we are going to use here is Postman (up to you of course :))
- Go to https://www.getpostman.com/ and follow the instructions to install a chrome app (or the mac version if you have a mac).
- Open the app:
- Set the method in the dropdown menu to POST (probably says GET when opening for the first time).
- Fill in your URL https://yourname.eu-gb.mybluemix.net/lorapost
- LoRa posts depend on query parameters, so try some here. Append
?query=example
to your URL at the end. Postman nicely opens other edditing options now, but you can ignore that. - You should also pass something in the body of the POST message. So click on the body tab below the URL part and type something.
- Click on the Send button. Go back to your NodeRed, click on the debugger on the right panel and see if any data has come in. If so, you are in business :)!
- But wait, what about the query parameters? You can find these in another property of the msg created by the HTTPS node:
msg.req.query
. Click on the debug node in your flow and change the "Output" toreq.msg
(or create a new debug node and add this one to the existing.
If you now go back to Postman you will see that Postman is still sending (of if you wait to long say something like 502 Bad Gateway: Registered endpoint failed to handle the request.
). This is because NodeRed does not answer. If you want you can cancel the request if you look at the bottom of the Postman screen. Later on we are going to need some feedback from our server if something went wrong. It might seem redundant now, but to save time later on we will add a response to the HTTPS post:
- Select the HTTP Response node. Name it "Create Post Response". In would be enough to connect it directly to your HTTP node, but I like to be able to change the response a bit so let's add some more.
- Select a function node. In a Function you can execute Node-JS or JavaScript code to make changes to your msg. Here we want to change the payload to "I have received something!". Adjust your code to do this and return the adjust msg.
msg.payload = "I have received something!";
return msg;
- Now hookup the output of the HTTP node to the input of the Function Node and the output of the Function node to the HTTPS reponse node and hit deploy.
- Make a post again in postman. You should receive something back now and your data should still be visible in the NodeRed debugger.
Your done with the first exercise. You can now move on to 2. Parsing KPN LoRa.
You can find the complete implementation of the exercise at https://github.com/iotacademy/NodeRed_KPN_LoRa/blob/master/01_ReceivingHTTPS.md if you are stuck somewhere.