目录

项目背景

 实现过程

参考代码


项目背景

最近看中智能小夜灯的项目,通过淘宝几块钱购买人体感应小夜灯(使用外壳),然后机子画板子加入esp8266联网控制,接入homeassistant,做智能控制。

目前以实现以下功能:

  • 红外人体侦测
  • ld2410微波侦测
  • 环境光亮度侦测
  • 夜灯控制
  • RGB彩灯控制
  • dht11温湿度读值(不准)
  • 红外发射与接收

既然已经实现红外发射与接收,那么有没有可能通过这个设备,做为普通红外遥控转接信号来控制已接入homeassistant的其他设备呢?

 实现过程

网上论坛大神已提供相关代码,原作者帖子

实现步骤:

  • 红外(或者315 433射频)遥控器发射信号
  • 接收头接收到信号后给到ESP
  • ESP交给MQTT
  • HA里面根据MQTT内容执行相应的自动化

仅验证红外部分,实测可用:

  1. 使用esphome来识别红外遥控按键代码[main:094]: nec: 1886437949:0
[20:26:22][D][remote.pronto:229]: Received Pronto: data=0000 006D 0001 0000 0003 06C3
[20:26:23][I][main:094]: nec: 1886437949:0
[20:26:23][D][remote.pronto:229]: Received Pronto: data=0000 006D 0001 0000 0002 06C3
[20:26:23][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AD 00AB 0018 003F 0019 06C3
[20:26:24][I][main:094]: nec: 1886437949:0
[20:26:24][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AD 00AB 0019 003E 0018 06C3
[20:26:31][D][remote.pronto:229]: Received Pronto: data=0000 006D 0001 0000 0001 06C3
[20:26:35][I][main:094]: nec: 1886413469:0
[20:26:35][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AE 00AA 0019 003E 0018 06C3
[20:26:37][I][main:094]: nec: 1886413469:0
[20:26:37][D][remote.pronto:229]: Received Pronto: data=0000 006D 0001 0000 0003 06C3
[20:26:37][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AE 00AA 0018 003F 0018 06C3
[20:26:38][I][main:094]: nec: 1886413469:0
[20:26:38][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AE 00AA 0019 003E 0018 06C3
[20:26:40][I][main:094]: nec: 1886413469:0
[20:26:40][D][remote.pronto:229]: Received Pronto: data=0000 006D 0003 0000 00AF 00A9 0018 003F 0019 06C3

建立自动化例子

alias: num4 toggle ir_conf
trigger:
  - platform: mqtt
    topic: ir_conf/senso/ir
    payload: '1886413469:0'
action:
  - service: switch.toggle
    data:
      entity_id: switch.wall_switch_left_158d0xxxx

使用红外遥控来验证

 

参考代码

附上esphome代码:

esphome:
  name: ir_conf

esp8266:
  framework:
    version: 2.7.4
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password
ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    # Set this to the IP of the ESP
    static_ip: 192.168.1.178
    #static_ip: 192.168.1.10
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.1.1
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0
    #DNS1: 192.168.31.1
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Light Xiaoyedeng"
    password: "MynRqBY96AUC"

captive_portal:

# remote_receiver:
  # id: rcvr
  # pin:
    # number: D5
    # inverted: true
    # # mode:
      # # input: true
      # # pullup: true
  # dump: #all
    # - nec
    # - raw
mqtt:
  broker: !secret broker
  username: !secret mqtt_name
  password: !secret mqtt_password
  discovery: true
remote_receiver:
  pin: 
    number: D5
    inverted: True
    mode: INPUT_PULLUP
  dump: all
  on_nec:
    then:
    - mqtt.publish:
        topic: ir_conf/sensor/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: ir_conf/sensor/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: ir_conf/sensor/ir
        payload: !lambda |-
          char temp[20];
          sprintf(temp, "%d:%d", x.data, x.nbits);
          ESP_LOGI("main", "nec: %s", temp);
          return temp; 
  on_samsung:
    then:
    - mqtt.publish:
        topic: ir_conf/sensor/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: ir_conf/sensor/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: ir_conf/sensor/ir
        payload: !lambda |-
          char temp[20];
          sprintf(temp, "%d:%d", x.address, x.command);
          ESP_LOGI("main", "nec: %s", temp);
          return temp;

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐