Skip to content

Instantly share code, notes, and snippets.

@amorfinvdev
Created January 27, 2024 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amorfinvdev/e02f8c416e4fa3e110d5c9c3bd5de0ec to your computer and use it in GitHub Desktop.
Save amorfinvdev/e02f8c416e4fa3e110d5c9c3bd5de0ec to your computer and use it in GitHub Desktop.
ESPHOME GoDice BLE Client
esphome:
name: dice-bleclient
friendly_name: Dice bluetooth client
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
captive_portal:
esp32_ble_tracker:
ble_client:
- mac_address: DC:53:1D:57:85:BE
id: dice1
on_connect:
then:
- lambda: |-
ESP_LOGD("ble_client_lambda", "Connected to Dice 1");
- delay: 1s
- ble_client.ble_write:
id: dice1
service_uuid: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
characteristic_uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
# List of bytes to write.
value: [0x10, 0x03, 0x28, 0x32, 0x00, 0x00, 0xff, 0x01, 0x00]
button:
- platform: template
name: "pulse blue"
on_press:
- ble_client.ble_write:
id: dice1
service_uuid: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
characteristic_uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
# List of bytes to write.
value: [0x10, 0x03, 0x28, 0x32, 0x00, 0x00, 0xff, 0x01, 0x00]
sensor:
- platform: ble_client
type: characteristic
ble_client_id: dice1
name: "Dice state"
id: dice_state
service_uuid: '6e400001-b5a3-f393-e0a9-e50e24dcca9e'
characteristic_uuid: '6e400003-b5a3-f393-e0a9-e50e24dcca9e'
notify: True
lambda: |-
int16_t first_byte = x[0];
// Rolling if birst byte is 82
if (first_byte == 82) {
return 0.0;
}
// Get second and third byte
int16_t second_byte = x[1];
int16_t third_byte = x[2];
// It's a message with a battery update, skip parsing it and return -1
if (first_byte == 66 && second_byte == 97 && third_byte == 116) {
return -1.0;
}
// It is a message with a color update, skip parsing and return -2
if (first_byte == 67 && second_byte == 111 && third_byte == 108) {
return -2.0;
}
// Define NUM_VECTORS inside the loop
const int NUM_VECTORS = 6;
// define d_6 vectors of dice
int d6Vectors[NUM_VECTORS][3] = {
{-64, 0, 0},
{0, 0, 64},
{0, 64, 0},
{0, -64, 0},
{0, 0, -64},
{64, 0, 0}
};
// This is a roll, so parse the bytes
if (first_byte == 83) {
// Get 2nd, 3rd, and fourth bytes and unpack
int8_t x_v, y_v, z_v;
// Unpack bytes into unsigned 8-bit integers
x_v = static_cast<int8_t>(x[1]);
y_v = static_cast<int8_t>(x[2]);
z_v = static_cast<int8_t>(x[3]);
// Define xyz array outside the loop
int xyz[3] = {x_v, y_v, z_v};
float distances[NUM_VECTORS];
for (int i = 0; i < NUM_VECTORS; i++) {
distances[i] = 0;
for (int j = 0; j < 3; j++) {
distances[i] += pow(static_cast<float>(xyz[j]) - d6Vectors[i][j], 2);
}
distances[i] = sqrt(distances[i]);
}
int idxMinDistance = 0;
for (int i = 1; i < NUM_VECTORS; i++) {
if (distances[i] < distances[idxMinDistance]) {
idxMinDistance = i;
}
}
int rolledValue = idxMinDistance + 1; // Adding 1 since Arduino arrays are 0-indexed
ESP_LOGD("ble_client_lambda", "The rolled value is :%d", rolledValue);
return float(rolledValue);
}
return -10.0;
@amorfinvdev
Copy link
Author

This is adapted from the [GoDice](adapted from https://github.com/ParticulaCode/GoDicePythonAPI) python API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment