Page 1 of 1

Certain negative temperatures don't register - need help!

Posted: Fri Jan 03, 2014 3:23 pm
by BatIgor
Hi,

I'm trying to follow this tutorial:
http://raspberrywebserver.com/cgiscript ... ogger.html

However, there is a problem: whenever a temperature goes below -10 degrees the minus disappears and it looks like if it it's a positive temperature.
Image

The full code is here:
https://github.com/Pyplate/rpi_temp_log ... monitor.py

and I think the part responsible for converting the data from the sensor to a number is this one:

Code: Select all

# get temerature
# returns None on error, or the temperature as a float
def get_temp(devicefile):

    try:
        fileobj = open(devicefile,'r')
        lines = fileobj.readlines()
        fileobj.close()
    except:
        return None

    # get the status from the end of line 1 
    status = lines[0][-4:-1]

    # is the status is ok, get the temperature from line 2
    if status=="YES":
        print status
        tempstr= lines[1][-6:-1]
        tempvalue=float(tempstr)/1000
        print tempvalue
        return tempvalue
    else:
        print "There was an error."
        return None
Can anyone please help me solve this? Thanks!

Re: Certain negative temperatures don't register - need help

Posted: Fri Jan 03, 2014 5:15 pm
by paddyg
I would suspect the [-6:-1] and would print tempstr to check what's going on. It would be better to identify where the start of the number is and slice there. Python is good at that kind of thing.

Re: Certain negative temperatures don't register - need help

Posted: Fri Jan 03, 2014 5:28 pm
by BatIgor
It just gives 12937 on print tempstr while the actual temperature is -12,937
The whole file it reads is:

Code: Select all

31 ff 4b 46 7f ff 0f 10 24 : crc=24 YES
31 ff 4b 46 7f ff 0f 10 24 t=-12937

Re: Certain negative temperatures don't register - need help

Posted: Fri Jan 03, 2014 7:50 pm
by davef21370
Maybe search backwards from the end of lines[1] until you find a character that is neither a number nor a minus sign and build the string forwards from that point plus 1.

Dave.

Re: Certain negative temperatures don't register - need help

Posted: Fri Jan 03, 2014 8:24 pm
by Douglas6
Or a regular expression?

Code: Select all

import re
m = re.match('.*t=(-?[0-9\.]+)', lines[1])
tempstr = m.group(1)

Re: Certain negative temperatures don't register - need help

Posted: Fri Jan 03, 2014 11:38 pm
by paddyg
Or just split the line on = using the python built in function (slightly less boggling than regular expressions ;))

Re: Certain negative temperatures don't register - need help

Posted: Sat Jan 04, 2014 4:13 pm
by BatIgor
Thanks, everyone, Especially Douglas6 for the code!

Here is the fixed part of the code by the way:

Code: Select all

if status=="YES":
        print status
	m = re.match('.*t=(-?[0-9\.]+)', lines[1])
	
        tempstr= m.group(1)
	print tempstr
        tempvalue=float(tempstr)/1000
        print tempvalue
	
	
        return tempvalue

Re: Certain negative temperatures don't register - need help

Posted: Sat Jan 04, 2014 4:20 pm
by Douglas6
Hah, regular expressions rule! Paddyg is of course right in this case; splitting on the '=' sign is much more straightforward. Regex's are a good thing to have in your toolbox, though.