ESP32-C3 with ble_tracker - I'm sharing how it worked

Introduction:
I have this: Wemos C3 Mini - Lolin ESP32-C3 ( C3 mini — WEMOS documentation )
I wanted to use it as a Bluetooth proxy and to use also the onboard RGB Led so I wanted an arduino platform, not esp_idf.

Problem
I was having a strange behaviour: very loooong time t oconnect to WiFi (1 … 2 or even 3 minutes) or sometimes it was even not connecting (I was restarting it until it was connecting in some minutes).
I tried and tried, various variants and … nothing.

Solution
Until … I found this answer:

Thanks chose007 :slight_smile:

The final code I have:

esphome:
  name: my-c3-mini-bt
  on_boot: #this is to avoid slow startup time because of BT and WiFi "interactions?" , check https://github.com/esphome/issues/issues/2941#issuecomment-1331851692
    priority: 250
    then:
      - delay: 15s
      - lambda: |-
          id(ble_tracker).set_scan_continuous(true); 
          id(ble_tracker).start_scan();

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

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

ota:
  password: ":) :) :)"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: "8.5"  # sometimes results in a bootloop without it

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "My-C3-Mini-Bt Fallback Hotspot"
    password: ":) :) :)"

captive_portal:

esp32_ble_tracker:
  id: ble_tracker
  scan_parameters:
    continuous: false

bluetooth_proxy:
  active: true

light:
  - platform: neopixelbus
    id: myC3MiniBt_led
    type: GRB
    variant: WS2812
    method: ESP32_RMT_1
    num_leds: 1
    pin: GPIO7
    name: "My C3 Mini BT Led"
    effects:
      - addressable_rainbow:
          name: Rainbow Effect With Custom Values
          speed: 10
          width: 50
16 Likes

Thanks for sharing!
Finally got the RGB LED working on a M5stamp-C3U, for that specific board the pin is GPIO2.

Thanks for sharing this. Fixed my ESP32-C3’s WiFi connection issues. My garage door remote is now totally reliable (M5Stamp C3 + M5 3A Relay Unit).

Great find!

Thanks for sharing. My Seeed Xiao now works as my bluetooth proxy that enables my switchbot again (which failed since I installed USB3.0 SSD on HA).

However I have one observation. If i connect to my Seeed Xiao with ESPHome web through USB, it boots just fine. If I don’t do it and just plug in USB power, it doesn’t seem to boot and I can’t reach it wirelessly through esphome. That’s one weird thing. I’ll do more tests in the coming days.

I’m having the same issue. :frowning: Did you have any luck getting that sorted?

[edit] looks like there’s an issue with logger: ESPHome device does not restart without serial port active (apparently!) - #10 by tom_l

1 Like

Yeah I found that out too. However I still can’t get that resolved so I just disconnect the power and let my Seeed xiao collect dust now. I have another esp32 board serving as my bluetooth proxy now but ironically it stopped working suddenly two days ago, without me doing any software updates. Hence why I am back to this forum again.

I am still trying to turn on the RGB LED using NeoPixel, without success.
At the moment, the only way I have found to turn on the led is to ground pins 3,4 and 5. Which are Red, Green and Blue respectively.
So i’ve made this and it works:

light:
  - platform: rgb
    name: "RGB Led"
    red: output_component1
    green: output_component2
    blue: output_component3

output:
  - platform: ledc
    pin: GPIO3
    inverted: true
    id: output_component1
  - platform: ledc
    pin: GPIO4
    inverted: true
    id: output_component2
  - platform: ledc
    pin: GPIO5
    inverted: true
    id: output_component3

My board is a generic ESP32-C3-MINI-1

I found this thread, as I am trying to sort out if it actually is possible to get the BLE proxy to run on an ESP32-C3 (I have an ESP32-C3 Super Mini).

The config is like:

esphome:
  name: c3-ble-proxy
  friendly_name: c3-ble-proxy
  platformio_options:
    board_build.mcu: esp32c3
    board_build.variant: esp32c3  
  on_boot: #this is to avoid slow startup time because of BT and WiFi "interactions?" , check https://github.com/esphome/issues/issues/2941#issuecomment-1331851692
    priority: 250
    then:
      - delay: 15s
      - lambda: |-
          id(ble_tracker).set_scan_continuous(true); 
          id(ble_tracker).start_scan();

esp32:
  variant: ESP32C3
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
      CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
      CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"    


# Enable logging
logger:
  level: verbose

# Enable Home Assistant API
api:
  encryption:
    key: ":-) :-) :-)"

ota:
  password: ":-) .-) :-)"

button:
  - platform: safe_mode
    name: ${friendly_name} (Safe Mode)


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: "8.5"  # sometimes results in a bootloop without it

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "C3-Ble-Proxy Fallback Hotspot"
    password: "secret"

captive_portal:

esp32_ble_tracker:
  id: ble_tracker
  scan_parameters:
    continuous: false
    active: True

bluetooth_proxy:
  active: true

But the log after boot gives me:

[18:29:34][D][esp32_ble_tracker:266]: Starting scan...
[18:29:34][V][esp32_ble:314]: (BLE) gap_event_handler - 2
[18:29:34][V][esp32_ble:314]: (BLE) gap_event_handler - 7
[18:29:34][V][esp32_ble:314]: (BLE) gap_event_handler - 3
[18:29:34][V][esp32_ble:314]: (BLE) gap_event_handler - 3

Any hints ?

1 Like

What about using wait_until / wifi_connected as shown at https://github.com/damex/esphome/blob/116e5836fd4fb501bb72aad9fa970cd7fb4ce512/bluetooth-proxies/weactstudio-esp32c3-core-bt-proxy.yaml#L15-L22 ? A hardcoded sleep value is bound to be flaky. Pasting here for posterity:

esphome:
  # Rest of config (...)
  on_boot:
    then:
      - wait_until:
          condition:
            wifi.connected:
      - lambda: >
          id(ble_tracker).set_scan_continuous(true);
          id(ble_tracker).start_scan();
1 Like

You code is using the eps-idf framework, the original example is using arduino.

I too am having issues (I think) with the C3 SuperMinis. Did you get this resolved or simply resort to the normal sized ESP32-S?

Trying to decide which to buy next - mode SuperMini or normal.

So far having one BLE proxy on ESP32-C3 SuperMini.
Tried on Arduion & IDF frameworks - both works fine.
To solve issue with WiFi/BLE simultenious working using following:

binary_sensor:
  - platform: template
    id: ble_scan_torun
    internal: true
    entity_category: diagnostic
    device_class: connectivity
    lambda: !lambda |-
      return global_api_server->is_connected();
    on_press:
    - esp32_ble_tracker.start_scan:
        continuous: true
    on_release:
    - esp32_ble_tracker.stop_scan:

It ensure BLE scanning starts and continues while connection from HA is established.
See no idea to run BLE scan when not possible to send data to HA.

Another option is to start BLE scan as soon WiFi is connected:

wifi:
  on_connect:
    - esp32_ble_tracker.start_scan:
        continuous: true
  on_disconnect:
    - esp32_ble_tracker.stop_scan:

@Masterzz oh, mine is a mess compared to that. Here is what is on my C3 superminis that I copied from somewhere here or on the wiki:

substitutions:
  name: esp32-bluetooth-proxy-1c9da0
  friendly_name: Bluetooth Proxy Bathroom
packages:
  esphome.bluetooth-proxy: github://esphome/bluetooth-proxies/esp32-generic/esp32-generic-c3.yaml@main
esphome:
  name: ${name}
  name_add_mac_suffix: false
  friendly_name: ${friendly_name}

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

esp32:
  board: esp32-c3-devkitm-1  # Feel free to use a more specific board if that's what you have
  framework:
    type: esp-idf  # This is important: the default "arduino" framework does not perform well.
    sdkconfig_options:
      # @grigi found in testing that these options resulted in better responsiveness.
      # BLE 4.2 is supported by ALL ESP32 boards that have bluetooth, the original and derivatives.
      CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
      # Also enable this on any derivative boards (S2, C3 etc) but not the original ESP32.
      CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
      # Extend the watchdog timeout, so the device reboots if the device appears locked up for over 10 seconds.
      CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"

logger:
  baud_rate: 0  # 0 Enables logging, but disables serial-port logging to free CPU and memory

ota:
  platform: esphome

api:
  encryption:
    key: redacted
  # Only enable BLE tracking when wifi is up and api is connected
  # Gives single-core ESP32-C3 devices time to manage wifi and authenticate with api
  on_client_connected:
     - esp32_ble_tracker.start_scan:
        continuous: true
  # Disable BLE tracking when there are no api connections live
  on_client_disconnected:
    if:
      condition:
        not:
          api.connected:
      then:
        - esp32_ble_tracker.stop_scan:

esp32_ble_tracker:
  scan_parameters:
    # Don't auto start BLE scanning, we control it in the `api` block's automation.
    continuous: False
    
    active: True  # send scan-request packets to gather more info, like device name for some devices.

    interval: 320ms  # default 320ms - how long to spend on each advert channel
    window:   300ms  # default 30ms - how long to actually "listen" in each interval. Reduce this if device is unstable.
    # If the device cannot keep up or becomes unstable, reduce the "window" setting. This may be
    # required if your device is controlling other sensors or doing PWM for lights etc.

bluetooth_proxy:
  active: true  # allows outbound connections from HA to devices.

sensor:
  - platform: uptime
    # The uptime sensor is extremely helpful to know if your device is rebooting
    # when it shouldn't be. This might indicate your interval-to-window timing is
    # too tight, and the window needs to be reduced.
    name: "Uptime Sensor"
    update_interval: 60s

Hey guys, late to the party of BT tracking.
Want to use the BT devices as a replacement for my ESP8266+DFRobot SEN0395 presence sensors.

This C3: https://de.aliexpress.com/item/1005004740051202.html

My main goal is to create an dynamic HA dashboard for my phones which changes depending in which room you are.

So my question, is the C3-mini reliable for BT/BLE tracking/communication stuff?

Does

At the same time? So you can collect battery data but, having issues?

That statement gives me the impression that your collecting battery data by creating an active connection between the 2 devices which doesn’t make sense to me why someone would do that for BT Beacons when it could more easily transmit the battery level data and be received from just a passive(listening only) setup which is the overwhelmingly most common method.

Also, even if for some freakish reason you cant read both senors at the same time then its really no nig deal if you have to do 1 and the other delayed because theres absolutely no reason to need to be doing frequent sensor updates and just draining the battery even faster when its not necessary. You only need to update sensor values like batteries at the absolute most 10 times a day or arguably far fewer times like 3 times.

What beacons are using and how are you getting the battery data from them? I myself use BlueCharmBeacons and dont have any issues.

The code snippet below works fine if used alone on my ESP32-C3, S or S3 devices. But if I add most of the code to an existing ESPHome ESP32-S or ESP32-S3 already setup with BT Proxy and BLE tracking, the battery levels are not working for me when the active scan script is run. There is no active connection to each fob.

I have not spent any time looking into it as I use a stand-alone ESP32-C3 at the entrance just for beacon battery checks - Home Assistant runs the active scan script once a day to pick up the battery levels.

Using Holy-IOT NRF52810 and iTAG fobs.

esp32_ble_tracker:
  id: ble_tracker_id
  scan_parameters:
    interval: 10s
    window: 400ms
    continuous: false

  on_ble_service_data_advertise:
    - mac_address: xx:xx:xx:xx:xx:xx
      service_uuid: "5242"
      then:
        - lambda: |-
            ESP_LOGD("BLE", "Service Data Received: %s", format_hex_pretty(x).c_str());
            if (x.size() > 1) {
              id(henry_holyiot_beacon_tracker_battery).publish_state(x[1]);
            }

sensor:
  - platform: template
    id: henry_holyiot_beacon_tracker_battery
    name: "Henry HolyIOT Beacon Tracker Battery"
    device_class: "battery"
    unit_of_measurement: "%"
    entity_category: "diagnostic"
    accuracy_decimals: 0

script:
  - id: ble_active_scan
    mode: single
    then:
      - lambda: id(ble_tracker_id).stop_scan();
      - delay: 500ms
      - lambda: |-
          id(ble_tracker_id).set_scan_active(true);
          id(ble_tracker_id).start_scan();
      - delay: 30s  # Enough for beacons to broadcast once
      - lambda: id(ble_tracker_id).stop_scan();
      - delay: 500ms
      - lambda: |-
          id(ble_tracker_id).set_scan_active(false);
          id(ble_tracker_id).start_scan();

button:
  - platform: template
    name: "BLE Active Scan"
    id: run_active_scan
    icon: "mdi:home-automation"
    on_press:
    - script.execute: ble_active_scan

Using Athom PS01 presence sensors now as my BT proxys (just with HA app as beacon) and the work well. They are using an ESP32-C3.

Where is your esphome config for where you’ve created sensors for the devices that correspond with each one’s mac address? For each device that you intend to track, there needs to be a sensor for it as well.

You really need to just pick either Espresence or Bermuda to handle the actual tracking because there aren’t any good native options specifically for Esphome that allows you to determine what devices are in what rooms and their distances. At best all your going to get is either (Home/Away) and or RSSI value which isnt all that helpful by itself.

I use Espresence myself but, Bermuda appears to work pretty good as far as i know. Im still playing around with how i want to display BT trackers/beacons in HA but this is sort of what im thinking, i just need to figire out how to keep everything lined up and to stop shifting…

This is how i used to have them displayed in HA. Just showing as examples if they help you out. If you have any suggestions of your own, id be glad to hear them because creativity/artistic abilities arent my strong area lol.