Encoder: How to?

Hello,
We have a project for school , where we need to send data to TTN.
I’m sending JSON but i need to encode it so that TTN gets bits, can someone push me in the right direction?
On how to write the encoder or does someone has a programming solution or library?
I can receive the data from TTN with NodeJs but i can’t send a message back to the device via TTN.
Thanks

You can just use a struct, eg

struct {
  unsigned short temp;
  unsigned short pres;
  byte power;
  byte rate2;
} mydata;

And fill and send that:

mydata.power= 26;

LMIC_setTxData2(1, (unsigned char *)&mydata, sizeof(mydata), 0);

Thanks for your reply, but i forgot to mention i’m using NodeJs to communicate with TTN.

Did you see https://www.thethingsnetwork.org/docs/applications/nodejs/ ?

2 Likes

Yes, i’ve read the docs, i can receive data from my device via TTN.
Let me explain the project, so the device sends GPS data via TTN, we capture the data and put it on a map to make a GPS tracker but with the first message from the device, i need to send a message back to link the device id to a name.
But when i try to send a message i get an error, i don’t remember exactly what the error was because i’m not at school at the moment.
The error was that there was no encoder to format json to bytes.
Now my question was, how can i make an encoder that formats json to bytes?

Then see https://www.thethingsnetwork.org/docs/devices/uno/quick-start.html#encode-messages along with

{
  "payload_fields": {
    "led": true
  }
}

…from https://www.thethingsnetwork.org/docs/applications/mqtt/quick-start.html#send-messages-down

Or use the following (without any Encoder function at all) from the last link:

{
  "payload_raw": "AQ=="
}
1 Like

I’ve found the error “Downlink Payload not valid: fields supplied, but no Encoder function set”.
I tried to convert the json string to base64 and send it but it doesn’t even arrive at the device.

I have fixed my error.
I formatted the JSON to base64 and that fixed my error :slight_smile:
Thanks a lot for the help!