IPV6+Home Assistant[ESP32+MQTT+GPIO]远程控制灯
- 1 背景
- 2 Home Assistant 配置
- 3 ESP32 配置
- 3-1 工程代码
- 3-2 硬件接线
- 4 实现效果
1 背景
在上一小节我们通过ESP32接入了Home Assistant,实现了温度、湿度、光照强度的数据采集,但智能家居更多的是实现控制,现在我们通过4个IO以及灯来模拟家庭的设备,通过Home Assistant远程控制开关。
如果有熟读上一节的内容 【IPV6从入门到起飞】5-2 IPV6+Home Assistant(ESP32+MQTT+DHT11+BH1750)传感器采集上传监测 的话,本节的内容相当简单。
还是老的套路。
2 Home Assistant 配置
配置yaml,添加四个灯,设置主题以及控制字
config/configuration.yaml
mqtt:
switch:
- name: "Lamp 1"
command_topic: "home/esp32/lamp1"
payload_on: "1"
payload_off: "0"
- name: "Lamp 2"
command_topic: "home/esp32/lamp2"
payload_on: "1"
payload_off: "0"
- name: "Lamp 3"
command_topic: "home/esp32/lamp3"
payload_on: "1"
payload_off: "0"
- name: "Lamp 4"
command_topic: "home/esp32/lamp4"
payload_on: "1"
payload_off: "0"
检查配置,重载配置
在我我们的概览页会出现四个开关
3 ESP32 配置
直接使用GPIO5、18、19、21作为输出
3-1 工程代码
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "mywifi_2.4G"; // 替换为你的 Wi-Fi SSID
const char* password = "12345666"; // 替换为你的 Wi-Fi 密码
const char* mqttServer = "192.168.66.118"; // 替换为你的 MQTT Broker IP
const int mqttPort = 1883; // 默认 MQTT 端口
const char* mqttUser = "YOUR_MQTT_USERNAME"; // 如果需要,替换为你的用户名
const char* mqttPassword = "YOUR_MQTT_PASSWORD"; // 如果需要,替换为你的密码
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// 定义灯的 GPIO 引脚
const int lamp1Pin = 5;
const int lamp2Pin = 18;
const int lamp3Pin = 19;
const int lamp4Pin = 21;
void setup() {
Serial.begin(115200);
// 设置 GPIO 引脚为输出
pinMode(lamp1Pin, OUTPUT);
pinMode(lamp2Pin, OUTPUT);
pinMode(lamp3Pin, OUTPUT);
pinMode(lamp4Pin, OUTPUT);
// 连接到 Wi-Fi
connectToWiFi();
// 设置 MQTT 服务器
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(mqttCallback);
}
void loop() {
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.loop();
}
void connectToWiFi() {
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi!");
}
void reconnectMQTT() {
while (!mqttClient.connected()) {
Serial.println("Attempting MQTT connection...");
if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) {
Serial.println("connected");
mqttClient.subscribe("home/esp32/lamp1");
mqttClient.subscribe("home/esp32/lamp2");
mqttClient.subscribe("home/esp32/lamp3");
mqttClient.subscribe("home/esp32/lamp4");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0'; // 确保字符串结束
String message = String((char*)payload);
if (strcmp(topic, "home/esp32/lamp1") == 0) {
digitalWrite(lamp1Pin, message == "1" ? HIGH : LOW);
} else if (strcmp(topic, "home/esp32/lamp2") == 0) {
digitalWrite(lamp2Pin, message == "1" ? HIGH : LOW);
} else if (strcmp(topic, "home/esp32/lamp3") == 0) {
digitalWrite(lamp3Pin, message == "1" ? HIGH : LOW);
} else if (strcmp(topic, "home/esp32/lamp4") == 0) {
digitalWrite(lamp4Pin, message == "1" ? HIGH : LOW);
}
}
3-2 硬件接线
(这里使用的是步进电机驱动板,恰好有个灯,拿来用一下)
4 实现效果
点击页面上的 lamp2 的开,板子的第二个灯亮起,其他灯的效果也是一样的,不做重复的演示