chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Problem displaying signed floats

Sat Dec 13, 2014 9:30 am

Hello folks

Until this month, my outdoor DHT22 temperature/humidity monitor has been working fine. However, with the cold weather, and temperatures now sub-zero, I've noticed that my routine is failing to handle negative temperatures - instead they are represented as being positive: the sign has been lost.

I've adjusted my re.search routine to include negative numbers where previously they were excluded,

Code: Select all

# Continuously append data
while(True):
  # Run the DHT program to get the humidity and temperature readings!
  # DHT22 (Credit to Adafruit)
  output = subprocess.check_output(["/home/pi/scripts/DHT/Adafruit_DHT", "22", "25"]);
  rasp = subprocess.check_output(["vcgencmd", "measure_temp"])
  print output
  matches = re.search("Temp =\s+-([0-9.]+)", output)
  if (not matches):
        time.sleep(3)
        continue
  tempa = float(matches.group(1))
  print tempa
    # search for humidity printout
  matches = re.search("Hum =\s+([0-9.]+)", output)
  if (not matches):
        time.sleep(3)
        continue
  humiditya = float(matches.group(1))
When I

Code: Select all

print output
a string is produced containing the negative temperature.

However, when I

Code: Select all

print tempa
it displays as a positive number.

I need to be able to carry the sign into the variable, as temperatures could be either positive or negative (even in a UK winter).

Can anyone help?

DirkS
Posts: 10595
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Problem displaying signed floats

Sat Dec 13, 2014 10:38 am

Can you post examples of 'output' and also of 'matches.group(1)' which is the value that is actually converted.

Gr.
Dirk.

User avatar
jojopi
Posts: 3424
Joined: Tue Oct 11, 2011 8:38 pm

Re: Problem displaying signed floats

Sat Dec 13, 2014 10:45 am

chorlton2080 wrote: matches = re.search("Temp =\s+-([0-9.]+)", output)
Match the minus sign inside the grouping parentheses, so that it is included in the string that is converted to float. You do not need two separate cases because you can make the sign optional by following it with a question mark:

Code: Select all

  matches = re.search("Temp =\s+(-?[0-9.]+)", output)

chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Re: Problem displaying signed floats

Sat Dec 13, 2014 10:54 am

jojopi wrote:
chorlton2080 wrote: matches = re.search("Temp =\s+-([0-9.]+)", output)
Match the minus sign inside the grouping parentheses, so that it is included in the string that is converted to float. You do not need two separate cases because you can make the sign optional by following it with a question mark:

Code: Select all

  matches = re.search("Temp =\s+(-?[0-9.]+)", output)
Worked perfectly. Thank you so much :)

chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Re: Problem displaying signed floats

Sat Dec 13, 2014 10:56 am

DirkS wrote:Can you post examples of 'output' and also of 'matches.group(1)' which is the value that is actually converted.

Gr.
Dirk.
Struggling to copy out of JuiceSSH on my mobile. The latest 'output' contains text including '-1.8C'; 'matches.group(1)' reports it as '1.8'.

chorlton2080
Posts: 128
Joined: Sun Dec 23, 2012 9:44 pm

Re: Problem displaying signed floats

Sat Dec 13, 2014 10:56 am

Now solved using the answer above. Thanks to both of you for your assistance.

Return to “Python”