A Step-by-Step Guide to Configuring ESP32-BLE as a Replacement for Xiaomi Gateway

Smart Home Cost-Saving Strategy Series Part 2 | Easy for Beginners

Introduction: Yesterday, I shared the cost advantages of using ESP32 to replace the Xiaomi Gateway, and many readers are eager to try it out. Today, we will perform the actual operation and configure the first ESP32_ble_tracker step by step!

Last Issue Review: Enthusiastic Feedback from Readers

After releasing the first article yesterday, I received a lot of feedback from readers, which encouraged me greatly:

“I calculated that with 3 temperature and humidity sensors + 2 human body sensors, I can save 180 yuan using the ESP32 solution!” “I have already ordered the ESP32-S3-N16R8 on Xianyu, 35 yuan is really a good deal!” “I am most concerned about the difficulty of configuration; I am a complete beginner, will I be able to learn it?”

Answering the Most Concerned Questions:

  • How difficult is it?: If you have a basic understanding of computers, you can complete it in 1-2 hours by following the tutorial.
  • What do I need to prepare?: ESP32 development board + USB cable + computer.
  • What if I fail?: The article includes a detailed troubleshooting guide.

This article is a complete practical tutorial specifically designed to address these concerns!

Preparation: Hardware and Software List

Hardware Preparation (Essentials)

📱 Core Hardware:
- ESP32 Development Board (Recommended ESP32-S3-N16R8): 35 yuan
- USB Data Cable (Type-C): already have or 10 yuan
- Xiaomi BLE Sensor: existing device

💻 Software Environment:
- Computer (Windows/Mac/Linux is fine)
- Stable internet connection
- Basic command line operation skills

Why Choose ESP32-S3-N16R8?

Advantages of ESP32-S3-N16R8 over the traditional ESP32-DevKitC:

  • Dual-core Processor: Smoother handling of multiple Bluetooth devices.
  • 16MB Large Memory: Supports more sensors simultaneously.
  • Bluetooth 5.0: Stronger signal, more stable connection.
  • Type-C Interface: Universal charging cable, more convenient.
  • Similar Price: Around 35 yuan, very cost-effective.

Purchase Recommendations

Recommended Purchase Channels:

  • Search for “ESP32-S3-N16R8” on Xianyu.
  • Official store on Taobao.
  • JD self-operated (slightly more expensive but guaranteed).

Notes:

  • Select the version with pin headers (for easy wiring).
  • Confirm that the seller provides testing services.
  • Ask if a USB cable is included before purchasing.

Installing ESPHome Environment: The Easiest Method

Method 1: Install via Home Assistant Add-on (Recommended for Beginners)

This is the easiest method, requiring no command line operations:

Step 1: Install ESPHome Add-on

  1. In Home Assistant, go to<span>Settings → Add-ons</span>
  2. Click on “Add-on Store” in the bottom right corner.
  3. Search for “ESPHome”.
  4. Click “Install”.

Step 2: Configure ESPHome Add-on

  1. After installation, click on ESPHome.
  2. Go to the “Configuration” tab.
  3. Confirm the configuration is correct (usually the default configuration is fine).
  4. Click “Start”.

Step 3: Create a New Device

  1. Enter the ESPHome Web interface (click “Open Web UI”).
  2. Click “New Device”.
  3. Enter the device name (e.g., esp32-ble-tracker).
  4. Select device type: ESP32.
  5. Select WiFi network and enter password.

Method 2: Command Line Installation (For Experienced Users)

If you are accustomed to using the command line, you can follow these steps:

Install Python Environment:

# Create a virtual environment (recommended)
python -m venv esphome-env
source esphome-env/bin/activate  # Windows: esphome-env\Scripts\activate
# Install ESPHome
pip install esphome

Verify Installation:

esphome version

If version information is displayed, the installation is successful.

Your First Configuration File: Starting from Scratch

Create Configuration File

Create via HACS:

  1. Open ESPHome in Home Assistant.
  2. Click “New Device”.
  3. Select device type: ESP32.
  4. Enter device name: esp32-ble-tracker.

Create via Command Line:

esphome new esp32-ble-tracker

Basic Configuration File Explanation

Open the generated configuration file; this is the optimal configuration I prepared for you:

# Basic Information
esphome:
  name: esp32-ble-tracker
  platform: ESP32
  board: esp32-s3-devkitc-1
  # Project Information
  project:
    name: "ESP32 BLE Tracker"
    version: "1.0.0"
# WiFi Configuration (please change to your WiFi information)
wifi:
  ssid: "Your WiFi Name"
  password: "Your WiFi Password"
  # Enable Fast Connect
  fast_connect: true
  # Power Management (do not enable power-saving mode to ensure performance)
power_save_mode: none
# Enable Captive Portal (backup solution when WiFi configuration fails)
captive_portal:
# Log Configuration (can be set to DEBUG during debugging)
logger:
  level: INFO
  logs:
    esp32_ble_tracker: DEBUG  # Bluetooth related logs
    api: DEBUG                # API related logs
# OTA Wireless Update (for easy future maintenance)
ota:
  password: "Set an OTA password"
# Bluetooth Tracker Configuration (core functionality)
esp32_ble_tracker:
  # Scan Parameter Optimization
  scan_parameters:
    interval: 300ms  # Scan interval
    window: 150ms    # Scan window
    active: true     # Active scanning
    duration: 5s     # Duration of each scan
  # Device Filter (optional, can scan only specific devices)
  # filters:
  #   - mac_address: "A4:C1:38:XX:XX:XX"
# Web Server (for debugging, can be turned off in production)
web_server:
  port: 80
  auth:
    username: admin
    password: "Set an admin password"
# Time Synchronization (for data timestamps)
time:
  - platform: sntp
    id: sntp_time
# Home Assistant API Integration (recommended, simpler than MQTT)
api:
  encryption:
    key: "Automatically generated encryption key"
  reboot_timeout: 10s  # Disable automatic reboot
# System Information Monitoring
sensor:
  - platform: wifi_signal
    name: "ESP32 WiFi Signal Strength"
    update_interval: 60s
  - platform: uptime
    name: "ESP32 Uptime"

Key Points of Configuration File

Required Fields:

  • <span>name</span>: Device name, recommended to use English.
  • <span>wifi</span>: Network configuration, ensure it is correct.
  • <span>esp32_ble_tracker</span>: Core functionality, must be enabled.

Recommended Configuration:

  • <span>logger</span>: For debugging issues.
  • <span>ota</span>: For easy future wireless updates.
  • <span>api</span>: For communication with Home Assistant.
  • <span>web_server</span>: Very useful during debugging.

Security Settings:

  • Set OTA password.
  • Set web management password.
  • Enable API encryption.

Compiling and Uploading: The Exciting Moment

Preparation Work

  1. Connect ESP32: Connect the ESP32 to the computer using a USB cable.
  2. Identify Port: The system will recognize it as a serial device (COMx for Windows, /dev/ttyUSBx for Mac/Linux).
  3. Enter Download Mode: Press and hold the BOOT button on the ESP32, then press the RESET button.

Compile Firmware

Compile via ESPHome Add-on:

  1. Click “Install” in the ESPHome Web interface.
  2. Select “Install via Serial”.
  3. Select the correct serial port.
  4. Click “Install”.

Command Line Compilation:

# Compile and upload
esphome run esp32-ble-tracker.yaml
# Compile only without uploading
esphome compile esp32-ble-tracker.yaml

Uploading Process

The compilation process takes about 2-5 minutes, and you will see output like this:

[INFO] Reading configuration esp32-ble-tracker.yaml...
[INFO] Generating C++ source...
[INFO] Compiling app...
[INFO] Successfully compiled program.
[INFO] Connecting to esp32-ble-tracker...
[INFO] Uploading binary...
[INFO] Successfully uploaded program.
[INFO] ESPHome runs successfully!

Seeing “Successfully uploaded program” means the upload was successful!

Verify Configuration

After a successful upload, the ESP32 will automatically restart. You can verify it in the following ways:

  1. Check Serial Logs: The device will output startup information.
  2. Check WiFi Connection: The device will connect to the specified WiFi.
  3. Access Web Interface: Use a browser to access the ESP32’s IP address.
  4. Home Assistant Discovery: HA should automatically discover the new device.

Integrating Xiaomi Devices: The Practical Start

First Device: LYWSD03MMC Temperature and Humidity Sensor

This is the most common Xiaomi sensor, let’s start with it:

Step 1: Find the Device MAC Address

Method 1: Via Xiaomi Mi Home App

  1. Open the Mi Home App and find the sensor.
  2. Go to the device details page.
  3. Click the top right “…” → “Device Information”.
  4. Record the MAC address (format: A4:C1:38:XX:XX:XX).

Method 2: Via ESP32 Logs

  1. Enable ESP32 logs:<span>logger: level: DEBUG</span>
  2. Re-upload the configuration.
  3. Check the logs for Xiaomi device broadcasts.
  4. Record the MAC address.

Step 2: Add Sensor Configuration

Add the following content to the configuration file:

# Xiaomi Temperature Sensor
sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:38:XX:XX:XX"  # Replace with actual MAC address
    temperature:
      name: "Living Room Temperature"
      accuracy_decimals: 1  # Keep 1 decimal
      device_class: "temperature"
      unit_of_measurement: "°C"
    humidity:
      name: "Living Room Humidity"
      accuracy_decimals: 1  # Keep 1 decimal
      device_class: "humidity"
      unit_of_measurement: "%"
    battery_level:
      name: "Living Room Sensor Battery"
      device_class: "battery"
      unit_of_measurement: "%"
    battery_voltage:
      name: "Living Room Sensor Voltage"
      unit_of_measurement: "V"
      accuracy_decimals: 2

Step 3: Upload and Test

  1. Save the configuration file.
  2. Recompile and upload:<span>esphome run esp32-ble-tracker.yaml</span>
  3. Wait for sensor data to update (may take a few minutes).
  4. Check the new entity in Home Assistant.

Second Device: RTCGQ02LM Human Body Sensor

# Human Body Sensor
binary_sensor:
  - platform: xiaomi_ble
    mac_address: "50:EC:XX:XX:XX:XX"  # Replace with actual MAC address
    device_class: "motion"
    name: "Living Room Human Body Sensor"

Third Device: MCCGQ02LM Door/Window Sensor

# Door/Window Sensor
binary_sensor:
  - platform: xiaomi_ble
    mac_address: "58:2D:34:XX:XX:XX"  # Replace with actual MAC address
    device_class: "door"
    name: "Front Door Sensor"

Device Discovery Tips

Automatic Discovery Configuration:

# Automatically discover all Xiaomi BLE devices
xiaomi_ble:

With this configuration, the ESP32 will automatically discover nearby Xiaomi devices without needing to manually input MAC addresses.

Troubleshooting Common Issues: Don’t Worry, Solutions Exist

Issue 1: Device Not Discovered

Symptoms: Configuration is correct but no device data is visible.

Solution:

# Increase scan time and frequency
esp32_ble_tracker:
  scan_parameters:
    interval: 200ms  # Reduce scan interval
    window: 100ms    # Reduce scan window
    duration: 10s     # Increase scan time
    active: true      # Ensure active scanning

Other Checks:

  • Ensure the sensor has sufficient battery.
  • Shake or press the sensor to wake the device.
  • Check the distance between the ESP32 and the sensor (recommended to test within 3 meters).

Issue 2: WiFi Connection Failure

Symptoms: ESP32 cannot connect to WiFi.

Solution:

  1. Check WiFi name and password (do not use Chinese WiFi names).
  2. Confirm WiFi signal strength (ESP32 supports 2.4GHz WiFi).
  3. Try restarting the router.
  4. Check if MAC address filtering is enabled.

Issue 3: Compilation Failure

Symptoms: Errors occur during compilation.

Solution:

  1. Check the syntax of the configuration file (YAML indentation is very important).
  2. Confirm that the ESPHome version is the latest.
  3. Try clearing the cache:
    esphome clean esp32-ble-tracker.yaml

Issue 4: Upload Failure

Symptoms: Unable to upload firmware to ESP32.

Solution:

  1. Ensure the USB cable and port are connected properly.
  2. Press the BOOT button to enter download mode.
  3. Try different USB ports or data cables.
  4. Check if the device driver is installed.

🎯 Today’s Practical Task

Now it’s your turn! Please complete the following tasks:

Basic Tasks:

  1. Buy an ESP32-S3-N16R8 development board.
  2. Complete the first configuration according to the tutorial.
  3. Integrate the first Xiaomi sensor.
  4. Take a screenshot and share your successful interface.

Advanced Tasks:

  1. Integrate more than 2 sensors.
  2. Configure the web server for debugging.
  3. Try modifying scan parameters to optimize performance.
  4. Create your first automation in Home Assistant.

Sharing Requirements: Share in the comments:

  • Your configuration screenshot.
  • Problems encountered and solutions.
  • List of successfully integrated devices.
  • Any questions you want to ask.

📝 Problem Collection

What problems did you encounter during the configuration process? Leave a message in the comments, and I will specifically answer the difficult issues everyone encounters in the next issue!

Common Problem Categories:

  • Configuration file syntax errors.
  • Device connection issues.
  • Performance optimization needs.
  • Implementation of advanced features.

🔔 Next Issue Preview

Part 3: “Advanced Guide to Replacing Xiaomi Gateway with ESP32”

Will delve into:

  • Complete troubleshooting guide.
  • Performance optimization and debugging tips.
  • Advanced configuration and security hardening.
  • Home Assistant automation practice.
  • Multi-device coordination and expansion solutions.

Preparation Items:

  • Summarize the problems encountered this week.
  • Think about the advanced features you want to implement.
  • Prepare to share your usage experience.

Series Navigation:

  • Part 1: Introduction (Published)
  • Part 2: Practical (This Article)
  • Part 3: Advanced (Coming Soon)

Article Statistics: Approximately 2400 words, reading time about 10 minutes.

Core Value: A step-by-step guide to completing ESP32 configuration, integrating Xiaomi sensors into Home Assistant, saving over 50% on hardware costs.

Technical Support: If you encounter problems, please leave a message in the comments, and I will answer them one by one. You can also join the technical exchange group to discuss with other enthusiasts.

Leave a Comment