add CSV serial sensor

This commit is contained in:
Samuel Sieb
2024-01-10 00:11:49 -08:00
parent d9af361322
commit c1d639faef
5 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# A component that reads CSV values from the uart.
A configured uart is required.
Configure a list of sensors. The index is required, the rest is the standard sensor config.
Example:
```yaml
sensor:
- platform: serial_csv
uart_id: my_uart # optional
sensors:
- index: 0
name: First value
- index: 3
name: Fourth value
```

View File

View File

@@ -0,0 +1,38 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import uart
from esphome.components import sensor
from esphome.const import CONF_ID, CONF_INDEX, CONF_SENSORS
CODEOWNERS = ["@ssieb"]
DEPENDENCIES = ['uart']
serial_ns = cg.esphome_ns.namespace('serial')
SerialCSV = serial_ns.class_('SerialCSV', cg.Component, sensor.Sensor, uart.UARTDevice)
CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(SerialCSV),
cv.Required(CONF_SENSORS): cv.ensure_list(
sensor.SENSOR_SCHEMA.extend(
{
cv.Required(CONF_INDEX): cv.positive_int,
}
)
),
}
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
for conf in config[CONF_SENSORS]:
sens = await sensor.new_sensor(conf)
index = conf[CONF_INDEX]
cg.add(var.add_sensor(index, sens))

View File

@@ -0,0 +1,50 @@
#include "serial_csv.h"
#include "esphome/core/log.h"
namespace esphome {
namespace serial {
static const char *TAG = "serial.csv";
void SerialCSV::loop() {
while (this->available()) {
uint8_t c;
this->read_byte(&c);
if (c == '\r')
continue;
if (c == '\n')
this->parse_values_();
else
this->rx_message_.push_back(c);
}
}
void SerialCSV::parse_values_() {
std::string s(this->rx_message_.begin(), this->rx_message_.end());
int spos = 0;
int epos = 0;
std::vector<float> values;
while (epos != std::string::npos) {
epos = s.find(',', spos);
int len = (epos == std::string::npos ? s.size() - spos : epos - spos);
values.push_back(parse_number<float>(s.substr(spos, len)).value_or(NAN));
if (epos != std::string::npos)
spos = epos + 1;
}
this->rx_message_.clear();
for (auto sens : this->sensors_) {
if (sens.first < values.size())
sens.second->publish_state(values[sens.first]);
}
}
void SerialCSV::dump_config() {
ESP_LOGCONFIG("", "Serial CSV Reader");
for (auto sens : this->sensors_) {
ESP_LOGCONFIG(TAG, "Index %d", sens.first);
LOG_SENSOR(TAG, "", sens.second);
}
}
} // namespace serial
} // namespace esphome

View File

@@ -0,0 +1,27 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace serial {
class SerialCSV : public Component, public uart::UARTDevice {
public:
float get_setup_priority() const override { return setup_priority::DATA; }
void loop() override;
void dump_config() override;
void add_sensor(int index, sensor::Sensor *sens) {
this->sensors_.push_back(std::make_pair(index, sens));
}
protected:
void parse_values_();
std::vector<uint8_t> rx_message_;
std::vector<std::pair<int, sensor::Sensor *>> sensors_;
};
} // namespace serial
} // namespace esphome