Compare the UART output data to a string

I have some esphome code that dumps the output of an rs232 module to the debug log.

Now I would like to compare the output line to a string so I can do something with it.

My problem is that I don’t understand how to convert UARTDebug::log_string(direction, bytes) to a string that works with strcmp.

Can anyone help?

uart:
  baud_rate: 9600
  tx_pin: 17
  rx_pin: 16
  id: UART2
  stop_bits: 1
  data_bits: 8
  parity: NONE
  debug:
    direction: RX
    dummy_receiver: true
    after:
      delimiter: ";78H\e[m"
      #delimiter: "\n"
      #bytes: 98
    sequence:
      lambda: !lambda |-
        UARTDebug::log_string(direction, bytes); // prints out the data recived from the rs232 port

        // I think I need to convert the " void esphome::uart::UARTDebug::log_string	(	UARTDirection 	direction,std::vector< uint8_t > 	bytes )	" to a regualr string for comparision ?

        // This does not work (or compile)
        
        if (strcmp((UARTDebug::log_string(direction, bytes)), "banana")) {
          ESP_LOGD("Found banana, do something here");
        }

A nice person on reddit gave me the answer, here’s the improved solution that allows one to search for a substring, which is far more flexible.

uart:
  baud_rate: 9600
  tx_pin: 17
  rx_pin: 16
  id: UART2
  stop_bits: 1
  data_bits: 8
  parity: NONE
  debug:
    direction: RX
    dummy_receiver: true
    after:
      delimiter: ";78H\e[m"
      #delimiter: "\n"
      #bytes: 98
    sequence:
      lambda: !lambda |-
        UARTDebug::log_string(direction, bytes); 
        std::string str(bytes.begin(), bytes.end());

        if (str.find("banana") != std::string::npos) {
           ESP_LOGD("custom", "We have a banana! :-)");
        }
        else {
           ESP_LOGD("custom", "Sad times, there is no banana");
        }
2 Likes