I have seen some examples on payload functions.
Is there any guide on writing payload functions - what language are you using - what functions are available?
I have seen some examples on payload functions.
Is there any guide on writing payload functions - what language are you using - what functions are available?
It looks like javascript, but either it is not or some of the convenient libraries are missing. Would have loved to use ArrayBuffer to convert from bytes to float. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
It’s JavaScript (ECMAScript 5) indeed. For uplinks:
bytes
: the binary payload as an array of numbers. Each number is a byte, 0 to 255 in decimal notation, which is the same as 0x00 to 0xFF in hexadecimal notitation.port
: the port number from 1 to 223.For downlinks:
Only the application payload and the port can be accessed, not any of the metadata such as the gateway(s) that received an uplink. See Access Metadata in decoder for alternatives.
To add the approximate time an uplink was received, one could use the server time:
var now = new Date().toISOString();
A good JavaScript resource is MDN Web Docs (formerly Mozilla Developer Network), and “mdn” is a great keyword for search engines: mdn array.
Also, you can use auto-completion in the developer tools of your browser. Just start by defining your payload with something like:
var bytes = [0xFF, 0x31, 0xF9, 0x12, 0x00, 0x31]
Or, to easily copy/paste a hexadecimal string such as 0xff31f9120031
:
var bytes = 'ff31f9120031'.match(/(..)/g).map(b => parseInt(b, 16))
Next, typing bytes.
will show you the supported functions and all:
Anything you send to console.log
is shown in TTN Console during testing:
console.log(bytes);
You should always test your format using the Payload input field in TTN Console, and the error events in +/devices/+/events/#
in the MQTT API can also be used to track errors at runtime, like:
Unable to decode payload fields: Internal error: Decoder threw error: ReferenceError: ‘myVariable’ is not defined
Unable to decode payload fields: Internal error: Interrupted javascript execution for Decoder after 153.496916ms
Payload Function output cannot be marshaled to JSON: json: unsupported value: NaN
Given the “Error” filter in the application/device’s Data page in TTN Console one might expect such errors to be shown there too, but: no. When not seeing payload fields in TTN Console, then paste the application payload bytes into the Decoder’s test input, or use MQTT to see any errors.
The payload formats may fail randomly, so consider decoding in your application instead. (And consider subscribing to the MQTT error events, regardless where you’re decoding.)
A nice read: Here is what you need to know about JavaScript’s Number type.
When doing bitwise operations, JavaScript will interpret its operands as 32-bits signed integers. (Other programming languages are unlikely to always assume 32-bits signed integers for bitwise operators.)
Don’t confuse binary with “hexadecimal”: How to send payload in Hex-format? - #4 by arjanvanb
Leading zeroes in number literals denote octal values.
While testing in TTN Console, null
values might not be shown. But integrations might actually get such null
values.
The Data Storage Integration silently fails for attributes with null
values and gets confused when the decoder outputs attributes that it uses itself, such as device_id
.
The Data Storage Integration stores nested objects as text.
An erroneous Converter might suppress output: "Fields" not being populated by decoder? - #2 by evs01
An erroneous Encoder may make a downlink being scheduled but not transmitted. Subscribe to the MQTT error events to see the error.
payload_raw
in your actual application (like an application that uses the MQTT Data API).Working with Bytes in the official documentation, also explaining bitwise operators such as <<
, |
and &
.
“Decode the Payload in the Console” in The Things Uno Workshop.
Handling (negative) 3 byte values in Best practices when sending GPS location data, using sign extension for both MSB and LSB.
A step-by-step example of byte shifting and sign extension in both binary and hexadecimal representation, in Payload Format / Decode for TM-901 / N1C2 sensor from KCS TraceME - #4 by arjanvanb.
Sending (negative) temperature and humidity values, optionally extended with battery level, using the port number to determine what type of packet is being decoded in Getting Badgerboard to work with TTN.
Decoding unsigned 32 bits numbers, getting a value from only a few bits from a byte, outputting arrays with nested data, pretty-printing hexadecimal output, and handling different message types in a single decoder, in Abeeway Microtracker Decoder - #2 by arjanvanb.
Working with a variable number of measurements, and variable types of measurements, indicated using bit masks, in Nemeus NIS-UL – Ultrasonic sensor - #13 by arjanvanb.
Converting a true IEEE-754 floating point in Decode float sent by Lopy as Node - #2 by arjanvanb.
Using the third-party lora-serialization library for encoding and decoding, a bit on accidentally mixing MSB and LSB, a bit more on (bytes[0] & 0x80 ? 0xFFFF<<16 : 0)
and bytes[0]<<24>>16
used for sign extension, and some more info on using port numbers, in Decrypting messages for dummies.
Decoding nibbles (4 bits) of a “packed binary-coded decimal” (BCD), like interpreting the hexadecimal bytes 0x0230 as decimal 230 (rather than, as more usual, 560), in Decoding Smart Waste Bin Detector DF702 payload.
Likewise in How to decode Elvaco CMi4110 standard M-Bus payload? - #22 by arjanvanb, which also shows iterating a variable-length payload based on metadata in that very payload.
Getting 3 12-bits counters, each encoded in 1.5 bytes (3 nibbles), from 4.5 bytes in Agoraopinion Feedback Button Payload decoder - #5 by arjanvanb.
Decoding 7 bytes into two 28 bits numbers (really; 3.5 bytes per number) in Help a Poor Guy with this Payload.
Getting two integers from a null-terminated string like "111 014"
in Simple payload conversion.
Working with struct
data on the node’s side, and decoding that in TTN Console, in How can I correctly cast my value in byte before sending - #3 by arjanvanb.
A Decoder for the Laird RS1xx, combining a byte for the signed integer part with another byte for the unsigned decimal part, in Laird RS1xx payload format.
A Decoder and Encoder for the Dragino LT-33222, also explaining why in the Encoder one needs & 0xFF
in bytes[2] = object.field>>8 & 0xFF
, in LT-33222-L LoRa I/O Controller - Payload decoder and downlink config.
Generic decoders on the Elsys website.
Decoders for their products on the Decentlab GitHub (but beware those may output a field device_id
which confuses the Data Storage Integration).
Decoders for, e.g., TEKTELIC Agriculture and KONA Sensor, Bosch Parking Lot Sensor, Smart Building Sensors, MCF88 LW12CO2, Netvox R312A Emergency Button and R718N37 3-phase current monitor, on the Sensational Systems GitHub.