wwj718 / microblocks_messaging_library

MicroBlocks and Python Communication with Messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

readme

MicroBlocks and Python Communication with Messages.

Install

# Python3
python -m pip install microblocks

Usage

MicroBlocks demo

wireless programming (BLE)

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()

serial

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):

message flow diagram

MicroBlocks interoperability

About

MicroBlocks and Python Communication with Messages.

License:Other


Languages

Language:Python 77.6%Language:Jupyter Notebook 22.4%