Skip to content

Commit 932397d

Browse files
committed
Moved "broadcast_changes_to_all_BMS" to a NEW switch.
See esp32-S3-example-jkpb-rs485_0master_2slaves.yaml or esp32-S3-example-jkpb-rs485_0master_7slaves.yaml. Those examples has been adapted with this change.
1 parent a312ccc commit 932397d

5 files changed

+54
-14
lines changed

components/jk_rs485_sniffer/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
CONF_RX_TIMEOUT = "rx_timeout"
1212
CONF_PROTOCOL_VERSION = "protocol_version"
1313
CONF_TALK_PIN = "talk_pin"
14-
CONF_BROADCAST_TO_ALL_BMS = "broadcast_to_all_bms"
1514

1615
jk_rs485_sniffer_ns = cg.esphome_ns.namespace("jk_rs485_sniffer")
1716

@@ -41,7 +40,6 @@
4140
cv.Optional(
4241
CONF_RX_TIMEOUT, default="50ms"
4342
): cv.positive_time_period_milliseconds,
44-
cv.Required(CONF_BROADCAST_TO_ALL_BMS): cv.boolean,
4543
}
4644
)
4745
.extend(cv.COMPONENT_SCHEMA)
@@ -70,7 +68,7 @@ async def to_code(config):
7068
else:
7169
cg.add(var.set_talk_pin_needed(False))
7270

73-
cg.add(var.set_broadcast_to_all_bms(config[CONF_BROADCAST_TO_ALL_BMS]))
71+
7472

7573
def jk_rs485_sniffer_device_schema():
7674
schema = {

components/jk_rs485_sniffer/jk_rs485_sniffer.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ uint16_t chksum(const uint8_t data[], const uint16_t len) {
6969
}
7070

7171

72+
void JkRS485Sniffer::set_broadcast_changes_to_all_bms(bool state) {
73+
this->broadcast_changes_to_all_bms_ = state;
74+
if (state) {
75+
ESP_LOGD(TAG, "Broadcast to All BMSs ACTIVATED");
76+
// Aquí puedes añadir el código que se ejecuta cuando se activa
77+
} else {
78+
ESP_LOGD(TAG, "Broadcast to All BMSs DEACTIVATED");
79+
// Aquí puedes añadir el código que se ejecuta cuando se desactiva
80+
}
81+
}
82+
83+
84+
85+
bool JkRS485Sniffer::get_broadcast_changes_to_all_bms() const {
86+
return this->broadcast_changes_to_all_bms_;
87+
}
88+
89+
7290
void JkRS485Sniffer::handle_bms2sniffer_event(std::uint8_t slave_address, std::string event, std::uint8_t frame_type){
7391
// Maneja el evento aquí. Por ejemplo, puedes imprimir el evento:
7492
ESP_LOGD(TAG,"Received Event from BMS.. [address:0x%02X] @ %d --> %s", slave_address, frame_type, event.c_str());
@@ -110,7 +128,7 @@ void JkRS485Sniffer::handle_bms2sniffer_switch_or_number_uint32_event(std::uint8
110128
send_command_switch_or_number_to_slave_uint32(slave_address,third_element_of_frame,register_address,value);
111129
}
112130

113-
if (this->broadcast_to_all_bms_==true){
131+
if (this->broadcast_changes_to_all_bms_==true){
114132
for (uint8_t j = 1; j < 16; ++j) {
115133
if (rs485_network_node[j].available && slave_address!=j) {
116134
delayMicroseconds(50000);
@@ -137,7 +155,7 @@ void JkRS485Sniffer::handle_bms2sniffer_switch_or_number_int32_event(std::uint8_
137155
send_command_switch_or_number_to_slave_int32(slave_address,third_element_of_frame,register_address,value);
138156
}
139157

140-
if (this->broadcast_to_all_bms_==true){
158+
if (this->broadcast_changes_to_all_bms_==true){
141159
for (uint8_t j = 1; j < 16; ++j) {
142160
if (rs485_network_node[j].available && slave_address!=j) {
143161
delayMicroseconds(50000);
@@ -159,7 +177,7 @@ void JkRS485Sniffer::handle_bms2sniffer_switch_or_number_uint16_event(std::uint8
159177
rs485_network_node[slave_address].last_device_info_request_received_OK=0;
160178
}
161179

162-
if (this->broadcast_to_all_bms_==true){
180+
if (this->broadcast_changes_to_all_bms_==true){
163181
for (uint8_t j = 1; j < 16; ++j) {
164182
if (rs485_network_node[j].available && slave_address!=j) {
165183
delayMicroseconds(50000);

components/jk_rs485_sniffer/jk_rs485_sniffer.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class JkRS485Sniffer : public uart::UARTDevice, public output::TalkPin, public C
2727
JkRS485Sniffer() = default;
2828

2929

30+
void set_broadcast_changes_to_all_bms(bool state);
31+
bool get_broadcast_changes_to_all_bms() const;
32+
3033
void set_talk_pin(GPIOPin *pin) { talk_pin_ = pin; }
3134
void set_talk_pin_needed(bool talk_pin_needed) { talk_pin_needed_= talk_pin_needed;}
3235

@@ -51,7 +54,6 @@ class JkRS485Sniffer : public uart::UARTDevice, public output::TalkPin, public C
5154
rs485_network_node[cont].counter_cell_info_received=0;
5255
rs485_network_node[cont].counter_device_settings_received=0;
5356
rs485_network_node[cont].counter_device_info_received=0;
54-
5557
}
5658
last_master_activity=0;
5759
last_message_received_acting_as_master=0;
@@ -81,8 +83,6 @@ class JkRS485Sniffer : public uart::UARTDevice, public output::TalkPin, public C
8183

8284
void set_rx_timeout(uint16_t rx_timeout) { rx_timeout_ = rx_timeout; }
8385

84-
void set_broadcast_to_all_bms (bool broadcast_to_all_bms) { broadcast_to_all_bms_ = broadcast_to_all_bms; }
85-
8686
void handle_bms2sniffer_event(std::uint8_t slave_address, std::string event, std::uint8_t frame_type);
8787

8888
void handle_bms2sniffer_switch_or_number_uint32_event(std::uint8_t slave_address, std::uint8_t third_element_of_frame, std::uint16_t register_address, std::uint32_t value);
@@ -114,7 +114,7 @@ class JkRS485Sniffer : public uart::UARTDevice, public output::TalkPin, public C
114114

115115
std::vector<uint8_t> rx_buffer_;
116116
uint16_t rx_timeout_{50};
117-
bool broadcast_to_all_bms_;
117+
bool broadcast_changes_to_all_bms_;
118118
uint32_t last_jk_rs485_network_activity_{0};
119119
uint32_t last_jk_rs485_pooling_trial_{0};
120120
std::vector<JkRS485SnifferDevice *> devices_;

esp32-S3-example-jkpb-rs485_0master_2slaves.yaml

+15-3
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,13 @@ uart:
107107
rx_buffer_size: 500
108108
tx_pin: ${tx_pin_uart_0}
109109
rx_pin: ${rx_pin_uart_0}
110-
110+
111111
jk_rs485_sniffer:
112112
- id: sniffer0
113113
protocol_version: "JK02_32S"
114114
rx_timeout: 500ms
115115
uart_id: uart_0
116116
talk_pin: ${talk_pin_rs485}
117-
broadcast_to_all_bms: true
118117

119118
jk_rs485_bms:
120119
- id: bms0
@@ -691,8 +690,20 @@ sensor:
691690
cell_overvoltage_protection_release_time:
692691
name: "${bms1_name} cell overvoltage protection release time"
693692

694-
695693
switch:
694+
- platform: template
695+
name: "Broadcast changes to all BMSs"
696+
id: broadcast_active
697+
icon: "mdi:cast"
698+
restore_mode: RESTORE_DEFAULT_ON
699+
optimistic: true
700+
turn_on_action:
701+
- lambda: |-
702+
id(sniffer0).set_broadcast_changes_to_all_bms(true);
703+
turn_off_action:
704+
- lambda: |-
705+
id(sniffer0).set_broadcast_changes_to_all_bms(false);
706+
696707
- platform: jk_rs485_bms
697708
jk_rs485_bms_id: bms0
698709
precharging:
@@ -759,6 +770,7 @@ switch:
759770
name: "${bms1_name} port selection"
760771
special_charger:
761772
name: "${bms1_name} special charger"
773+
762774

763775
text_sensor:
764776
# +--------------------------------------+

esp32-S3-example-jkpb-rs485_0master_7slaves.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ jk_rs485_sniffer:
119119
rx_timeout: 500ms
120120
uart_id: uart_0
121121
talk_pin: ${talk_pin_rs485}
122-
broadcast_to_all_bms: true
123122

124123
jk_rs485_bms:
125124
- id: bms0
@@ -2075,6 +2074,19 @@ sensor:
20752074

20762075

20772076
switch:
2077+
- platform: template
2078+
name: "Broadcast changes to all BMSs"
2079+
id: broadcast_active
2080+
icon: "mdi:cast"
2081+
restore_mode: RESTORE_DEFAULT_ON
2082+
optimistic: true
2083+
turn_on_action:
2084+
- lambda: |-
2085+
id(sniffer0).set_broadcast_to_all_bms(true);
2086+
turn_off_action:
2087+
- lambda: |-
2088+
id(sniffer0).set_broadcast_to_all_bms(false);
2089+
20782090
- platform: jk_rs485_bms
20792091
jk_rs485_bms_id: bms0
20802092
precharging:

0 commit comments

Comments
 (0)