HTTP Response Node Use, Sigfox Downlink

#1

I receive an HTTP Post, depending of the packet type received I should send back a packet.
HTTP Response is thought to send this packet, or only is an automatic answer with code type 200?
I have tried this node in my project but I'm not getting results.

To create the payload of this downlink, I need constructing a packet like this (Sigfox downlink)
{
'{{deviceId}}': {
'downlinkData':{{data}}
}
}
Example: { "234556":{ 'donwlinkData': "dadadadadadadada"}}
With a function node I tried to create this packet, where deviceId is an input:

var device= context.get('device')||0;
var data="dadadadadadadada"
if (msg.topic=="deviceId") {
    device={payload:msg.payload}
    context.set ("device",device)
}
var deviceId=device.payload;

var payload={
   deviceId : {
	'downlinkData':data
 }}

msg.payload=payload;
return msg;

The result of this msg payload

{"deviceId":{"downlinkData":"dadadadadadadada"}}, instead of what is desired:
{ "234556":{ 'donwlinkData': "dadadadadadadada"}}

0 Likes

#2

The http response node has to be the end of a flow that starts with the HTTP in node. But without seeing your flow its difficult to help you further.

0 Likes

#3

Thanks for your answer, I include here my flow, which is not working, in case it helps to clarify my question.

[{"id":"e9de0deb.075b6","type":"tab","label":"test","disabled":false,"info":""},{"id":"7281aa02.d4a504","type":"function","z":"e9de0deb.075b6","name":"get_device","func":"msg.payload=msg.payload.device\nmsg.topic=\"deviceId\"\nreturn msg;","outputs":1,"noerr":0,"x":365.5,"y":207.5,"wires":[["e420e000.ce009","16c1ab3a.ec4d25"]]},{"id":"468694fb.2cf1e4","type":"function","z":"e9de0deb.075b6","name":"get_data","func":"msg.payload= msg.payload.data\nmsg.topic=\"data\"\nreturn msg;","outputs":1,"noerr":0,"x":366.5,"y":261.5,"wires":[["af59c9ca.7ab848"]]},{"id":"21e946fb.aad83a","type":"ui_dropdown","z":"e9de0deb.075b6","name":"","label":"Input","place":"Select option","group":"eca7a136.be013","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":1,"type":"num"},{"label":"","value":2,"type":"num"},{"label":"","value":3,"type":"num"}],"payload":"","topic":"input","x":684.5,"y":108,"wires":[["16c1ab3a.ec4d25"]]},{"id":"1c1dbaa4.1fc005","type":"http response","z":"e9de0deb.075b6","name":"HTTP Downlink","statusCode":"","headers":{},"x":1155.5,"y":243,"wires":[]},{"id":"af59c9ca.7ab848","type":"function","z":"e9de0deb.075b6","name":"get_packetType","func":"var data=msg.payload;\nvar byte1=parseInt(data.substr(0,2),16);\nvar MASK_BIT_PACKET_TYPE = 0x80;\nvar int_packet_type = ((data & MASK_BIT_PACKET_TYPE) >> 7);\nmsg.payload=int_packet_type;\nmsg.topic=\"packetType\";\nreturn msg","outputs":1,"noerr":0,"x":559.5,"y":261.5,"wires":[["311cf8e0.10bdf"]]},{"id":"e420e000.ce009","type":"function","z":"e9de0deb.075b6","name":"response_2","func":"var device= context.get('device')||0;\nif (msg.topic==\"deviceId\") {\n    device={payload:msg.payload}\n    context.set (\"device\",device)\n}\nvar deviceId=device.payload\nmsg.payload={deviceId:{\"noData\":true}}\nreturn msg;","outputs":1,"noerr":0,"x":953.5,"y":312.5,"wires":[["1c1dbaa4.1fc005"]]},{"id":"16c1ab3a.ec4d25","type":"function","z":"e9de0deb.075b6","name":"response_1","func":"var device= context.get('device')||0;\nvar input= context.get('input')||0\n\nvar payload_zero=\"00000000000000\"\n\nif (msg.topic==\"deviceId\") {\n    device={payload:msg.payload}\n    context.set (\"device\",device)\n}\nif (msg.topic==\"input\") {\n    gps={payload:msg.payload}\n    context.set (\"input\", input)\n}\n\nvar deviceId=device.payload;\nvar inputdata=input.payload;\n\n\n/*\nNOT WORKING, WHY?\ninputdata=inputdata.toString(16)\nvar data=inputdata+payload_zero\n*/\nvar payload={\n    deviceId : {\n\t'downlinkData':data\n }}\n\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":946.5,"y":172.5,"wires":[["1c1dbaa4.1fc005"]]},{"id":"311cf8e0.10bdf","type":"switch","z":"e9de0deb.075b6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":727.5,"y":261.5,"wires":[["16c1ab3a.ec4d25"],["e420e000.ce009"]]},{"id":"ffc5dc88.8f6058","type":"http request","z":"e9de0deb.075b6","name":"","method":"GET","ret":"txt","url":"","x":175.5,"y":234.5,"wires":[["7281aa02.d4a504","468694fb.2cf1e4"]]},{"id":"eca7a136.be013","type":"ui_group","z":"","name":"Test","tab":"2cc6ac12.156964","disp":true,"width":"24","collapse":false},{"id":"2cc6ac12.156964","type":"ui_tab","z":"","name":"Test","icon":"dashboard"}]
0 Likes

#4

To receive a http request you need to start your flow with the HTTP node in the INPUT section.

The node named HTTP REQUEST in the FUNCTION section is for a flow initiating a http request within a flow.

image

0 Likes

#5

Sorry, my mistake, but appart from that I'm experienced errors like this:
PMnode: HTTP Downlink
msg : error
"Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"

correct flow:
[{"id":"e9de0deb.075b6","type":"tab","label":"test","disabled":false,"info":""},{"id":"7281aa02.d4a504","type":"function","z":"e9de0deb.075b6","name":"get_device","func":"msg.payload=msg.payload.device\nmsg.topic=\"deviceId\"\nreturn msg;","outputs":1,"noerr":0,"x":376.5,"y":221.5,"wires":[["e420e000.ce009","16c1ab3a.ec4d25"]]},{"id":"468694fb.2cf1e4","type":"function","z":"e9de0deb.075b6","name":"get_data","func":"msg.payload= msg.payload.data\nmsg.topic=\"data\"\nreturn msg;","outputs":1,"noerr":0,"x":366.5,"y":261.5,"wires":[["af59c9ca.7ab848"]]},{"id":"21e946fb.aad83a","type":"ui_dropdown","z":"e9de0deb.075b6","name":"","label":"Input","place":"Select option","group":"eca7a136.be013","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":1,"type":"num"},{"label":"","value":2,"type":"num"},{"label":"","value":3,"type":"num"}],"payload":"","topic":"input","x":678.5,"y":166,"wires":[["16c1ab3a.ec4d25"]]},{"id":"1c1dbaa4.1fc005","type":"http response","z":"e9de0deb.075b6","name":"HTTP Downlink","statusCode":"","headers":{},"x":1149.5,"y":261,"wires":[]},{"id":"af59c9ca.7ab848","type":"function","z":"e9de0deb.075b6","name":"get_packetType","func":"var data=msg.payload;\nvar byte1=parseInt(data.substr(0,2),16);\nvar MASK_BIT_PACKET_TYPE = 0x80;\nvar int_packet_type = ((data & MASK_BIT_PACKET_TYPE) >> 7);\nmsg.payload=int_packet_type;\nmsg.topic=\"packetType\";\nreturn msg","outputs":1,"noerr":0,"x":559.5,"y":261.5,"wires":[["311cf8e0.10bdf"]]},{"id":"e420e000.ce009","type":"function","z":"e9de0deb.075b6","name":"response_2","func":"var device= context.get('device')||0;\nif (msg.topic==\"deviceId\") {\n device={payload:msg.payload}\n context.set (\"device\",device)\n}\nvar deviceId=device.payload\nmsg.payload={deviceId:{\"noData\":true}}\nreturn msg;","outputs":1,"noerr":0,"x":953.5,"y":312.5,"wires":[["1c1dbaa4.1fc005"]]},{"id":"16c1ab3a.ec4d25","type":"function","z":"e9de0deb.075b6","name":"response_1","func":"var device= context.get('device')||0;\nvar input= context.get('input')||0\n\nvar payload_zero=\"00000000000000\"\n\nif (msg.topic==\"deviceId\") {\n device={payload:msg.payload}\n context.set (\"device\",device)\n}\nif (msg.topic==\"input\") {\n gps={payload:msg.payload}\n context.set (\"input\", input)\n}\n\nvar deviceId=device.payload;\nvar inputdata=input.payload;\n\n\n/*\nNOT WORKING, WHY?\ninputdata=inputdata.toString(16)\nvar data=inputdata+payload_zero\n*/\nvar payload={\n deviceId : {\n\t'downlinkData':data\n }}\n\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":945.5,"y":221.5,"wires":[["1c1dbaa4.1fc005"]]},{"id":"311cf8e0.10bdf","type":"switch","z":"e9de0deb.075b6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":727.5,"y":261.5,"wires":[["16c1ab3a.ec4d25"],["e420e000.ce009"]]},{"id":"80826ed1.78561","type":"http in","z":"e9de0deb.075b6","name":"","url":"","method":"post","upload":false,"swaggerDoc":"","x":192.5,"y":234.5,"wires":[["7281aa02.d4a504","468694fb.2cf1e4"]]},{"id":"eca7a136.be013","type":"ui_group","z":"","name":"Test","tab":"2cc6ac12.156964","disp":true,"width":"24","collapse":false},{"id":"2cc6ac12.156964","type":"ui_tab","z":"","name":"Test","icon":"dashboard"}]

0 Likes

#6

You can't have a dashboard node joining this flow like that.
For Node-RED to "serve" a webpage you need a flow that starts with a HTTP INPUT node and ends with a HTTP OUTPUT node.
If you want to use a variable from the dashboard you will need to save that to a context variable ( see documentation on website) and use that context variable in your flow.

1 Like

#7

Thanks for your answer, I'm completely halt with this issue. I thought I was already saving the dashboard variable in a context variable, in the function where it inputs, I thought I was saving it in a variable context name input.
Why I'm no able to use .toString() function over this variable.
Thanks for you help, is very much appreciated.

var device= context.get('device')||0;
var input= context.get('input')||0

var payload_zero="00000000000000"

if (msg.topic=="deviceId") {
    device={payload:msg.payload}
    context.set ("device",device)
}
if (msg.topic=="input") {
    gps={payload:msg.payload}
    context.set ("input", input)
}

var deviceId=device.payload;
var inputdata=input.payload;


/*
NOT WORKING, WHY?
inputdata=inputdata.toString(16)
var data=inputdata+payload_zero
*/
var payload={
    deviceId : {
	'downlinkData':data
 }}

msg.payload=payload;
return msg;
0 Likes

#8

So can we agree that a http request starts with a HTTP-IN node and ends with the HTTP-OUT node?

So when a message comes in from your dashboard a message then follows the path arrives at the HTTP-OUT node but hasn't been initiated by the HTTP-IN node, so you get an error. Thats why you need to separate your flows.

[DASHBOARD]------[Save global context value ]
[HTTP-IN]----[Get global content value]---[HTTP_OUT]

see https://nodered.org/docs/writing-functions#storing-data

In your example function above
var device= context.get('device')||0; if it doesn't exist device = 0 i.e. a variable
But then later in your if statement you set it to device={payload:msg.payload} i.e an object

But the line var inputdata=input.payload; assumes it is an object, so if it is 0 this line doesn't work.

Also

if (msg.topic=="input") {
    gps={payload:msg.payload}
    context.set ("input", input)
}

You set gps to something but never use it, so if msg.topic ==="input' you set input to the value you got from the context earlier. Is this what you mean to do?

0 Likes

#9

Thanks!! now I understand better how to store variables and I was able to http a response without errors.
Now there is only one thing that is not working. I tried different options, but I'm not being able to form the correct downlink packet.
I need to create a donwlink packet with this format:
{
'{{deviceId}}': {
'downlinkData':{{data}}
}
}
Example: { "234556":{ 'donwlinkData': "dadadadadadadada"}}

But I'm only getting this:
"deviceId":{"downlinkData":"0c01020000000000"}}
instead of deviceId, shoud appear the value of deviceId, like in the example.

Here is the specific code:
var deviceId = flow.get('deviceId')
var payload={
deviceId : {
'downlinkData':data
}}
msg.payload=payload

Thank you so much

Here is the updated node-red flow storing variables:

[{"id":"e9de0deb.075b6","type":"tab","label":"test","disabled":false,"info":""},{"id":"7281aa02.d4a504","type":"function","z":"e9de0deb.075b6","name":"get_device","func":"var deviceId=flow.set('deviceId',msg.payload.device)\nmsg.payload=msg.payload.device\nmsg.topic=\"deviceId\"\nreturn msg;","outputs":1,"noerr":0,"x":395.5,"y":192.5,"wires":[[]]},{"id":"468694fb.2cf1e4","type":"function","z":"e9de0deb.075b6","name":"get_data","func":"msg.payload= msg.payload.data\nmsg.topic=\"data\"\nreturn msg;","outputs":1,"noerr":0,"x":366.5,"y":261.5,"wires":[["af59c9ca.7ab848"]]},{"id":"21e946fb.aad83a","type":"ui_dropdown","z":"e9de0deb.075b6","name":"","label":"Input","place":"Select option","group":"eca7a136.be013","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":1,"type":"num"},{"label":"","value":2,"type":"num"},{"label":"","value":3,"type":"num"}],"payload":"","topic":"input","x":495.5,"y":114,"wires":[["df39a0c3.85ff9"]]},{"id":"1c1dbaa4.1fc005","type":"http response","z":"e9de0deb.075b6","name":"HTTP Downlink","statusCode":"","headers":{},"x":1091.5,"y":240,"wires":[]},{"id":"af59c9ca.7ab848","type":"function","z":"e9de0deb.075b6","name":"get_packetType","func":"var data=msg.payload;\nvar byte1=parseInt(data.substr(0,2),16);\nvar MASK_BIT_PACKET_TYPE = 0x80;\nvar int_packet_type = ((data & MASK_BIT_PACKET_TYPE) >> 7);\nmsg.payload=int_packet_type;\nmsg.topic=\"packetType\";\nreturn msg","outputs":1,"noerr":0,"x":559.5,"y":261.5,"wires":[["311cf8e0.10bdf"]]},{"id":"16c1ab3a.ec4d25","type":"function","z":"e9de0deb.075b6","name":"response_1","func":"var input = flow.get('input')\nvar deviceId = flow.get('deviceId')\nvar payload_zero=\"00000000000000\"\n\ninput=input.toString(16)\nif (input.length===1)\n{ var str=\"0\";\n input=str.concat(input)}\n\nvar data= input.concat(payload_zero)\n\nvar payload={\n deviceId : {\n\t'downlinkData':data\n }}\nmsg.payload=payload\nreturn msg;","outputs":1,"noerr":0,"x":883.5,"y":215.5,"wires":[["1c1dbaa4.1fc005","27f4edc7.dfe542"]]},{"id":"311cf8e0.10bdf","type":"switch","z":"e9de0deb.075b6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":727.5,"y":261.5,"wires":[["16c1ab3a.ec4d25"]]},{"id":"80826ed1.78561","type":"http in","z":"e9de0deb.075b6","name":"","url":"/test","method":"post","upload":false,"swaggerDoc":"","x":201.5,"y":259.5,"wires":[["7281aa02.d4a504","468694fb.2cf1e4"]]},{"id":"27f4edc7.dfe542","type":"debug","z":"e9de0deb.075b6","name":"response1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1082.5,"y":201,"wires":[]},{"id":"df39a0c3.85ff9","type":"function","z":"e9de0deb.075b6","name":"Store var","func":"var data=msg.payload;\nvar input=flow.set('input',data)||{}\n","outputs":1,"noerr":0,"x":656.5,"y":113.5,"wires":[[]]},{"id":"eca7a136.be013","type":"ui_group","z":"","name":"Test","tab":"2cc6ac12.156964","disp":true,"width":"24","collapse":false},{"id":"2cc6ac12.156964","type":"ui_tab","z":"","name":"Test","icon":"dashboard"}]

0 Likes