Skip to content

Instantly share code, notes, and snippets.

@jacobw
Created April 26, 2024 22:40
Show Gist options
  • Save jacobw/e613e7f802bf44c21698a395eaba9870 to your computer and use it in GitHub Desktop.
Save jacobw/e613e7f802bf44c21698a395eaba9870 to your computer and use it in GitHub Desktop.
ESPHOME BTHome
esphome:
name: dev4
friendly_name: BTHome dev4
interval:
- interval: 5s
startup_delay: 1s
then:
- lambda: |-
// Update sensor then wait
id(sht).update();
- delay: 0.01s
- lambda: |-
// Get temperature and humitidy with .1 accuracy
int temperature = ((int)roundf(id(sht_temperature).state * 10)) * 10;
int humidity = ((int)roundf(id(sht_humidity).state * 10)) * 10;
// Create points to enable little endian conversion
uint8_t *pTemperature = (uint8_t*)&temperature;
uint8_t *pHumidity = (uint8_t*)&humidity;
// Create and send BTHome payload
std::vector<uint8_t> service_data = {
0xD2, 0xFC, // BTHome UUID
0x40, // BTHome Device Information
0x02, // Temperature type
pTemperature[0], pTemperature[1], // Actual temperature (little endian)
0x03, // Humidity type
pHumidity[0], pHumidity[1] // Actual humidity (little endian)
};
esphome::esp32_ble::global_ble->advertising_set_service_data(service_data);
ESP_LOGD("BThome", "Sent Temperature: %i, Humidity: %i", temperature, humidity);
ESP_LOGD("BThome", "Hex Temperature: 0x%02X%02X, Humidity: 0x%02X%02X", pTemperature[0], pTemperature[1], pHumidity[0], pHumidity[1]);
esp32:
board: adafruit_feather_esp32s3_nopsram
framework:
type: arduino
# Enable logging
logger:
level: very_verbose
# Enable Home Assistant API
api:
encryption:
key: <REDACTED>
ota:
password: <REDACTED>
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
i2c:
sda:
number: GPIO3
ignore_strapping_warning: true
scl: GPIO4
sensor:
- platform: sht4x
id: sht
temperature:
id: sht_temperature
name: "Temperature"
accuracy_decimals: 1
humidity:
id: sht_humidity
name: "Relative Humidity"
accuracy_decimals: 1
esp32_ble_server:
id: ble
manufacturer: "Orange"
manufacturer_data: [0x4C, 0, 0x23, 77, 0xF0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment