How to send bytes data to downlink

Hello :slight_smile:
I have downlink url from uplink request body
and want to post it on postman.

on Application console - device - I sent bytes data to change interval of checking temp.
34

It work like charm!!!

but wonder how to do it on postman.
I searched it for it but says I should put device id and payload_raw on body.
is it so?
should I put bytes data to payload_raw?
like below?

{
  "dev_id": "xxxxxxxxx", // The device ID
  "port": 11,            // LoRaWAN FPort
  "confirmed": false,    // Whether the downlink should be confirmed by the device
  "payload_fields": {    // The JSON object to be encoded by your encoder payload function
    0B 81 04 00 00 01 2C
  }
}

Yes, you’ll have to Base64 encode the bytes, and put that in payload_raw. So, for 0x0B81040000012C, use:

{
  "dev_id": ...,
  ...,
  "payload_raw": "C4EEAAABLA=="
}

Alternatively, provide an Encoder function in TTN Console, and pass human-readable values in payload_fields. Like, just as an example, if your encoder would be:

function Encoder(object, port) {
  var bytes = [];
  // Encode 8 boolean values into a single byte
  bytes[0] |= object.my1stBoolean ? 1<<0 : 0;
  bytes[0] |= object.my2ndBoolean ? 1<<1 : 0;
  bytes[0] |= object.my3rdBoolean ? 1<<2 : 0;
  bytes[0] |= object.my4thBoolean ? 1<<3 : 0;
  bytes[0] |= object.my5thBoolean ? 1<<4 : 0;
  bytes[0] |= object.my6thBoolean ? 1<<5 : 0;
  bytes[0] |= object.my7thBoolean ? 1<<6 : 0;
  bytes[0] |= object.my8thBoolean ? 1<<7 : 0;

  // Unsigned decimal in range from 0 to 655.35, or signed decimal in
  // range from -327.68 to +327.67, multiplied by 100 to be sent as 
  // signed integer; Most Significant Byte first
  bytes[1] = (object.my16bitsDecimal * 100)>>8 & 0xFF;
  bytes[2] = (object.my16bitsDecimal * 100) & 0xFF;

  // 32 bits value, MSB
  bytes[3] = object.my24bitsInteger>>24 & 0xFF; 
  bytes[4] = object.my24bitsInteger>>16 & 0xFF; 
  bytes[5] = object.my24bitsInteger>>8 & 0xFF; 
  bytes[6] = object.my24bitsInteger & 0xFF;

  return bytes;
}

…then for the exact same result, you could use:

{
  "dev_id": ...,
  ...,
  "payload_fields": {
    "my1stBoolean": true,
    "my2ndBoolean": true,
    "my3rdBoolean": false,
    "my4thBoolean": true,
    "my5thBoolean": false,
    "my6thBoolean": false,
    "my7thBoolean": false,
    "my8thBoolean": false,
    "my16bitsDecimal": -325.08,
    "my24bitsInteger": 300
  }
}

Thanks for answer :slight_smile:

I tried your first way base 64 encode, but 400 bad request error. why is it?
can you guess?

23

The screenshot shows you’re using payload_fields instead of payload_raw. The Base64-encoded payload should go into payload_raw. Only use payload_fields if you’ve defined an Encoder in TTN Console.

Your MHgwQjgxMDQwMDAwMDAxRQ== converts to hexadecimal 30783042383130343030303030303145 which is ASCII text. Converting that hexadecimal representation into an ASCII representation gives me 0x0B81040000001E again.

So, you’ve encoded twice. That’s wrong.

To send the byte 0x0B you should send a single value, basically the bits 00001011. But above you’re sending the human-readable ASCII characters 0 and B, which in hexadecimal notation are the bytes 0x30 and 0x78, or when displayed as bits are 00110000 01111000, so twice as many bits. See also https://www.thethingsnetwork.org/docs/devices/bytes.html

So, you should not Base64-encode the human-readable hexadecimal text representation, but you should Base64-encode the actual binary value. For manual testing use the Base64 encoder I already linked to in my answer above, which already expects human-readable hexadecimal text as its input. See also Advantage of Base64 encoding in MQTT?

In short, you should use the example I gave you:

How did you Base64-encode the value you’re using?

1 Like

Hi @arjanvanb

i have dragino lora i/o (LT-33222-L), according to datasheet for activate relay on/of just sending 0x030101 to downlink , i try send downlink via ttn console and it works i can activate on/off relay, but why i can’t activate relay via button in my node-red

im using this json payload format
{
“dev_id”: “dev_dragino”,
“confirmed”: false,
“port”: 2,
“payload_raw”: “AwEB”
}

i can see data on schedule in my downlink but not activate the relay, any idea to solve this?