Las temperaturas negativas de mi nodo LoRa aparecen como 65486 en Hermetry
Traducido del English por IA ·
Desarrollador backend, primer nodo LoRaWAN, y me siento tonto.Arduino Pro Mini + RFM95, LMIC, OTAA hacia Hermetry, EU868. Envía la temperatura de un DS18B20 como dos bytes, valor por 100:
int t = (int)(tempC * 100);
payload[0] = t >> 8;
payload[1] = t & 0xFF;
Escribí mi propio codec (copié el ejemplo de layout, fmt: "u16be", scale: 0.01). Funcionó todo el otoño. Anoche bajó de cero en el cobertizo y el gráfico saltó a 654.86. El registro de tramas muestra FF CE para ese uplink.¿Cómo pueden dos bytes convertirse en un número negativo? Pensaba que los bytes solo iban de 0 a 255.
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.
Traducido del English por IA ·
Sinceramente, la misma pregunta. Nunca entendí cómo se guarda el signo.
Same question honestly, I never understood how the sign is stored.
Traducido del English por IA ·
¡Ja, tuve exactamente esto con mi sensor del invernadero en enero pasado, así que por una vez puedo responder!El Arduino guarda int en complemento a dos. -0,5 °C por 100 da -50, y -50 en 16 bits es 0xFFCE. El bit más significativo es el signo. Leído como sin signo son 65486, por 0,01 da su 654,86. Leído como con signo es -50, por 0,01 da -0,5. Los mismos bytes; el codec solo tiene que saber que son con signo.Así que en su codec cambie el formato de u16be a i16be y mantenga la escala. Añada un vector de prueba para un valor negativo para que no vuelva a colarse:
{ "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 } }
]
En el lado del Arduino, use int16_t en lugar de int, para que siga ocupando 2 bytes si pasa a una placa de 32 bits. Luego pulse Check y Publish. El nombre de la clave y la unidad no cambian, así que puede publicarse como una nueva revisión del mismo codec. Referencia: Write your own codec
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
Traducido del English por IA ·
Las lecturas guardadas antes de la corrección conservan el valor incorrecto; si esa noche es importante, expórtala y corrígela a mano.
The readings stored before the fix keep the wrong value, so if that night matters, export it and correct it by hand.
Traducido del English por IA ·
Volví a publicar con i16be, ambos vectores de prueba pasan, y los uplinks de esta noche muestran -1.25 y -1.5, igual que el termómetro de al lado. También cambié el sketch a int16_t. Dejaré los dos puntos erróneos de anoche en el gráfico como recordatorio 😅
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 😅
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

