BatIgor
Posts: 3
Joined: Fri Jan 03, 2014 3:13 pm

Certain negative temperatures don't register - need help!

Fri Jan 03, 2014 3:23 pm

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!
Last edited by BatIgor on Sat Jan 04, 2014 4:15 pm, edited 1 time in total.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

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

Fri Jan 03, 2014 5:15 pm

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.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

BatIgor
Posts: 3
Joined: Fri Jan 03, 2014 3:13 pm

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

Fri Jan 03, 2014 5:28 pm

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

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

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

Fri Jan 03, 2014 7:50 pm

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.
Apple say... Monkey do !!

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

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

Fri Jan 03, 2014 8:24 pm

Or a regular expression?

Code: Select all

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

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

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

Fri Jan 03, 2014 11:38 pm

Or just split the line on = using the python built in function (slightly less boggling than regular expressions ;))
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

BatIgor
Posts: 3
Joined: Fri Jan 03, 2014 3:13 pm

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

Sat Jan 04, 2014 4:13 pm

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

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

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

Sat Jan 04, 2014 4:20 pm

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.

Return to “Python”