Fan Component -> UART

Hi,

I have a 3-channel PWM generator for controlling fans. It supports to set the Duty values via UART TX/RX, so I added an ESP32 to it. I can set the value with uart.write.

I played with the fan component code, but I am stuck at basic steps cince I cannot retrieve the value I changed in the UI.

My code:

uart:
#- id: uart_1
tx_pin: GPIO08
rx_pin: GPIO10
baud_rate: 9600
debug:
direction: BOTH
dummy_receiver: false
after:
delimiter: “\n”
sequence:
- lambda: UARTDebug::log_string(direction, bytes);

output:

  • platform: template
    id: Ruck_125L_EC_Output
    type: float
    write_action:
    • uart.write: ‘D1:099’ # for testing

fan:

  • platform: speed
    name: Ruck 125L EC
    id: Ruck_125L_EC
    output: Ruck_125L_EC_Output
    on_speed_set:
    • uart.write: !lambda |-
      char buf[256];
      sprintf(buf, “D1:0%d”, Ruck_125L_EC->speed));
      return ((std::string) buf).c_str();

I dont know how to get the value so I tried above code, but get an error int the lamba part:

“/config/esphome/m5stampc3u-b.yaml:63:39: error: could not convert ‘std::__cxx11::basic_string(((const char*)((char*)(& buf))), std::allocator()).std::__cxx11::basic_string::c_str()’ from ‘const char*’ to ‘std::vector’”

The fan component entity is visible and I get the on_speed_set event. just dont know how the get the value…

Any hellp is highly appreciated :slight_smile:
best geetings

EDIT:
I manged to get the value with this code:

  • platform: speed
    name: Ruck 125L EC
    id: Ruck_125L_EC
    output: Ruck_125L_EC_Output
    speed_count: 100
    on_speed_set:
    • uart.write: !lambda |-
      char buf[256];
      sprintf(buf, “D1:0%d”, Ruck_125L_EC->speed);
      std::string s = buf;
      return (std::vector( s.begin(), s.end()));

Can anyone help me how to get a slider? Like in the first picture on the fan component documentation.

Post yaml please.

Hi,
thanks for the reply, which yaml code do you mean, exactly? I’ve posted the key code blocks from my ESPs yaml configuration.
But I managed to get it to work with:

on_speed_set:
  - uart.write:
      id: uart_1
      data: !lambda |-
              char buf[256];
              sprintf(buf,  "D1:0%d", Ruck_125L_EC->speed);
              std::string s = buf;
              return (std::vector<unsigned char>( s.begin(), s.end()));

Now struggling how to read data…

I think Nick is possibly hinting about correct code formatting. We can see you’ve tried but it’s not quite right. Please take a look at this and try to correct it. Otherwise it hurts our eyeballs too much to see proper indentation etc.

correct:
  like_this:

Ok thx, I will take a look tomorrow and edit it.

1 Like

What PWM generator are you using?