Hi, everybody,
I’m new to this community.
I’d like to get a string from the UART and divide it up in several values.
basically it works. Communication is fine, the raw string is shown up correctly.
Unfortunately the string contains spaces and comma separated float values. Conversion stops at first space. To handle this, my idea was to do something with
std::replace,
but compiler says no.
How can I do this in ESPhome?
Format of the string is different like:
11:22:33 12,3 234,5 1:3 2:4\r\n
11:22:34 1,5 -24,5 1:254 2:12\r\n
sensor:
- platform: template
name: "TimeH"
id: "hour"
- platform: template
name: "TimeM"
id: "minute"
- platform: template
name: "TimeS"
id: "second"
- platform: template
name: "Temp 1"
id: "temp1"
- platform: template
name: "Temp 2"
id: "temp2"
- platform: template
name: "Binary Data"
id: binData
uart:
baud_rate: 9600
tx_pin: 1
rx_pin: 3
debug:
direction: RX
dummy_receiver: true
after:
delimiter: "\r\n"
sequence:
- lambda: |-
UARTDebug::log_string(direction, bytes); // log the data
int iTime,iHour,iMinute,iSecond,idata1,idata2=0;
float fTemp1,fTemp2,=0;
// convert uart text to string
std::string str(bytes.begin(), bytes.end());
// get raw string
id(rawString).publish_state(str.c_str()); // data comes in correct
//std::cout<<strString<<std::endl; // this do not work
//std::replace (str.begin(), str.end(), ",", ";"); // this too
id(convString).publish_state(str.c_str());
// teststring to values
if (sscanf(str.c_str(), "%d:%d:%d %f %f 1:%d 2:%d", &iHour, &iMinute, &iSecond, &fTemp1, &fTemp2, &idata1, &idata2) == 7 ) { // seems to stop at a space?!
id(hour).publish_state(iHour);
id(minute).publish_state(iMinute);
id(second).publish_state(iSecond);
id(temp1).publish_state(fTemp1);
id(temp2).publish_state(fTemp2);
// ...
}
text_sensor:
- platform: template
name: "Raw String"
id: "rawString"
- platform: template
name: "converted String"
id: "convString"