Skip to content

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

Open
srroys58 opened this issue Dec 16, 2016 · 12 comments
Open

Negative temps #45

srroys58 opened this issue Dec 16, 2016 · 12 comments

Comments

@srroys58
Copy link

srroys58 commented Dec 16, 2016

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.

@ultrathew
Copy link

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) {
temp = (~temp & 0xffff) + 1;
}

With JUST:

temp = -(temp & 0x7fff);

Or should the statement be:

if (temp & 0x8000) {
temp = -(temp & 0x7fff);
}

Thanks!

@jmcgnh
Copy link

jmcgnh commented Dec 21, 2020

I've not tested this with a real device, but if you don't include the conditional, all your temps would be negative

@hfiennes
Copy link
Contributor

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".

@ultrathew
Copy link

Thank you so much, this worked perfectly.

@hfiennes
Copy link
Contributor

Change checked in for future reference :)

@ultrathew
Copy link

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.

@hfiennes
Copy link
Contributor

Can you print the raw hex you're receiving for these?

@ultrathew
Copy link

Hopefully this is what you need. I uncommented out this line:
server.log(format("parsed: 0x %02x%02x %02x%02x %02x",result[0],result[1],result[2],result[3],result[4]));

And what I'm getting is:

2020-12-26T22:02:06.167 +00:00 | [Device] | parsed: 0x 024f 8007 d8

@hfiennes
Copy link
Contributor

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
0x800a = -1C

(vs 2's complement, where 0xfff6 would be -1C)

@hfiennes hfiennes reopened this Dec 26, 2020
@ultrathew
Copy link

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.

@hfiennes
Copy link
Contributor

If you just check the DHT22 code, it should be correct now.

@ultrathew
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants