Unable to flash my ESP32

Heyo !
I have to admit, it’s my first time with HA. I have a RPI 4B with the HA OS and a ESP32 that I will use to connect an ENS160 through ESPHome.
Tried all afternoon and I’m hitting a roadblock with this error message always appearing whatever I do… (the […] are for the sake of shortening the error message.

Notes:

I’ve already tried removing build files and remove/reinstalling ESPHome (at some point, it helped)

Do you have any idea or hints has to what I can do to progress ?

Context

YAML

All secrets have been changed

esphome:
  name: civiaq
  friendly_name: CIVIAQ

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "slk4g6s55f5s5d5f48f8sr8g8dv8sd8fd7fb7xc7v7t="
ota:
  password: "12345678912345678912345678912345"

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

i2c:
  sda: GPIO21
  scl: GPIO22
  scan: true
  id: bus_a

sensor:
  - platform: ens160
    eco2:
      name: "ENS160 eCO2"
    tvoc:
      name: "ENS160 Total Volatile Organic Compounds"
    aqi:
      name: "ENS160 Air Quality Index"
    update_interval: 10s
    address: 0x53

Error log

INFO ESPHome 2024.4.2
INFO Reading configuration /config/esphome/civiaq.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing civiaq (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
Tool Manager: Installing platformio/framework-arduinoespressif32 @ ~3.20005.0
INFO Installing platformio/framework-arduinoespressif32 @ ~3.20005.0
Unpacking  [####################################]  100%          
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port/include' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port/include`
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port' 

[...]

Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk`
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools`
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32`
Tool Manager: Warning! Package Mirror: [Errno 17] File exists: '/data/cache/platformio/packages/framework-arduinoespressif32'
WARNING Warning! Package Mirror: [Errno 17] File exists: '/data/cache/platformio/packages/framework-arduinoespressif32'
Tool Manager: Looking for another mirror...
WARNING Looking for another mirror...
Unpacking  [####################################]  100%          
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port/include' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port/include`
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/include/esp_system/port`

[...]

[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32/tools' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32/tools`
[Errno 39] Directory not empty: '/data/cache/platformio/packages/framework-arduinoespressif32' 
Please manually remove the file `/data/cache/platformio/packages/framework-arduinoespressif32`
Tool Manager: Warning! Package Mirror: [Errno 17] File exists: '/data/cache/platformio/packages/framework-arduinoespressif32'
WARNING Warning! Package Mirror: [Errno 17] File exists: '/data/cache/platformio/packages/framework-arduinoespressif32'
Tool Manager: Looking for another mirror...
WARNING Looking for another mirror...
Unpacking  [####################################]  100%          

etc...

You should show the code you are trying to use in esphome.

Have you attempted to “Clean build files”? Esphome is failing to compile your firmware image, and cleaning the build files often helps.

Updated the post in this regard, thanks for the suggestion !

I did but to not results… Updated the post to include this information

esphome:
  name: civiaq
  friendly_name: CIVIAQ

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "slk4g6s55f5s5d5f48f8sr8g8dv8sd8fd7fb7xc7v7t="
ota:
  password: "12345678912345678912345678912345"

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

Have you tried just flashing with the minimal code to see it that working?
I see esp32c3 is mentioned in the log. Is it a C3?

Well, I resolved the issue, but not the way I wanted:
When I left yesterday, I shuft the power and stored it. When, this afternoon, I powered it on and started looking, everything went well except for loose connector that kept the ESP32 from seing the sensor. After a short correction of the problem, It installed correctly and I let the ESN160 warm up (1h). After some time, I realized that HA OS had crash yet again(Error 500 on the brower) so I shut it off. After rebboting, I started seeing an other strange error (again) with some files. And that’s when it hit me: “Maybe files are getting corrupt… They would go well with me shutting it off be shutting down the power… Does it exist a way to shut it of cleanly, via the interface ?” After a quick google search, it seems like yeah, it does ^^.
A clean install of HA OS (the corruption broke the installation of ESPHome) later, everything is going well* !

I don’t know if the error 500 has anything to do with that (we will see)

TL;DR: I had problem with corrupt files because I was cutting the power source to power it power it off…

* I’m struggling to get Home Assistant to “accept the sensor” but I don’t kown if it’s because of the warm-up time of one hour of the sensor…

INFO ESPHome 2024.4.2
INFO Reading configuration /config/esphome/civiaq.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing civiaq (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- AsyncTCP-esphome @ 2.1.3
|-- WiFi @ 2.0.0
|-- FS @ 2.0.0
|-- Update @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- noise-c @ 0.1.4
|-- Wire @ 2.0.0
RAM:   [=         ]  12.5% (used 40992 bytes from 327680 bytes)
Flash: [=====     ]  50.9% (used 934165 bytes from 1835008 bytes)
========================= [SUCCESS] Took 18.10 seconds =========================
INFO Successfully compiled program.
esptool.py v4.7.0
Serial port /dev/ttyUSB0
Connecting.....
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 24:dc:c3:9e:5e:44
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00010000 to 0x000f5fff...
Flash will be erased from 0x00001000 to 0x00005fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Compressed 939936 bytes to 621918...
Wrote 939936 bytes (621918 compressed) at 0x00010000 in 15.8 seconds (effective 475.5 kbit/s)...
Hash of data verified.
Compressed 17440 bytes to 12128...
Wrote 17440 bytes (12128 compressed) at 0x00001000 in 0.7 seconds (effective 208.7 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 144...
Wrote 3072 bytes (144 compressed) at 0x00008000 in 0.1 seconds (effective 325.4 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 445.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
INFO Successfully uploaded program.
INFO Starting log output from /dev/ttyUSB0 with baud rate 115200
[19:45:34][I][logger:158]: Log initialized
[19:45:34][C][ota:483]: There have been 1 suspected unsuccessful boot attempts.
[19:45:34][D][esp32.preferences:114]: Saving 1 preferences to flash...
[19:45:34][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[19:45:34][I][app:029]: Running through setup()...
[19:45:34][I][i2c.arduino:218]: Performing I2C bus recovery
[19:45:34][C][ens160:052]: Setting up ENS160...
[19:45:34][C][wifi:038]: Setting up WiFi...
[19:45:34][C][wifi:051]: Starting WiFi...
[19:45:34][C][wifi:052]:   Local MAC: 24:DC:C3:9E:5E:44
[19:45:34][D][wifi:462]: Starting scan...
[19:45:34][D][ens160:172]: ENS160 readings unavailable - Normal Operation but readings not ready
[19:45:34][W][component:157]: Component wifi set Warning flag: unspecified
[19:45:37][I][ens160:179]: ENS160 readings unavailable - 1 hour startup required after first power on
[19:45:40][D][wifi:477]: Found networks:
[19:45:40][I][wifi:521]: - 'WIFI' [redacted]▂▄▆█
[19:45:40][D][wifi:522]:     Channel: 1
[19:45:40][D][wifi:523]:     RSSI: -61 dB
[19:45:40][I][wifi:521]: - 'WIFI' [redacted]▂▄▆█
[19:45:40][D][wifi:522]:     Channel: 11
[19:45:40][D][wifi:523]:     RSSI: -64 dB
[19:45:40][I][wifi:521]: - 'WIFI' [redacted]▂▄▆█
[19:45:40][D][wifi:522]:     Channel: 1
[19:45:40][D][wifi:523]:     RSSI: -68 dB
[19:45:40][I][wifi:521]: - 'WIFI' [redacted]▂▄▆█
[19:45:40][D][wifi:522]:     Channel: 1
[19:45:40][D][wifi:523]:     RSSI: -84 dB
[19:45:40][I][wifi:521]: - 'WIFI' [redacted]▂▄▆█
[19:45:40][D][wifi:522]:     Channel: 11
[19:45:40][D][wifi:523]:     RSSI: -94 dB
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][D][wifi:526]: - [redacted] [redacted]▂▄▆█
[19:45:40][I][wifi:303]: WiFi Connecting to 'ICAM_IoT'...
[19:45:42][I][wifi:597]: WiFi Connected!
[19:45:42][C][wifi:408]:   Local MAC: 24:DC:C3:9E:5E:44
[19:45:42][C][wifi:413]:   SSID: [redacted]
[19:45:42][C][wifi:416]:   IP Address: 10.254.4.13
[19:45:42][C][wifi:420]:   BSSID: [redacted]
[19:45:42][C][wifi:421]:   Hostname: 'civiaq'
[19:45:42][C][wifi:423]:   Signal strength: -72 dB ▂▄▆█
[19:45:42][C][wifi:427]:   Channel: 1
[19:45:42][C][wifi:428]:   Subnet: 255.255.252.0
[19:45:42][C][wifi:429]:   Gateway: 10.254.7.254
[19:45:42][C][wifi:430]:   DNS1: 10.85.12.6
[19:45:42][C][wifi:431]:   DNS2: 10.85.12.3
[19:45:42][D][wifi:606]: Disabling AP...
[19:45:42][C][ota:096]: Over-The-Air Updates:
[19:45:42][C][ota:097]:   Address: civiaq.local:3232
[19:45:42][C][ota:100]:   Using Password.
[19:45:42][C][ota:103]:   OTA version: 2.
[19:45:42][C][api:025]: Setting up Home Assistant API server...
[19:45:42][I][app:062]: setup() finished successfully!
[19:45:42][W][component:170]: Component wifi cleared Warning flag
[19:45:42][W][component:157]: Component api set Warning flag: unspecified
[19:45:42][I][app:100]: ESPHome version 2024.4.2 compiled on May 14 2024, 17:29:31
[19:45:42][C][wifi:580]: WiFi:
[19:45:42][C][wifi:408]:   Local MAC: 24:DC:C3:9E:5E:44
[19:45:42][C][wifi:413]:   SSID: [redacted]
[19:45:42][C][wifi:416]:   IP Address: 10.254.4.13
[19:45:42][C][wifi:420]:   BSSID: [redacted]
[19:45:42][C][wifi:421]:   Hostname: 'civiaq'
[19:45:42][C][wifi:423]:   Signal strength: -71 dB ▂▄▆█
[19:45:42][C][wifi:427]:   Channel: 1
[19:45:42][C][wifi:428]:   Subnet: 255.255.252.0
[19:45:42][C][wifi:429]:   Gateway: 10.254.7.254
[19:45:42][C][wifi:430]:   DNS1: 10.85.12.6
[19:45:42][C][wifi:431]:   DNS2: 10.85.12.3
[19:45:42][C][logger:166]: Logger:
[19:45:42][C][logger:167]:   Level: DEBUG
[19:45:42][C][logger:169]:   Log Baud Rate: 115200
[19:45:42][C][logger:170]:   Hardware UART: UART0
[19:45:42][C][i2c.arduino:071]: I2C Bus:
[19:45:42][C][i2c.arduino:072]:   SDA Pin: GPIO21
[19:45:42][C][i2c.arduino:073]:   SCL Pin: GPIO22
[19:45:42][C][i2c.arduino:074]:   Frequency: 50000 Hz
[19:45:42][C][i2c.arduino:086]:   Recovery: bus successfully recovered
[19:45:42][I][i2c.arduino:096]: Results from i2c bus scan:
[19:45:42][I][i2c.arduino:102]: Found i2c device at address 0x53
[19:45:42][C][ens160:278]: ENS160:
[19:45:42][D][ens160:300]: Setup successful
[19:45:42][I][ens160:304]: Firmware Version: 5.4.6
[19:45:42][C][ens160:306]:   Address: 0x53
[19:45:42][C][ens160:307]:   Update Interval: 5.0s
[19:45:42][C][ens160:308]:   CO2 Sensor: 'ENS160 eCO2'
[19:45:42][C][ens160:308]:     Device Class: 'carbon_dioxide'
[19:45:42][C][ens160:308]:     State Class: 'measurement'
[19:45:42][C][ens160:308]:     Unit of Measurement: 'ppm'
[19:45:42][C][ens160:308]:     Accuracy Decimals: 0
[19:45:42][C][ens160:308]:     Icon: 'mdi:molecule-co2'
[19:45:42][C][ens160:309]:   TVOC Sensor: 'ENS160 Total Volatile Organic Compounds'
[19:45:42][C][ens160:309]:     Device Class: 'volatile_organic_compounds_parts'
[19:45:42][C][ens160:309]:     State Class: 'measurement'
[19:45:42][C][ens160:309]:     Unit of Measurement: 'ppb'
[19:45:42][C][ens160:309]:     Accuracy Decimals: 0
[19:45:42][C][ens160:309]:     Icon: 'mdi:radiator'
[19:45:42][C][ens160:310]:   AQI Sensor: 'ENS160 Air Quality Index'
[19:45:42][C][ens160:310]:     Device Class: 'aqi'
[19:45:42][C][ens160:310]:     State Class: 'measurement'
[19:45:42][C][ens160:310]:     Unit of Measurement: ''
[19:45:42][C][ens160:310]:     Accuracy Decimals: 0
[19:45:42][C][ens160:310]:     Icon: 'mdi:chemical-weapon'
[19:45:42][C][ens160:316]:   Compensation: Not configured
[19:45:42][C][captive_portal:088]: Captive Portal:
[19:45:42][C][mdns:115]: mDNS:
[19:45:42][C][mdns:116]:   Hostname: civiaq
[19:45:42][C][ota:096]: Over-The-Air Updates:
[19:45:42][C][ota:097]:   Address: civiaq.local:3232
[19:45:42][C][ota:100]:   Using Password.
[19:45:42][C][ota:103]:   OTA version: 2.
[19:45:42][C][api:139]: API Server:
[19:45:42][C][api:140]:   Address: civiaq.local:6053
[19:45:42][C][api:142]:   Using noise encryption: YES
```
It will then recycle again and again after a dozen minutes, with "[19:50:34][I][ota:117]: Boot seems successful, resetting boot loop counter.
[19:50:34][D][esp32.preferences:114]: Saving 1 preferences to flash...
[19:50:34][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed"