官术网_书友最值得收藏!

DHT11 temperature and humidity sensor

The DHT11 temperature and humidity sensor is a low-cost device used in IoT applications. There are API libraries for this sensor from such sources as Adafruit Industries, which makes it easy to implement the DHT11 into an IoT or edge analytics application.

The following is a Fritzing diagram of how a DHT11 temperature and humidity sensor could be hooked up in an edge analytics application:

First released on June 3, 2016, Fritzing is an open source software program used to design electronic circuits. It's free to use and is available for macOS, Windows, and Linux. It features an easy-to-use GUI interface with an extensive library of electronic components to include in the user's designs. Outside components may be easily added as well. To find out more about this amazing program, go to www.fritzing.org.

The following is a code example using the DHT11 and the Python programming language. The code was written for the Raspberry Pi and is taken from my first book, Internet of Things Programming Projects (Packt Publishing). This code uses the Adafruit DHT library to measure temperature and humidity and send this information to a cloud-based dashboard using the MQTT protocol:

from time import sleep
import Adafruit_DHT
import paho.mqtt.client as mqtt
import json

host = 'demo.thingsboard.io'
access_token = '<<access token>>'
dht_sensor = Adafruit_DHT.DHT11
pin = 19

sensor_data = {'temperature': 0, 'humidity': 0}

client = mqtt.Client()
client.username_pw_set(access_token)

while True:
humidity, temperature = Adafruit_DHT
.read_retry(dht_sensor, pin)

print(u"Temperature: {:g}\u00b0C, Humidity{:g}%".format(temperature, \
humidity))

sensor_data['temperature'] = temperature
sensor_data['humidity'] = humidity
client.connect(host, 1883, 20)
client.publish('v1/devices/me/telemetry',
json.dumps(sensor_data), 1)
client.disconnect()
sleep(10)

The preceding code was written for an IoT application, not an edge analytics one. However, it does point out how easily information from a sensor may be read and utilized.

主站蜘蛛池模板: 阳东县| 延庆县| 卓资县| 海门市| 固镇县| 娄烦县| 喀喇| 民丰县| 河西区| 青田县| 疏勒县| 项城市| 南平市| 阿克陶县| 晋宁县| 沙坪坝区| 衡南县| 和顺县| 太原市| 綦江县| 静宁县| 建瓯市| 分宜县| 乐亭县| 唐河县| 灵武市| 邳州市| 沂南县| 年辖:市辖区| 芮城县| 蓝山县| 永登县| 临夏市| 思茅市| 古丈县| 衡东县| 百色市| 巫溪县| 天门市| 那坡县| 曲靖市|