Downlink payloads

Just wondering if anyone has an example downlink payload you can share with me - I can send successful downlinks from the console. But i cannot for the life of me understand how to setup the encodedDownlink to do so from http or mqtt function. no matter what I have tried it always seems to send a blank command to port 0.

just noticed it doesn’t seem to decode the bas64 correctly if i send it as base64 from the console i get the same problem - whereas if i send it directly as a hex string from console it works.


this is the base64 encoded string i’m sending 0200010008010300030001740a

Your base64 encoded string has encoded the ASCII value of each character.

You will want to convert the hex to an array or buffer and then convert that to base64.

1 Like

Thanks that lets me send a downlink via MQTT so one down one to go.

The errors I get now are to do with the encodeDownlink(link) as I have no idea how the data sent is structured when it arrives to the encoder to encode the data - would be cool if this could be added to the docs for silly people like me.

curl -X 'POST' \
  'https://chirpstack-rest-api.example.com/api/devices/ac1f09fffe013738/queue' \
  -H 'accept: application/json' \
  -H 'Grpc-Metadata-Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjaGlycHN0YWNrIiwiaXNzIjoiY2hpcnBzdGFjayIsInN1YiI6ImIwZmE1Mzk0LTAzOTQtNGY0ZC1iMGU2LWQxNTgwYmE3NzQ0NiIsInR5cCI6ImtleSJ9.BFBCslkPA4bvWyJKaMosDXOHgTIa_VD2RDGPooiPECc' \
  -H 'Content-Type: application/json' \
  -d '{
  "queueItem": {
    "confirmed": true,
    "data": "AgAJAAgBAwADAAF0Cg==",
    "fCntDown": 9,
    "fPort": 129,
    "id": "string",
    "isPending": true,
    "object": {}
  }
}'

the error below explains I have screwed up - but idk how to structure this function atm.

{
  "code": 13,
  "message": "Exception generated by quickjs:  The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object\n    at from (buffer:167)\n    at <anonymous> (buffer:179)\n    at encodeDownlink (eval_script:134)\n    at <eval> (eval_script:150)\n",
  "details": []
}

I would look at the API call made by the UI using the browser developer tools. Inspect the network calls to see the JSON payloads.

This looks suspect, not sure why an empty field would need to be passed or named “object”.

"object": {}

This is odd too. Can you link to any example or doc you are working from?

"id": "string"

sure, thats just how it is structured in the swagger api - here is a screen shot. It must make it to the payload encoder - as I keep getting errors there (i’m not the best at js)

that one ^ showed this error

{
  "code": 13,
  "message": "Exception generated by quickjs: [eval_script]:139 invalid property name\n    at eval_script:139\n",
  "details": []
}

but the error is in the encode downlink function… this is just what i tried and got that error.

// Encode downlink function.
//
// Input is an object with the following fields:
// - data = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.

function encodeDownlink(input) {
  	let port = input.fPort;
  	//let data = atob(input.data);
  	//let data = Buffer.from(input.data);
  	//let data = input.data;
  	let data = hexToBytes(input.data);
 	
  	return {bytes: data};
  	//return {bytes: data, {fPort: port}};
}

function hexToBytes(hex) {
    for (var bytes = [], c = 0; c < hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}
function encodeDownlink(input) {
  	let port = input.fPort;
  	let data = input.data;
  	let buffer = Buffer.from(data, 'base64');
  	let payload = new Uint8Array(buffer);

  	return {bytes: payload, fPort: port};
}

produces this error… assuming data is at input.data??

{
  "code": 13,
  "message": "Exception generated by quickjs:  The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object\n    at from (buffer:167)\n    at <anonymous> (buffer:179)\n    at encodeDownlink (eval_script:134)\n    at <eval> (eval_script:147)\n",
  "details": []
}

so i’m kind of stuck here.

Okay if I remove the "object" : {} from the json payload my encodeDownlink function seems to work for rest/http requests now :wink:

thanks for helping me rubber duck this @Jason_Reiss especially with the base64 encoding.

PAYLOAD json body:

{
  "queueItem": {
    "confirmed": true,
    "data": "AgAKAAgBAwADAAF0Cg==",
    "fCntDown": 0,
    "fPort": 129,
    "id": "ArrayBuffer",
    "isPending": true
  }
}

Unchanged downlink function.

function encodeDownlink(input) {
	let port = input.fPort;
	let data = input.data;
	let buffer = Buffer.from(data, 'base64');
	let payload = new Uint8Array(buffer);

	return {bytes: payload, fPort: port};
}

This is why removing the object worked for my case - when you check the model of the json body in the chirpstack-rest-api.