HomeAssistant搭建,并将ESP8266通过MQTT接入
HTML 2024-03-24 15:49 706
之前将ESP8266接入巴法云,使用一段时间后,发现不够稳定,干脆直接在内网搭建一个HomeAssistant实现灯的控制.这里使用docker搭建:
homeassistat(Home Assistant 2021.12.8):
version: '3' services: homeassistant: privileged: true image: 'homeassistant/home-assistant:stable' container_name: 'homeassistant' environment: - TZ=Asia/Shanghai network_mode: host volumes: - /home/file/docker/homeassistant/config:/config restart: unless-stopped
启动完成后访问服务器8123端口完成初始化.
还需要安装一个mqtt服务端,这里同样使用docker搭建
mosquitto:
version: "3.0" services: mosquitto: container_name: 'mosquitto' image: 'eclipse-mosquitto:2.0.18' ports: - 1883:1883 - 9001:9001 volumes: - /home/file/docker/mosquitto/config:/mosquitto/config - /home/file/docker/mosquitto/data:/mosquitto/data - /home/file/docker/mosquitto/log:/mosquitto/log
mosquitto.conf
persistence true #persistence_location /mosquitto/data #log_dest file /mosquitto/log/mosquitto.log listener 9001 port 1883 allow_anonymous true
启动mqtt服务器,然后来到HomeAssistant管理页面,
左侧配置-设备与服务,右下角添加集成,找到MQTT,输入刚刚搭建的mqtt服务器.
烧录开发板,跟前一篇文章的巴法云代码一样,这里再贴一下,设备接线参考 ESP8266通过巴法云接入米家
我这边使用的WiFiManager版本为0.16.0,发现使用最新版2.0.17有无法连接MQTT服务器的问题
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> #include <Ticker.h> //#include <PubSubClient.h> #include "PubSubClient.h" //默认,加载MQTT库文件 WiFiClient espClient; //wifi客户端模式 PubSubClient client(espClient); Ticker ticker; //定时器对象 #define ID_MQTT "esp8266-01" //客户端id #define D1 5 //D1 #define D2 4 //D2 const char* topic = "light001"; //主题名字,与巴法云控制台的主题一致 const int B_led = D1; //引脚值,此引脚用于连接继电器的信号端 const int led_switch = D2; //引脚值,此引脚用于连接继电器的信号端 //**************************************************// const char* mqtt_server = "192.168.4.200"; //默认,MQTT服务器 const int mqtt_server_port = 1883; //默认,MQTT服务器端口 int count; // Ticker计数用的变量 int state = 0; //灯的状态 // 建立WiFiManager对象 WiFiManager wifiManager; //灯光函数及引脚定义 void turnOnLed(); void turnOffLed(); //计数 void tickerCount(){ count++; } // 连接MQTT服务器 void reconnect() { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(ID_MQTT)) { Serial.println("connected"); Serial.print("subscribe:"); Serial.println(topic); //订阅主题,如果需要订阅多个主题,可发送多条订阅指令client.subscribe(topic2);client.subscribe(topic3); client.subscribe(topic); } else { Serial.print("failed, rc="); Serial.println(client.state()); Serial.println(" try again in 1 seconds"); // Wait 5 seconds before retrying delay(1000); } //} } // 收到信息后的回调函数 void receiveCallback(char* topic, byte* payload, unsigned int length) { Serial.print("来了Topic:"); Serial.println(topic); String msg = ""; for (int i = 0; i < length; i++) { msg += (char)payload[i]; } Serial.print("Msg:"); Serial.println(msg); if (msg == "on") {//如果接收字符on,亮灯 turnOnLed();//开灯函数 } else if (msg == "off") {//如果接收字符off,关灯 turnOffLed();//关灯函数 } msg = ""; } //开灯 void turnOnLed() { Serial.println("turn on light"); digitalWrite(B_led, HIGH); state = 1; } //关灯 void turnOffLed() { Serial.println("turn off light"); digitalWrite(B_led, LOW); state = 0; } // 发布信息 void pubMQTTmsg(){ // 建立发布主题 //巴法云个性设置,推送消息时:主题名后加/set推送消息,表示向所有订阅这个主题的设备们推送消息, //假如推送者自己也订阅了这个主题,消息不会被推送给它自己,以防止自己推送的消息被自己接收。 String topicString = "light001/set" ; char publishTopic[topicString.length() + 1]; //转换成字符数组 strcpy(publishTopic, topicString.c_str()); // 建立发布信息,当前D1引脚状态 String messageString; if(digitalRead(B_led)){ messageString = "on"; } else { messageString = "off"; } char publishMsg[messageString.length() + 1]; //转换成字符数组 strcpy(publishMsg, messageString.c_str()); // 实现ESP8266向主题发布信息,并在串口监视器显示出来 if(client.publish(publishTopic, publishMsg)){ Serial.println("Publish Topic:"); Serial.println(publishTopic); Serial.println("Publish message:"); Serial.println(publishMsg); } else { Serial.println("Message Publish Failed."); } } //程序入口 void setup() { pinMode(B_led, OUTPUT); //设置引脚为输出模式 pinMode(led_switch, OUTPUT); // 开关引脚为输入模式 digitalWrite(B_led, LOW);//默认引脚上电低电平 Serial.begin(9600); //设置波特率9600 //********************自动配置网络************************ // wifiManager.setTimeout(20); wifiManager.setConfigPortalTimeout(120); // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称 // wifiManager.autoConnect("AutoConnectAP"); // 如果您希望该WiFi添加密码,可以使用以下语句: wifiManager.autoConnect("8266","xxxxx"); //重置wifi设置 // wifiManager.resetSettings(); // WiFi连接成功后将通过串口监视器输出连接成功信息 Serial.println(""); Serial.print("ESP8266 Connected to "); Serial.println(WiFi.SSID()); // WiFi名称 Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // IP //******************************************************* client.setServer(mqtt_server, mqtt_server_port);//设置mqtt服务器 client.setCallback(receiveCallback); //mqtt消息处理 ticker.attach(1, tickerCount); // Ticker定时对象 Serial.println("setup结束"); } //循环执行 void loop() { // Serial.println("循环"); int val = digitalRead(B_led); int ctl_val = digitalRead(led_switch); if(ctl_val == 1){//按键按下 delay(400); state = !state; digitalWrite(B_led, state); // digitalWrite(led_switch, LOW); digitalWrite(led_switch, LOW); int ctl_val = digitalRead(led_switch); Serial.print("开2关值为 :\t"); Serial.println(ctl_val); } if (client.connected()) { client.loop(); if (count >= 10){ // 每隔10秒钟发布一次信息 Serial.println("心跳报"); pubMQTTmsg(); count = 0; } }else{ reconnect(); } }
烧录完成后,现在为HomeAssistant添加该设备
在homeassistant的configuration.yaml中最后添加一行
light: !include light01.yaml
随后在configuration.yaml同级目录添加light01.yaml,内容为
- platform: mqtt name: "卧室灯" state_topic: "light001/set" command_topic: "light001" payload_on: "on" payload_off: "off" optimistic: false qos: 0 retain: true
完成后重启homeassistant即可
连接HomeKit
配置-设备与服务中添加HomeKit,然后开放端口
在通知中可以看到绑定二维码,使用iPhone的家庭APP扫码绑定
用户名-高级模式
参考文章
发布于 2024-03-24 15:49, 最后修改于2024-10-29 10:16
© 2019 - ZXQ's Diary - zhangxiaoqiang.top