Bluepy-scratch-link is Scratch-link implemented on bluepy as a small python script. As of October 2019, Scratch-link is a software module which connects Scratch and Bluetooth devices such as micro:bit. However, it works only on Windows and MacOS, and cannot connect Scratch and micro:bit on Linux.
Bluepy-scratch-link allows Linux PCs to connect Scratch and micro:bit. It uses Linux Bluetooth protocol stack Bluez and its python interface bluepy to handle Bluetooth Low Energy, or BLE, connections with micro:bit. It is confirmed that bluepy-scratch-link connects Scratch 3.0 and a micro:bit.
This is a minimal implementation to support micro:bit. Some of Scratch-link features are not implemented. For example, Bluetooth (non-BLE) devices are not supported. BLE device support other than micro:bit is not confirmed.
Bluepy-scratch-link requires python version 3.6 and later to use websockets. If your system has python older than version 3.6, install newer version. If your Linux system has explicit command names python3 and pip3 for python version 3, use them in the instructions below.
The instructions below was confirmed with elementary OS 5.0 Juno which is based on Ubuntu 18.04 LTS and Arch Linux. Trial with other distros and feed-backs will be appreciated.
-
Prepare BLE controller Confirm that your Linux PC has a Bluetooth controller with BLE support. Bluetooth 4.0 controller supports BLE. If your PC does not have it, need to plug USB Bluetooth 4.0 adapter.
-
Install Bluez package
Ubuntu $ sudo apt install bluez Arch $ sudo pacman -S bluez
-
Install python modules
$ sudo pip install bluepy websockets Or if your system has python3 command, $ sudo pip3 install bluepy websockets
-
Get bluepy-scratch-link Example below installs bluepy-scratch-link under your home directory.
$ cd ~ $ git clone git@github.com:kawasaki/bluepy-scratch-link.git
-
Prepare web server certificate Scratch-link requires local Secure WebSocket server with certificate. Generate and prepare a PEM certificate file.
$ cd ~/bluepy-scratch-link $ openssl req -x509 -out scratch-device-manager.cer \ -keyout scratch-device-manager.key -newkey rsa:2048 -nodes -sha256 \ -subj '/CN=scratch-device-manager' -extensions EXT -config <( \ printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth") $ openssl pkcs12 -inkey scratch-device-manager.key \ -in scratch-device-manager.cer \ -name "Scratch Link & Scratch Device Manager" \ -passout pass:Scratch -export -out scratch-device-manager.pfx $ grep -h ^ scratch-device-manager.cer scratch-device-manager.key \ | tr -d '\r' > scratch-device-manager.pem
-
Install Scratch-link hex in micro:bit
- Download and unzip the micro:bit Scratch Hex file.
- Flash the micro:bit over USB with the Scratch .Hex File, you will see the five character name of the micro:bit scroll across the screen such as 'zo9ev'.
-
Turn on Bluetooth Low Energy controller
$ sudo btmgmt le on $ sudo btmgmt power on
-
Start scratch-link python script
$ cd ~/bluepy-scratch-link $ sudo ./scratch_link.py Or if your system has python3 command, $ sudo python3 ./scratch_link.py
-
Start Firefox or Chrome and allow local server certificate
- This action is required only the first time to access.
- Open Firefox or Chrome and open https://device-manager.scratch.mit.edu:20110/. You will see a security risk warning.
- In Firefox: Click "Advanced" and click "Accept Risk and Continue".
- In Chrome: type the special bypass keyword
thisisunsafe
. - Immediately, you will see "Failed to open a WebSocket connection". This is expected.
-
Connect scratch to micro:bit
- Open Scratch 3.0
- Select the "Add Extension" button
- Select micro:bit extension and follow the prompts to connect micro:bit
- Build your project with the extension blocks