DHT传感器Python脚本错误

DHT传感器Python脚本错误,python,raspberry-pi,sensors,Python,Raspberry Pi,Sensors,我有一个DHT22型传感器连接到树莓上。 我已经用python编写了一个脚本,但当我运行它时,会出现错误 #!/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): d

我有一个DHT22型传感器连接到树莓上。 我已经用python编写了一个脚本,但当我运行它时,会出现错误

#!/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 INTO data_th (date, clock, temp, hum) VALUES (%s, %s,%s, %s)",(date, clock, temp, humidity))

#print "DB Loaded"

time.sleep(360)
这是运行脚本时遇到的错误:

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
root@raspberrypi:/home#/hdt.py
回溯(最近一次呼叫最后一次):
文件“/dht.py”,第22行,在
匹配=重新搜索(“Temp=\s+([0-9.]+)”,输出)
文件“/usr/lib/python2.7/re.py”,第142行,搜索中
返回编译(模式、标志)。搜索(字符串)
TypeError:应为字符串或缓冲区
Adafruit\u DHT.read\u retry()不返回字符串。re.search要求字符串作为第二个参数

请查看以下代码(摘自):

Adafruit_DHT.read_retry()不返回字符串。re.search要求字符串作为第二个参数

请查看以下代码(摘自):


我注意到的唯一一件事是你在一个字符串中使用正则表达式。我通常使用这样的原始字符串方法:
searchObj=re.search(r'(.*)是(.*?.*),行,re.M | re.I)
您的
输出可能不是正确的类型。你能强迫它串起来吗
repr(output)
?我注意到的唯一一件事是在字符串中执行正则表达式。我通常使用这样的原始字符串方法:
searchObj=re.search(r'(.*)是(.*?.*),行,re.M | re.I)
您的
输出可能不是正确的类型。你能强迫它串起来吗<代码>报告(输出)
# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).  
# If this happens try again!
if humidity is not None and temperature is not None:
    print 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)
else:
    print 'Failed to get reading. Try again!'
    sys.exit(1)