MicroBlocks and Python Communication with Messages.
# Python3
python -m pip install microblocks
import time
from microblocks import Message
m = Message()
# found_devices = m.discover() # Discover MicroBlocks devices
m.connect('MicroBlocks KCY') # replace the string with the device name
# broadcast message from Python to MicroBlocks
m.send('happy')
time.sleep(1)
m.send('sad')
# receive broadcasts from MicroBlocks
while True:
message = m.receive()
if message:
print(message)
# receive(non-blocking) broadcasts from MicroBlocks. Work with m.on_message = <a function>
# m.loopStart()
import time
from microblocks import SerialMessage
m = SerialMessage()
m.connect('/dev/tty.usbmodem1402') # replace the string with micro:bit port
# broadcast message from Python to MicroBlocks
m.send('happy')
time.sleep(1)
m.send('sad')
# receive broadcasts from MicroBlocks
while True:
message = m.receive()
if message:
print(message)
# receive(non-blocking) broadcasts from MicroBlocks. Work with m.on_message = <a function>
# m.loopStart()
Work with the MicroBlocks code (you can save this PNG file, then drag it into MicroBlocks to load the scripts):