DHT2302 Sensor

Talk about Adafruit Raspberry Pi® accessories!

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: http://www.raspberrypi.org/phpBB3/

Re: DHT2302 Sensor

Postby thogue » Wed Oct 24, 2012 2: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
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()
thogue
 
Posts: 2
Joined: Mon Oct 15, 2012 10:22 pm

Previous

Return to Adafruit Raspberry Pi® accessories

Who is online

Users browsing this forum: idem, mibignistinly and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]
cron