more updates

This commit is contained in:
Daniël Koek 2024-02-02 13:13:55 +00:00
parent 033475bc83
commit 61c25ae822
3 changed files with 89 additions and 32 deletions

View file

@ -3,30 +3,58 @@ import esphome.config_validation as cv
from esphome import pins
from esphome.components import sensor, text_sensor, uart
from esphome.const import *
ebyte_lora_e220_ns = cg.esphome_ns.namespace('ebyte_lora_e220')
EbyteLoraE220 = ebyte_lora_e220_ns.class_('EbyteLoraE220', cg.PollingComponent)
DEPENDENCIES = ['uart']
AUTO_LOAD = ['uart', 'sensor', 'text_sensor']
ebyte_lora_e220_ns = cg.esphome_ns.namespace("ebyte_lora_e220")
EbyteLoraE220 = ebyte_lora_e220_ns.class_(
"EbyteLoraE220", cg.Component, uart.UARTDevice
)
DEPENDENCIES = ["uart"]
AUTO_LOAD = ["uart", "sensor", "text_sensor"]
CONF_PIN_AUX = "pin_aux"
CONF_PIN_M0 = "pin_m0"
CONF_PIN_M1 = "pin_m1"
CONF_LORA_STATUS = "lora_status"
CONF_LORA_MESSAGE = "lora_message"
CONF_LORA_RSSI = "lora_rssi"
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(EbyteLoraE220),
cv.Required(CONF_PIN_AUX): pins.gpio_input_pin_schema,
cv.Required(CONF_PIN_M0): pins.gpio_output_pin_schema,
cv.Required(CONF_PIN_M1): pins.gpio_output_pin_schema,
cv.Optional(CONF_LORA_MESSAGE): text_sensor.text_sensor_schema(entity_category=ENTITY_CATEGORY_NONE,),
cv.Optional(CONF_LORA_STATUS): text_sensor.text_sensor_schema(entity_category=ENTITY_CATEGORY_DIAGNOSTIC,),
cv.Optional(CONF_LORA_RSSI):
sensor.sensor_schema(device_class=DEVICE_CLASS_SIGNAL_STRENGTH,unit_of_measurement=UNIT_DECIBEL_MILLIWATT,accuracy_decimals=0,state_class=STATE_CLASS_MEASUREMENT).extend(),
CONFIG_SCHEMA = (
cv.Schema(
{
# if you send gps locations over lora, this will be able to read it
cv.Optional(CONF_LATITUDE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES,
accuracy_decimals=6,
),
# if you send gps locations over lora, this will be able to read it
cv.Optional(CONF_LONGITUDE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES,
accuracy_decimals=6,
),
cv.GenerateID(): cv.declare_id(EbyteLoraE220),
# for communication to let us know that we can receive data
cv.Required(CONF_PIN_AUX): pins.gpio_input_pin_schema,
# for communication set the mode
cv.Required(CONF_PIN_M0): pins.gpio_output_pin_schema,
# for communication set the mode
cv.Required(CONF_PIN_M1): pins.gpio_output_pin_schema,
# if you want to see the raw messages
cv.Optional(CONF_LORA_MESSAGE): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_NONE,
),
# if you want to see the rssi
cv.Optional(CONF_LORA_RSSI): sensor.sensor_schema(
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
unit_of_measurement=UNIT_DECIBEL_MILLIWATT,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
# check values every 20s
.extend(cv.polling_component_schema("20s")).extend(uart.UART_DEVICE_SCHEMA)
)
}).extend(uart.UART_DEVICE_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
@ -41,10 +69,6 @@ async def to_code(config):
p = await cg.gpio_pin_expression(config[CONF_PIN_M1])
cg.add(var.set_pin_m1(p))
if CONF_LORA_STATUS in config:
sens = await text_sensor.new_text_sensor(config[CONF_LORA_STATUS])
cg.add(var.set_status_sensor(sens))
if CONF_LORA_MESSAGE in config:
sens = await text_sensor.new_text_sensor(config[CONF_LORA_MESSAGE])
cg.add(var.set_message_sensor(sens))
@ -53,4 +77,11 @@ async def to_code(config):
sens = await sensor.new_sensor(config[CONF_LORA_RSSI])
cg.add(var.set_rssi_sensor(sens))
# if you are sending gps data over lora
if CONF_LATITUDE in config:
sens = await sensor.new_sensor(config[CONF_LATITUDE])
cg.add(var.set_latitude_sensor(sens))
if CONF_LONGITUDE in config:
sens = await sensor.new_sensor(config[CONF_LONGITUDE])
cg.add(var.set_longitude_sensor(sens))

View file

@ -2,7 +2,20 @@
namespace esphome {
namespace ebyte_lora_e220 {
void EbyteLoraE220::update() {
if (this->latitude_sensor_ != nullptr)
this->latitude_sensor_->publish_state(this->latitude_);
if (this->longitude_sensor_ != nullptr)
this->longitude_sensor_->publish_state(this->longitude_);
if (this->rssi_sensor != nullptr)
this->rssi_sensor->publish_state(this->rssi_);
// raw info
if (this->message_text_sensor != nullptr)
this->message_text_sensor->publish_state(this->raw_message_);
}
void EbyteLoraE220::setup() {
this->pin_aux->setup();
this->pin_m0->setup();
@ -131,8 +144,15 @@ void EbyteLoraE220::loop() {
}
}
ESP_LOGD(TAG, "%s", buffer);
this->message_text_sensor->publish_state(buffer.substr(0, buffer.length() - 1));
this->rssi_sensor->publish_state(atoi(buffer.substr(buffer.length() - 1, 1).c_str()));
raw_message_ = buffer.substr(0, buffer.length() - 1);
if (raw_message_.find('gps:') != std::string::npos) {
int start = raw_message_.find(',');
// minus gps
latitude_ = atof(raw_message_.substr(4, start).c_str());
latitude_ = atof(raw_message_.substr(start + 1, raw_message_.length()).c_str());
}
// set the rssi
rssi_ = atoi(buffer.substr(buffer.length() - 1, 1).c_str());
}
} // namespace ebyte_lora_e220

View file

@ -26,14 +26,16 @@ enum MODE_TYPE {
MODE_3_SLEEP = 3,
MODE_INIT = 0xFF
};
class EbyteLoraE220 : public Component, public uart::UARTDevice {
class EbyteLoraE220 : public PollingComponent, public uart::UARTDevice {
public:
void setup() override;
void update() override;
void loop() override;
void dump_config() override;
// local
void set_latitude_sensor(sensor::Sensor *latitude_sensor) { latitude_sensor_ = latitude_sensor; }
void set_longitude_sensor(sensor::Sensor *longitude_sensor) { longitude_sensor_ = longitude_sensor; }
void set_message_sensor(text_sensor::TextSensor *s) { message_text_sensor = s; }
void set_status_sensor(text_sensor::TextSensor *s) { status_text_sensor = s; }
void set_rssi_sensor(sensor::Sensor *s) { rssi_sensor = s; }
void set_pin_aux(GPIOPin *s) { pin_aux = s; }
void set_pin_m0(GPIOPin *s) { pin_m0 = s; }
@ -47,13 +49,17 @@ class EbyteLoraE220 : public Component, public uart::UARTDevice {
bool waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100);
protected:
std::vector<uint8_t> buffer_;
text_sensor::TextSensor *message_text_sensor;
text_sensor::TextSensor *status_text_sensor;
sensor::Sensor *rssi_sensor;
GPIOPin *pin_aux;
GPIOPin *pin_m0;
GPIOPin *pin_m1;
int rssi_ = 0;
float latitude_ = -1;
float longitude_ = -1;
std::string raw_message_;
sensor::Sensor *latitude_sensor_{nullptr};
sensor::Sensor *longitude_sensor_{nullptr};
text_sensor::TextSensor *message_text_sensor{nullptr};
sensor::Sensor *rssi_sensor{nullptr};
GPIOPin *pin_aux{nullptr};
GPIOPin *pin_m0{nullptr};
GPIOPin *pin_m1{nullptr};
};
} // namespace ebyte_lora_e220