0

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,

 # 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

When I

 print output

a string is produced containing the negative temperature.

However, when I

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?

2 Answers 2

2

The negative sign is not captured in the match group because currently it's not part of it.

"Temp =\s+(-[0-9.]+)"
          ^ fix
Sign up to request clarification or add additional context in comments.

Comments

0

I just tried it, and only "-" sign is not enough.. If you want to measure positive and negative temperatures, regexp string should look like this:

"Temp =\s+([-+]?[0-9.]+)"  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.