Skip to main content

A python package to connect to the Bluetooth LE GATT services of paired BBC micro:bit devices. Use your micro:bit as a wireless game controller!

Project description

Kasper's micro:bit

So you finished Python for kids? Wouldn't it be awesome to use your BBC micro:bit as a wireless game controller for your own DIY games? You can do it with this python package! Pair your micro:bit to your computer with bluetooth and use buttons A and B or the accelerometer to control your game. Like it? Give us a :star: on github!

Lint and Test Documentation Status PyPi Nederlands

Kasper's microbit is a python package to make a connection to a BBC micro:bit by means of the Bluetooth LE GATT services exposed by the micro:bit.

Video of games created with kaspersmicrobit

Watch the full video on youtube

Installation

$ pip install kaspersmicrobit

Or upgrade to the latest version with:

$  pip install --upgrade kaspersmicrobit  

Example

import time

from kaspersmicrobit import KaspersMicrobit

CHANGE_THIS_TO_YOUR_MICROBIT_BLUETOOTH_ADDRESS = 'E3:7E:99:0D:C1:BA'


def pressed(button):
    print(f"button {button} pressed")


with KaspersMicrobit(CHANGE_THIS_TO_YOUR_MICROBIT_BLUETOOTH_ADDRESS) as microbit:
    microbit.buttons.on_button_a(press=pressed)
    time.sleep(10)

Connecting

To connect to your micro:bit you'll have to enable the bluetooth services you want to use, and pair your micro:bit to your computer.

To enable the services you can use MakeCode or copy a hex file to your micro:bit.

For detailed instructions take a look in Getting started - Enable Bluetooth

Documentation

On https://kaspersmicrobit.readthedocs.io you can find:

  • Simple examples, showing how to use the functions of each service offered by the micro:bit
  • Full Api documentation
  • Examples that show kaspersmicrobit in combination with tkinter

The source code of all examples from the documentation can be found in the examples directory.

Troubleshooting

Bluetooth connection

Problems related to connecting to the micro:bit over bluetooth are often solved by pairing your computer again to your micro:bit

See also: https://support.microbit.org/helpdesk/attachments/19075694226

Also, if you are not using the "with"-block, but calling .connect() yourself, always make sure that in any case you call .disconnect() when you don't need the connection anymore (for instance when you exit your application)

The micro:bit V1 and Microsoft Windows

To use kaspersmicrobit, your micro:bit should be paired with your computer, but not "connected". We noticed that our micro:bit V1 always connects to Windows when it is paired. This prevents kaspersmicrobit to connect to it. On linux it behaves correctly. The micro:bit V2 worked on both Windows and linux.

tkinter "main thread is not in main loop"

When combining kaspersmicrobit with tkinter (the window library used in Python for kids) you could bump into the TK error "main thread is not in main loop". This is probably because you call TK code from within a callback function that you registered to be called when a button press occurs or new accelerometer data is present (or some other notification). The callback is executed on a different thread and tkinter does not like this. There are at least 2 solutions for this:

  • don't call tkinter code in a callback
  • wrap the callback with kaspersmicrobit.tkinter.do_in_tkinter(tk, your_callback) this makes sure that your callback will be executed on the tk thread, avoiding the error

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

kaspersmicrobit-0.3.4.tar.gz (33.1 kB view hashes)

Uploaded Source

Built Distribution

kaspersmicrobit-0.3.4-py3-none-any.whl (44.7 kB view hashes)

Uploaded Python 3

Supported by

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