-
Notifications
You must be signed in to change notification settings - Fork 9
5. Sending Downlink Messages
In the previous exercises you have created a server that receives HTTPS LoRa Posts, parses the data, checks the token and communicates with it's own website. Here we will now forward the data from the website to the KPN LoRa network.
Please refer to the topic on the KPN forum to see how a downlink should be set up.
- Remember when we did the parser that we added some error handling to have less trouble debugging? We need to revisit this catch node before continuing, because errors in the nodes not related to the HTTP post catcher cannot be handled in that flow. Click on the catch node, rename it to "Errors from catcher" and under "Catch errors from" select "selected nodes". Now Select the node names of the flow that handles the incoming LoRa messsages.
- Now we need to create a LoRa message. Select a function node & call it "Configure Device & Profile" and connect it to the "Receive from website". Here we will set up a message as outlined here. The below code results in a query string for the post to Thingpark, withouth the token.
/* Format the basics parameters of the message
Please adjust your DevEUI, AS_ID & SHAkey
*/
newmsg = {
"DevEUI" : "your_DevEUI",
"AS_ID" : "your_AS_ID",
"SHAkey" : "xxxxxxxxxxxxxxxx",
"ServerURL" : "https://loradeveloper.mendixcloud.com/rest/sendkpnloramessage",
"FPort" : "1"
};
/* First we check if the payload is correct in hex */
function isHex(h) {
var a = parseInt(h,16);
return (a.toString(16) === h.toLowerCase());
}
if (!isHex(msg.payload)){
node.error("Received non hexadecimal value '" + msg.payload + "'", msg);
return;
} else {
// Then we pass it on to the NodeRed Payload as a LoRaPayload
newmsg.LoRaPayload = msg.payload;
}
// Timestamp for the post message must be formatted in the right format.
// A "0" is added in front of the number of the date elements and only
// the last two characters are sliced of (making 5 -> 05 and 10 -> 10)
var date = new Date(Date.now());
var MonthIns = ("0" + (date.getMonth() + 1)).slice(-2);
var DateIns = ("0" + date.getDate()).slice(-2);
var HourIns = ("0" + (date.getHours() + 0 )).slice(-2);
var MinIns = ("0" + date.getMinutes()).slice(-2);
var SecIns = ("0" + date.getSeconds()).slice(-2);
var Offset = "Z";
var timestr = date.getFullYear() + "-" + MonthIns +"-" + DateIns + "T" + HourIns + ":" + MinIns + ":" + SecIns + Offset;
// Format the query string without the SHA token
newmsg.query_string_no_token = 'DevEUI='+ newmsg.DevEUI + '&FPort=' + newmsg.FPort + '&Payload=' + newmsg.LoRaPayload + '&AS_ID=' + newmsg.AS_ID + '&Time=' + timestr;
return newmsg;
Note that you will have to adjust the parameters to match your own DevEUI, AS_ID and LRC-AS key. You can find the last two at your profile in the Developer Portal or at the Application Server profile in Actility deviceManager. Furthermore you will have to provide the URL to post to. For the developer portal it's https://loradeveloper.mendixcloud.com/rest/sendkpnloramessage
for Thingpark https://api.kpn-lora.com/thingpark/lrc/rest/downlink
.
- Now we need to add the token. Like we did in exercise 3 for the uplink, please refer to https://github.com/emn178/js-sha256/blob/master/src/sha256.js to get the code. Copy paste it in a new function called "Add Token & Create URL" and add the code at the bottom:
msg.token = sha256(msg.query_string_no_token+msg.SHAkey);
NewMsg = {url : msg.ServerURL + '?' + msg.query_string_no_token + '&Token=' + msg.token};
NewMsg.payload = '{}';
return NewMsg;
- Note that we have created a message containing a
url
parameter and an empty payload. Now add a "http request" function and make sure this function is set to the POST method. The rest of the options of this node can be left as default. Connect it to your "Add Token & Create URL" node. - This node will make the request but also receives an answer. Connect a debug tab to it to see this answer. Your flow should look like this.
- You can now send some data. Go to your simple website from exercise 4 and enter some hexadecimal payload and hit "Send Downlink". View the response in your debug tab. Thingpark should say something like "Queued by the LRC" and the Developer Portal should answer with a json message
{"UniqueID" : "some_number"}
. - Using the debug tab is not very convenient so let's forward the response to our website. Create a function called "Resp 2 Web" and connect it to the output of the http-request node. Connect the output of the function to the "Send to Website" websocket node from exercise 4. Add this code to "Resp 2 Web".
NewMsg = {
"payload" : {
"type" : "ServerResponse",
"ServerResponse" : msg.payload
}
};
return NewMsg;
- Also Let's forward the used POST URL to our website, to be able to use it somewhere else (e.g. in postman). Create a function called "URL 2 web", hook this up to your "Add Token & Create URL" and output to "Send to Website". Add this code to your "URL 2 web" function:
NewMsg = {
"payload" : {
"type" : "DownlinkURL",
"DownlinkURL" : msg.url
}
};
return NewMsg;
- Your total flow should look like the one below. You can now use your simple website to send downlink messages and see the response :D!
This concludes the exercises for the NodeRed and LoRa. You can find the complete implementation of the exercise at https://github.com/iotacademy/NodeRed_KPN_LoRa/blob/master/NodeRed_KPN_LoRa_server.md. Hopefully, you will now understand enough of the mechanics around LoRa to create your own applications, whether it is with NodeRed or some other platform or language!