DHT22温湿度传感器的使用

使用python读取dht22温湿度传感器的值。

 import sys
 import Adafruit_DHT
 import requests
 import json
 url = 'https://www.3gcomet.com/iot/iot_receive.php'
 sensor = Adafruit_DHT.DHT22
 pin = 4
 humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
 if humidity is not None and temperature is not None:
     temp = '{:.1f}'.format(temperature)
     humi = '{:.1f}'.format(humidity)
     payload = {"ac":'iotofict',"loc":'CloudDC',"tem":temp,"hum":humi}
     response = requests.post(url=url,data=payload)
     print(response.text)
     #print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
 else:
     print('Failed to get reading. Try again!')
     sys.exit(1)