ESP32 how to configure API

Hello all,
First of all please be warned - I have no idea what the heck I’m doing :wink:

I am trying to make a DIY presence detector with ESP32 and LD2410. I have found many youtube videos on the subject but absolutely none of them are beginner user-friendly. I’m trying to follow along one of those videos but I can’t even start the first step.

I’ve plugged in the ESP32 board and I see it powers up (good news!), but when I try to add it to HomeAssistant I get the following message: “homeassistant can’t connect to esp. please make sure your yaml file contains an ‘api:’ line.”. I understand that ESP now requires an optional encryption key, so that API line requirement is fairly new and the youtube videos I’ve seen don’t consider this.

Furthermore, according to this : Native API Component — ESPHome

The only configuration I need to add in the YAML config file is this (as everything is optional) :

Example configuration entry

api:

So I went ahead and entered this, yet I still have that first error message. Obviously I don’t understand the instructions I’ve seen.

How can I fix this? Please provide step-by-step instructions so that even a 5-year-old could understand :wink: For some reason after I get past this hurdle another one will be waiting for me. So you’ll probably hear from me again :stuck_out_tongue:

Thanks :slight_smile:

Start here, then come back when stuck again :wink:

Thanks! Although I am unsure the response is appropriate for a 5-years-old ;). Kidding aside I won’t have time to mess around with this until next week but I’ll let you know how it goes when I do give it a go.

Furthermore, after you add the API line, you have to re-flash the device for it to run the new code. Is that what you did?

Posting your code would make it easier for others to identify the problem. Anyway, I would use the following as a bare bones yaml to get started.

esphome:
  name: esptest

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG
  baud_rate: 0

# Enable Home Assistant API
api: {} # This spawns an AP with the device name and mac address, no password.

# Enable web server
web_server:
  port: 80

ota:
  safe_mode: true

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  power_save_mode: none
  manual_ip:
    static_ip: !secret ip_esptest
    gateway: !secret ip_gateway
    subnet: !secret ip_subnet
    dns1: !secret ip_dns1

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap: {}
    #ssid: "esptest"
    #password: "password"

captive_portal:

Hi, I am using the managed instance of HomeAssistant. I’ve installed the ESP32 add-on. I’ve connected the ESP32 board to a computer via USB. When I tried adding the node, I received this message: “unable to compile the configuration esphome will now create your configuration and install it on your device. connect your esp8266 or esp32 with a usb cable to your computer and click on connect. you need to do this once. later updates install wirelessly. learn more…”

I then figured I had to put the ESP WROOM module in “flash mode” so that the firmware is updated. From what I understood, in order to put it in this flash mode, I need to plug it in the computer and press on the IO0 button, wait a few seconds and then release it. I’ve done this a few times and nothing happened. Then at one point I briefly got a popup where it appeared the node got installed in ESPHome but it disappeared as quickly as it appeared.

I decided to look if any config files were created, and in /homeassistant/esphome/trash the following file was created :


esphome:
name: motion1
friendly_name: Motion1

esp32:
board: esp32dev
framework:
type: arduino

Enable logging

logger:

Enable Home Assistant API

api:
encryption:
key: “(I’ve hidden this)”

ota:
password: “(I’ve hidden this)”

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

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Motion1 Fallback Hotspot”
password: “(I’ve hidden this)”

captive_portal:


I have copied this file in /homeassistant/esphome/ and now I see the node but I see it is offline.

I looked at the logs and I see this :


INFO ESPHome 2024.4.2
INFO Reading configuration /config/esphome/motion1.yaml…
ERROR Error while reading config: Invalid YAML syntax:

Error reading file /config/esphome/secrets.yaml: [Errno 2] No such file or directory: ‘/config/esphome/secrets.yaml’


I am using a managed instance of HomeAssistant and at this point I am figuring I probably shouldn’t have to tinker with a secrets.yaml file and so on, I’ve probably missed a step somewhere in the process. Then again I’m thinking that maybe I should try to create a secrets.yaml file and see if this solve the problem.

Would you have a recommendation? Many thanks !

To the same computer running ESPhome?

Added a node in the ESPhome add-on?

Press and hold the button while plugging it in (so you should start holding the button before plugging it in) and let go a few seconds after connecting it.

The first time you setup a node in the ESPhome add-on, it should ask you for wifi credentials which will result in ESPhome creating the secrets.yaml file and adding the credentials to it.

Hi and thanks for the reply!

I am running Homeassistant on a dedicated headless computer and tried configuring on another computer (meaning accessing HomeAssistant via that other computer and also connecting the ESP32 module via USB on that computer). Both are physically connected to a LAN.

I did add the node in the ESPhome add-on (or tried to, as the resulting file was stored in the trash subdirectory).

Also according to what you wrote (thanks again!) it seems I did the right procedure for the firmware update - I did press and hold the button while plugging it in and let go a few seconds after connecting it. I have a red LED on the ESP board, it remained lit up all the time. Unsure if it should have blinked to signal it was ready to receive the firmware.

I confirm the first time I set up the node in the ESPhome add-on it asked for wifi credentials. I don’t see the secrets.yalm file in the ESPhome directory. Is the file stored somewhere else?

While using ESPHome Dashobard locate SECRETS on right-top position.
It will open secrets.yaml located in same directory together with devices YAML files.

Put into rows like:

wifi_ssid: "XXX"
wifi_password: "ZZZ"

and use them in configs:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
1 Like
  1. Make sure the have the drivers installed for the ESP32 board.
  2. Make sure you have your wifi credentials in the secrets file like @Masterzz mentioned.
  3. Once you get your configuration corrected, start here: An Introduction to ESPhome - #2 by FriedCheese

Many thanks to you and @Masterzz I was able to install my ESP32 board and I now see that it is online! I now have one device and one entity (firmware). This is awesome, I’m slowly getting there! I’m trying to set up a motion sensor with an LD2410B, I don’t see it. I’ll try to find the answer (but not tonight), I’ll keep you post. Maybe the wiring between the ESP32 and LD2410B is incorrect. I can see it when I try to add a device with Bluetooth but can’t successfully connect to it. At least this tells me the LD2410B is indeed powered on.

2 Likes