2025-01-09 4:52 AM
Hi everyone,
I am working on an STM32WL-based device and have successfully operated downlink in LoRaWAN communication when using the following JSON payload format:
{
"devEui": "0102030405060708",
"confirmed": true,
"fPort": 10,
"data": "ESIzRFU="
}
In this case, I sent a base64-encoded data string, and the downlink was successfully received by the device. Here's the relevant log output from the device:
[16:16:14:337] 1735817707s657:RX_C on freq 869525000 Hz at DR 0␍␊
[16:17:20:696] 1735817774s061:MAC rxDone␍␊
[16:17:40:846] +EVT:RX_C, PORT 10, DR 0, RSSI -32, SNR 5␍␊
[16:17:40:890] +EVT:12:1122334455␍␊
However, when I attempt to send the following JSON payload, where I pass individual parameters in the object field instead of a base64-encoded string, the payload is successfully sent from the server, but the STM32WL device only receives the fport value. The data and datasize fields are both zero on the device.
{
"devEui": "0102030405060708",
"confirmed": true,
"fPort": 10,
"object": {
"deviceId": 01020304AABBCCDD,
"cmdType": 01,
"cmdId": 01,
"epochTime": 677FAA90
}
}
Has anyone encountered a similar issue or can suggest how to resolve this?
Thanks in advance for your help!
Best regards,
Pratham
2025-01-17 2:11 AM
@PrathamSalunkhe wrote:Thank you for pointing out the 84 bytes. Could you explain what exactly contributes to this size? .
Just count the characters!
1 character = 1 byte.
@PrathamSalunkhe wrote:does this imply that JSON payloads are inherently inefficient for LoRaWAN due to their size?.
JSON payloads are inherently inefficient for any use due to their size!
I would be 99.999999999% certain that the JSON is not sent over LoRaWAN - it is purely used as way for you to put data into your network's API.
2025-02-09 6:45 PM - edited 2025-02-09 6:54 PM
Hi @PrathamSalunkhe,
I understand the issue you're facing. I'm also working on some projects related to Class C with the ChirpStack server. When I use the JSON data field for downlink, I have to convert the data from ASCII encoding to a byte array. ChirpStack provides a place to write code for the encode() function for downlink data, so I think you also need to find a similar place to configure the encode() function for JSON data to ensure the device receives the correct data when downlinking.
If you are using the AWS IoT Core application integrated with the TTN server, TTN provides a place where you can modify the decoder() or encoder() functions for the payload using JavaScript.
https://aws.amazon.com/blogs/iot/connect-your-devices-to-aws-iot-using-lorawan/
// Encode encodes the given object into an array of bytes.
// - fPort contains the LoRaWAN fPort number
// - obj is an object, e.g. {"temperature": 22.5}
// - variables contains the device variables e.g. {"calibration": "3.5"} (both the key // value are of type string)
// The function must return an array of bytes, e.g. [225, 230, 255, 0]
function Encode(fPort, obj, variables) {
var bytes = [];
// Encode obj values
var jsonString = JSON.stringify(obj);
// Encode the entire JSON string including quotes
for (var i = 0; i < jsonString.length; i++) {
bytes.push(jsonString.charCodeAt(i)); // ASCII value of each character
}
return bytes;
}