thesolarnomad / lora-serialization

LoraWAN serialization/deserialization library for The Things Network
MIT License
164 stars 26 forks source link

TTN Encoder and Payload Format #24

Closed coastalplain closed 4 years ago

coastalplain commented 4 years ago

Thank you for creating these functions for TTN decoding and encoding! I have the decoding functioning properly, and I cannot figure out the encoder on the TTN console.

For the decoder, I followed most of the images to end up with this code working correctly:

function Decoder(bytes, port) { return decode(bytes, [temperature, humidity], ['temperature_C','humidity']); } //insert decoder.js below

Testing with hex 09 44 1D 0F as the Payload, it correctly gives the proper JSON output: { "humidity": 38.69, "temperature_C": 23.72 }

When I try to setup the encoder, I cannot figure out the proper way to set it up on TTN, with or without theLoraMessage(encoder) convenience class. The following is what I am trying, with several variations I don't want to enumerate. The 'temperature_C',' humidity' are my variables on arduino.

function Encoder(object, port) { return encode(object, [temperature, humidity], ['temperature_C','humidity']); } //lora-serialization/src/encoder.js //lora-serialization/src/LoraMessage.js

The fields I am using are the JSON format from the decoder: { "humidity": 38.69, "temperature_C": 23.72 } and the error comes out as: Internal error: Encoder threw error: TypeError("Values must be an array")

Thank you for your time.

joscha commented 4 years ago

hi @coastalplain , sorry I haven't had time to look at this yet in detail. The error message suggests that the setup is wrong, have you seen the images in the readme with the examples?

coastalplain commented 4 years ago

Hi @joscha: No worries, thank you for your time. edit: Yes, the images helped with the decoder. However, the TTN-side Payload Format encoder is currently my sticking point (to send downlinks). I have tried several options in the initial encoder function followed by encoder.js (I have also tried substituting function Encoder(json, port) with function Encoder(object, port). The eventual goal is to send a simple bitmap or uint8 to various ports to change node functionality.

image

I have the decoder working well: DECODER on TTN image

joscha commented 4 years ago

Just had time to look at this and I see the problem now. So the names of the fields are not actually sent over the wire in the encoder. If you look at the sample in the readme you can see:

var bytes = encode([timestamp, [latitude, longitude]], [unixtime, latLng]);

this effectively means:

In your case the Encoder you wrote 1) doesn't actually use the json object to read the values from that you want to encode 2) uses the strings ['temperature_C','humidity'] as the encoder functions.

So this one would actually work:

function Encoder(object, port) {
  return encode([object.temperature_C, object.humidity], [temperature, humidity]);
}

// encoder.js follows:

with a sample payload of:

{ "temperature_C": 10, "humidity": 20 }

however I just tried the TTN console to run it and I am getting a ReferenceError("'Buffer' is not defined") - it seems that TTN has dropped Buffer support maybe?

joscha commented 4 years ago

I posted in the TTN forum to see what happened there: https://www.thethingsnetwork.org/forum/t/buffer-not-defined-any-more/30168/1

coastalplain commented 4 years ago

Thank you; I will keep an eye on the TTN forum, and try to research the 'Buffer' is not defined error.