diff --git a/esphome/components/ebyte_lora/__init__.py b/esphome/components/ebyte_lora/__init__.py index 6f9615468d..ff71344a63 100644 --- a/esphome/components/ebyte_lora/__init__.py +++ b/esphome/components/ebyte_lora/__init__.py @@ -101,7 +101,9 @@ CONF_ENABLE_LBT = "enable_lbt" CONF_RSSI_NOISE = "rssi_noise" CONF_UART_PARITY = "uart_parity" CONF_SUB_PACKET = "sub_packet" -CONF_SWITCH_INFO_RECEIVER = "switch_info_receiver" +CONF_SENT_SWITCH_STATE = "sent_switch_state" +CONF_REPEATER = "repeater" +CONF_NETWORK_ID = "network_id" CONFIG_SCHEMA = ( cv.Schema( { @@ -112,7 +114,10 @@ CONFIG_SCHEMA = ( cv.Required(CONF_PIN_M0): pins.internal_gpio_output_pin_schema, # for communication set the mode cv.Required(CONF_PIN_M1): pins.internal_gpio_output_pin_schema, - cv.Optional(CONF_SWITCH_INFO_RECEIVER, default=False): cv.boolean, + # to be able to repeater hop + cv.Required(CONF_NETWORK_ID): cv.int_range(min=0, max=255), + cv.Optional(CONF_SENT_SWITCH_STATE, default=True): cv.boolean, + cv.Optional(CONF_REPEATER, default=False): cv.boolean, # if you want to see the rssi cv.Optional(CONF_LORA_RSSI): sensor.sensor_schema( device_class=DEVICE_CLASS_SIGNAL_STRENGTH, @@ -175,7 +180,9 @@ async def to_code(config): cg.add(var.set_pin_m0(pin_m0)) pin_m1 = await cg.gpio_pin_expression(config[CONF_PIN_M1]) cg.add(var.set_pin_m1(pin_m1)) - cg.add(var.set_switch_info_receiver(config[CONF_SWITCH_INFO_RECEIVER])) + cg.add(var.set_sent_switch_state(config[CONF_SENT_SWITCH_STATE])) + cg.add(var.set_repeater(config[CONF_REPEATER])) + cg.add(var.set_network_id(config[CONF_NETWORK_ID])) cg.add(var.set_addh(config[CONF_ADDH])) cg.add(var.set_addl(config[CONF_ADDL])) cg.add(var.set_air_data_rate(config[CONF_AIR_DATA_RATE])) diff --git a/esphome/components/ebyte_lora/ebyte_lora_component.cpp b/esphome/components/ebyte_lora/ebyte_lora_component.cpp index dd5049faca..c63b68b3c4 100644 --- a/esphome/components/ebyte_lora/ebyte_lora_component.cpp +++ b/esphome/components/ebyte_lora/ebyte_lora_component.cpp @@ -1,8 +1,10 @@ #include "ebyte_lora_component.h" namespace esphome { namespace ebyte_lora { -static const uint8_t SWITCH_PUSH = 0x55; static const uint8_t SWITCH_INFO = 0x66; +// when this is called it is asking peers to say something about repeater +static const uint8_t REQUEST_REPEATER_INFO = 0x88; +static const uint8_t REPEATER_INFO = 0x99; static const uint8_t PROGRAM_CONF = 0xC1; bool EbyteLoraComponent::check_config_() { bool success = true; @@ -222,10 +224,12 @@ void EbyteLoraComponent::update() { ESP_LOGD(TAG, "Mode is not set right"); this->set_mode_(NORMAL); } -#ifdef USE_SWITCH - if (!this->switch_info_receiver_) + + if (!this->sent_switch_state) this->send_switch_info_(); -#endif + // we always request repeater info, since nodes will response too that they are around + // you can see it more of a health info + this->request_repeater_info(); } void EbyteLoraComponent::set_config_() { uint8_t data[11]; @@ -379,24 +383,7 @@ void EbyteLoraComponent::dump_config() { LOG_PIN("M0 Pin:", this->pin_m0_); LOG_PIN("M1 Pin:", this->pin_m1_); }; -#ifdef USE_SWITCH -void EbyteLoraComponent::digital_write(uint8_t pin, bool value) { this->send_switch_push_(pin, value); } -void EbyteLoraComponent::send_switch_push_(uint8_t pin, bool value) { - if (!this->can_send_message_()) { - return; - } - uint8_t data[3]; - data[0] = SWITCH_PUSH; // number one to indicate - data[1] = pin; // Pin to send - data[2] = value; // Inverted for the pcf8574 - ESP_LOGD(TAG, "Sending message to remote lora"); - ESP_LOGD(TAG, "PIN: %u ", data[1]); - ESP_LOGD(TAG, "VALUE: %u ", data[2]); - this->write_array(data, sizeof(data)); - this->setup_wait_response_(5000); - ESP_LOGD(TAG, "Successfully put in queue"); -} -#endif + void EbyteLoraComponent::loop() { std::string buffer; std::vector data; @@ -408,32 +395,27 @@ void EbyteLoraComponent::loop() { this->read_byte(&c); data.push_back(c); } -#ifdef USE_SWITCH - // if it is only push info - if (data[0] == SWITCH_PUSH) { - ESP_LOGD(TAG, "GOT SWITCH PUSH"); - ESP_LOGD(TAG, "Total: %u", data.size()); - ESP_LOGD(TAG, "Start bit: %u", data[0]); - ESP_LOGD(TAG, "PIN: %u", data[1]); - ESP_LOGD(TAG, "VALUE: %u", data[2]); - ESP_LOGD(TAG, "RSSI: %f", (data[3] / 255.0) * 100); - if (this->rssi_sensor_ != nullptr) - this->rssi_sensor_->publish_state((data[3] / 255.0) * 100); - for (auto *sensor : this->sensors_) { - if (sensor->get_pin() == data[1]) { - ESP_LOGD(TAG, "Updating switch"); - sensor->publish_state(data[2]); - } + if (data[0] == REQUEST_REPEATER_INFO) { + this->send_repeater_info(); + } + if (data[0] == REPEATER_INFO) { + ESP_LOGD(TAG, "Got repeater info "); + for (int i = 0; i < data.size(); i++) { + ESP_LOGD(TAG, "%u", data[i]); } } // starting info loop if (data[0] == SWITCH_INFO) { - for (int i = 0; i < data.size(); i++) { - if (data[i] == SWITCH_INFO) { - ESP_LOGD(TAG, "GOT INFO"); - uint8_t pin = data[i + 1]; - bool value = data[i + 2]; + if (this->repeater_) { + this->repeat_message(data); + } + // it is it's own info + if (network_id != data[1]) { + ESP_LOGD(TAG, "Got switch info"); + for (int i = 2; i < data.size(); i = i + 2) { + uint8_t pin = data[i]; + bool value = data[i + 1]; for (auto *sensor : this->sensors_) { if (pin == sensor->get_pin()) { sensor->publish_state(value); @@ -444,7 +426,7 @@ void EbyteLoraComponent::loop() { this->rssi_sensor_->publish_state((data[data.size() - 1] / 255.0) * 100); ESP_LOGD(TAG, "RSSI: %f", (data[data.size() - 1] / 255.0) * 100); } -#endif + if (data[0] == PROGRAM_CONF) { ESP_LOGD(TAG, "GOT PROGRAM_CONF"); this->setup_conf_(data); @@ -491,24 +473,53 @@ void EbyteLoraComponent::setup_conf_(std::vector data) { } } } -#ifdef USE_SWITCH + void EbyteLoraComponent::send_switch_info_() { if (!this->can_send_message_()) { return; } std::vector data; - + data.push_back(SWITCH_INFO); + data.push_back(network_id); for (auto *sensor : this->sensors_) { - uint8_t pin = sensor->get_pin(); - uint8_t value = sensor->state; - data.push_back(SWITCH_INFO); // number one to indicate - data.push_back(pin); - data.push_back(value); // Pin to send + data.push_back(sensor->get_pin()); + data.push_back(sensor->state); } ESP_LOGD(TAG, "Sending switch info"); this->write_array(data); this->setup_wait_response_(5000); } -#endif + +void EbyteLoraComponent::send_repeater_info() { + if (!this->can_send_message_()) { + return; + } + uint8_t data[3]; + data[0] = REPEATER_INFO; // response + data[1] = this->repeater_; + data[2] = network_id; + ESP_LOGD(TAG, "Telling system if i am a repeater and what my network_id is"); + this->write_array(data, sizeof(data)); + this->setup_wait_response_(5000); +} +void EbyteLoraComponent::request_repeater_info() { + if (!this->can_send_message_()) { + return; + } + uint8_t data[1]; + data[0] = REQUEST_REPEATER_INFO; // Request + ESP_LOGD(TAG, "Asking for repeater info"); + this->write_array(data, sizeof(data)); + this->setup_wait_response_(5000); +} +void EbyteLoraComponent::repeat_message(std::vector data) { + ESP_LOGD(TAG, "Got some info that i need to repeat for network %u", data[1]); + if (!this->can_send_message_()) { + return; + } + this->write_array(data.data(), sizeof(data)); + this->setup_wait_response_(5000); +} + } // namespace ebyte_lora } // namespace esphome diff --git a/esphome/components/ebyte_lora/ebyte_lora_component.h b/esphome/components/ebyte_lora/ebyte_lora_component.h index 690f629e13..d9789a91ce 100644 --- a/esphome/components/ebyte_lora/ebyte_lora_component.h +++ b/esphome/components/ebyte_lora/ebyte_lora_component.h @@ -31,13 +31,10 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { void loop() override; void dump_config() override; + void send_switch_info_(); void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; } void set_pin_aux(InternalGPIOPin *pin_aux) { pin_aux_ = pin_aux; } -#ifdef USE_SWITCH void set_switch(EbyteLoraSwitch *obj) { this->sensors_.push_back(obj); } - /// Helper function to write the value of a pin. - void digital_write(uint8_t pin, bool value); -#endif void set_pin_m0(InternalGPIOPin *pin_m0) { pin_m0_ = pin_m0; } void set_pin_m1(InternalGPIOPin *pin_m1) { pin_m1_ = pin_m1; } void set_addh(uint8_t addh) { expected_config_.addh = addh; } @@ -53,12 +50,12 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { void set_enable_lbt(EnableByte enable) { expected_config_.enable_lbt = enable; } void set_transmission_mode(TransmissionMode mode) { expected_config_.transmission_mode = mode; } void set_enable_rssi(EnableByte enable) { expected_config_.enable_rssi = enable; } - void set_switch_info_receiver(bool enable) { switch_info_receiver_ = enable; } + void set_sent_switch_state(bool enable) { sent_switch_state = enable; } + void set_repeater(bool enable) { repeater_ = enable; } + void set_network_id(int id) { network_id = id; } private: -#ifdef USE_SWITCH std::vector sensors_; -#endif ModeType mode_ = MODE_INIT; // set WOR mode void set_mode_(ModeType mode); @@ -69,15 +66,19 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { bool check_config_(); void set_config_(); void get_current_config_(); -#ifdef USE_SWITCH - void send_switch_push_(uint8_t pin, bool value); - void send_switch_info_(); -#endif void setup_conf_(std::vector data); + void request_repeater_info(); + void send_repeater_info(); + void repeat_message(std::vector data); protected: bool update_needed_ = false; - bool switch_info_receiver_ = false; + // if enabled will sent information about itself + bool sent_switch_state = false; + // if set it will function as a repeater + bool repeater_ = false; + // used to tell one lora device apart from another + int network_id = 0; int rssi_ = 0; uint32_t starting_to_check_; uint32_t time_out_after_; @@ -96,7 +97,7 @@ class EbyteLoraSwitch : public switch_::Switch, public Parentedparent_->digital_write(this->pin_, state); } + void write_state(bool state) override { this->parent_->send_switch_info_(); } uint8_t pin_; }; #endif