There is no USPS pickup on Monday Feb 15th, 2016 because of President's Day. USPS orders placed after 11am EST Fri February 12th will go out Tuesday February 16. UPS orders will ship as normal.

DHT2302 Sensor
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit:
Please be positive and constructive with your questions and comments.

Re: DHT2302 Sensor

by thogue on Wed Oct 24, 2012 3:43 pm

Afternoon everyone,

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
   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'}"


   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"
         temps = run_cmd(cmd)
      temp, humid = temps.split()
      temp = 9.0 * float(temp) / 5.0 + 32
      print "Temp: " + str(temp) + "F Humidity: " + str(humid)
      # Prevent lcd curruption, while lcd is clearing
   #       lcd.message('%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) + '%')     
except KeyboardInterrupt:
Posts: 3
Joined: Mon Oct 15, 2012 11:22 pm

Please be positive and constructive with your questions and comments.