|
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:
Acquire x-axis acceleration every 50 ms and trace them in realtime in a graphics window.
Program:
# DataAcquisition.py
import radio
from microbit import *
radio.on()
display.show("D")
t = 0
T = 5
dt = 50 # ms
while not button_b.was_pressed():
v = accelerometer.get_x()
data = str(t) + ":" + str(v)
print(data)
radio.send(data)
t += (dt / 1000)
sleep(dt)
display.show(Image.NO)
|
# DataAgent.py
import radio
from microbit import *
radio.on()
display.show('A')
print("starting") # Triggers data display
rec = ""
while True:
rec = radio.receive()
if rec != None:
print(str(rec))
else:
sleep(10)
|
# DataTracer.py
from mbm import *
from gpanel import *
import time
def init():
clear()
drawGrid(0, 10, -1000, 1000, "darkgray")
makeGPanel(-1, 11, -1200, 1200)
run("DataAgent.py")
enableDataCapture(True)
startData = False
title("DataTracer")
init()
tOld = 10
while not isTerminalDisposed():
data = getDataLines() # empty if nothing received
if startData:
for s in data:
z = s.split(":")
t = float(z[0]) % 10
v = float(z[1])
if t < tOld:
init()
pos(t, v)
else:
draw(t, v)
tOld = t
else:
for s in data:
if s == "starting": # got from agent
startData = True
title("Finished")
|
Typical output of DataTracer
|
Remarks:
To start capturing lines in the terminal (REPL) window, enableDataCapture(True) is called. From now on, all lines in the terminal window are copied into an internal buffer (a list of strings). The content of the line buffer is returned by calling getDateLines() and the buffer is cleared.
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:
# DataAgent.py
from microbit import *
display.show("D")
t = 0
dt = 50 # ms
print("starting") # signal that data transmission starts
while not button_b.was_pressed():
v = accelerometer.get_x()
print("%6.2f:%d" %(t / 1000, v))
t += dt
sleep(dt)
display.show(Image.NO)
|
This scenario can also be useful if the micro:bit is used as data acquisition front end to a Raspberry Pi. |
|