esphome:
name: rfbridge
friendly_name: RFBridge
esp32:
board: nodemcu-32s
framework:
type: arduino
#external_components:
# - source: my_components
# components: [ remote_base, remote_receiver, remote_transmitter]
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "oio02kQmD6MJn6zWnZx5lAbSWyYIYddpwYGipMy/1hY="
services:
- service: rf_433_send_raw
variables:
code: string
repeat: int
then:
- remote_transmitter.transmit_rc_switch_raw:
code: !lambda 'return code;'
repeat:
times: !lambda 'return repeat;'
wait_time: 0.01s
- service: rf_433_send_typea
variables:
repeat: int
group: string
device: string
state: int
then:
- remote_transmitter.transmit_rc_switch_type_a:
repeat:
times: !lambda 'return repeat;'
group: !lambda 'return group;'
device: !lambda 'return device;'
state: !lambda 'return state;'
protocol: 1
ota:
password: "f6b09c4d30124fc97f22dd74899c5342"
wifi:
ssid: 2.4ghome
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Rfbridge Fallback Hotspot"
password: "iTbOmVzigCuo"
use_address: 192.168.10.40
captive_portal:
web_server:
port: 80
globals:
- id: global_group
type: int
restore_value: no
initial_value: '0'
- id: global_device
type: int
restore_value: no
initial_value: '0'
- id: global_state
type: int
restore_value: no
initial_value: '0'
- id: global_rcvstr
type: std::string
restore_value: no # Strings cannot be saved/restored
- id: global_rcvvalstr
type: std::string
restore_value: no # Strings cannot be saved/restored
- id: global_rf_rcvd
type: bool
restore_value: no
initial_value: '0'
- id: global_oldrcv
type: long long int
restore_value: no
initial_value: '0'
- id: global_oldrcv2
type: long long int
restore_value: no
initial_value: '0'
- id: global_oldrcvtime
type: int
restore_value: no
initial_value: '0'
- id: global_oldrcvtime2
type: int
restore_value: no
initial_value: '0'
- id: global_rcv_skip
type: int
restore_value: no
initial_value: '0'
- id: global_same_cnt
type: int
restore_value: no
initial_value: '0'
- id: global_snd_grop
type: std::string
restore_value: no
- id: global_snd_dev
type: std::string
restore_value: no
- id: global_snd_state
type: int
restore_value: no
initial_value: '0'
mqtt:
broker: 192.168.xx.xxx
username: xxx
password: xxxt
on_json_message:
- topic: rfbridge/rftosend/raw
qos: 0
then:
- remote_transmitter.transmit_rc_switch_raw:
code: !lambda |-
int code = 0;
int i=0;
char strcode[25];
strcode[24]=0;
if (x.containsKey("code")){
code = x["code"];
ESP_LOGD("RFRAWSND", "%d",code);
for(i=0;i<24;i++)
{
strcode[24-i-1] = '0';
if(code&1)
strcode[24-i-1] = '1';
code=code/2;
//ESP_LOGD("MQSEND", "%c",strcode[i]);
}
//itoa(code,strcode,2);
ESP_LOGD("RFRAWSND", "%s",strcode);
return strcode;}
return 0;
protocol: 1
- topic: rfbridge/rftosend/atype
qos: 0
then:
- remote_transmitter.transmit_rc_switch_type_a:
group: !lambda |-
/*{
"group": "25", max31
"device": "18", max31
"state": 1 1 or 0
}*/
int grop = 0;
int i=0;
char strcode[6];
strcode[5]=0;
id(global_snd_grop) = "0";
if (x.containsKey("group")){
grop = x["group"];
id(global_snd_grop) = grop;
//ESP_LOGD("RFASND", "%d",grop);
/*strcode[0] = grop/10000 + 0x30;
strcode[1] = (grop%10000)/1000 + 0x30;
strcode[2] = (grop%1000)/100 + 0x30;
strcode[3] = (grop%100)/10 + 0x30;
strcode[4] = (grop%10) + 0x30 ;*/
for(i=0;i<5;i++)
{
strcode[4-i] = (grop&1) + 0x30;
grop = grop/2;
}
ESP_LOGD("RFASND", "Group:%s",strcode);
id(global_snd_grop) = strcode;
return strcode;}
return 0;
device: !lambda |-
int devc = 0;
int i=0;
char strcode[6];
strcode[5]=0;
id(global_snd_dev) = "0";
if (x.containsKey("device")){
devc = x["device"];
id(global_snd_dev) = devc;
//ESP_LOGD("RFASND", "%d",devc);
/*strcode[0] = devc/10000 + 0x30;
strcode[1] = (devc%10000)/1000 + 0x30;
strcode[2] = (devc%1000)/100 + 0x30;
strcode[3] = (devc%100)/10 + 0x30;
strcode[4] = (devc%10) + 0x30;*/
for(i=0;i<5;i++)
{
strcode[4-i] = (devc&1) + 0x30;
devc = devc/2;
}
ESP_LOGD("RFASND", "Device:%s",strcode);
id(global_snd_dev) = strcode;
return strcode;}
return 0;
state: !lambda |-
int stat;
stat = x["state"];
id(global_snd_state) = stat;
ESP_LOGD("RFASND", "state:%d",stat);
return stat;
protocol: 1
repeat:
times: 7
wait_time : 30ms
remote_receiver:
pin:
number: GPIO19
#inverted: True
mode: INPUT_PULLUP
dump:
- rc_switch
# Settings to optimize recognition of RF devices
tolerance: 20%
filter: 250us
idle: 6ms
# buffer_size: 3kb
on_rc_switch:
then:
- homeassistant.event:
event: esphome.rf_code_received
data:
code: !lambda |-
//'char buffer [10];return itoa(data.code,buffer,16);'
char rcvdata[80];
std::string nullbuf;
int grop=0;
int devc=0;
int channel=0;
int cmd=0;
int subcmd=0;
int i;
int curtime;
long long temp;
int state=0;
id(global_rcv_skip) = 0;
//root["code"] = x.code;
temp = x.code;
ESP_LOGD("RCV", "temp:%llx",temp);
sprintf(rcvdata, "%llx", temp);
id(global_rcvvalstr) = rcvdata;
//if(temp>0xffffffff)
curtime = millis();
if(id(global_oldrcv)==temp)
{
if(curtime-id(global_oldrcvtime)<120)
{
id(global_rcv_skip) = 1;
id(global_same_cnt)++;
}else
id(global_same_cnt) = 0;
}
else if(id(global_oldrcv2)==temp)
{
if(curtime-id(global_oldrcvtime2)<120)
{
id(global_rcv_skip) = 1;
id(global_same_cnt)++;
}else
id(global_same_cnt) = 0;
}else
id(global_same_cnt)=0;
//if(((temp>>1)&0x3)==0)
// id(global_rcv_skip) = 1;
//if((id(global_same_cnt)>=20)&&(id(global_same_cnt)%2==0))
//{
// id(global_rcv_skip) = 0;
//}
if(id(global_same_cnt)==0)
{
id(global_rcv_skip) = 0;
}
ESP_LOGD("RCV", "Millis() %d diff: %d skip:%d cnt:%d", curtime,curtime-id(global_oldrcvtime),id(global_rcv_skip),id(global_same_cnt));
id(global_oldrcv2) = id(global_oldrcv);
id(global_oldrcv) = temp;
//if(id(global_rcv_skip)==0)
id(global_oldrcvtime2) = id(global_oldrcvtime);
id(global_oldrcvtime) = curtime;
if(((temp>>16)&0xffffff)==0x3191f9)
{
sprintf(rcvdata, "%llx", temp>>3);
//root["dooya"] = rcvdata;
grop = (temp>>19);
devc = (temp&0x7ffff) >> 11;
channel = (temp&0x7ff) >> 7;
cmd = (temp&0x7f) >> 3;
subcmd = (temp&0x7);
//ESP_LOGD("RFDOOYA", "Group:%x",grop);
//ESP_LOGD("RFDOOYA", "Device:%x",devc);
//ESP_LOGD("RFDOOYA", "Channel:%x",channel);
//ESP_LOGD("RFDOOYA", "Command:%x",cmd);
//ESP_LOGD("RFDOOYA", "SubCommand:%x",subcmd);
id(global_rcvstr) = rcvdata;
return id(global_rcvstr);
}
if(temp<0x555555)
{
for(i=0;i<24;i++)
{
rcvdata[24-i-1]=0;
if(temp&1)
rcvdata[24-i-1]=1;
temp = temp/2;
}
//for(i=0;i<24;i++)
// ESP_LOGD("RCV", "%d,%d",i,rcvdata[i]);
for(i=0;i<5;i++)
{
if((rcvdata[1+2*i]==0)&&(rcvdata[2+2*i]==0))
grop += 1;
if(i==4)
break;
grop *=2;
}
//ESP_LOGD("RCV", "Group:%d",grop);
for(i=0;i<5;i++)
{
if((rcvdata[11+2*i]==0)&&(rcvdata[12+2*i]==0))
devc += 1;
if(i==4)
break;
devc *=2;
}
//ESP_LOGD("RCV", "Device:%d",devc);
if((rcvdata[21]==0)&&(rcvdata[22]==0)&&(rcvdata[23]==0))
state = 1;
else if((rcvdata[21]==0)&&(rcvdata[22]==1)&&(rcvdata[23]==0))
state = 0;
else
{
grop = 0;
devc = 0;
state = 0;
id(global_rcv_skip) = 1;
}
ESP_LOGD("RCV", "skip %d", id(global_rcv_skip));
//ESP_LOGD("RCV", "State:%d",state);
id(global_group) = grop;
id(global_device) = devc;
id(global_state) = state;
sprintf(rcvdata, "%02d", grop);
//root["Group"] = rcvdata;//grop;
sprintf(rcvdata, "%02d", devc);
//root["Device"] = rcvdata;//devc;
sprintf(rcvdata, "%d", state);
//root["State"] = rcvdata;//state;
sprintf(rcvdata, "%02d%02d%d", grop,devc,state);
//root["msg"] = rcvdata;
id(global_rcvstr) = rcvdata;
}
return id(global_rcvstr);
value: !lambda 'return id(global_rcvvalstr);'
codenum: !lambda return id(global_group)*1000+id(global_device)*10+id(global_state);
onecode: !lambda |-
if(id(global_rcv_skip)==1)
{
return 10000;
}else
return id(global_group)*100+id(global_device);
#- mqtt.publish_json:
# topic: rfbridge/rfrcv
# payload: !lambda |-
- mqtt.publish:
topic: !lambda |-
if(id(global_rcv_skip)==1)
{
return "rfbridge/rcvstrnull";
}
else
return "rfbridge/rcvstr";
payload: !lambda |-
if(id(global_rcv_skip)==1)
{
return "0";
}
else
return id(global_rcvstr);
remote_transmitter:
- pin: GPIO21
# id: tr433
# RF uses a 100% carrier signal
carrier_duty_percent: 100%
# - pin: GPIO20
# id: tr315
# carrier_duty_percent: 100%
switch:
- platform: template
name: swforrfsnd
id: rfsw
turn_on_action:
- delay: 0.2s
- switch.turn_off: rfsw
#下面的代码几个开关测试RF发送,没什么用
- platform: template
name: RF2 raw Button
turn_on_action:
- remote_transmitter.transmit_rc_switch_raw:
code: '100010000000000010111110'
protocol: 2
- platform: template
name: RFA1 Power Button
turn_on_action:
- remote_transmitter.transmit_rc_switch_type_a:
group: '01001'
device: '10110'
state: off
protocol: 1
turn_off_action:
- remote_transmitter.transmit_rc_switch_type_a:
group: '01001'
device: '10110'
state: on
protocol: 1
- platform: template
name: RFA2 Power Button
turn_on_action:
- remote_transmitter.transmit_rc_switch_type_a:
group: '01001'
device: '10111'
state: off
protocol: 1
turn_off_action:
- remote_transmitter.transmit_rc_switch_type_a:
group: '01001'
device: '10111'
state: on
protocol: 1
- platform: template
name: RFCA Power Button
turn_on_action:
- remote_transmitter.transmit_rc_switch_type_c:
family: 'c'
group: 3
device: 1
state: off
protocol: 1
turn_off_action:
- remote_transmitter.transmit_rc_switch_type_c:
family: 'c'
group: 3
device: 1
state: on
protocol: 1
- platform: template
name: RFCC Power Button
turn_on_action:
- remote_transmitter.transmit_rc_switch_type_c:
family: 'a'
group: 3
device: 1
state: off
protocol: 1
turn_off_action:
- remote_transmitter.transmit_rc_switch_type_c:
family: 'a'
group: 3
device: 1
state: on
protocol: 1