-
Notifications
You must be signed in to change notification settings - Fork 65
Negative temps #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I realize this is a pretty old post, but I'm struggling to get this fix to work. Should I be replacing these three lines: if (temp & 0x8000) { With JUST: temp = -(temp & 0x7fff); Or should the statement be: if (temp & 0x8000) { Thanks! |
I've not tested this with a real device, but if you don't include the conditional, all your temps would be negative |
You can sign extend much more easily than this: temp = (temp<<16)>>16; This will sign extend a 16 bit value with no conditional. See https://developer.electricimp.com/squirrel/squirrel-guide/operators and "bit-shift operators". |
Thank you so much, this worked perfectly. |
Change checked in for future reference :) |
I spoke too soon, the temperature has fallen below 0 celsius and I'm back to getting readings like "-32762.0" (at least they're negative now). RH readings are working perfectly, so I don't think it's the sensor. |
Can you print the raw hex you're receiving for these? |
Hopefully this is what you need. I uncommented out this line: And what I'm getting is: 2020-12-26T22:02:06.167 +00:00 | [Device] | parsed: 0x 024f 8007 d8 |
The DHT22 datasheet leaves a lot to be desired, but from what I can see, this is not standard 2's complement negative. I don't have a DHT22 to test with, but going to revert the change here. It seems like if bit 15 is set, bits 0-14 are the negative temperature, in 1/10ths of a degree. ie: 0x000a = +1C (vs 2's complement, where 0xfff6 would be -1C) |
Thanks for the quick reply! Unfortunately I'm not a very experienced coder, so I'm not sure what to do what the info above. |
If you just check the DHT22 code, it should be correct now. |
Sorry for the previous comment - I just noticed the update to the code. I've updated my device code and what used to break things (any temp below 0C) seems to be working properly now. I'll jump back in once it warms up here if anything strange happens above 0C. Thanks again! |
The code to handle negative temperatures reported by a DHT22/RHT03 sensor (flagged by a high bit 16):
if (temp & 0x8000) {
temp = (~temp & 0xffff) + 1;
}
gives large positive numbers for temperatures less than 0C (ex: 0x8005 = -5 gives 0x7ffb = 32763).
temp = -(temp & 0x7fff); Works.
The text was updated successfully, but these errors were encountered: