@PITiMAT
wrzucam to co przetestowałem z podanym przez Ciebie input’em:
1.0;2.0;3.0;4.0;5.0;6.0;7.0;8.0;9.0;10.0;11.0;12.0;13.0;14.0;15.0;16.0;17.0;18.0;19.0;20.0;21.0;22.0
yaml:
substitutions:
hostname: "uart"
name: "UART"
esphome:
name: $hostname
platform: ESP8266
board: d1_mini
includes:
- uart_read_line_sensor.h
wifi:
#use_address: 192.168.4.40
ssid: !secret wifi_IoT_Devices_SSID
password: !secret wifi_IoT_Devices_password
manual_ip:
static_ip: 192.168.4.40
gateway: !secret IoT_Devices_gateway
subnet: !secret subnet
captive_portal:
logger:
level: DEBUG
baud_rate: 0
ota:
password: !secret ota_password
api:
web_server:
port: 80
uart:
id: uart_bus
baud_rate: 9600
rx_pin: 3
sensor:
- platform: custom
lambda: |-
auto my_sensor = new UartReadLineSensor(id(uart_bus));
App.register_component(my_sensor);
return {
my_sensor->sensor_0,
my_sensor->sensor_1,
my_sensor->sensor_2,
my_sensor->sensor_3,
my_sensor->sensor_4,
my_sensor->sensor_5,
my_sensor->sensor_6,
my_sensor->sensor_7,
my_sensor->sensor_8,
my_sensor->sensor_9,
my_sensor->sensor_10,
my_sensor->sensor_11,
my_sensor->sensor_12,
my_sensor->sensor_13,
my_sensor->sensor_14,
my_sensor->sensor_15,
my_sensor->sensor_16,
my_sensor->sensor_17,
my_sensor->sensor_18,
my_sensor->sensor_19,
my_sensor->sensor_20,
my_sensor->sensor_21
};
sensors:
- name: "My Custom0 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom1 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom2 Temperature Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom3 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom4 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom5 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom6 Temperature Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom7 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom8 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom9 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom10 Temperature Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom11 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom12 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom13 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom14 Temperature Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom15 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom16 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom17 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom18 Temperature Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom19 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- name: "My Custom20 Sensor"
unit_of_measurement: °C
accuracy_decimals: 1
- name: "My Custom21 Pressure Sensor"
unit_of_measurement: hPa
accuracy_decimals: 2
- pliczek uart_read_line_sensor.h:
#include "esphome.h"
#include <string>
#include <vector>
#include <sstream>
class UartReadLineSensor: public Component, public UARTDevice {
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
Sensor *sensor_0 = new Sensor();
Sensor *sensor_1 = new Sensor();
Sensor *sensor_2 = new Sensor();
Sensor *sensor_3 = new Sensor();
Sensor *sensor_4 = new Sensor();
Sensor *sensor_5 = new Sensor();
Sensor *sensor_6 = new Sensor();
Sensor *sensor_7 = new Sensor();
Sensor *sensor_8 = new Sensor();
Sensor *sensor_9 = new Sensor();
Sensor *sensor_10 = new Sensor();
Sensor *sensor_11 = new Sensor();
Sensor *sensor_12 = new Sensor();
Sensor *sensor_13 = new Sensor();
Sensor *sensor_14 = new Sensor();
Sensor *sensor_15 = new Sensor();
Sensor *sensor_16 = new Sensor();
Sensor *sensor_17 = new Sensor();
Sensor *sensor_18 = new Sensor();
Sensor *sensor_19 = new Sensor();
Sensor *sensor_20 = new Sensor();
Sensor *sensor_21 = new Sensor();
void setup() override {
// nothing to do here
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int npos;
if (readch > 0) {
switch (readch) {
case '\n': // return on new line
npos = pos;
pos = 0; // Reset position index ready for next time
return npos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void loop() override {
const int max_line_length = 120;
static char buffer[max_line_length];
while (available()) {
if(readline(read(), buffer, max_line_length) > 0) {
ESP_LOGD("loop", "Got new msg");
std::stringstream ss(buffer);
std::string segment;
std::vector<std::string> seglist;
while(std::getline(ss, segment, ';'))
{
seglist.push_back(segment);
}
ESP_LOGD("loop", "The numer of got sensors is: %d", seglist.size());
ESP_LOGD("loop", "Publishing states");
sensor_0->publish_state(std::stof(seglist[0]));
sensor_1->publish_state(std::stof(seglist[1]));
sensor_2->publish_state(std::stof(seglist[2]));
sensor_3->publish_state(std::stof(seglist[3]));
sensor_4->publish_state(std::stof(seglist[4]));
sensor_5->publish_state(std::stof(seglist[5]));
sensor_6->publish_state(std::stof(seglist[6]));
sensor_7->publish_state(std::stof(seglist[7]));
sensor_8->publish_state(std::stof(seglist[8]));
sensor_9->publish_state(std::stof(seglist[9]));
sensor_10->publish_state(std::stof(seglist[10]));
sensor_11->publish_state(std::stof(seglist[11]));
sensor_12->publish_state(std::stof(seglist[12]));
sensor_13->publish_state(std::stof(seglist[13]));
sensor_14->publish_state(std::stof(seglist[14]));
sensor_15->publish_state(std::stof(seglist[15]));
sensor_16->publish_state(std::stof(seglist[16]));
sensor_17->publish_state(std::stof(seglist[17]));
sensor_18->publish_state(std::stof(seglist[18]));
sensor_19->publish_state(std::stof(seglist[19]));
sensor_20->publish_state(std::stof(seglist[20]));
sensor_21->publish_state(std::stof(seglist[21]));
}
}
}
};
Efekt:
DeepSleep w ESP8266 jest rozwiązany tylko czasowo(nie GPIO), więc tego nie dodaję, ale jeśli wybierzesz docelowe rozwiązanie to mogę pomóc z yaml’em do tego.