First, I thought I would chime in here, I have the dht2302 working great on my pi, It took lots of playing with in order to get it to read properly. I recompiled the C code to exclude the prints except the Temp / Humidity line, this made working with it in python a little easier on me, for efficiencies sake this might not be the best. I had trouble recompiling and after a little research, this solved my error
- Code: Select all
gcc -l rt Adafruit_DHT.c -l bcm2835 -std=gnu99 -o Adafruit_DHT
my python code runs the c app to get the data,
writes it to the lcd,
sleeps 2 seconds.
if data is not received, it sleeps for .05 and tries again. So far the most failures in a row I've seen is 3. Timing seems very important when reading the sensor. I have played with the sleeps in my program for a bit, and this is the best results I have gotten so far.
here is my code for anything that might want to take a look, most of the code was taken from adafruits examples. (Thanks again Adafruit)
- Code: Select all
try:
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
#from datetime import datetime
lcd = Adafruit_CharLCD()
# Returns a strink containing Temp / Humidity seperated by a space
cmd = "/root/tempclock/Adafruit_DHT 2302 4 | awk -F' ' '{ print $3\" \"$7'}"
lcd.begin(16,1)
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
while 1:
temps = run_cmd(cmd)
# Handle misreads properly, this setting seems to be best of both worlds quick/reliable
while temps == "":
print "sleeping .05, didnt read"
sleep(.05)
temps = run_cmd(cmd)
temps.rstrip()
temp, humid = temps.split()
temp = 9.0 * float(temp) / 5.0 + 32
print "Temp: " + str(temp) + "F Humidity: " + str(humid)
lcd.clear()
# Prevent lcd curruption, while lcd is clearing
sleep(.1)
# lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
# lcd.message('Temp: ' + str(temp) + 'Hum: ' + str(humid) + '%' )
lcd.message('Temp: ' + str(temp) + 'F\n')
lcd.message('Hum: ' + str(humid) + '%')
sleep(2)
except KeyboardInterrupt:
lcd.cleanup()

