root@Sensor:/home/pi/Desktop/kepo# ./DHTxx.py
press ctrl+c to stop!
Traceback (most recent call last):
File "./DHTxx.py", line 22, in
humidity, temperature = getDHT()
File "./DHTxx.py", line 14, in getDHT
return Adafruit_DHT.read_retry(sensor, pin)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/common.py", line 81, in read
return platform.read(sensor, pin)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/Beaglebone_Black.py", line 202, in read
match = re.match('GPIO([0123])_(\d+)', pin, re.IGNORECASE)
File "/usr/lib/python3.7/re.py", line 173, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object
原代码报上:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import Adafruit_DHT
import time
sensor = Adafruit_DHT.DHT22 #DHT21/DHT22 the same driver
pin = 17 #GPIO4
def getDHT():
return Adafruit_DHT.read_retry(sensor, pin)
if __name__ == "__main__":
print('press ctrl+c to stop!')
n=1;
while True:
t1=time.time()
humidity, temperature = getDHT()
t2=time.time()
if humidity is not None and temperature is not None:
print('No {3:0d} Temp={0:0.1f}*C Humidity={1:0.1f}% ,cost time {2:0.1f} sec'.format(temperature, humidity,t2-t1,n))
n+=1
else:
print('Failed to get reading. Try again!')