Skip to main content

A MicroPython Bluetooth library for remote controlling LEGO hubs via BLE

Project description

btbricks

btbricks logo

PyPI Version License MicroPython

A MicroPython Bluetooth library. It implements BLE (Bluetooth 5, Bluetooth Low Energy). Of the know BLE services, this library implements Nordic Uart Service (NUS), LEGO Service and MIDI service. The library contains both the BLE Central (client) and BLE Peripheral (server) classes.

These BLE services allow for controlling LEGO hubs, running official firmware. The services also allow creating custom Bluetooth peripherals: RC controllers, MIDI devices, etc. To control the LEGO hubs, you can best use a hub expansion board, like the LMS-ESP32.

Table of Contents

Features

  • 🔌 BLE Communication: Comprehensive Bluetooth Low Energy support via MicroPython's ubluetooth
  • 🎮 Hub Control: Control LEGO MINDSTORMS hubs, SPIKE sets, and smart hubs over Bluetooth
  • 📱 Custom Peripherals: Create RC controllers, MIDI controllers, and other BLE peripherals compatible with LEGO hubs
  • 🚀 MicroPython Ready: Optimized for MicroPython on ESP32, LEGO SPIKE, and other platforms
  • 📡 LEGO Protocol: Full support for LEGO Bluetooth protocols (LPF2, LPUP, CTRL+)
  • 🎛️ Multiple Interfaces: Nordic UART, MIDI, RC control, and native LEGO hub communication
  • ⚙️ Advanced BLE: Automatic MTU negotiation, descriptor handling, and efficient payload management

Installation

On LMS-ESP32

The module should be included in the latest Micropython firmware from https://wwww.antonsmindstorms.com. If not, use ViperIDE or Thonny and create a new file called rcservo.py. Copy the contents from the same file in this repository inside.

On MicroPython device using micropip from PyPI

import micropip
await micropip.install("btbricks")

Note: micropip must be available on the target board and may require an internet connection from the device.

On SPIKE Legacy or MINDSTORMS Robot Inventor

Use the installer script in mpy-robot-tools: https://github.com/antonvh/mpy-robot-tools/blob/master/Installer/install_mpy_robot_tools.py

Quick Start

Connect to a LEGO Hub

from btbricks import BtHub

# Create hub instance
hub = BtHub()

# Connect to a nearby hub
hub.connect()

if hub.is_connected():
    # Set hub LED to green
    hub.set_led_color(6)  # GREEN constant
    
    # Read accelerometer data
    acc = hub.acc()
    if acc:
        print(f"Accelerometer: {acc}")
    
    # Control motor on port A with 50% power
    hub.dc("A", 50)
    
    hub.disconnect()

Create an RC Receiver (Hub-side)

Use the examples in the examples/ folder for full, runnable code. Minimal receiver/transmitter snippets:

from btbricks import RCReceiver, R_STICK_HOR, R_STICK_VER
from time import sleep_ms

# Create RC receiver (advertises as "robot" by default)
rcv = RCReceiver(name="robot")

print("Waiting for RC transmitter to connect...")
try:
    while True:
        if rcv.is_connected():
            steering = rcv.controller_state(R_STICK_HOR)
            throttle = rcv.controller_state(R_STICK_VER)
            print(f"Steering: {steering}, Throttle: {throttle}")
            sleep_ms(100)
        else:
            sleep_ms(500)
except KeyboardInterrupt:
    rcv.disconnect()
from btbricks import RCTransmitter, L_STICK_HOR, R_STICK_VER
from time import sleep

# Create RC transmitter (central)
tx = RCTransmitter()

if tx.connect(name="robot"):
    try:
        while tx.is_connected():
            # Set stick values in range [-100, 100]
            tx.set_stick(L_STICK_HOR, 0)
            tx.set_stick(R_STICK_VER, 50)
            tx.transmit()
            sleep(0.1)
    except KeyboardInterrupt:
        tx.disconnect()

Create a MIDI Controller

from btbricks import MidiController
from time import sleep

# Create MIDI controller (advertises as "amh-midi" by default)
midi = MidiController(name="amh-midi")

print("MIDI controller started, connect from your DAW...")

try:
    while True:
        # Send MIDI note on: middle C (note 60), velocity 100
        midi.note_on(60, 100)
        sleep(0.5)
        
        # Send MIDI note off
        midi.note_off(60)
        sleep(0.5)
        
        # Or send a chord
        midi.chord_on("C4", velocity=100, style="M")  # C major chord
        sleep(1)
        midi.note_off(60)  # Stop the chord
        
except KeyboardInterrupt:
    print("MIDI controller stopped")

Documentation and API reference

See the full documentation and API reference at:

https://docs.antonsmindstorms.com/en/latest/Software/btbricks/docs/index.html

Core Classes

  • BLEHandler: Low-level Bluetooth communication
  • UARTCentral: Nordic UART client mode
  • UARTPeripheral: Nordic UART server mode
  • RCReceiver: Receive RC control signals
  • RCTransmitter: Send RC control signals
  • MidiController: Send MIDI commands over BLE
  • BtHub: High-level hub communication interface

Control Constants

  • Sticks: L_STICK_HOR, L_STICK_VER, R_STICK_HOR, R_STICK_VER
  • Triggers: L_TRIGGER, R_TRIGGER
  • Buttons: BUTTONS
  • Settings: SETTING1, SETTING2

Supported Platforms

  • LEGO MINDSTORMS EV3 (with MicroPython firmware)
  • LEGO SPIKE Prime/Prime Essential (with MINDSTORMS firmware)
  • LEGO SPIKE Robot Inventor
  • ESP32 with MicroPython
  • Other MicroPython boards with ubluetooth support

License

MIT License

Author

Anton Vanhoucke

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

btbricks-0.2.1.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

btbricks-0.2.1-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file btbricks-0.2.1.tar.gz.

File metadata

  • Download URL: btbricks-0.2.1.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for btbricks-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5ad70871f5631c16e8aaec2861ea8397588213c8c7eba47b93d2cb0c7b94ee15
MD5 f5f5ec2df76e9c47c7a7c5f46d220145
BLAKE2b-256 af0cdc1819d8696fd4cb5050faf3117cea1244893936b86ae9d1574c7f05b5e9

See more details on using hashes here.

File details

Details for the file btbricks-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: btbricks-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for btbricks-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac3d2274f79ed099814d823ddc9a2d8c3a74b9d50f8a79516ef2cda26c96c75b
MD5 a40e097fb65f71c524d02a2673923417
BLAKE2b-256 d27888e07579d5b3599d7b60498af83c1290d26a5f56e60f42386ed2cc2637a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page