[Follow me Season 2 Episode 2] Smart Home - Smart Study - Advanced Tasks
[Copy link]
This post was last edited by 鲜de芒果 on 2024-9-8 15:13
1. Task Requirements
Access the open source smart home platform HA (HomeAssistant) via Wi-Fi using the MQTT protocol
2. HomeAssistant integration
Discovery of MQTT devices will enable people to use MQTT devices with very little configuration work on the HomeAssistant side . Configuration is done on the device itself and the topics used by the device.
MQTT discovery is enabled by default, but can be disabled. The prefix of the discovery topics (default: homeassistant ) can be changed. See the MQTT options section for configuration details.
2.1 Configure the theme
After sending the configuration topic to MQTT , HomeAssistant will automatically discover the current sensor. In the current task, an onboard LED light is used as the Light component in HomeAssistant . This component can remotely control the onboard LED on and off in the HomeAssistant dashboard . Use the MQTT client to send a message to the configuration topic. The configuration topic is as follows:
// 板载LED灯 主题消息
{
"name":"led-builtin-Light",
"device_class": "light",
"command_topic":"homeassistant/light/FollowMe2-2-LED_BUILTIN/switch",
"state_topic":"homeassistant/sensor/FollowMe2-2/state",
"state_value_template": "{{ value_json.builtinLed }}",
"unique_id":"FollowMe2-2-LED_BUILTIN-light",
"device":{
"identifiers":[
"Arduino UNO R4 WiFi"
],
"name":"UNO R4 WiFi",
"manufacturer": "Arduino",
"model": "UNO R4 WiFi",
"hw_version": "1.0"
}
}
3. Code Implementation
/**
* FollowMe 2-2 任务3:
* 1. 通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)
*/
#include <WiFiS3.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
#include "arduino_secrets.h"
#define STATE_ON "ON" // HA LED开状态
#define STATE_OFF "OFF" // HA LED关状态
char ssid[] = SECRET_SSID; // WIFI SSID
char pass[] = SECRET_PASS; // WIFI PASSWD
int status = WL_IDLE_STATUS; // WIFI 状态
// MQTT
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "192.168.2.120"; // HomeAssistant MQTT服务器地址
int port = 1883; // HomeAssistant MQTT服务器端口
char mqtt_user[] = MQTT_USER; // MQTT 用户名
char mqtt_pass[] = MQTT_PASS; // MQTT 密码
// 订阅主题(接收 HomeAssistant 控制板载LED开关指令)
const char subscribe_topic[] = "homeassistant/light/FollowMe2-2-LED_BUILTIN/switch";
bool isNeedReportLedState = true; // 是否需要更新板载LED状态到HA
bool ledState = false; // 板载LED状态
// 发布主题(上报板载LED状态至 HomeAssistant的状态主题)
const char publish_topic[] = "homeassistant/sensor/FollowMe2-2/state";
void setup() {
// 初始化串口
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); // 初始化板载LED引脚为输出
// 检查WIFI模块
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi模块通信失败!");
while (true);
}
// 连接到WIFI
Serial.print("尝试连接到 WIFI SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(5 * 1000);
}
Serial.println("WIFI 连接成功!");
// 连接MQTT
mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
Serial.print("尝试连接到MQTT服务器: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT 连接失败! ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("MQTT连接成功!");
// 订阅主题
mqttClient.onMessage(onMqttMessage);
mqttClient.subscribe(subscribe_topic);
}
void loop() {
if(isNeedReportLedState) {
// 汇报当前板载LED状态到HA
char msg[30] = {0};
sprintf(msg, "{\"builtinLed\": \"%s\"}", ledState ? STATE_ON : STATE_OFF);
mqttClient.beginMessage(publish_topic);
mqttClient.print(msg);
mqttClient.endMessage();
isNeedReportLedState = false; // 上报板载LED状态到HA后重置汇报状态
}
// 更新板载LED状态
if(ledState) { // 亮
digitalWrite(LED_BUILTIN, HIGH);
} else { // 灭
digitalWrite(LED_BUILTIN, LOW);
}
mqttClient.poll(); // 定期检查新MQTT消息
}
// MQTT 订阅消息回调,接收HA控制板载LED的指令(ON:开,OFF:关)
void onMqttMessage(int messageSize) {
// 读取消息内容
String message;
for (int i = 0; i < messageSize; i++) {
message += (char)mqttClient.read();
}
// 打印消息的内容
Serial.print("Received message: ");
Serial.println(message);
if(0 == strcmp(STATE_ON, message.c_str())) {
if(false == ledState) {
// 需要更新板载LED状态到HA
isNeedReportLedState = true;
}
ledState = true;
} else {
if(true == ledState) {
// 需要更新板载LED状态到HA
isNeedReportLedState = true;
}
ledState = false;
}
}
4. Effect display
The effect of turning off the lights in the smart home platform HA (HomeAssistant)
The effect of turning on the light in the smart home platform HA (HomeAssistant)
Serial port print log
Effect animation
|