Make a ESP32 with a dallas temp sensor conect via Bluetooth to proxy

Hey, did you find the answer, Im also not past this

id(ble).get_advertising()->set_manufacturer_data

error: ‘class esphome::esp32_ble::ESP32BLE’ has no member named ‘get_advertising’; did you mean ‘advertising_’?

I installed a new access point to connect via Wi-Fi but I still have one device behind some concrete in a metal box… so I am still interested in resolving this but I haven’t spent any time on it. I will revisit it so if anyone has solve / came up with a solution I am all ears.

Blacky :smiley:

Im following another person’s idea which is working partially, I just need some help decoding the message. Im stuck on getting the proper conversion of Hex to Decimal. On this example you don’t need to include the .h file.

On the ep32 server I have this config:

esp32_ble_server:
  id: ble
  manufacturer_data: [0xFF, 0xFF, 0xF1, 0x00]

time:
  - platform: sntp
    id: ntp  
    on_time:
    - seconds: 0,10
      then:
        - lambda: |-
            ESP_LOGD("main", "Advertisement updated");           
            float f_value = id(sensor_data).state;
            uint8_t *float_data = (uint8_t*)&f_value;
            ESP_LOGD("main", "Sending updated Level %f", id(sensor_data).state );
            id(ble).set_manufacturer_data({ 0xFF, 0xFF, 0xF9, float_data[0], float_data[1], float_data[2], float_data[3]});

Then, on the Client side I have this setting:

 on_ble_manufacturer_data_advertise:
    - mac_address: #your MAC address
      manufacturer_id: FFFF
      then:
        - lambda: |-
            int value = x[1] + (x[2] << 8) + (x[3] << 16) + (x[3] << 24);
            id(ble_sensor).publish_state(value);

The client Log showed that with wrong value. Im not sure how to convert Hex to Decimal properly.

[17:10:14][VV][esp32_ble_tracker:440]:   Address: 68:B6:B3:AF:9A:22 (PUBLIC)
[17:10:14][VV][esp32_ble_tracker:442]:   RSSI: -60
[17:10:14][VV][esp32_ble_tracker:443]:   Name: ''
[17:10:14][VV][esp32_ble_tracker:451]:   Ad Flag: 6
[17:10:14][VV][esp32_ble_tracker:457]:   Manufacturer data: F9.74.A5.F7.42 (5)
[17:10:14][VV][esp32_ble_tracker:473]: Adv data: 02.01.06.08.FF.FF.FF.F9.74.A5.F7.42 (12)
[17:10:14][V][sensor:043]: 'Test Voltage': Received new state -134765200.000000
[17:10:14][D][sensor:094]: 'Test Voltage': Sending state -134765200.00000 V with 0 decimals of accuracy
[17:10:14][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
  key: 3719805054
  state: -1.34765e+08
  missing_state: NO
}

I need the lambda to decode this Hex data where the last 4 bytes is the sensor_data → Manufacturer data: F9.74.A5.F7.42 (5)

@dsaul

Thanks for sharing the data of what you got so far :+1:, you got me going again on this… I have changed a few things that could help you and it could keep the conversation flowing. Please note, I am not good at Bluetooth… I know just enough to get me into trouble. I found this to report the correct data.

esp32_ble_tracker:
  scan_parameters:
#    interval: 1100ms
#    window: 1100ms
    active: true

  on_ble_manufacturer_data_advertise:
    - mac_address: #your MAC address
      manufacturer_id: FFFF
      then:
        - lambda: |-
            float value = *((float*)&x[1]);
            id(temp_sensor).publish_state(value);

sensor:
  - platform: template
    name: "BLE Sensor"
    id: temp_sensor

Even though that is working and I can get that Bluetooth data into HA and I am still working on getting it right. I would just like HA to see the ESP32 and adopt it in like all other Bluetooth devices… be discoverable so you dont have to use “on_ble_manufacturer_data_advertise”

This is my ESP32 code. It report every 60 sec


dallas:
  - pin: 32

sensor:
  - platform: dallas
    address: # Enter your address here
    name: "Temp Sensor"
    id: temp_sensor

esp32_ble_server:
  id: ble
  manufacturer_data: [0xFF, 0xFF, 0xF1, 0x00]


time:
  - platform: sntp
    id: ntp

    on_time:
      - seconds: "/60"
        then:
          - lambda: |-
              ESP_LOGD("main", "Sending advertisement...");
              float current_value = id(temp_sensor).state;
              uint8_t *float_data = (uint8_t*)&current_value;
              ESP_LOGD("main", "Sending updated Level %f", current_value);
              id(ble).set_manufacturer_data({ 0xFF, 0xFF, 0xF9, float_data[0], float_data[1], float_data[2], float_data[3]});

I am still working on it. As I get more info I will post it here.

If anyone knows how to just get the esp32 to talk directly to HA where HA will just discover the ESP32 device then please jump in.

It needs to be just a Bluetooth connection no Wi-Fi and flashed via cable to the ESP32. Like all other Bluetooth devices.

Blacky :smiley:

1 Like

That worked for me! Thanks a lot!

Have you tried Passive BLE Monitor, I use that instead of Bluetooth Proxy because have some BLE Xiaomi buttons that doesn’t work with it yet. But, the Passive BLE monitor can pick up DIY BLE devices configured using BTHome. As matter effect I think BT Home can be picked up directly by Home Assistant / Bluetooth Proxy. Maybe you can learn how to advertise a DIY BTHome BLE device and teach us. :slight_smile:

If your sending your sensor data over BT then just disable the esp32 wifi. You will probably need a template switch or binary sensor on the esp32 that will call, wifi.disable or wifi.enable from HA. You would still want the option to turn on wifi for troubleshooting or updating and then keep it off the rest of the time. Without a switch or sensor to toggle the state, your only options to enable the wifi are physically restarting the esp32, i believe.

To my knowledge though, the HA BT integration only works as a passive BT scanner so, you wont be able to toggle the wifi over BT as far as I know. You might have to get creative to solve this problem.

Why so adamant about BT? Getting things to work over BT seems like the equivalent of reinventing the wheel for each thing you do over BT. Everything BT for me has been a pain in my turd-cutter. Plus, it doesnt seem to have the range or stability/dependability that wifi has and dont even get me started on the lack of documentation.

Its interesting for sure but, doesnt seem very advantageous to purposely make esp32 nodes BT only IMO.

Have you tried Passive BLE Monitor, I use that instead of Bluetooth Proxy because have some BLE Xiaomi buttons that doesn’t work with it yet.

You don’t need Passive BLE monitor, you can use the official BTHome integration, which should allow you to also make use of the Bluetooth proxies in your house. BTHome format should be automatically recognized in HA.

BTW. I have added the Xiaomi BLE buttons to the Xiaomi-BLE integration in 2024.2, so these should be available now.

1 Like

@Fallingaway24 @Ernst

Thanks guys I will have to do more testing on this. I started working on it but then put it down. As I said I not good at Bluetooth but this is what I have got so far. Using Arduino I tried to create a Dallas sensor and a button using BTHome but HA can see it… still working on it when I get time. See below… I am sure the Bluetooth experts will be laughing at me :rofl:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Define pin 32 for the DS18B20 sensor
#define ONE_WIRE_BUS 32
#define BUTTON_PIN 39

// UUID for the temperature measurement service
#define UUID16_SVC_TEMPERATURE_MEASUREMENT BLEUUID((uint16_t)0x1809)

// Create instances for the OneWire and DallasTemperature libraries
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// BLE server
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;

// Advertisement data
#define BTHOME_UUID_SIZE 2
uint8_t bthome_uuid[BTHOME_UUID_SIZE] = {0xFC, 0xD2}; // Reversed UUID per byte
uint8_t bthome_data_byte = 0x41; // BTHome device data byte

// Callback class for BLE server
class MyServerCallbacks : public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
    }

    void onDisconnect(BLEServer* pServer) {
    }
};

void setup() {
    Serial.begin(115200);

    // Initialize BLE
    BLEDevice::init("BTHome Temperature Sensor");
    pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    // Create the service
    BLEService *pService = pServer->createService(UUID16_SVC_TEMPERATURE_MEASUREMENT);

    // Create a BLE characteristic
    pCharacteristic = pService->createCharacteristic(
                        BLEUUID(UUID16_SVC_TEMPERATURE_MEASUREMENT),
                        BLECharacteristic::PROPERTY_READ |
                        BLECharacteristic::PROPERTY_NOTIFY
                    );

    // Start the service
    pService->start();

    // Start advertising
    pServer->getAdvertising()->addServiceUUID(UUID16_SVC_TEMPERATURE_MEASUREMENT);
    pServer->getAdvertising()->setScanResponse(false);
    pServer->getAdvertising()->setMinPreferred(0x06);
    pServer->getAdvertising()->setMaxPreferred(0x12);
    BLEAdvertising *pAdvertising = pServer->getAdvertising();
    pAdvertising->start();

    // Initialize DS18B20 sensor
    sensors.begin();

    // Setup button pin
    pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
    // Request temperature from sensor
    sensors.requestTemperatures();
    float temperatureC = sensors.getTempCByIndex(0);

    // Update characteristic value
    pCharacteristic->setValue((uint8_t*)&temperatureC, sizeof(temperatureC));

    // Print temperature to serial monitor
    Serial.print("Temperature: ");
    Serial.print(temperatureC);
    Serial.println(" °C");

    // Check button state
    if (digitalRead(BUTTON_PIN) == LOW) {
        advertiseButtonEvent();
        delay(500); // Button debounce delay
    }

    delay(1000); // Adjust delay as needed
}

void advertiseButtonEvent() {
    // Create button event advertisement
    BLEAdvertising *pAdvertising = pServer->getAdvertising();
    BLEAdvertisementData oScanResponseData = BLEAdvertisementData();

    std::string strServiceData2 = "";
    strServiceData2 += (char)11;     // Length
    strServiceData2 += (char)0x16;   // Type = Service Data - 16-bit UUID

    strServiceData2 += (char)0x1C;    // Use 0x1C18 for non-encrypted messages
    strServiceData2 += (char)0x18;

    // Button event packet
    strServiceData2 += (char)0x23;
    strServiceData2 += (char)0x02;

    // Button event value
    strServiceData2 += (char)0x00;

    oScanResponseData.addData(strServiceData2);
    pAdvertising->setScanResponseData(oScanResponseData);
    pAdvertising->start();
}

I do respect your no quitting attitude you have for this, i just dont get it. Why reinvent the wheel here just to use BT? I use some BT stuff but only because it was only the real option. In my experience BT is less reliable, has less range and its been a huge pain in mine and just about everyones a$$. Why BT over wifi? You could even install RF transmitters/receivers far easier and its arguably better. It just seems like when BT Proxie came out, there has been a huge “BT or die!” bandwagon on here. I just dont get jt personally.

In your code, you use BTHome format V1, but better should use V2. The message you want to send is

020106 07 16 D2FC 40 3A01

020106 = Flags

07 = length
16 = uuid flag
D2FC = uuid reversed
40 = non-encrypted BTHome v2
3A = button
01 = press

I had a problem were Wi-Fi was not available in an area where I needed to connect some devices. I installed a Unify wifie 6 mesh AP. This gave coverage to allot of areas but this point is just behind a concrete wall in a metal box stopped the Wi-Fi coverage. So $$ was spent for no result but I do have heaps of Wi-Fi coverage outside now so I am happy with that. In the metal box I have a Shelly 3 hard wired (no I dont want to use the cable and add another AP at this point because it will do nothing but connect one device + I need the reliability of hard wired to the Shelly 3 and that is why I chose it. No I dont want to connect a AP with a pass through and power an AP and connect to the Shelly 3 just for this one ESP32 device running 24/7) and I can use the Shelly to connect my Bluetooth devices into HA and they are doing this with other devices in this area already. So I need to have a Dallas temp sensor (the one with cable) to read water temp. So if I can create an ESP32 to read temp through Bluetooth then I will achieve what I set out to do in the first place. I sure it is not that hard but for me not knowing Bluetooth it is a bit challenging. Once I fingered it out I will post a how to so anyone finding the same challenges as me will be able to copy paste.

Hope this gives you the Why.

Blacky :smiley:

1 Like