Environment_Monitoring Example no negative temperature readings

I have been working on a LoRaWan deep sleep sensor node for outdoor weather monitoring, temperature, humidity, air pressure, etc. and I have run into a snag where by the readings do not go negative. I used some of your example sketches to create my own and I thought I did something wrong so I tried one of your example sketches to verify and I see it also can only send temperature values of zero and above. At least the way I am testing it. I am using the Environment_Monitoring example with the BME680 and created a end device in TTN and have the programmed node running off a battery in my freezer. I created a payload formater to display the values the error could be there or in how the data is put in to the lorwan payload. I see you are using

uint16_t t = temp * 100;
uint16_t h = hum * 100;
uint32_t pre = pres * 100;

Does this not convert the originally define sensor value from double to an unsigned 16 bit integer value? and then when you move the data bytes into the payload you use uint8_t. I did some experimentation and printed out the value “t” in the example with a uint16_t and int16_t and found the sign and hence negative temperatures get preserved. However as they get moved to the payload I tried int8_t but the resulting value on the TTN was a crazy high number. Something isn’t right. So my question is How do you send negative values through LoRaWan to the TTN using your example sketches?

Hi @mdede439 you are correct, you can change data types from unsigned to signed (uint16_t to int16_t).
Try this code in extracting the negative values of temp variable in your Payload formatters parameter using TTN:

var temp = (bytes[1] & 0x80 ? 0xFFFF<<16 : 0) | bytes[1]<<8 | bytes[2];
temp = (temp/100.0);
decoded.temp = temp;

Hope this code will help.

1 Like

Yes, that does help and works.

Appreciate the support!!

1 Like

Hi @mdede439, I’m happy the code solves your problem, thank you.