Skip to content

Latest commit

 

History

History
6 lines (5 loc) · 3.67 KB

02_ParsingKPNLoRa.md

File metadata and controls

6 lines (5 loc) · 3.67 KB

Below you will find an example of how to send and receive HTTPS in NodeRed and parse the LoRa data. Please refer to the wiki to get more explanation.

Copy the below code, and import through the clipboard in NodeRed

[{"id":"e0e4fb1f.e20908","type":"http in","z":"65bb5a5b.a49fc4","name":"LoRa Post Catcher","url":"/lorapost","method":"post","swaggerDoc":"","x":311.61456298828125,"y":322.0035400390625,"wires":[["5671845e.7e62dc"]]},{"id":"40bca731.7a2f48","type":"debug","z":"65bb5a5b.a49fc4","name":"","active":true,"console":"false","complete":"lorapayload","x":803.6111145019531,"y":320.3263854980469,"wires":[]},{"id":"c4e87a33.a564f8","type":"http response","z":"65bb5a5b.a49fc4","name":"LoRa Post Catcher","x":1040.8958435058594,"y":234,"wires":[]},{"id":"5671845e.7e62dc","type":"function","z":"65bb5a5b.a49fc4","name":"Parse Catch Input","func":"// Check if the body has a DevEUI_uplink parameter (Thingpark) or uses a simple format (KPN LoRa Developer Portal)\nif (msg.payload.hasOwnProperty(\"DevEUI_uplink\")) {\n    body = msg.payload.DevEUI_uplink;\n} // Check if the body has the right parameters for Developer Portal\nelse if (msg.payload.hasOwnProperty(\"payload_hex\")){\n    // Developer portal has a simpler json format without DevEUI_uplink\n    body = msg.payload;\n}else{\n    node.error(\"Not a valid Thingpark or Developer Portal message\", msg);\n    return;   \n}\n\n// Check if the query paramaters are there before continuing\nvar RequiredKeys = [\"LrnDevEui\",\"LrnFPort\",\"LrnInfos\",\"AS_ID\",\"Time\",\"Token\"];\nfor(var i=0; i < RequiredKeys.length; i++) {\n    key = RequiredKeys[i];\n    if (!msg.req.query.hasOwnProperty(key)){\n        node.error(\"Missing Query Parameter '\" + key + \"'\", msg);\n        return;\n    }\n}\n\n// Check if the body has the right parameters\nRequiredBodyElements = [\"CustomerID\",\"DevEUI\",\"FPort\",\"FCntUp\",\"payload_hex\"];\nfor(var i=0; i < RequiredBodyElements.length; i++) {\n    key = RequiredBodyElements[i];\n    if (!body.hasOwnProperty(key)){\n        node.error(\"Missing DevEUI_uplink Element  '\" + key + \"'\", msg);\n        return;\n    }\n}\n\nmsg.lorapayload = body.payload_hex;\nmsg.body = body;\n\nreturn msg;","outputs":1,"noerr":0,"x":532.8957824707031,"y":321,"wires":[["40bca731.7a2f48","a05a01db.44e9b"]]},{"id":"1954c90d.e316a7","type":"catch","z":"65bb5a5b.a49fc4","name":"","scope":null,"x":355.1632080078125,"y":236.2916717529297,"wires":[["ef742052.e5f7a"]]},{"id":"ef742052.e5f7a","type":"function","z":"65bb5a5b.a49fc4","name":"Handle Error","func":"// Add a topic property to our message to let other nodes know\n// that this message is an error.\nmsg.topic = \"error\";\n\n// The payload of this error message should say something \n// about the error. This payload is going to be passed back\n// to our HTTPS Response node. Note that the msg.error.message\n// is the message we gave as input in the node.error()\nmsg.payload = \"Encoutered Error: \"+ msg.error.message;\n\nreturn msg;","outputs":1,"noerr":0,"x":549.4514465332031,"y":236,"wires":[["a05a01db.44e9b"]]},{"id":"a05a01db.44e9b","type":"function","z":"65bb5a5b.a49fc4","name":"Create Post Resp","func":"if (msg.topic === \"error\"){\n    // In case of an error, do nothing with the message and\n    // pass it on to the reponse node. The msg.payload\n    // already contains the right information\n    return msg;\n}else{\n    // We know we have received a valid message\n    // Let's just pass back the lora data we have received\n    msg.payload = \"Received payload: \" + msg.lorapayload;\n}\nreturn msg;","outputs":1,"noerr":0,"x":794.4514465332031,"y":234,"wires":[["c4e87a33.a564f8"]]}]