diff --git a/esphome/components/lora/lora.cpp b/esphome/components/lora/lora.cpp index 26eb33f491..90e9cd989e 100644 --- a/esphome/components/lora/lora.cpp +++ b/esphome/components/lora/lora.cpp @@ -125,8 +125,18 @@ void Lora::dump_config() { LOG_PIN("M1 Pin:", this->pin_m1); }; -bool Lora::SendMessage(){ -MAX_SIZE_TX_PACKET +bool Lora::sendMessage(std::string message) { + uint32 size = message.length(); + char messageFixed[size]; + memcpy(messageFixed, message.c_str(), size); + if (size > MAX_SIZE_TX_PACKET + 2) { + ESP_LOGD(TAG, "Package to big"); + return false; + } + ESP_LOGD(TAG, "Sending: %s", message); + this->write_array((uint8_t *) &messageFixed, size); + bool result = this->waitCompleteResponse(5000, 5000); + return result; } void Lora::loop() { if (!available()) { @@ -154,8 +164,8 @@ void Lora::loop() { latitude_ = atof(raw_message_.substr(4, start).c_str()); latitude_ = atof(raw_message_.substr(start + 1, raw_message_.length()).c_str()); ESP_LOGD(TAG, "Location:"); - ESP_LOGD(TAG, " Lat: %f", this->latitude_); - ESP_LOGD(TAG, " Lon: %f", this->longitude_); + ESP_LOGD(TAG, "Lat: %f", this->latitude_); + ESP_LOGD(TAG, "Lon: %f", this->longitude_); } } diff --git a/esphome/components/lora/lora.h b/esphome/components/lora/lora.h index 8c7c7d4d76..60fa6d40a7 100644 --- a/esphome/components/lora/lora.h +++ b/esphome/components/lora/lora.h @@ -47,6 +47,7 @@ class Lora : public PollingComponent, public uart::UARTDevice { bool setMode(MODE_TYPE type); // checks the aux port to see if it is done setting bool waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100); + bool sendMessage(std::string message); protected: int rssi_ = 0; diff --git a/esphome/components/lora/switch/__init__.py b/esphome/components/lora/switch/__init__.py index 8853a61ae3..8b48f233aa 100644 --- a/esphome/components/lora/switch/__init__.py +++ b/esphome/components/lora/switch/__init__.py @@ -3,17 +3,30 @@ import esphome.config_validation as cv from esphome.components import switch, uart from esphome.const import CONF_DATA, CONF_SEND_EVERY from esphome.core import HexInt -from .. import uart_ns, validate_raw_data +from .. import lora_ns + + +def validate_raw_data(value): + if isinstance(value, str): + return value.encode("utf-8") + if isinstance(value, str): + return value + if isinstance(value, list): + return cv.Schema([cv.hex_uint8_t])(value) + raise cv.Invalid( + "data must either be a string wrapped in quotes or a list of bytes" + ) + DEPENDENCIES = ["uart"] -UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component) +LoraSwitch = lora_ns.class_("LoraSwitch", switch.Switch, uart.UARTDevice, cg.Component) CONF_TURN_OFF = "turn_off" CONF_TURN_ON = "turn_on" CONFIG_SCHEMA = ( - switch.switch_schema(UARTSwitch, block_inverted=True) + switch.switch_schema(LoraSwitch, block_inverted=True) .extend( { cv.Required(CONF_DATA): cv.Any(