I feel honored to share my first project here with the community!
A year ago I bought a Lego Duplo train set for my son. I was disappointed that it had to be operated with a smartphone or tablet.
So I made a Bluetooth controller that can control the train with a ESP32 .
Now I thought it would be fun to set up the train as a decoration for Christmas and operate it using a home assistant.
Electronics:
ESP32 (ESP-WROOM-32)
TP4056 Type-C
2x 13300 cells
The 13300 cells i recycled from old vapes(there are all over here on the streets…).
I cleaned them and tested if there were still useable.
With Esphome I wrote a code that makes it possible for me to send Bluetooth commands to the train using buttons.
esphome:
name: Duplo_Train_Control
platform: ESP32
board: esp-wrover-kit
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: ''********''
ota:
password: "************"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Duplo_Train_Control"
password: "gPYZ2uE4eb8W"
# Configure Bluetooth
esp32_ble_tracker:
# Configure BLE Client
ble_client:
- mac_address: "a0:6c:65:55:26:2c"
id: Duplo_Train_Control
captive_portal:
button:
- platform: template
name: "Speed 1"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0x1e};
- platform: template
name: "Speed 2"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0x32};
- platform: template
name: "Speed 3"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0x64};
- platform: template
name: "Stop"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0x00};
- platform: template
name: "Backward 1"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0xce};
- platform: template
name: "Backward 2"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x00, 0x01, 0x51, 0x00, 0xce};
- platform: template
name: "horn"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x01, 0x09};
- platform: template
name: "fill water"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x01, 0x07};
- platform: template
name: "light"
on_press:
- logger.log: "Switch toggled"
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# List of bytes to write.
value: [0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]
- ble_client.ble_write:
id: my_ble_client
service_uuid: 00001623-1212-efde-1623-785feabcd123
characteristic_uuid: 00001624-1212-efde-1623-785feabcd123
# A lambda returning an std::vector<uint8_t>.
value: !lambda |-
return {0x08, 0x00, 0x81, 0x11, 0x11, 0x51, 0x00, 0x0a};
Finally i made a control card:
type: vertical-stack
cards:
- type: vertical-stack
cards:
- type: custom:mushroom-title-card
title: Trein contoller
alignment: center
- type: horizontal-stack
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.speed_1
name: Versnelling 1
icon: mdi:chevron-right
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
icon: mdi:chevron-double-right
entity: button.speed_2
name: Versnelling 2
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.speed_3
icon: mdi:transfer-right
name: Versnelling 3
- type: horizontal-stack
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.stop
icon: mdi:stop
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.backward_1
icon: mdi:chevron-left
name: Terug 1
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.backward_2
icon: mdi:chevron-double-left
name: Terug 2
- type: horizontal-stack
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.horn
icon: mdi:bugle
name: Toeter
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.fill_water
name: Water vullen
icon: mdi:water-pump
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: button.light
name: Licht
icon: mdi:car-parking-lights
The title’s are in dutch so need to be changed to the preferred language.
I would like to hear if if you guys liked this project and love to hear the comments.!