本帖最后由 dscao 于 2022-1-18 20:19 编辑
之前玩12元的rf-bridge感觉还不错,但信号弱是唯一不足的地方,本来想着淘个信号放在转发之类的设备,成品设备也要80以上。干脆弄个sonoff rf bridge玩玩,成品小巧,看有不少文章介绍这个。于是买了个新款的好看,回来后发现刷esphome的资料很少,中文的更少。还好国外论坛有一些资料。
先在编译esphome固件:按官方的代码下载bin文件,https://esphome.io/components/rf_bridge.html
esphome:
name: sonoff-rf-bridge
esp8266:
board: esp01_1m
# Enable Home Assistant API
web_server:
port: 80
api:
services:
- service: send_rf_code
variables:
sync: int
low: int
high: int
code: int
then:
- rf_bridge.send_code:
sync: !lambda 'return sync;'
low: !lambda 'return low;'
high: !lambda 'return high;'
code: !lambda 'return code;'
- service: learn
then:
- rf_bridge.learn
uart:
tx_pin: 1
rx_pin: 3
baud_rate: 19200
logger:
baud_rate: 0
rf_bridge:
on_code_received:
then:
- homeassistant.event:
event: esphome.rf_code_received
data:
sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'
ota:
password: !secret ota_password
status_led:
pin:
number: GPIO13
inverted: false
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Sonoff-Rf-Bridge"
password: "wifipassword"
captive_portal:
复制代码
参考网上资料用ttl将编译的固件通过ESPHome-Flasher-1.4.0-Windows-x64.exe刷入:
可以使用,收发也都没问题,就是信号的格式只能是这种。发可以在hass的服务中调用服务,事件中订阅esphome.rf_code_received也能收到json的消息。
已经刷入esphome固件了,升级或修改或刷其它固件都非常方便了。
(参考刷机接线图,按住唯一的按钮插入电脑usb,点工具刷机)
后找到硬改的教程:
https://community.home-assistant ... hing-help/344326/17
代码要参考:https://esphome.io/components/re ... emote-setting-up-rf
先升级固件:
esphome:
name: sonoff-rf-bridge
esp8266:
board: esp01_1m
# Enable Home Assistant API
web_server:
port: 80
api:
services:
- service: send_rf_code
variables:
sync: int
low: int
high: int
code: int
then:
- rf_bridge.send_code:
sync: !lambda 'return sync;'
low: !lambda 'return low;'
high: !lambda 'return high;'
code: !lambda 'return code;'
- service: learn
then:
- rf_bridge.learn
uart:
tx_pin: 1
rx_pin: 3
baud_rate: 19200
logger:
baud_rate: 0
rf_bridge:
on_code_received:
then:
- homeassistant.event:
event: esphome.rf_code_received
data:
sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'
ota:
password: !secret ota_password
remote_receiver:
pin: 4
dump: rc_switch
# Settings to optimize recognition of RF devices
tolerance: 50%
filter: 250us
idle: 4ms
buffer_size: 2kb
remote_transmitter:
pin: 5
carrier_duty_percent: 100%
# this will log received commands, and can also transmit. Read up here:
# https://esphome.io/components/remote_transmitter.html#remote-setting-up-rf
status_led:
pin:
number: GPIO13
inverted: false
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Sonoff-Rf-Bridge"
password: "wifipassword"
captive_portal:
switch:
- platform: template
name: Sample Remote
turn_on_action:
- remote_transmitter.transmit_rc_switch_raw:
code: '011000000011111001010100'
protocol: 1
repeat: 5
复制代码
只是接上两根线,开机发现接收到多种格式的信号了,包括原来的信号和新的rc_switch格式的了。如果只接两根线,接收就是功能较全的,也不影响原来的功能。但只能用原来的格式发送。(发送Sample Remote时没反应)
找来美工刀,划断教程中图片中指示的位置,再操作发现发送Sample Remote时有反应了,接收到相同信号了。但是外界的设备没反应,外部射频接收器也没收到信号 。于是继续cut,反复试了多次,难道是没断开,又加深切口。还是不行,最后把教程中的三个地方都切了。
反复弄和修改代码,都是无法发送,但接收信号正常。
断断续续试了一天多,发送信号发不出去,但esphome内部却能接收到,用射频接收器帖着sonoff边上也能收到信号,但内容不一致。想了一天多时间没想明白,以为又给玩废掉一个设备。郁闷时突然找到了线索,最终找到了正确的答案:
https://github.com/esphome/issues/issues/1828 Remote transmitter is not working after update [1.15.3]->[1.16.0]
这个问题不仅是这个设备,基本上所有用新版本的esphome编译的固件作为信号发射时都会出现这个问题。而esphome官网介绍中却没看到说明。
最后正常使用的代码:
esphome:
name: sonoff-rf-bridge
esp8266:
board: esp01_1m
# Enable Home Assistant API
web_server:
port: 80
api:
services:
- service: send_rf_code
variables:
sync: int
low: int
high: int
code: int
then:
- rf_bridge.send_code:
sync: !lambda 'return sync;'
low: !lambda 'return low;'
high: !lambda 'return high;'
code: !lambda 'return code;'
- service: learn
then:
- rf_bridge.learn
uart:
tx_pin: 1
rx_pin: 3
baud_rate: 19200
logger:
baud_rate: 0
rf_bridge:
on_code_received:
then:
- homeassistant.event:
event: esphome.rf_code_received
data:
sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'
ota:
password: !secret ota_password
remote_receiver:
pin: 4
dump: rc_switch
# Settings to optimize recognition of RF devices
tolerance: 50%
filter: 250us
idle: 4ms
buffer_size: 2kb
remote_transmitter:
pin: 5
carrier_duty_percent: 100%
# this will log received commands, and can also transmit. Read up here:
# https://esphome.io/components/remote_transmitter.html#remote-setting-up-rf
status_led:
pin:
number: GPIO13
inverted: false
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Sonoff-Rf-Bridge"
password: "wifipassowrd"
captive_portal:
switch:
- platform: template
name: Sample Remote
turn_on_action:
- remote_transmitter.transmit_rc_switch_raw:
code: '011000000011111001010100'
protocol: 1
repeat:
times: 10
wait_time: 0s
# wait_time: 100ms
- platform: template
name: Sample Switch
id: switch_1
optimistic: true
assumed_state: true
turn_on_action:
remote_transmitter.transmit_rc_switch_raw:
code: '010101101010100000100001'
protocol: 1
repeat:
times: 10
wait_time: 0s
turn_off_action:
remote_transmitter.transmit_rc_switch_raw:
code: '010101101010100000100010'
protocol: 1
repeat:
times: 10
wait_time: 0s
binary_sensor:
- platform: status
name: "Sonoff RF Bridge Status"
- platform: remote_receiver
name: "Sample Blinds Up"
rc_switch_raw:
code: '010101101010100000100010'
protocol: 1
filters:
- delayed_off: 500ms
复制代码
这里的rf-bridge相关的代码已经没用了,因为线都给剪断了。有空再研究一下看看api和homeassistant.homeassistant.event部分能否修改成现在可用的,这样方便直接在hass中调用而不用修改固件。
这里重点问题是新版esphome编译 时, remote_transmitter.transmit_rc_switch_raw 默认的是wait_time: 250us 一定要改成或加上: wait_time: 0s 才能正常发送信号。