wifwucite / esphome-ble-controller

Bluetooth Low Energy (BLE) controller for ESPHome
MIT License
114 stars 23 forks source link

How to use with ble_client? #15

Closed evlo closed 1 year ago

evlo commented 1 year ago

This is not an issue, but I'm trying to figure out a way, how to use this to directly communicate between two ESP32s (toggle a light directly without wifi)

Form this https://github.com/wifwucite/esphome-ble-controller/issues/4 it seems possible

server exposes switch my goal is when client calls the switch service it would toggle the light, however I do get nothing in the log on server. I do NOT even get message Cannot write to BLE characteristic - not connected on client if the server is not powered on.

wifwucite commented 1 year ago

Hmm, I have not worked with the BLE client yet. But have you tried to connect to your server ESP32 via tablet/phone and made sure that it is working in principle? Could you also paste the logs of both ESP32s? But I agree this is a valid use case and we should get it working. And then I should describe it in the Readme.

evlo commented 1 year ago

when i tried connect from the phone i do get https://user-images.githubusercontent.com/8813196/229642024-af6989ec-225e-4e8d-b6c0-7c87ce39e671.png

and i do get message that server is connected in the log but if it try to send value as byte array 0001 or byte 01 esp does restart - that is captured in the log bellow it is weird - both are logs for the light/server, bot one on the right is over usb - i don't get at all why that is - probably esphome bug/feature image

It might also by that esp32-s3 does have different Bluetooth then esp32, but i do not think I have any working plain esp32 boards

I will also try doing it in a way that switch publishes sensor that indicates click, so in that case I should be able to see change on the phone

On the client esp32 I get pretty much nothing

[21:22:47][I][app:102]: ESPHome version 2023.3.2 compiled on Apr  3 2023, 21:22:07
[21:22:47][C][wifi:504]: WiFi:
[21:22:47][C][wifi:362]:   Local MAC: F4:12:FA:61:5C:58
[21:22:47][C][wifi:363]:   SSID: 'v2'
[21:22:47][C][wifi:364]:   IP Address: 192.168.50.154
[21:22:47][C][wifi:366]:   BSSID: 24:4B:FE:72:07:78
[21:22:47][C][wifi:367]:   Hostname: 'demo-eh-switch-ble'
[21:22:47][C][wifi:369]:   Signal strength: -63 dB ▂▄▆█
[21:22:47][C][wifi:373]:   Channel: 11
[21:22:47][C][wifi:374]:   Subnet: 255.255.255.0
[21:22:47][C][wifi:375]:   Gateway: 192.168.50.1
[21:22:47][C][wifi:376]:   DNS1: 192.168.50.1
[21:22:47][C][wifi:377]:   DNS2: 0.0.0.0
[21:22:47][C][logger:293]: Logger:
[21:22:47][C][logger:294]:   Level: VERY_VERBOSE
[21:22:47][C][logger:295]:   Log Baud Rate: 0
[21:22:47][C][logger:296]:   Hardware UART: UART0
[21:22:47][C][logger:298]:   Level for 'wifi_now': VERY_VERBOSE
[21:22:47][C][logger:298]:   Level for 'wifi_now.component': VERBOSE
[21:22:47][C][logger:298]:   Level for 'api': DEBUG
[21:22:47][C][logger:298]:   Level for 'api.service': DEBUG
[21:22:47][C][logger:298]:   Level for 'wifi': DEBUG
[21:22:47][C][logger:298]:   Level for 'web_server': WARN
[21:22:47][C][logger:298]:   Level for 'logger': VERY_VERBOSE
[21:22:47][C][logger:298]:   Level for 'light': VERBOSE
[21:22:47][C][logger:298]:   Level for 'scheduler': DEBUG
[21:22:47][C][logger:298]:   Level for 'light.addressable': DEBUG
[21:22:47][C][logger:298]:   Level for 'mdns': DEBUG
[21:22:48][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'button0'
[21:22:48][C][gpio.binary_sensor:016]:   Pin: GPIO41
[21:22:48][C][light:104]: Light 'demo-eh-switch-ble RGB LED'
[21:22:48][C][light:106]:   Default Transition Length: 1.0s
[21:22:48][C][light:107]:   Gamma Correct: 2.80
[21:22:48][C][esp32_ble:214]: ESP32 BLE:
[21:22:48][C][ble_client:027]: BLE Client:
[21:22:48][C][ble_client:028]:   Address: F4:12:FA:61:5B:F4
[21:22:48][C][esp32_ble_tracker:591]: BLE Tracker:
[21:22:48][C][esp32_ble_tracker:592]:   Scan Duration: 300 s
[21:22:48][C][esp32_ble_tracker:593]:   Scan Interval: 320.0 ms
[21:22:48][C][esp32_ble_tracker:594]:   Scan Window: 30.0 ms
[21:22:48][C][esp32_ble_tracker:595]:   Scan Type: ACTIVE
[21:22:48][C][esp32_ble_tracker:596]:   Continuous Scanning: True
[21:22:48][C][mdns:108]: mDNS:
[21:22:48][C][mdns:109]:   Hostname: demo-eh-switch-ble
[21:22:48][C][ota:093]: Over-The-Air Updates:
[21:22:48][C][ota:094]:   Address: demo-eh-switch-ble.local:3232
[21:22:48][C][api:138]: API Server:
[21:22:48][C][api:139]:   Address: demo-eh-switch-ble.local:6053
[21:22:48][C][api:143]:   Using noise encryption: NO
[21:23:16][D][binary_sensor:036]: 'button0': Sending state OFF
[21:23:16][D][binary_sensor:036]: 'button0': Sending state ON
[21:23:30][D][binary_sensor:036]: 'button0': Sending state OFF
[21:23:30][D][binary_sensor:036]: 'button0': Sending state ON


btw with

logger:
  baud_rate: 115200
  deassert_rts_dtr: true
  level: DEBUG

I get nothing in the log over usb, but in the one OTA I do get messages on connecting/disconnecting from phone




client (light): https://pastebin.com/k3FCaUc7 server (switch) https://pastebin.com/Sr0qH8pv

log on client:

[01:34:40][C][wifi:504]: WiFi:
[01:34:40][C][wifi:362]:   Local MAC: F4:12:FA:61:5B:F4
[01:34:40][C][wifi:363]:   SSID: 'v2'
[01:34:40][C][wifi:364]:   IP Address: 192.168.50.50
[01:34:40][C][wifi:366]:   BSSID: 24:4B:FE:72:07:78
[01:34:40][C][wifi:367]:   Hostname: 'demo-eh-light-ble'
[01:34:40][C][wifi:369]:   Signal strength: -55 dB ▂▄▆█
[01:34:40][C][wifi:373]:   Channel: 11
[01:34:40][C][wifi:374]:   Subnet: 255.255.255.0
[01:34:40][C][wifi:375]:   Gateway: 192.168.50.1
[01:34:40][C][wifi:376]:   DNS1: 192.168.50.1
[01:34:40][C][wifi:377]:   DNS2: 0.0.0.0
[01:34:40][C][logger:293]: Logger:
[01:34:40][C][logger:294]:   Level: DEBUG
[01:34:40][C][logger:295]:   Log Baud Rate: 115200
[01:34:40][C][logger:296]:   Hardware UART: UART0
[01:34:40][C][template.switch:076]: Template Switch 'light_virtual_switch'
[01:34:40][C][template.switch:099]:   Restore Mode: restore defaults to OFF
[01:34:40][C][template.switch:060]:   Restore State: NO
[01:34:40][C][template.switch:061]:   Optimistic: NO
[01:34:40][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'switch_onboard_btn'
[01:34:40][C][gpio.binary_sensor:016]:   Pin: GPIO41
[01:34:40][C][light:104]: Light 'demo-eh-light-ble RGB LED'
[01:34:40][C][light:106]:   Default Transition Length: 1.0s
[01:34:40][C][light:107]:   Gamma Correct: 2.80
[01:34:40][C][ble_sensor:017]: BLE Sensor 'switch_switch_state'
[01:34:40][C][ble_sensor:017]:   State Class: ''
[01:34:40][C][ble_sensor:017]:   Unit of Measurement: ''
[01:34:40][C][ble_sensor:017]:   Accuracy Decimals: 0
[01:34:40][C][ble_sensor:018]:   MAC address        : F4:12:FA:61:5C:58
[01:34:40][C][ble_sensor:019]:   Service UUID       : 2DC01D40-D24D-11ED-AFA1-0242AC120002
[01:34:40][C][ble_sensor:020]:   Characteristic UUID: 5289E9EB-BC59-423E-916E-18012FE6DE1A
[01:34:40][C][ble_sensor:021]:   Descriptor UUID    : 00000000-0000-0000-0000-000000000000
[01:34:40][C][ble_sensor:022]:   Notifications      : YES
[01:34:40][C][ble_sensor:023]:   Update Interval: 60.0s
[01:34:40][C][esp32_ble:214]: ESP32 BLE:
[01:34:40][C][ble_client:027]: BLE Client:
[01:34:40][C][ble_client:028]:   Address: F4:12:FA:61:5C:58
[01:34:40][C][esp32_ble_tracker:591]: BLE Tracker:
[01:34:40][C][esp32_ble_tracker:592]:   Scan Duration: 300 s
[01:34:40][C][esp32_ble_tracker:593]:   Scan Interval: 320.0 ms
[01:34:40][C][esp32_ble_tracker:594]:   Scan Window: 30.0 ms
[01:34:40][C][esp32_ble_tracker:595]:   Scan Type: ACTIVE
[01:34:40][C][esp32_ble_tracker:596]:   Continuous Scanning: True
[01:34:40][C][web_server:151]: Web Server:
[01:34:40][C][web_server:152]:   Address: demo-eh-light-ble.local:80
[01:34:40][C][mdns:108]: mDNS:
[01:34:40][C][mdns:109]:   Hostname: demo-eh-light-ble
[01:34:40][C][ota:093]: Over-The-Air Updates:
[01:34:40][C][ota:094]:   Address: demo-eh-light-ble.local:3232
[01:34:40][C][api:138]: API Server:
[01:34:40][C][api:139]:   Address: demo-eh-light-ble.local:6053
[01:34:40][C][api:143]:   Using noise encryption: NO
[01:35:14][W][ble_sensor:117]: [switch_switch_state] Cannot poll, not connected
[01:36:14][W][ble_sensor:117]: [switch_switch_state] Cannot poll, not connected
[01:37:14][W][ble_sensor:117]: [switch_switch_state] Cannot poll, not connected
[01:38:14][W][ble_sensor:117]: [switch_switch_state] Cannot poll, not connected

log on server:

[01:42:54][I][app:102]: ESPHome version 2023.3.2 compiled on Apr  4 2023, 01:42:08
[01:42:54][C][wifi:504]: WiFi:
[01:42:54][C][wifi:362]:   Local MAC: F4:12:FA:61:5C:58
[01:42:54][C][wifi:363]:   SSID: 'v2'
[01:42:54][C][wifi:364]:   IP Address: 192.168.50.154
[01:42:54][C][wifi:366]:   BSSID: 24:4B:FE:72:07:78
[01:42:54][C][wifi:367]:   Hostname: 'demo-eh-switch-ble'
[01:42:54][C][wifi:369]:   Signal strength: -60 dB ▂▄▆█
[01:42:54][C][wifi:373]:   Channel: 11
[01:42:54][C][wifi:374]:   Subnet: 255.255.255.0
[01:42:54][C][wifi:375]:   Gateway: 192.168.50.1
[01:42:54][C][wifi:376]:   DNS1: 192.168.50.1
[01:42:54][C][wifi:377]:   DNS2: 0.0.0.0
[01:42:54][C][logger:293]: Logger:
[01:42:54][C][logger:294]:   Level: DEBUG
[01:42:54][C][logger:295]:   Log Baud Rate: 115200
[01:42:54][C][logger:296]:   Hardware UART: UART0
[01:42:54][C][template.switch:076]: Template Switch 'switch_virtual_switch'
[01:42:54][C][template.switch:099]:   Restore Mode: restore defaults to OFF
[01:42:54][C][template.switch:060]:   Restore State: NO
[01:42:54][C][template.switch:061]:   Optimistic: NO
[01:42:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'switch_onboard_btn'
[01:42:54][C][gpio.binary_sensor:016]:   Pin: GPIO41
[01:42:54][C][light:104]: Light 'demo-eh-switch-ble RGB LED'
[01:42:54][C][light:106]:   Default Transition Length: 1.0s
[01:42:54][C][light:107]:   Gamma Correct: 2.80
[01:42:54][C][esp32_ble_controller:318]: Bluetooth Low Energy Controller:
[01:42:54][C][esp32_ble_controller:319]:   BLE device address: f4:12:fa:61:5c:59
[01:42:54][C][esp32_ble_controller:320]:   BLE mode: 3
[01:42:54][C][esp32_ble_controller:340]:   security disabled
[01:42:54][C][web_server:156]: Web Server:
[01:42:54][C][web_server:157]:   Address: demo-eh-switch-ble.local:80
[01:42:54][C][mdns:108]: mDNS:
[01:42:54][C][mdns:109]:   Hostname: demo-eh-switch-ble
[01:42:54][C][ota:093]: Over-The-Air Updates:
[01:42:54][C][ota:094]:   Address: demo-eh-switch-ble.local:3232
[01:42:54][C][api:138]: API Server:
[01:42:54][C][api:139]:   Address: demo-eh-switch-ble.local:6053
[01:42:54][C][api:143]:   Using noise encryption: NO
[01:42:55][D][binary_sensor:036]: 'switch_onboard_btn': Sending state ON
[01:42:55][D][binary_sensor:036]: 'switch_onboard_btn': Sending state OFF
[01:42:55][D][main:307]: switch clicked
[01:42:55][D][light:035]: 'demo-eh-switch-ble RGB LED' Setting:
[01:42:55][D][light:046]:   State: OFF
[01:42:55][D][light:084]:   Transition length: 1.0s
[01:42:55][D][switch:021]: 'switch_virtual_switch' Toggling ON.
[01:42:55][D][main:354]: Switch Virtual Switch ON

from phone on server:

[01:46:15][D][esp32_ble_controller:491]: BLE server - connected
[01:46:15][D][main:411]: I am connected.

https://user-images.githubusercontent.com/8813196/229651223-94baa89f-1cfc-4561-a3db-03036cf84e4e.png

since I can connect and read sensor values from phone your component is verified working, but my issue is with connecting to it from esphome ble

evlo commented 1 year ago

I just noticed this

[02:20:01][C][wifi:504]: WiFi:
[02:20:01][C][wifi:362]:   Local MAC: F4:12:FA:61:5B:F4
[02:20:01][C][esp32_ble_controller:318]: Bluetooth Low Energy Controller:
[02:20:01][C][esp32_ble_controller:319]:   BLE device address: f4:12:fa:61:5b:f5

wifi mac is F4, but bluetooth is F5

It works - even the scenario where the light is server and switch sends it notifications, also it works pretty much instantly.

wifwucite commented 1 year ago

I am glad that it is working. I feel I should really add a short tutorial about this use case to the Readme. And maybe I should think about exposing a light directly, so you do not have to use a switch. But I have to get familiar with my own code again. ;-)

evlo commented 1 year ago

I have created PR with the example configs added to the readme, at least maybe that cam help a bit