该蓝牙通用网关与Passive BLE Monitor 集成搭配使用.
从而解决因为蓝牙穿墙能力弱导致蓝牙信号无法穿墙的问题,蓝牙通用网关的主要用途有:
- 蓝牙信号过不来的就走开源ESPHome通用蓝牙网关,通过wifi信号再到HA上的 Passive BLE Monitor 集成.
- 接入只支持Active BLE connection的蓝牙设备
关于蓝牙设备的相关介绍,请参阅: HA智能硬件采购避坑指要(三) 蓝牙篇
硬件很简单,你只需要自备ESP32的主板一枚,建议最好选4M Flash的, 虽然2M的也能刷,但是稍微麻烦些(而且因为闪存小无法支持OTA了). 我选的是ESP-C3-32S(4M)
的开发板. 可在某陶上搜索"ESP-C3-32S-Kit"记得问清楚是否4M(2M和4M只差几毛钱).
ESP32-C3
是Espressif新出的RISC-V 32位单核处理器(160MHz)支持蓝牙5.0(BLE支持),以前的ESP32是基于蓝牙4.2.
然后准备好ESPHome开发环境,请升级到ESPHome的最新版(至少2022.1版本以后),请不要在arm环境下开发,因为Espressif
目前还不支持ESP-C3
在arm下的编译.
在Linux环境下安装使用很简单:
sudo apt -y install python3 python3-pip python3-pip-whl
sudo -H pip3 install --upgrade pip
pip3 install --user esphome
# To set this permanently, you can run echo 'export PATH=$PATHHOME/.local/bin' >> $HOME/.bashrc
export PATH=$PATHHOME/.local/bin
# You may need to logout and back in for the new group to take effect.
sudo usermod -a -G dialout $USER
好了,刷机的YAMLbluetooth-gateway.yaml
配置如下(如果板子不是ESP32-C3
请自行调整board参数),记得在相同目录下的secrets.yaml
文件中填好参数:
substitutions:
# Name the device and it's entities
device: ble_gateway
device_name: blegateway1
esphome:
name: $device_name
comment: $device
platformio_options:
board_build.flash_mode: dio
esp32:
board: esp32-c3-devkitm-1
variant: esp32c3
framework:
type: arduino
version: 2.0.2
platform_version: https://github.com/tasmota/platform-espressif32/releases/download/v2.0.2.3/platform-espressif32-2.0.2.3.zip
external_components:
- source: github://myhomeiot/esphome-components
esp32_ble_tracker:
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 1h
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot in case wifi connection fails
ap:
ssid: "$device_name Fallback Hotspot"
password: !secret ap_password
ble_gateway:
id: $device_name
# devices:
# - mac_address: 01:23:45:67:89:AB
# - mac_address: !secret lywsd03mmc_mac
on_ble_advertise:
then:
- homeassistant.service:
service: ble_monitor.parse_data
data:
packet: !lambda return packet;
gateway_id: $device_name
- homeassistant.event:
event: esphome.on_ble_advertise
data:
packet: !lambda return packet;
binary_sensor:
- platform: homeassistant
id: ble_gateway_discovery
entity_id: binary_sensor.ble_gateway
attribute: discovery
on_state:
then:
lambda: id($device_name).set_discovery(x);
text_sensor:
- platform: homeassistant
id: ble_gateway_devices
entity_id: binary_sensor.ble_gateway
attribute: devices
on_value:
then:
lambda: id($device_name).set_devices(x);
# IP address of device. Not really needed for HA (as HA already knows it), but for showing on the display during startup. The startup screen will leave on if no instance connects to the API.
- platform: wifi_info
ip_address:
name: $device_name IP address
id: ip_address
# ESPHome version used to compile the app
- platform: version
name: $device_name ESPHome Version
sensor:
# WiFi signals strength sensor
- platform: wifi_signal
name: $device_name WiFi Signal Sensor
update_interval: 60s
USB插上ESP32开发板,看清楚串口号(ls /dev/tty*
),一般是/dev/ttyUSB0
,然后执行如下语句编译烧录固件:
# choose the port after compiling
esphome run bluetooth-gateway.yaml
至此,通用蓝牙网关的硬件烧录部分完成.
接着,还要在HA上配置与Passive BLE Monitor 集成的搭配部分.
修改HA configuration.yaml
文件:
input_boolean:
ha_started:
initial: false
settings_ble_gateway:
name: BLE Gateway
icon: mdi:bluetooth
settings_ble_gateway_discovery:
name: BLE Gateway Discovery
icon: mdi:bluetooth-connect
input_text:
settings_ble_gateway_add_device:
name: BLE Gateway Add Device
icon: mdi:bluetooth-connect
initial: ''
template:
- binary_sensor:
- name: BLE Gateway Add Device
state: "{{ (state_attr('binary_sensor.ble_gateway_add_device', 'mac_address')) }}"
availability: "{{ is_state('input_boolean.ha_started', 'on') }}"
attributes:
mac_address: "{{ states('input_text.settings_ble_gateway_add_device') | replace(':', '') | trim }}"
- binary_sensor:
- name: BLE Gateway
icon: mdi:bluetooth
state: "{{ is_state('input_boolean.settings_ble_gateway', 'on') }}"
availability: "{{ is_state('input_boolean.ha_started', 'on') }}"
attributes:
discovery: "{{ is_state('input_boolean.settings_ble_gateway_discovery', 'on') }}"
devices: "{{ states | selectattr('entity_id', 'search', '^(device_tracker||(binary_)?sensor).ble_') | selectattr('attributes.mac_address', 'defined') | map(attribute='attributes.mac_address') | unique | sort | join('') | replace(':', '') if is_state('binary_sensor.ble_gateway', 'on') }}"
然后修改automations.yaml
文件:
- alias: HA Start automation
initial_state: true
trigger:
platform: homeassistant
event: start
action:
service: input_boolean.turn_on
entity_id: input_boolean.ha_started
id: ha_start
重启HA即可. 对了记得把刚刚新鲜出炉的蓝牙网关加入进来.
然后该蓝牙网关就会自动转发已发现的并且在Passive BLE Monitor 集成中设备.
如果要添加新的蓝牙设备请在Passive BLE Monitor 集成上添加.
如果想只针对个别设备在指定的蓝牙网关上转发,那么请使用input_text.settings_ble_gateway_add_device
添加该设备的MAC地址.
蓝牙网关接收到已注册设备的广播后会同时触发事件esphome.on_ble_advertise
发送原始数据.
最后,记得设置input_boolean.settings_ble_gateway
为"on",启用蓝牙网关功能.
设置input_boolean.settings_ble_gateway_discovery
为"on",启用自动发现功能.
ESPHome 蓝牙网关的日志查看:
- 在 PC 上通过直接连接的USB:
esphome logs bluegateway.yaml --device /dev/ttyUSB0
- PC 远程查看:
esphome logs bluegateway.yaml
选择远程
- HA上远程查看: 启用 ESPHome addon, 建一个同名的空配置,点
LOGS
.
版本更新
2022-5-9: [Bug] esp-idf 下无法更改 scan_parameters, 使用 Arduino framework now.
2022-5-21: [feat] 新增 启用自动发现开关