raspberry pi - DHT Sensor Python script error -
i have sensor type dht22 connected raspberry. have written script in python when run errors
#!/usr/bin/python import mysqldb import subprocess import re import sys import time import datetime import adafruit_dht conn = mysqldb.connect("localhost","zeus","gee3g673r","logi") while(true): date = time.strftime("%d/%m/%y") clock = time.strftime("%h:%m") #output = subprocess.check_output(["/usr/bin/adafruitdht.py 2302", "4"]); output = adafruit_dht.read_retry(adafruit_dht.am2302, 4) matches = re.search("temp =\s+([0-9.]+)", output) if (not matches): time.sleep(0) continue temp = float(matches.group(1)) matches = re.search("hum =\s+([0-9.]+)", output) if (not matches): time.sleep(0) continue humidity = float(matches.group(1)) # mysql data processing c = conn.cursor() c.execute("insert data_th (date, clock, temp, hum) values (%s, %s,%s, %s)",(date, clock, temp, humidity)) #print "db loaded" time.sleep(360)
this error encountered on running script:
root@raspberrypi:/home# ./hdt.py traceback (most recent call last): file "./dht.py", line 22, in <module> matches = re.search("temp =\s+([0-9.]+)", output) file "/usr/lib/python2.7/re.py", line 142, in search return _compile(pattern, flags).search(string) typeerror: expected string or buffer
adafruit_dht.read_retry() not return string. re.search expects string second parameter.
please have @ code below (taken adafruit_python_dht/examples):
# try grab sensor reading. use read_retry method retry # 15 times sensor reading (waiting 2 seconds between each retry). humidity, temperature = adafruit_dht.read_retry(sensor, pin) # un-comment line below convert temperature fahrenheit. # temperature = temperature * 9/5.0 + 32 # note won't reading , # results null (because linux can't # guarantee timing of calls read sensor). # if happens try again! if humidity not none , temperature not none: print 'temp={0:0.1f}* humidity={1:0.1f}%'.format(temperature, humidity) else: print 'failed reading. try again!' sys.exit(1)
Comments
Post a Comment