之前的工作看这里
修改homeassistantconfiguration.yaml 编辑/docker/homeassistantconfiguration.yaml
在末尾加入
1 2 3 4 5 6 7 8 9 10 11 12 sensor: - platform: mqtt state_topic: "esp/sensor1" name: 'Temperature' unit_of_measurement: '°C' value_template: '{{ value_json.Temperature }}' - platform: mqtt state_topic: "esp/sensor1" name: 'Humidity' unit_of_measurement: '%' value_template: '{{ value_json.Humidity }}' device_class: humidity
esp8266程序 将DHT22连接到NodeMCU的D2
口也就是IO4
上 烧写程序如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include "DHT.h" #define DHTPIN 4 #define DHTTYPE DHT22 DHT dht (DHTPIN, DHTTYPE) ;const char * ssid = "xxx" ;const char * password = "xxx" ;const char * mqtt_server = "xxx.xxx.xxx.xxx" ;const char * mqtt_topic = "esp/sensor1" ;WiFiClient espClient; PubSubClient client (espClient) ;void setup () { Serial.begin(9600 ); setup_wifi(); client.setServer(mqtt_server, 1883 ); dht.begin(); } void setup_wifi () { delay(10 ); Serial.println(); Serial.print("Connecting to " ); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500 ); Serial.print("." ); } Serial.println("" ); Serial.println("WiFi connected" ); Serial.println("IP address: " ); Serial.println(WiFi.localIP()); } void reconnect () { while (!client.connected()) { Serial.print("Attempting MQTT connection..." ); if (client.connect("arduinoClient" )) { Serial.println("connected" ); client.subscribe("esp/#" ); } else { Serial.print("failed, rc=" ); Serial.print(client.state()); Serial.println(" try again in 5 seconds" ); delay(5000 ); } } } void loop () { if (!client.connected()) { reconnect(); } client.loop(); delay(2000 ); float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!" ); return ; } StaticJsonBuffer<300 > JSONbuffer; JsonObject& JSONencoder = JSONbuffer.createObject(); JSONencoder["Humidity" ] = h; JSONencoder["Temperature" ] = t; char JSONmessageBuffer[300 ]; JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof (JSONmessageBuffer)); Serial.println(JSONmessageBuffer); boolean isOK = client.publish(mqtt_topic, JSONmessageBuffer, true ); Serial.println(isOK); }
测试 在开发者工具/MQTT
中 订阅主题Topic
为esp/sensor1
注意监听
在主页面中右上角选择配置UI
在右下角选择添加卡片
在实体
里选择sensor.temperature
和sensor.humidity
完成添加到主页
在iPhone的家庭APP里可以看到
注意 湿度传感器需要在修改homeassistantconfiguration.yaml的时候加入device_class: humidity
Device Class支持的传感器类型有
None: Generic sensor. This is the default and doesn’t need to be set. battery: Percentage of battery that is left. humidity: Percentage of humidity in the air. illuminance: The current light level in lx or lm. signal_strength: Signal strength in dB or dBm. temperature: Temperature in °C or °F. power: Power in W or kW. pressure: Pressure in hPa or mbar. timestamp: Datetime object or timestamp string.