I need some insights about how to parse a serial string, from an arduino connected to an Esp8266 in ESPHome.
The text string has the following format: <00:11:22:33>, being the digits the position of 4 diferent cover blinds.
I need to isolate the digits and convert them in the current positions of the blinds in Home Assistant.
I’ve managed to send commands from the ESP to the Arduino, but i have no luck updating the ESP from the arduino. I have some code that i could post here, but i was hoping to have a fresh start from your inputs.
The chevrons are added by the arduino code as start and end markers.
The meaning of the digits is not has i’ve stated above (i had forgoted) The digits mean: < blind number | Blind position | temperature | light level >
The code below is a mess but… i’m not a coder.
#include "esphome.h"
class UartReadLineSensor : public Component, public UARTDevice, public TextSensor {
public:
UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}
void setup() override {
// nothing to do here
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
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 {
char startMarker = '<';
char endMarker = '>';
char * stepper ;
char * position ;
char * temp ;
char * lum ;
const int max_line_length = 80;
static char buffer[max_line_length];
while (available()) {
if(readline(read(), buffer, max_line_length) > 0) {
char * StrTokIndx; // this is used by strtok() as an index
StrTokIndx = strtok(buffer-1,"|"); // get the first control word
//strcpy(MessageFromPC, StrTokIndx); // copy it to messageFromPC
stepper = StrTokIndx;
StrTokIndx = strtok(NULL, "|"); // this continues after 1st ',' in the previous call
position = StrTokIndx; // convert this part to the first integer
StrTokIndx = strtok(NULL, "|"); // this continues after 2nd ',' in the previous call
temp = StrTokIndx; // convert this part to the first integer
StrTokIndx = strtok(NULL, ">");
lum = StrTokIndx; // last integer
publish_state(stepper);
publish_state(position);
publish_state(temp);
publish_state(lum);
}
}
}
};
It is based on the Custom UART Text Sensor example from esphome.
It kinda works but it slows down the ESP and makes it loose the connection.
I was hoping for a simpler solution, (a fresh start) via the uart bus
But i dont have a clue how to parse the data.