ESP32 only connects to wifi when serial debugging is active

Hi guys. I talk to a solar inverter via 485 with an ESP32 on its UART2 (gpio 16 and 17). But it only connects to wifi when I also activate the serial debug monitor via PIO. I implemented the talking to the inverter via a custom uart device (Custom UART Device — ESPHome). It also can successfully communicate, I see it on the inverters communication LED, but the wifi connection fails. I already set everything static.

Here is my ESPHome config:

substitutions:
    devicename: "Solar-Inverter"
  
esphome:
  name: espinverter
  includes:
    - my_custom_sensor.h
  libraries:
    - "NETSGPClient"
  
esp32:
  board: esp32dev
  framework:
    type: arduino

uart:
  - tx_pin: 17
    rx_pin: 16
    baud_rate: 9600
    id: f2

api:
  password: ""

ota:
  password: ""

wifi:
  ssid: ""
  password: ""
  manual_ip:
    static_ip: 192.168.1.140
    gateway: 192.168.1.1
    subnet: 255.255.255.0

  ap:
    ssid: "Espinverter Fallback Hotspot"
    password: ""

captive_portal:

web_server:
  port: 80

- platform: custom
  lambda: |-
    auto my_sensor{new MyCustomSensor(id(f2))};
    App.register_component(my_sensor);
    return my_sensor->getSensors();

  sensors:
  - name: "Inverter Temperature"
    unit_of_measurement: °C
    accuracy_decimals: 1
  - name: "Inverter AC-Power"
    unit_of_measurement: W
    accuracy_decimals: 2
  - name: "Inverter DC-Power"
    unit_of_measurement: W
    accuracy_decimals: 2
  - name: "Inverter total Production"
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    accuracy_decimals: 2
- platform: wifi_signal
  name: "${devicename} WiFi Signal"
  device_class: signal_strength
  update_interval: 15s

And thats my C++ implementation:

#include "esphome.h"
#include "AsyncNETSGPClient.h"

constexpr uint8_t PROG_PIN = 4;            /// Programming enable pin of RF module
int16_t RX_PIN = 16;             /// RX pin of RF module
int16_t TX_PIN = 17;             /// TX pin of RF module
uint32_t inverterID{0x26002761}; /// Identifier of your inverter (see label on inverter)

Sensor *temperature_sensor1{new Sensor()};
Sensor *acpower_sensor1{new Sensor()};
Sensor *dcpower_sensor1{new Sensor()};
Sensor *overall_production1{new Sensor()};

void onInverterStatus(AsyncNETSGPClient::InverterStatus const& status)
{
    if (status.deviceID == inverterID){
        temperature_sensor1->publish_state(status.temperature);
        acpower_sensor1->publish_state(status.acPower);
        dcpower_sensor1->publish_state(status.dcPower);
        overall_production1->publish_state(status.totalGeneratedPower);
    }
}

class MyCustomSensor : public PollingComponent, public Sensor
{
public:
    MyCustomSensor(UARTComponent *parent) : PollingComponent(3000), _stream{parent}, _client{_stream, PROG_PIN} {}
private:
    class MyStream : public Stream {
    public:
        uart::UARTDevice _device;

        MyStream(UARTComponent *parent) : Stream{}, _device{parent} {}
        size_t write(uint8_t const byte) override {
            return _device.write(byte);
        }

        int available() override {
            return _device.available();
        }

        int read() override {
            return _device.read();
        }

        int peek() override {
            return _device.peek();
        }

        void flush() override {
            return _device.flush();
        }
    };

    MyStream _stream;
    AsyncNETSGPClient _client;

    void setup() override
    {
        _client.setDefaultRFSettings();
        _client.setStatusCallback(onInverterStatus);
        _client.registerInverter(inverterID);
    }

    void update() override
    {
        _client.update();
    }

    std::vector<esphome::sensor::Sensor*> const getSensors() {
        return {
            temperature_sensor1,
            acpower_sensor1,
            dcpower_sensor1,
            overall_production1,
        };
    }
};

How can I debug this? I’am completely out of ideas. Thank you for any help :slight_smile: