Negative temperatures from my LoRa node show up as 65486 in Hermetry
Backend dev, first LoRaWAN node, and I feel silly.
Arduino Pro Mini + RFM95, LMIC, OTAA into Hermetry, EU868. It sends temperature from a DS18B20 as two bytes, value times 100:
int t = (int)(tempC * 100);
payload[0] = t >> 8;
payload[1] = t & 0xFF;
I wrote my own codec (copied the layout example, fmt: "u16be", scale: 0.01). Worked all autumn. Last night it went below zero in the shed and the chart jumped to 654.86. The frames log shows FF CE for that uplink.
How do two bytes turn into a negative number at all? I thought bytes were just 0 to 255.
Same question honestly, I never understood how the sign is stored.
Ha, I had exactly this with my greenhouse sensor last January, so I can answer for once!
The Arduino stores int as two’s complement. -0.5 °C times 100 is -50, and -50 in 16 bits is 0xFFCE. The top bit is the sign. Read as unsigned that’s 65486, times 0.01 gives your 654.86. Read as signed it’s -50, times 0.01 gives -0.5. Same bytes, the codec just has to know they’re signed.
So in your codec change the format from u16be to i16be, keep the scale. Add a test vector for a negative value so it never sneaks back:
{ "at": 0, "field": "temperature", "fmt": "i16be", "scale": 0.01, "unit": "°C" }
"tests": [
{ "name": "positive", "fport": 1, "hex": "0960", "expect": { "temperature": 24.0 } },
{ "name": "below zero", "fport": 1, "hex": "FFCE", "expect": { "temperature": -0.5 } }
]
On the Arduino side use int16_t instead of int, so it stays 2 bytes if you move to a 32-bit board. Then Check and Publish. The key name and unit stay the same, so it can go out as a new revision of the same codec. Reference: Write your own codec
The readings stored before the fix keep the wrong value, so if that night matters, export it and correct it by hand.
Republished with i16be, both test vectors pass, and tonight’s uplinks show -1.25 and -1.5 like the thermometer next to it. Changed the sketch to int16_t too. I’ll leave last night’s two wrong points in the chart as a reminder 😅
- You must be logged in to reply to this topic.
Join the conversation
Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

