This commit is contained in:
Daniël Koek 2024-02-20 15:25:42 +00:00
parent 8ded4b02f7
commit aca94e5cc1
3 changed files with 31 additions and 7 deletions

View file

@ -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_);
}
}

View file

@ -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;

View file

@ -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(