To try the simulator use the micro:bit Python Editor.
The rest of this page explains how to embed the simulator in an application and develop the simulator software.
This software is under the MIT open source license.
The simulator is designed to be embedded into other web applications as an iframe. The embedding API and URLs are not yet stable and are subject to change. If you are interested in embedding the simulator in your application please get in touch.
The URL to embed is https://python-simulator.usermbit.org/v/0.1/simulator.html. You can embed this in your application directly. We would love to hear about your use of the simulator so please open an issue to get in touch.
The iframe provides the micro:bit board user interface and some limited interactions. It does not provide a terminal for serial/the REPL or any UI to change the board sensor state.
A value for a brand color can be passed to the simulator via a query string and is used to style the play button. E.g., https://python-simulator.usermbit.org/v/0.1/simulator.html?color=blue
demo.html is an example of embedding the simulator. It connects the iframe to a terminal and provides a simple interface for sensors.
The following sections document the messages supported by the iframe embed via postMessage.
These messages are sent as the result of user interactions or actions taken by the running program. The simulator starts stopped with no program flashed.
Kind | Example | Description |
---|---|---|
ready | ```javascript { "kind": "ready", "state": { "lightLevel": { "id": "lightLevel", "type": "range", "min": 0, "max": 255 }, "soundLevel": { "id": "soundLevel", "type": "range", "min": 0, "max": 255 // Microphone sensor only: "lowThreshold": 50, "highThreshold": 150 } // Full state continues here. } } ``` | Sent when the simulator is ready for input. Includes a description of the available sensors. |
sensor_change | ```javascript { "kind": "state_change", "change": { "soundLevel": { "id": "soundLevel", "type": "range", "min": 0, "max": 255 // Microphone sensor only: "lowThreshold": 50, "highThreshold": 150 } // Optionally, further keys here. } ] } ``` | Sent when the simulator state changes. The keys are a subset of the original state. The values are always sent in full. |
request_flash | ```javascript { "kind": "request_flash", } ``` | Sent when the user requests the simulator starts. The embedder should flash the latest code via the flash message.
|
serial_output | ```javascript { "kind": "serial_output", "data": "text" } ``` | Serial output suitable for a terminal or other use. |
radio_output | ```javascript { "kind": "radio_output", "data": new Uint8Array([]) } ``` | Radio output (sent from the user's program) as bytes. If you send string data from the program then it will be prepended with the three bytes 0x01, 0x00, 0x01. |
internal_error | ```javascript { "kind": "internal_error", "error": new Error() } ``` | A debug message sent for internal (unexpected) errors thrown by the simulator. Suitable for application-level logging. Please raise issues in this project as these indicate a bug in the simulator. |
Kind | Example | Description |
---|---|---|
flash | ```javascript { "kind": "flash", "filesystem": { "main.py": new TextEncoder() .encode("# your program here") } } ``` | Update the micro:bit filesystem and restart the program. You must send this in response to the request_flash message. |
stop | ```javascript { "kind": "stop" } ``` | Stop the program. |
reset | ```javascript { "kind": "reset" } ``` | Reset the program. |
mute | ```javascript { "kind": "mute" } ``` | Mute the simulator. |
unmute | ```javascript { "kind": "unmute" } ``` | Unmute the simulator. |
serial_input | ```javascript { "kind": "serial_input", "data": "text" } ``` | Serial input. If the REPL is active it will echo this text via serial_write .
|
sensor_set | ```javascript { "kind": "set_value", "id": "lightLevel", "value": 255 } ``` | Set a sensor, button or pin value. The sensor, button or pin is identified by the top-level key in the state. Buttons and pins (touch state) have 0 and 1 values. In future, analog values will be supported for pins. |
radio_input | ```javascript { "kind": "radio_input", "data": new Uint8Array([]) } ``` | Radio input (received by the user's program as if sent from another micro:bit) as bytes.
If you want to send string data then prepend the byte array with the three bytes 0x01 , 0x00 , 0x01 .
Otherwise, the user will need to use radio.receive_bytes or radio.receive_full . The input is assumed to be sent to the currently configured radio group.
|
The simulator is a variant of the MicroPython codal_port which is compiled with Emscripten. It provides a simulated micro:bit (including REPL) in the browser.
To build, first fetch the submodules (don't use recursive fetch):
$ git submodule update --init lib/micropython-microbit-v2
$ git -C lib/micropython-microbit-v2 submodule update --init lib/micropython
Then run (from this top-level directory):
$ make
Once it is built the pages in build/ need to be served, e.g. via:
$ npx serve build
View at http://localhost:3000/demo.html
$ (cd build && python -m http.server)
View at http://localhost:8000/demo.html
There is a CloudFlare pages based build for development purposes only. Do not embed the simulator via this URL as it may be removed at any time. CloudFlare's GitHub integration will comment on PRs with deployment details.
Branches in this repository are also deployed via CircleCI to https://review-python-simulator.usermbit.org/{branchName}/. This requires the user pushing code to have permissions for the relevant Micro:bit Educational Foundation infrastructure.
Similarly, the main branch is deployed to https://python-simulator.usermbit.org/staging/.
Tagged releases with a v
prefix are deployed to https://python-simulator.usermbit.org/v/{number}/
Steps for WASM debugging in Chrome:
Trust, partnership, simplicity and passion are our core values we live and breathe in our daily work life and within our projects. Our open-source projects are no exception. We have an active community which spans the globe and we welcome and encourage participation and contributions to our projects by everyone. We work to foster a positive, open, inclusive and supportive environment and trust that our community respects the micro:bit code of conduct. Please see our code of conduct which outlines our expectations for all those that participate in our community and details on how to report any concerns and what would happen should breaches occur.