- 积分
- 150
- 金钱
- 143
- 威望
- 0
- 贡献
- 0
- HASS币
- 0
注册会员
- 积分
- 150
- 金钱
- 143
- HASS币
- 0
|
本帖最后由 xiaolong123 于 2020-10-12 17:09 编辑
需要具备一定的基础,至少要配置了mqtt服务器.要用arduino软件编译,修改地址等.
==================
1.主要是homeassistant可以直接快速触发米家自动化操作.比如离家模式,回家模式等等.
因灯光都是蓝牙mesh,无法接入homeassistant.
----------------------------------------------------
2.实现方法:esp8266与小米无线开关共用电源,然后按键检测引脚接上拉的引脚.我暂时使用的是nodemcu板子,按键接的jpio2.这个引脚带了上拉电阻.
----------------------------------------------------
如图
1
```
#include <Regexp.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
#include <RemoteDebug.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#define SWITCH_OFF 0
#define SWITCH_ON 1
const char* ssid = "xxxxxxx"; //Wi-Fi热点
const char* password = "xxxxxxxx"; //Wi-Fi密码
const char* mqtt_server = "192.168.xxxx.xxx"; //服务器网址或者IP地址
const char* TOPIC_CONFIG = "ESP-01/switch/config";
const char* TOPIC_COMMAND = "ESP-01/switch/set";
const char* TOPIC_STATE = "ESP-01/switch/state";
const char* client_id = "client-001"; // 标识当前设备的客户端编号
const char* device_name = "esp_01_2_switch_1";
const char* mqtt_user = "homeassistant";
const char* mqtt_password = "home";
WiFiClient espClient;
PubSubClient client(espClient);
const int PIN_SWITCH = 2;
const int PIN_BUTTON = 0;
long last_msg = 0;
char msg[64];
int switch_value = SWITCH_OFF;
String set_str = "";
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(PIN_SWITCH, OUTPUT);
digitalWrite(PIN_SWITCH, SWITCH_ON);
pinMode(PIN_BUTTON, INPUT);
}
void setup_wifi() {
delay(100);
WiFi.begin(ssid, password); // 连接到WiFi网络
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
set_str = "";
for (int i = 0; i < length; i++) {
set_str += (char)payload;
}
String topic_str = topic;
Serial.println(set_str);
if (topic_str.equals(TOPIC_COMMAND)) {
set_switch(set_str);
}
}
void reconnect( ) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi is not connected, retry setup");
setup_wifi();
}
while (!client.connected()) {
Serial.println("MQTT connecting...");
if (client.connect(client_id, mqtt_user, mqtt_password)) {
Serial.println("MQTT connect success.");
mqtt_config();
client.subscribe(TOPIC_COMMAND);
} else {
delay(5000);
}
}
}
void loop() {
static bool i=true;
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - last_msg > 5000) {
last_msg = now;
pub_mqtt_state();
}
int buttonState = digitalRead(PIN_BUTTON);
if (buttonState == LOW) {
if(i)
set_switch("ON");
else
set_switch("OFF");
i=!i;
delay(1000);
}
delay(100);
}
char* get_state(){
if (switch_value == SWITCH_ON) {
return "ON";
} else {
return "OFF";
}
}
boolean set_switch(String set_str) {
client.publish(TOPIC_STATE, set_str.c_str());
Serial.print("Recv command:");
Serial.println(set_str.c_str());
pub_mqtt_state();
if (set_str.equals("0OFF")){
switch_value = SWITCH_OFF;
digitalWrite(PIN_SWITCH, SWITCH_OFF);
delay(20);
digitalWrite(PIN_SWITCH, SWITCH_ON);
} else if (set_str.equals("1OFF"))
{
digitalWrite(PIN_SWITCH, SWITCH_OFF);
delay(1000);
digitalWrite(PIN_SWITCH, SWITCH_ON);
}else if (set_str.equals("2OFF"))
{
digitalWrite(PIN_SWITCH, SWITCH_OFF);
delay(20);
digitalWrite(PIN_SWITCH, SWITCH_ON);
delay(20);
digitalWrite(PIN_SWITCH, SWITCH_OFF);
delay(20);
digitalWrite(PIN_SWITCH, SWITCH_ON);
}
return true;
}
void pub_mqtt_state() {
//if(switch_value == SWITCH_ON){
client.publish(TOPIC_STATE, "0ON");
client.publish(TOPIC_STATE, "1ON");
client.publish(TOPIC_STATE, "2ON");
//} else {
//client.publish(TOPIC_STATE, "OFF");
//}
}
void mqtt_config() {
String head = "{";
String name_str = "name: " + String(device_name) + ", ";
String cmd_topic = "command_topic: " + String(TOPIC_COMMAND) + ", ";
String state_topic = "state_topic: " + String(TOPIC_STATE) ;
String end_str = "}";
String config = "{" + name_str + ", " + state_topic + ", " + cmd_topic + "}";
Serial.println(config.c_str());
int msgLen = head.length() + name_str.length() + cmd_topic.length() + state_topic.length() + end_str.length();
//发送长文本需要另外处理
client.beginPublish(TOPIC_CONFIG, msgLen, false);
client.print(head);
client.print(name_str);
client.print(cmd_topic);
client.print(state_topic);
client.print(end_str);
client.endPublish();
}
```
configuration.yaml中添加
```
switch: !include switchs.yaml
```
homeassistant中单独用了一个文件switchs.yaml
```
- platform: mqtt
name: "ESP-01 switch_one"
state_topic: "ESP-01/switch/state"
command_topic: "ESP-01/switch/set"
payload_on: "0ON"
payload_off: "0OFF"
state_on: "0ON"
state_off: "0OFF"
optimistic: false
qos: 0
retain: true
- platform: mqtt
name: "ESP-01 switch_long"
state_topic: "ESP-01/switch/state"
command_topic: "ESP-01/switch/set"
payload_on: "1ON"
payload_off: "1OFF"
state_on: "1ON"
state_off: "1OFF"
optimistic: false
qos: 0
retain: true
- platform: mqtt
name: "ESP-01 switch_two"
state_topic: "ESP-01/switch/state"
command_topic: "ESP-01/switch/set"
payload_on: "2ON"
payload_off: "2OFF"
state_on: "2ON"
state_off: "2OFF"
optimistic: false
qos: 0
retain: true
```
3
homeassistant界面
|
|