|
The source code of all examples can be downloaded from a link in the right side bar. Bluetooth communication is supported by the application processor of the micro:bit, but the Bluetooth protocol is not fully implemented in the current version of MicroPython. Bluetooth communication is only available between two micro:bit devices (here called nodes). But the good news is that the code is extremely simple compared to programming with the standard Bluetooth API. Typically both nodes must call radio.on() to establish a Bluetooth link (no Bluetooth paring is necessary, there is no server-client hierarchy). Either of them may send a string message any time by calling radio.send(msg) and the message is buffered in the other node's receiver buffer, where it can fetched by calling radio.receive(). If the buffer is empty, None is returned. |
||
Example 1: Two persons exchange number information |
Aim: Program:
Remarks: |
Example 2: Realtime sensor data collection |
Because there is no way to receive data on a PC from the micro:bit via Bluetooth, the acquired data are first transferred from the data collecting micro:bit (that may be completely stand-alone) to an micro:bit that acts as an intermediate agent . The idea is the following: A Python program running in TigerJython starts the agent program in a MBM terminal window. The agent receives measurement values as text lines from the Bluetooth link and print them out in the terminal window. The PC program captures the lines with getDataLines() and process the data. Aim: Program:
Remarks: As soon as the data capture is enabled, all REPL data is captured that also includes user information written to the terminal window. We must wait in the data receiving loop until the agent sends a "starting" message that signals the start of time/value information. If you want capture sensor data by a PC from a micro:bit front end that collects the data and sends them over the serial line to the data tracer, exactly the same DataTracer program can be used, but DataAgent has to be modified as follows:
|