I'm having problem to separate string into values

hello, I’m having trouble separating a string into values ​​referring to the sensing, I can visualize the entry, but I can’t do anything else, including, I can’t “print” the information to test the middle of the program.

i tried logger but it does not accept in custom_component
please. where am i going wrong?

captive_portal:

uart:

  id: uart_bus

  tx_pin: GPIO15

  rx_pin: GPIO13

  baud_rate: 9600

  debug:

    direction: BOTH

    dummy_receiver: true

    after:

      timeout: 100ms

    sequence:

      - lambda: UARTDebug::log_string(direction, bytes);

binary_sensor:  

  - platform: gpio

    name: "Interruptor"

    pin: D5

    on_press:

      - uart.write: "Hello World"

    on_release:

      - uart.write: "Byebye World"

       

custom_component:

- lambda: |-

    auto my_custom = new meusensor(id(uart_bus));

    return {my_custom};

#include "esphome.h"

class meusensor : public Component, public UARTDevice {
 public:
  meusensor(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }
  void loop() override {
    // Use Arduino API to read data, for example
    const int max_line_length = 180;
    static char buffer[max_line_length];
    buffer[0] = '=';
    buffer[1] = '=';
    buffer[2] = '=';
    buffer[3] = '=';
    buffer[4] = '=';
    buffer[5] = '=';
    buffer[6] = '=';
    // etc
  }
  
  
  void readline(int readch, char *buffer, int len)
  {
    static int pos = 0;

    if (readch >= 0)
    {
      if (pos < len - 1)
      {
        buffer[pos++] = readch;
        
        buffer[pos] = 0;
        if ( buffer[pos++] == ':')
        {
        buffer[pos++] = '=';
        }
        
      }
      else
      {
        pos = 0;
      }
      
    }
    
    buffer[0]='=';
    buffer[1]='=';
    buffer[2]='=';
    buffer[3]='=';
    
    return;
  }
};

It’s a bit hard to understand what you are trying to do + what your goals are.

Do you want to split the string and then use the individual values in your custom component? Also, you mentioned that you tried logger but not accepted. Check this page here on how to use it or show us what you already tried. That way we have a better idea how to help you.

thanks for answering
at first I’m just wanting to manipulate to be able to see if it’s really doing something. for example 10:110:010:15 for 10=110=010=15
then wanted to separate this string into values ​​10 110 10 15 in int or float values. I also wanted to be able to see what is being done, programming is not my strong point, but I usually program in C, Java and Matlab, and I have no problem printing the value of what I am manipulating in those, since in eshome I am having this difficulty,you mentioned using ESP_LOGD , I have already tried it, but when placing it, the red error lines appear.
I noticed that what I passed was a test version that I had done without calling readline. I keep beating myself up about it and it’s a time consuming process as it takes a long time to compile and install.
thanks in advance

So, you wanna convert a string with numbers and a known separator, like 10:110:010:15 to a matrix of integers like [10, 110, 10, 15], is that correct?

As @ckxsmart mentioned, it’s usually easier to help when we know what are you trying to achieve… So if you explain what are those numbers, how (from where) you get that string and what are you trying to achieve in the end by using those values it will be easier to propose a solution.

I’m currently doing it, for a three-phase energy meter that I made a few years ago I want to integrate it into the system, in this specific case I want all the values ​​to be in float, I wanted to do something similar to the DHT output, but I wanted to learn how to do it have both float, int and binary output, because if I need it in the future I’ll already know.

ex: 847:126.20:1.20:125.70:2.0:xxxxx

for

847
126.20
1.20
125.70
2.00
which when displayed later will be

847W
126.2V
1.20A
125.7V
2.00A

has other history data but would be similar in type.

Take a look at the example here. The sscanf() function can process the formatted text to extract the numeric values to separate variables.

1 Like

Thanks, I’ll see, these days, I don’t know if I’ll have time, but I will.

To what I tested here, it worked, I don’t know how happy I am, thank you very much. saved me.

1 Like