esphome自制红外遥控器接入HA,用来控制灯光,开关等,可使用任意遥控控制

本文介绍了如何将低价购得的人体感应小夜灯改造为智能设备,通过ESP8266模块接入HomeAssistant系统,实现红外人体侦测、微波侦测、环境光亮度控制等功能。作者进一步探索了利用该设备接收红外遥控信号并通过MQTT发送到HomeAssistant,以控制其他智能设备的可能性。通过示例代码展示了红外遥控按键识别和自动化设置。
摘要由CSDN通过智能技术生成

目录

项目背景

 实现过程

参考代码


项目背景

最近看中智能小夜灯的项目,通过淘宝几块钱购买人体感应小夜灯(使用外壳),然后机子画板子加入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;

好的,你可以使用红外遥控模块和一个蜂鸣模块来实现这个功能。 首先,你需要连接红外遥控模块和 Arduino 板。可以参考这个红外遥控模块的连接图: ![红外遥控模块连接图](https://img-blog.csdnimg.cn/20210729153200401.png) 接下来,你需要连接蜂鸣模块。可以参考这个蜂鸣模块的连接图: ![蜂鸣模块连接图](https://img-blog.csdnimg.cn/20210729153224395.png) 接下来,你需要下载并安装红外遥控库和蜂鸣库。可以在 Arduino IDE 中使用 "Sketch->Include Library->Manage Libraries" 来搜索和安装这些库。 然后,你可以使用下面的代码来控制蜂鸣开关: ```C++ #include <IRremote.h> // 引入红外遥控库 #include <toneAC.h> // 引入蜂鸣库 int RECV_PIN = 11; // 红外遥控模块连接到Arduino的11号引脚 int buzzerPin = 9; // 蜂鸣模块连接到Arduino的9号引脚 IRrecv irrecv(RECV_PIN); decode_results results; void setup() { irrecv.enableIRIn(); // 初始化红外接收模块 pinMode(buzzerPin, OUTPUT); // 设置蜂鸣引脚为输出模式 } void loop() { if (irrecv.decode(&results)) { if (results.value == 0xFF6897) { // 如果接收到红外码为0xFF6897 toneAC(buzzerPin, 1000, 500); // 播放1000Hz的蜂鸣声500毫秒 delay(500); // 延时500毫秒 noToneAC(buzzerPin); // 停止播放蜂鸣声 } irrecv.resume(); // 继续接收红外信号 } } ``` 在这个代码中,我们使用了 `IRremote` 库和 `toneAC` 库来实现红外遥控和蜂鸣控制。在 `setup()` 函数中,我们初始化了红外接收模块和设置了蜂鸣引脚为输出模式。在 `loop()` 函数中,我们使用 `irrecv.decode(&results)` 函数来接收红外信号,并判断接收到的红外码是否为0xFF6897。如果接收到了这个红外码,我们就使用 `toneAC` 函数来播放1000Hz的蜂鸣声500毫秒,并在播放完毕后使用 `noToneAC` 函数来停止播放蜂鸣声。最后,我们使用 `irrecv.resume()` 函数来继续接收红外信号。 希望这个代码可以帮助你实现你的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值