前言
本贴主要内容是为HA主机添加一个常用的DHT11温度传感器,实现温湿度定时采集。为了方便演示,本教程会用核桃派PicoW 驱动DHT11,然后每两秒往核桃派HA主机上报温湿度数据。
- DHT11
实验目的
核桃派HA主机添加啊DHT11温湿度传感器,采集温湿度数据
实验讲解
需要用到HA MQTT组件中的sensor。实验的关键是搞清楚发现MQTT设备的主题信息以及控制方法说明如下:
MQTT主题
下面这两个主题用于HA主机通过MQTT发现该设备:
homeassistant/sensor/picow_dht11_temp/config
homeassistant/sensor/picow_dht11_hum/config
homeassistant
:默认的前缀homeassistant
:默认的前缀picow_dht11_temp
: 实体的ID,需要唯一,这里自定义的内容,表示核桃派PicoW的温度传感器;picow_dht11_hum
: 实体的ID,需要唯一,这里自定义的内容,表示核桃派PicoW的湿度传感器;config
:默认的后缀
MQTT消息
温度传感器消息:
"""{
"name": "Temperature Sensor",
"device_class": "temperature",
"state_topic": "picow_dht11/sensor/temperature/state",
"unique_id": "picow_dht11_temp",
"unit_of_measurement": "C",
"value_template": "{{ value_json.temperature }}",
"device": {
"identifiers": ["picow_dht11_device"],
"name": "Picow DHT11 Temperature Sensor",
"manufacturer": "WalnutPi",
"model": "DHT11",
"sw_version": "1.0.0"
}
}"""
实体1
"name":"temp"
: 实体名称,自定义填写;"device_class":"temperature"
: 组件类型,跟前面主题配置信息相关,不能填错,比如这里的temperature
是组件sensor
下的一个可用实体;"state_topic":"picow_dht11/sensor/temperature/state"
: 用于注册实体后发布相关属性主题,这里用来发送湿度数据,主题内容自定义,保证不同实体的主题不一样即可;"unique_id":"picow_dht11_temp"
: 实体ID,自定义,务必保证每个实体唯一;
湿度传感器消息:
"""{
"name": "Humidity Sensor",
"device_class": "humidity",
"state_topic": "picow_dht11/sensor/humidity/state",
"unique_id": "picow_dht11_hum",
"unit_of_measurement": "%",
"value_template": "{{ value_json.humidity }}",
"device": {
"identifiers": ["picow_dht11_device"],
"name": "Picow DHT11 Humidity Sensor",
"manufacturer": "WalnutPi",
"model": "DHT11",
"sw_version": "1.0.0"
}
}"""
实体2
"name":"temp"
: 实体名称,自定义填写;"device_class":"humidity"
: 组件类型,跟前面主题配置信息相关,不能填错,比如这里的humidity
是组件sensor
下的一个可用实体;"state_topic":"picow_dht11/sensor/humidity/state"
: 用于注册实体后发布相关属性主题,这里用来发送湿度数据,主题内容自定义,保证不同实体的主题不一样即可;"unique_id":"picow_dht11_hum"
: 实体ID,自定义,务必保证每个实体唯一;
设备
告知Home Assistant实体对应的设备。
"identifiers": ["picow_dht11_device"]
: 识别标识符,每个设备唯一,例:picow_dht11_device"name":"picow"
: 设备名称,自定义;"manufacturer": "WalnutPi"
:设备的制造商
*"model": "DHT11"
:设备的型号,例:DHT11
*"sw_version": "1.0.0"
:设备的固件版本
更多MQTT sensor内容可查阅官方文档:MQTT Sensor - Home Assistant
代码编写流程如下:
导入相关模块
构建DHT11对象
连接wifi
连接MQTT服务器
注册DHT11设备与实体
采集温湿度信息
MQTT发送温湿度信息
延时2秒。再次循环采集温湿度信息
基于核桃派PicoW实现
本实验使用核桃派PicoW(ESP32-S3)连接DHT11温湿度传感器,使用方法参考:核桃派PicoW教程,保证核桃派PicoW和核桃派1B连接到同一个路由器下即可:
参考代码
'''
实验名称:Home Assistant 温度传感器DHT11
实验平台:核桃派1B + 核桃派PicoW
说明:编程实现Home Assistant 采集温度传感器DHT11温度数据
'''
import network
from simple import MQTTClient
from machine import Pin,Timer
import dht
import time
# 初始化WIFI指示灯
LED = Pin(46, Pin.OUT) # 请确认这个引脚号是否正确
#WIFI连接函数
def WIFI_Connect():
global LED
wlan = network.WLAN(network.STA_IF) #STA模式
wlan.active(True) #激活接口
start_time=time.time() #记录时间做超时判断
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('01Studio', '88888888') #输入WIFI账号密码
while not wlan.isconnected():
#LED闪烁提示
LED.value(1)
time.sleep_ms(300)
LED.value(0)
time.sleep_ms(300)
#超时判断,15秒没连接成功判定为超时
if time.time()-start_time > 15 :
print('WIFI Connected Timeout!')
break
if wlan.isconnected():
#LED点亮
LED.value(1)
#串口打印信息
print('network information:', wlan.ifconfig())
return True
else:
return False
#接收数据任务
def MQTT_Rev(tim):
client.check_msg()
#执行WIFI连接函数并判断是否已经连接成功
if WIFI_Connect():
CLIENT_ID = 'WalnutPi' # 客户端ID
SERVER = '192.168.1.1' # MQTT服务器地址
PORT = 1883
USER='pi'
PASSWORD='pi'
client = MQTTClient(CLIENT_ID, SERVER, PORT, USER, PASSWORD) #建立客户端对象
client.connect()
TOPIC_TEMP = "homeassistant/sensor/picow_dht11_temp/config"
mssage_temp = """{
"name": "Temperature Sensor",
"device_class": "temperature",
"state_topic": "picow_dht11/sensor/temperature/state",
"unique_id": "picow_dht11_temp",
"unit_of_measurement": "C",
"value_template": "{{ value_json.temperature }}",
"device": {
"identifiers": ["picow_dht11_device"],
"name": "Picow DHT11 Temperature Sensor",
"manufacturer": "WalnutPi",
"model": "DHT11",
"sw_version": "1.0.0"
}
}"""
client.publish(TOPIC_TEMP, mssage_temp)
# 发布湿度传感器配置信息到Home Assistant
TOPIC_HUM = "homeassistant/sensor/picow_dht11_hum/config"
mssage_hum = """{
"name": "Humidity Sensor",
"device_class": "humidity",
"state_topic": "picow_dht11/sensor/humidity/state",
"unique_id": "picow_dht11_hum",
"unit_of_measurement": "%",
"value_template": "{{ value_json.humidity }}",
"device": {
"identifiers": ["picow_dht11_device"],
"name": "Picow DHT11 Humidity Sensor",
"manufacturer": "WalnutPi",
"model": "DHT11",
"sw_version": "1.0.0"
}
}"""
client.publish(TOPIC_HUM, mssage_hum)
d = dht.DHT11(Pin(2)) #传感器连接引脚
time.sleep(2) #首次启动停顿2秒让传感器稳定
def dht_get(tim):
d.measure()
temp = d.temperature()
hum = d.humidity()
#终端打印温湿度信息
print(str(d.temperature() )+' C')
print(str(d.humidity())+' %')
TOPIC_TEMP = "picow_dht11/sensor/temperature/state"
TOPIC_HUM = "picow_dht11/sensor/humidity/state"
mssage_temp = '{"temperature": %.2f}'%temp
mssage_hum = '{"humidity": %.2f}'%hum
client.publish(TOPIC_TEMP, mssage_temp)
client.publish(TOPIC_HUM, mssage_hum)
tim = Timer(1)
tim.init(period=2000, mode=Timer.PERIODIC, callback=dht_get)
while True:
pass
实验结果
根据上述代码将DHT11连接到核桃派PicoW的引脚2,接线图如下:
使用Thonny IDE连接核桃派PicoW开发板,运行上面代码:
运行成功后可以在MQTT集成找到DHT11传感器设备:
可以添加到仪表盘: