ESPHome custom component for beginner

Hi everybody,

I am trying to create a custom component for ESPHome. These are the docs which I tried to follow.

Below is the .h file I used as custom component and the .yaml used in ESPHome dashboard.

This nodeMCU was previously in use and connected to Home Assistant, so I assumed it to just add to the existing integration.

However: while I could ping the device, there was no connection. When I tried to click on Logs in ESPHome dashboard, I would get

INFO Reading configuration /config/esphome/testcoinslot.yaml...
INFO Starting log output from 10.0.20.147 using esphome API
WARNING Can't connect to ESPHome API for 10.0.20.147: Timeout while connecting to ('10.0.20.147', 6053)
INFO Trying to reconnect to 10.0.20.147 in the background

I have not yet worked with a custom component in ESPHome, but this looks to me as if the device would connect to the wifi (as I could ping it) but would not load the main loop that is responsible for presenting an API to Home Assisstant.

What am I doing wrong? Thank you in advance for your help.

/config/esphome/my_custom_component.h

#include "esphome.h"

class MyCustomComponent : public Component {
 public:
  void setup() override {
    // This will be called once to set up the component
    // think of it as the setup() call in Arduino
    pinMode(5, INPUT);
    pinMode(6, OUTPUT);
  }
  void loop() override {
    // This will be called very often after setup time.
    // think of it as the loop() call in Arduino
    if (digitalRead(5)) {
      digitalWrite(6, HIGH);

      // You can also log messages
      ESP_LOGD("custom", "The GPIO pin 5 is HIGH!");
    }
  }
};

/config/esphome/testcoinslot.yaml

esphome:
  name: testcoinslot
  friendly_name: 00/Test/Coinslot
  includes:
    - my_custom_component.h

esp8266:
  board: nodemcuv2


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "asdasdasd"

ota:
  password: "asdasdasdas"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Testcoinslot Fallback Hotspot"
    password: "dasdasdasd"

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: D4
      inverted: True
    name: "Counter"
  - platform: gpio
    pin:
      number: D7
    name: "Pulse"

custom_component:
- lambda: |-
    auto my_custom = new MyCustomComponent();
    return {my_custom};
  components:
  - id: my_custom_id

I know you just copied this from the example, but my understanding is that 6 here is the GPIO pin, and according to this handy documentation:

GPIO6 to GPIO11 are usually connected to the flash chip in ESP8266 boards. So, these pins are not recommended to use.

Maybe as a first step you could simplify your custom component and just put a single log message into the setup method, just to see if HA is able to connect to your ESP again.

1 Like

Yes! Thank you. When I just use the ESP_LOGD without accessing any pins, it works. Hopefully I can build upon that :slight_smile:

1 Like

Hi! I have the library from arduino: GitHub - climateguard/CG-Anem: Arduino library for thermoanemometer CG-Anem to simplify integration into compatible platforms.
How can i do it works in esphome?