Skip to content

Commit

Permalink
Fix message parsing #1
Browse files Browse the repository at this point in the history
- Only proceed if first byte is 1
(previous code would proceed nevertheless
in case of EZO_CALIBRATION commands)

- Truncate returned value only for EZO_READ commands
(previous code would truncate it always, making parsing impossible)
  • Loading branch information
alfredopironti committed May 8, 2023
1 parent f2dfba2 commit cd5dae3
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions esphome/components/ezo/ezo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,16 @@ void EZOSensor::loop() {

ESP_LOGV(TAG, "Received buffer \"%s\" for command type %s", &buf[1], EZO_COMMAND_TYPE_STRINGS[to_run->command_type]);

if ((buf[0] == 1) || (to_run->command_type == EzoCommandType::EZO_CALIBRATION)) { // EZO_CALIBRATION returns 0-3
// some sensors return multiple comma-separated values, terminate string after first one
for (size_t i = 1; i < sizeof(buf) - 1; i++) {
if (buf[i] == ',') {
buf[i] = '\0';
break;
}
}
if (buf[0] == 1) {
std::string payload = reinterpret_cast<char *>(&buf[1]);
if (!payload.empty()) {
switch (to_run->command_type) {
case EzoCommandType::EZO_READ: {
// some sensors return multiple comma-separated values, terminate string after first one
int start_location = 0;
if ((start_location = payload.find(',')) != std::string::npos) {
payload.erase(start_location);
}
auto val = parse_number<float>(payload);
if (!val.has_value()) {
ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
Expand Down

2 comments on commit cd5dae3

@Assunto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw from the writeup that you would be able to implement a breaking change in order to get the RGB data parsed. I was wondering if you could do the same for the HUM and EC sensors as they also have comma separated values?

I was trying to figure out how to get a readout of the parsed values by creating a template sensor (or binary sensor) utilizing lambdas and strtok, but I can't quite get there (no coding education). If you were able to figure it out, perhaps adding a description on how to template the comma separated values would give users a solution without introducing a breaking change?

Thanks!!

@TheRealFalseReality
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This out be great to get all data in the string parsed out. Humidity would be a perfect sensor for this. It sends out Humidity, Temp and Dew Point. In ESPHome, we can only utilize one of those sensors.... But we technically have 3... Although not as important, an EC sensor would benefit from this as well. Salinity, Specific Gravity, TDS and Conductivity, all at once.

Please sign in to comment.