@nickrout You’re right, my bad. I’m a little confused about what I’m trying to do here. For now, I managed to transfer the previous code from arduino and import it into esphome. The code looks like this:
yaml:
esphome:
name: co-overdrive
includes:
- piec-uart.h
esp8266:
board: esp01_1m
# Enable logging
logger:
level: VERBOSE #makes uart stream available in esphome logstream
baud_rate: 0 #disable logging over uart
# Enable Home Assistant API
api:
encryption:
key: "xxx"
ota:
password: "xxx"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "xxx"
password: "xxx"
captive_portal:
uart:
id: uart_bus
tx_pin: 4
rx_pin: 5
baud_rate: 9600
debug:
direction: RX
dummy_receiver: false
after:
delimiter: "\n"
sensor:
- platform: custom
lambda: |-
auto heating_control = new coSensor(id(uart_bus));
App.register_component(heating_control);
return {
heating_control -> COtemp_sensor
};
sensors:
name: "COtemp Sensor"
- platform: custom
lambda: |-
auto heating_control = new coSensor(id(uart_bus));
App.register_component(heating_control);
return {
heating_control -> CWUtemp_sensor
};
sensors:
name: "CWUtemp Sensor"
- platform: custom
lambda: |-
auto heating_control = new coSensor(id(uart_bus));
App.register_component(heating_control);
return {
heating_control -> PODtemp_sensor
};
sensors:
name: "PODtemp Sensor"
- platform: custom
lambda: |-
auto heating_control = new coSensor(id(uart_bus));
App.register_component(heating_control);
return {
heating_control -> gotStr_sensor
};
sensors:
name: "CO RS transmission"
custom component piec-uart.h:
#include "esphome.h"
class coSensor : public PollingComponent, public UARTDevice, public Sensor {
public:
// constructor - where we “initialize” the custom sensor
coSensor(UARTComponent *parent) : UARTDevice(parent), PollingComponent(5000){}
Sensor *COtemp_sensor = new Sensor();
Sensor *CWUtemp_sensor = new Sensor();
Sensor *PODtemp_sensor = new Sensor();
//Sensor *gotStr_sensor = new Sensor();
//Sensor *PODstate_sensor = new Sensor();
//Sensor *PUMPSmode_sensor = new Sensor();
//Sensor *coPUMPstate_sensor = new Sensor();
//Sensor *cwuPUMPstate_sensor = new Sensor();
//Sensor *WORKmode_sensor = new Sensor();
void setup() override {
pinMode (5, INPUT_PULLUP);
// This will be called by App.setup()
}
String readSerial(){
int inChar;
String inStr = "";
String finalStr = "";
char buff[2];
long startTime = millis();
if (available()) {
while (millis() - startTime < 300) {
inChar = -1;
inChar = read();
if (inChar > -1) {
sprintf(buff,"%02X",inChar);
inStr = inStr + buff;
}
}
if(inStr.length() > 190){
finalStr = inStr;
}
}
return finalStr;
}
void update() override {
// This will be called every "update_interval" milliseconds.
float COtemp;
float CWUtemp;
float PODtemp;
float PODstate;
float PUMPSmode;
//float gotStr;
String line = readSerial();
if(line.length() > 200){
COtemp = line.substring(38,41).toFloat();
CWUtemp = line.substring(54,57).toFloat();
PODtemp = line.substring(142,145).toFloat();
//PODstate = line.substring(21,28).toFloat();
//PUMPSmode = line.substring(29,34).toFloat();
COtemp_sensor -> publish_state(COtemp / 10);
CWUtemp_sensor -> publish_state(CWUtemp / 10);
PODtemp_sensor -> publish_state(PODtemp / 10);
//PODstate_sensor -> publish_state(PODstate);
//PUMPSmode_sensor -> publish_state(PUMPSmode);
}
else{
//Does nothing
}
}
};
some logs from the esp:
[18:42:42][V][sensor:076]: 'COtemp Sensor': Received new state 6.100000
[18:42:42][D][sensor:126]: 'COtemp Sensor': Sending state 6.10000 with 0 decimals of accuracy
[18:42:42][V][sensor:076]: '': Received new state 28.100000
[18:42:42][D][sensor:126]: '': Sending state 28.10000 with 0 decimals of accuracy
[18:42:42][V][sensor:076]: '': Received new state 0.100000
[18:42:42][D][sensor:126]: '': Sending state 0.10000 with 0 decimals of accuracy
[18:42:42][V][component:200]: Component <unknown> took a long time for an operation (0.35 s).
[18:42:42][V][component:201]: Components should block for at most 20-30ms.
[18:42:42][D][uart_debug:114]: <<< 00:06:02:18:E1:85
[18:42:47][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:47][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:47][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:47][V][sensor:076]: 'COtemp Sensor': Received new state 6.100000
[18:42:47][D][sensor:126]: 'COtemp Sensor': Sending state 6.10000 with 0 decimals of accuracy
[18:42:47][V][sensor:076]: '': Received new state 28.100000
[18:42:47][D][sensor:126]: '': Sending state 28.10000 with 0 decimals of accuracy
[18:42:47][V][sensor:076]: '': Received new state 0.100000
[18:42:47][D][sensor:126]: '': Sending state 0.10000 with 0 decimals of accuracy
[18:42:47][V][component:200]: Component <unknown> took a long time for an operation (0.33 s).
[18:42:47][V][component:201]: Components should block for at most 20-30ms.
[18:42:47][D][uart_debug:114]: <<< 02:26:FF:FA:16:EF:00:01:16:C2:00:00:15:A7:00:09:16:FF:00:06:16:9E:50:32:16:9F:4B:28:15:7F:00:00:15:7D:02:BC:15:7E:00:3C:16:6E:02:BC:16:16:00:3C:16:81:FC:18:15:7C:00:50:15:CD:00:03:16:20:09:14:16:21:00:01:15:89:00:00:15:8B:00:00:01:F6:00:00:02:FC:00:00:02:8E:00:00:01:F9:00:00:02:98:00:00:02:99:00:00:02:45:00:00:03:12:00:00:03:11:00:00:16:81:F8:30:02:18:C5:75
[18:42:48][V][component:200]: Component <unknown> took a long time for an operation (0.30 s).
[18:42:48][V][component:201]: Components should block for at most 20-30ms.
[18:42:48][D][uart_debug:114]: <<< 02:26:FF:F4:16:9E:50:32:16:9F:4B:28:15:7C:00:50:15:7D:02:BC:15:7E:00:3C:16:6E:02:BC:16:16:00:3C:16:81:F8:30:15:CD:00:03:16:20:09:14:16:21:00:01:15:89:00:00:15:8B:00:00:15:87:00:00:15:88:00:00:15:9B:00:00:16:F8:01:86:16
[18:42:52][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:52][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:52][V][sensor:076]: 'COtemp Sensor': Received new state 2.400000
[18:42:52][D][sensor:126]: 'COtemp Sensor': Sending state 2.40000 with 0 decimals of accuracy
[18:42:52][V][sensor:076]: '': Received new state 2.200000
[18:42:52][D][sensor:126]: '': Sending state 2.20000 with 0 decimals of accuracy
[18:42:52][V][sensor:076]: '': Received new state 16.100000
[18:42:52][D][sensor:126]: '': Sending state 16.10000 with 0 decimals of accuracy
[18:42:52][V][component:200]: Component <unknown> took a long time for an operation (0.31 s).
[18:42:52][V][component:201]: Components should block for at most 20-30ms.
[18:42:52][D][uart_debug:114]: <<< 84:00:00:01:F6:00:00:02:8E:00:00:02:98:00:00:02:99:00:00:02:45:00:00:02:18:67:58:02:26:FF:FA:16:EF:00:02:16:C2:00:00:15:A7:00:09:16:FF:00:06:16:9E:50:32:16:9F:4B:28:15:7F:00:00:15:7D:02:BC:15:7E:00:3C:16:6E:02:BC:16:16:00:3C:16:81:FC:18:15:7C:00:50:15:CD:00:03:16:20:09:14:16:21:00:01:15:89:00:00:15:8B:00:00:01:F6:00:00:02:FC:00:00:02:8E:00:00:01:F9:00:00:02:98:00:00:02:99:00:00:02:45:00:00:03:12:00:00:03:11:00:00:16:81:F8:30:02:18:A1:C3
[18:42:57][E][uart:015]: Reading from UART timed out at byte 0!
[18:42:57][V][component:200]: Component <unknown> took a long time for an operation (0.30 s).
[18:42:57][V][component:201]: Components should block for at most 20-30ms.
[18:42:57][V][component:200]: Component <unknown> took a long time for an operation (0.30 s).
[18:42:57][V][component:201]: Components should block for at most 20-30ms.
[18:42:58][V][component:200]: Component <unknown> took a long time for an operation (0.31 s).
[18:42:58][V][component:201]: Components should block for at most 20-30ms.
And at this point I’m stuck. The data returned by the sensors is for now to be ignored (because the transmission is not read correctly and the returned values are complete nonsense). The uart_debug logs displayed in the esphome logs, I believe, are not returning values received by my imported code. Is there a way to output the contents of this string in esphome logs?
The entire correct HEX frame I was before able to receive (using the arduino code posted in the original post, without esphome’s involvement, just reading the logs from esp via serial) looks more or less like this:
0226FFFA16EF000116C2000015A7000916FF0006169E5032169F4B28157F0000157D02C6157E003C166E02C61616003C1681FC18157C005015CD0003162003051621000015890000158B000001F6000002FC0000028E000001F9000002980000029900000245000003120000031100001681F83002181214
Which can be then decoded as in the screenshot below (sorry for polish language):
I know that the correctness of this transmission can be verified using crc-16, but I can’t quite even implement that in the code in esp. I would also like to collect only those frames that have a fixed length and the order of the data that they contain (so that they can then be decoded from HEX to DEC and returned as values for the sensors in HomeAssistant.
To summarize: what I am trying to achieve here is reading the raw transmission from the UART, checking the correctness of the read data (crc, length of the collected string, order of the data in it) and then sending specific parameters as sensor values to the HA.
PS.
Frankly, I wonder if it wouldn’t be easier for me to build my own (for example, based on a combination of an ATmel and esp8266/32) controller to handle central heating and simply replace the current one (also based on some ATMega`s μC) with it.