Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESPHome as an ble gateway #34

Open
plesetsk opened this issue Nov 23, 2023 · 11 comments
Open

ESPHome as an ble gateway #34

plesetsk opened this issue Nov 23, 2023 · 11 comments
Labels
bluetooth Poweron using bluetooth enhancement New feature or request

Comments

@plesetsk
Copy link

Hi,

thanks for you code.

Since my HA installed on Hyper-v I have no access to bluetooth on the host.
I am trying to use ESPHome as a gateway for command but it seems that I have to configure it in the way to send ble advertise message.
I do not know how to do it.
May be some one can help me on this.
Thanks.

@manymuch
Copy link
Owner

I believe it is possible now, and you can refer to here and here.
It may require some serious coding to get it work, but I don't have any esp device at hand currently.

@plesetsk
Copy link
Author

plesetsk commented Nov 24, 2023

Hi @manymuch
Thanks.

Here is my solution for ESPHome

esp32_ble:
  id: ble 

esp32_ble_server:

button:
  - platform: template
    name: Xgimi_ON
    id: xgimi_on

    # Optional variables:
    icon: "mdi:projector"
    on_press:
      then:
        - lambda: |-

            uint8_t manufacturer_data[] = {0x46, 0x00, 0x0f, 0x26, 0xc2, 0xdf, 0x3a, 0xef, 0x18, 0xff, 0xff, 0xff, 0x30, 0x43, 0x52, 0x4b, 0x54, 0x4d};
            uint8_t manufacturer_data_rem[] = {0x46, 0x00};
            // Convert manufacturer_data_set to std::vector<uint8_t>
            std::vector<uint8_t> manufacturer_data_vector(manufacturer_data, manufacturer_data + sizeof(manufacturer_data));

            id(ble).advertising_set_manufacturer_data(manufacturer_data_vector);
                 
            id(ble).advertising_start();
            delay(5000);
            std::vector<uint8_t> manufacturer_data_vector_rem(manufacturer_data_rem, manufacturer_data_rem + sizeof(manufacturer_data_rem));

            id(ble).advertising_set_manufacturer_data(manufacturer_data_vector_rem);
         
          
            id(ble).advertising_start();

@helgek
Copy link

helgek commented Nov 24, 2023

Looking forward to test this once implemented. My experience with bluetooth stick wasn't good but since I set up an ESPHome bluetooth proxy device I already had good experience with the HA SwitchBot integration. If the bluetooth proxy device could be used for turning my projector on as well this would be really cool. Thanks both!

@helgek
Copy link

helgek commented Nov 24, 2023

@wxyj1234
Copy link

你好@manymuch 谢谢。

这是我的 ESPHome 解决方案

manufacturer_data_vector ???

@manymuch manymuch added enhancement New feature or request bluetooth Poweron using bluetooth labels Feb 3, 2024
@Drafteed
Copy link

Drafteed commented Feb 4, 2024

@plesetsk Thanks for the solution. I slightly changed the snippet so that it does not block ESPHome due to delay in the lambda function, which causes the system to completely freeze. I also added a few iterations of how the original remote does it. Now the code works fine, even at a very large distance from the projector.

esp32:
  framework:
    type: esp-idf

esp32_ble:
  id: ble 

esp32_ble_server:

button:
  - platform: template
    name: XGIMI Projector Wakeup
    icon: "mdi:projector"
    on_press:
      - repeat:
          count: 3
          then:
            - lambda: |-
                std::vector<uint8_t> data = {0x46, 0x00, 0xe7, 0x12, 0x97, 0x30, 0x35, 0xf2, 0x78, 0xff, 0xff, 0xff, 0x30, 0x43, 0x52, 0x4b, 0x54, 0x4d};
                id(ble).advertising_set_manufacturer_data(data);
            - delay: 1s
            - lambda: |-
                std::vector<uint8_t> data = {0x46, 0x00};
                id(ble).advertising_set_manufacturer_data(data);
            - delay: 1s

@plesetsk
Copy link
Author

plesetsk commented Feb 4, 2024

@Drafteed I have just tested your code in my environment and it works perfect. Thanks!

@helgek
Copy link

helgek commented Feb 4, 2024

Thank you! Do I understand it right that this code requires a dedicated ESP32 device which only serves for this command? The SwitchBot integration uses any ESP32 Bluetooth Proxy device set up in ESPHome and works for various BLE commands, so it acts like a bluetooth stick which is not limited to one command. I was just wondering if the advertising command could also be sent through the ESP32 Bluetooth Proxy device? Then the manufacturer code set in the XGIMI remote configuration could be used and no additional ESP32 device would be required.

@Drafteed
Copy link

Drafteed commented Feb 5, 2024

Thank you! Do I understand it right that this code requires a dedicated ESP32 device which only serves for this command?

I don't know if it's possible to send advertising command through bluetooth_proxy, but you can use command to wake up XGIMI with bluetooth_proxy together on one ESP32 device without any problems:

esp32:
  framework:
    type: esp-idf

# Enable BLE
esp32_ble:
  id: ble 

# Enable BLE Server
esp32_ble_server:

# Enable Bluetooth Proxy
bluetooth_proxy:

button:
  - platform: template
    name: XGIMI Projector Wakeup
    icon: "mdi:projector"
    on_press:
      - repeat:
          count: 3
          then:
            - lambda: |-
                std::vector<uint8_t> data = {0x46, 0x00, 0xe7, 0x12, 0x97, 0x30, 0x35, 0xf2, 0x78, 0xff, 0xff, 0xff, 0x30, 0x43, 0x52, 0x4b, 0x54, 0x4d};
                id(ble).advertising_set_manufacturer_data(data);
            - delay: 1s
            - lambda: |-
                std::vector<uint8_t> data = {0x46, 0x00};
                id(ble).advertising_set_manufacturer_data(data);
            - delay: 1s

@helgek
Copy link

helgek commented Feb 6, 2024

@Drafteed Thank you for the great hint! It works indeed, I have now a working BT Proxy and the working wakeup command running on the same ESP device. Thank you again!

@helgek
Copy link

helgek commented Feb 6, 2024

It seems that instead of hard coding the advertising data it could also be provided e.g. by the XGIMI Remote integration, e.g.: https://community.home-assistant.io/t/how-to-pass-a-variable-from-home-assistant-front-end-to-esphome/183845/23 and https://schinckel.net/2021/01/24/esphome-variables-from-home-assistant/

I will certainly further look into this myself later, just wanted to also share this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bluetooth Poweron using bluetooth enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants