群晖搭建HomeAssistant连接HomeKit

发布于 2020-07-02  66 次阅读


在群晖Docker的注册表里搜索并下载homeassistant/home-assistant
高级设置里勾选启动自动重新启动
里设置/docker/homeassistant映射装载路径/config
网络里勾选使用与Docker Host相同的网络
环境里设置TZ数值为Asia/Shanghai
浏览器访问http://群晖的ip:8123即可完成剩下的设置

添加HomeKit支持
编辑/docker/homeassistantconfiguration.yaml在末尾加入

1homekit:

末尾有空格

返回HomeAssistant页面,会收到HomeKit的配置代码
如果忘记了此代码,可以删除.homekit.state文件

安装MQTT服务器

在群晖Docker的注册表里搜索并下载ncarlier/mqtt
高级设置里勾选启动自动重新启动
网络里勾选使用与Docker Host相同的网络
完成设置
编辑/docker/homeassistantconfiguration.yaml在末尾加入

1
2
3
4
mqtt:
broker: 192.168.1.110
discovery: true
discovery_prefix: homeassistant

烧写esp8266程序

基础设置参考这里
(https://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/)
此外安装库PubSubClient
烧写程序如下

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

#include <PubSubClient.h>


const char* ssid = "xxx";
const char* password = "xxx";
const char* mqtt_server = "xxx.xxx.xxx.xxx";

WiFiClient espClient;
PubSubClient (espClient);
String switch1;
String strTopic;
String strPayload;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
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 callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '';
strTopic = String((char*)topic);
if(strTopic == "esp/switch1")
{
switch1 = String((char*)payload);
if(switch1 == "ON")
{
Serial.println("ON");
digitalWrite(LED_BUILTIN, LOW);
}
else
{
Serial.println("OFF");
digitalWrite(LED_BUILTIN, HIGH);
}
}
}


void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("esp/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}

void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}

测试

编辑/docker/homeassistantconfiguration.yaml在末尾加入

1
2
3
4
5
6
7
switch:
- platform: mqtt
name: "light"
state_topic: "esp/switch1"
command_topic: "esp/switch1"
payload_on: "ON"
payload_off: "OFF"

开发者工具/MQTT
填写主题Topicesp/switch1
分别发送有效负载为ONOFF进行测试
注意在客户端监听

在主页面中右上角选择配置UI
在右下角选择添加卡片
实体里选择switch.light
完成添加到主页

最后在iPhone的家庭APP里添加配件,简单配置后即可使用Siri控制开关

引用

(https://www.instructables.com/id/Using-an-Esp8266-Arduino-to-Control-a-Relay-Using-/)

https://www.dazhuanlan.com/2020/04/01/5e847a3391f82/

一个追求爱搞的小逗逼