本帖最后由 zro22 于 2020-3-28 20:04 编辑
现在啥东西都想用手机控制,倒是到最后我发现很多时候用手机反而很麻烦,打开APP-找到按钮-点击。
或者用小爱啥的,经常说几遍听不懂,气的想扔了。
所以我就思量有时候还是用遥控器最靠谱。
如果利用现有红外遥控器控制HA呢
总体架构是这样,红外(或者315 433射频)遥控器发射信号,接收头接收到信号后给到ESP,然后ESP交给MQTT。
HA里面根据MQTT内容执行相应的自动化。
硬件很简单,首先买个红外一体接收头 2块多钱。再买个ESP8266或者ESP32.
下载ESPHOME
连线也简单,红外接收头三个pin,看规格书,一个5v 一个GND 一个信号。
随便在esp上找一个脚连上信号。
esphome 加入如下代码.
也就是说在收到对应码的时候转发到MQTT。
remote_receiver:
pin:
number: GPIO23
inverted: True
mode: INPUT_PULLUP
dump: all
on_nec:
then:
- mqtt.publish:
topic: esphome32/senso/ir
payload: !lambda |-
char temp[20];
sprintf(temp, "%d:%d", x.address, x.command);
ESP_LOGI("main", "nec: %s", temp);
return temp;
on_panasonic:
then:
- mqtt.publish:
topic: esphome32/senso/ir
payload: !lambda |-
char temp[20];
sprintf(temp, "%d:%d", x.address, x.command);
ESP_LOGI("main", "nec: %s", temp);
return temp;
on_sony:
then:
- mqtt.publish:
topic: esphome32/senso/ir
payload: !lambda |-
char temp[20];
sprintf(temp, "%d:%d", x.data, x.nbits);
ESP_LOGI("main", "nec: %s", temp);
return temp;
on_jvc:
then:
- mqtt.publish:
topic: esphome32/senso/ir
payload: !lambda |-
char temp[20];
sprintf(temp, "%d", x.data);
ESP_LOGI("main", "nec: %s", temp);
return temp;
on_rc5:
then:
- mqtt.publish:
topic: esphome32/senso/ir
payload: !lambda |-
char temp[20];
sprintf(temp, "%d:%d", x.address, x.command);
ESP_LOGI("main", "nec: %s", temp);
return temp;
然后在HA里面写自动化就好了,
比如开灯
- alias: num4 toggle zoulangdeng
trigger:
platform: mqtt
topic: 'esphome32/senso/ir'
payload: "52530:29070" # 4
action:
- service: switch.toggle
data:
entity_id: switch.wall_switch_left_158d0xxxx
以上是红外的例子,
还可以搞一个射频的中转,但是苦于没有好看的按键比较多的射频遥控器,所以我还没有做,但是ESP到MQTT我已经尝试了。
首先需要买一个可以解码射频转UART的模块。
然后把模块的TX GND VCC随便在esp板子上找个pin接好。
我参照网上的例子,自己简单改了一下,把如下代码保存成uarttext_sensor.h或者随便你想叫什么都可以。
#include "esphome.h"
using namespace esphome;
#define P1_MAXTELEGRAMLENGTH 1500
#define DELAY_MS 60000 // Delay in miliseconds before reading another telegram
#define WAIT_FOR_DATA_MS 2000
// Use data structure according to: https://github.com/matthijskooijman/arduino-dsmr
class CustomP1UartComponent : public Component, public uart::UARTDevice {
protected:
char telegram[P1_MAXTELEGRAMLENGTH];
char c;
int telegramlen;
bool headerfound;
bool footerfound;
unsigned long lastread;
int bytes_read;
bool data_available() {
// See if there's data available.
unsigned long currentMillis = millis();
unsigned long previousMillis = currentMillis;
//while (currentMillis - previousMillis < WAIT_FOR_DATA_MS) { // wait in miliseconds
// currentMillis = millis();
if (available()) {
return true;
}
// }
return false;
}
bool read_message() {
//ESP_LOGD("DmsrCustom","Read message");
headerfound = false;
footerfound = false;
telegramlen = 0;
bytes_read = 0;
unsigned long currentMillis = millis();
unsigned long previousMillis = currentMillis;
if (available()) { // Check to be sure
// Messages come in batches. Read until footer.
//while (!footerfound && currentMillis - previousMillis < 5000) { // Loop while there's no footer found with a maximum of 5 seconds
// currentMillis = millis();
// Loop while there's data to read
while (available()) { // Loop while there's data
if (telegramlen >= P1_MAXTELEGRAMLENGTH) { // Buffer overflow
headerfound = false;
footerfound = false;
ESP_LOGD("DmsrCustom","Error: Message larger than buffer");
break;
}
//bytes_read++;
//c = read();
telegram[telegramlen++] = read();
} // While data available
telegram[telegramlen] = 0;
//} // !footerfound
if(telegramlen>1)
s_energy_delivered_tariff1->publish_state(telegram);
}
return false;
}
public:
CustomP1UartComponent(UARTComponent *parent) : UARTDevice(parent) {}
TextSensor *s_energy_delivered_tariff1 = new TextSensor();
void setup() override {
lastread = 0;
}
void loop() override {
unsigned long now = millis();
// if (now - lastread > DELAY_MS || lastread == 0) {
// lastread = now;
if (data_available()) { // Check for x seconds if there's data available
bool have_message = read_message();
}
// }
}
};
然后在esphome里面配置text sensor
esphome:
name: esphome32
platform: ESP32
board: nodemcu-32s
includes:
- uarttext_sensor.h
text_sensor:
- platform: custom
lambda: |-
auto dsmr_p1_sensor = new CustomP1UartComponent(id(uart_bus1));
App.register_component(dsmr_p1_sensor);
return {dsmr_p1_sensor->s_energy_delivered_tariff1};
text_sensors:
name: "My Custom Text Sensor"
uart:
- id: uart_bus1
tx_pin: GPIO15
rx_pin: GPIO35
baud_rate: 9600
当uart收到消息时会更新textsensor,然后在MQTT里面就可以看到更新了。
当然esphome的mqtt别忘了打开
mqtt:
broker: 192.168.xx.xxx
username: admin
password: xxxx
帖子没说esphome怎么搞,如果不懂esphome的先去搜索别的帖子搞懂esphome。
|