DHT11 温湿度传感器 取值
作者:互联网
引脚 功能图示
湿度检测范围 : 20-80% (5% 精度)
温度检测范围 : 0-50°C (±2°C 精度)
接线 用了 引脚 1,18,20 。
引脚 1 -- 3.3V正极,
引脚 18 -- 数据,
引脚 10 -- 负极,
sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install
输入 python3 AdafruitDHT.py 11 24 ,调取信息, 参数 11为DHT11 ,24为 引脚的 GPIO 编号
也可以自己写个 循环的
import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 24 while True: humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
为了方便 远程 查看与 获取 数据,写个可被http 用get 访问的 接口
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import urllib
import datetime
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 24
host = ('',8787)
def getData():
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
return {"value":'%d'%temperature +","+'%d'%humidity ,"time":datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
class Resquest(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(getData()).encode())
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = json.loads(self.rfile.read(length))
#print(post_data)
if __name__ == '__main__':
server = HTTPServer(host, Resquest)
print("service is running..")
server.serve_forever()
网页查看
标签:__,Adafruit,引脚,温湿度,self,import,DHT,取值,DHT11 来源: https://www.cnblogs.com/likehc/p/14018487.html