The Weble IoT universal gateway LoRaWAN driver turns the gateway into a LoRaWAN end-device: values coming from Modbus, BACnet, M-Bus or any other driver are sent as uplinks to a LoRaWAN network server such as The Things Stack (The Things Network) or ChirpStack. Each uplink carries one value, encoded in binary according to the address settings. Without a payload decoder, the network server only shows these bytes in hexadecimal.
This article explains how the value is encoded, gives a ready-to-use decoder, and shows how to size the traffic. The gateway configuration (module, OTAA keys, radio parameters) is described on the LoRaWAN driver page.
The gateway must already be registered on the network server (OTAA) and joined:
%lora:LoRaWAN/networkStatusshowsactive. Until then, every write to an Emit address is refused withDevice not active.
Each address of the Emit tab sends its value on its own LoRaWAN port (FPort):
| Address | Uplink |
|---|---|
%lora:U<port> |
Unconfirmed uplink (Unreliable data) on FPort <port>. |
%lora:C<port> |
Confirmed uplink (Reliable data) on FPort <port>: the network acknowledges it, and the module retransmits it up to CData Retransmissions times (default 7) if no acknowledgment is received. |
The encoding is chosen with Value encode in the address form. The form proposes floatBE.
| Value encode | Bytes | Content | Accepted values |
|---|---|---|---|
floatBE / floatLE |
4 | IEEE 754 single-precision float, big-endian / little-endian | Numbers. About 7 significant digits. |
int16BE / int16LE |
2 | Signed integer, two's complement | Numbers from -32768 to 32767. Decimals are dropped. |
int32BE / int32LE |
4 | Signed integer, two's complement | Numbers from -2147483648 to 2147483647. Decimals are dropped. |
uint32BE / uint32LE |
4 | Unsigned integer | Numbers from 0 to 4294967295. Decimals are dropped. |
utf8 |
variable | The value as text | Any value. |
object |
variable | The value as JSON text, for example {"t":21.5,"on":true} |
Objects only. |
BE (big-endian) sends the most significant byte first, LE (little-endian) the least significant byte first. For example, 21.5 is sent as 41 AC 00 00 in floatBE and as 00 00 AC 41 in floatLE.
"21.5", is refused with Invalid number value (address status ! Invalid number value). Convert it in the route if needed.21.5 → 215 in int16BE) and divide by 10 in the decoder.utf8). Open the address and set Value encode explicitly.10: the address is %lora:U10. Set Value encode to floatBE.%M10) to %lora:U10. See Routes.0.2 and Disable delay 600000 (one uplink every 10 minutes at most), with Repeat */30 * * * * to send the value at least every 30 minutes even when it does not change.When the route writes a value, the address status goes to pending, then online when the module has accepted the uplink. %lora:LoRaWAN/pendingActions shows how many uplinks are waiting.
The same gateway can send other quantities on other ports, for example a meter index on %lora:U11 in uint32BE and an alarm text on %lora:U12 in utf8.
In The Things Stack, open the end device (or the application), then Payload formatters › Uplink, choose Custom Javascript formatter and paste the decoder below. Adapt the FORMATS table: one line per FPort, with the Value encode of the matching address.
// Decoder for WBox LoRaWAN uplinks (The Things Stack and ChirpStack v4)
var FORMATS = {
10: { name: "temperature", type: "floatBE" },
11: { name: "energy_kwh", type: "uint32BE" },
12: { name: "alarm_text", type: "utf8" }
};
function u32(b, i, le) {
return (le ? (b[i+3] << 24) | (b[i+2] << 16) | (b[i+1] << 8) | b[i]
: (b[i] << 24) | (b[i+1] << 16) | (b[i+2] << 8) | b[i+3]) >>> 0;
}
function float32(b, i, le) {
var bits = u32(b, i, le);
var sign = (bits >>> 31) ? -1 : 1;
var exp = (bits >>> 23) & 0xff;
var man = bits & 0x7fffff;
if (exp === 0xff) return man ? NaN : sign * Infinity;
if (exp === 0) return sign * man * Math.pow(2, -149);
return sign * (1 + man / 8388608) * Math.pow(2, exp - 127);
}
function decodeValue(bytes, type) {
switch (type) {
case "floatBE": return float32(bytes, 0, false);
case "floatLE": return float32(bytes, 0, true);
case "int16BE": var a = (bytes[0] << 8) | bytes[1]; return a & 0x8000 ? a - 0x10000 : a;
case "int16LE": var c = (bytes[1] << 8) | bytes[0]; return c & 0x8000 ? c - 0x10000 : c;
case "int32BE": return u32(bytes, 0, false) | 0;
case "int32LE": return u32(bytes, 0, true) | 0;
case "uint32BE": return u32(bytes, 0, false);
case "uint32LE": return u32(bytes, 0, true);
default: // utf8 and object: plain text
var s = "";
for (var k = 0; k < bytes.length; k++) s += String.fromCharCode(bytes[k]);
return type === "object" ? JSON.parse(s) : s;
}
}
function decodeUplink(input) {
var f = FORMATS[input.fPort];
if (!f) return { errors: ["unknown fPort " + input.fPort] };
var data = {};
data[f.name] = decodeValue(input.bytes, f.type);
return { data: data };
}
With the example of section 3.1, an uplink on FPort 10 with the bytes 41 AC 00 00 is decoded as {"temperature": 21.5}.
0.1 arrives as 0.10000000149011612): this is the precision of a 4-byte float. Round it in the decoder if needed, for example Math.round(value * 100) / 100.utf8 and object values.ChirpStack v4 uses the same decodeUplink(input) function: in the device profile, open Codec, choose JavaScript functions and paste the same code.
ChirpStack v3 expects a function named Decode. Add this wrapper at the end of the code:
function Decode(fPort, bytes, variables) {
return decodeUplink({ fPort: fPort, bytes: bytes }).data;
}
The gateway also receives downlinks. The address is created automatically when the first downlink arrives on a port:
| Address | Downlink |
|---|---|
%lora:RU<port> |
Unconfirmed downlink received on FPort <port>. |
%lora:RC<port> |
Confirmed downlink received on FPort <port>. |
32 31 2E 35 give the text 21.5.{"setpoint":21.5}) and set Value encode of the receive address to object: the value becomes an object that a route can use directly.parseFloat(value) in the route function).When downlinks arrive depends on the LoRaWAN class. A class A device only receives a downlink just after one of its uplinks, so the delay depends on how often the gateway sends. With Class C - Support enabled (the default), the module can also receive at any time if the network server runs the device in class C.
LoRaWAN is designed for a few small messages per hour. The available air time is limited:
pending. Keep it enabled outside a laboratory.LoRa SF12 / 125 kHz / 250 bps, even a 4-byte uplink takes more than a second of air time, so the 1 % rule allows roughly one uplink every two minutes. At LoRa SF9 / 125 kHz / 1760 bps (default) it is several times faster. Adaptive data rate is active, so the network server may change the data rate depending on the radio conditions.HCI message length is invalid or radio payload size is too large.How the gateway queues the uplinks:
%lora:LoRaWAN/pendingActions gives the length of the queue. A value that keeps growing means that you send more than the radio can carry.Good practice:
floatBE, int16BE...) rather than text.| Symptom | Cause and fix |
|---|---|
| The network server shows the payload in hexadecimal only | No uplink decoder: add the decoder of section 3.2. |
Decoded values are absurd (for example 1.1e-41 instead of 21.5) |
The decoder and the address do not use the same format or byte order: compare Value encode with the FORMATS table. |
The decoder returns unknown fPort |
The FPort is missing from the FORMATS table. |
Address status ! Invalid number value |
A numeric format is selected but the value written is not a number. |
Address stays pending and pendingActions grows |
Duty cycle reached: send less often or use a faster data rate. |
For the other symptoms (join, serial port, licence), see the troubleshooting section of the LoRaWAN driver page.