mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 01:24:17 +00:00
These code samples are rendered in the docs, so we include them here. For the git history of these snippets, see: https://github.com/pybricks/pybricks-projects/commits/f4914069aaacfd9ab1e0dd6f05524f62cfc56b29/snippets
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
from pybricks.messaging import BluetoothMailboxClient, TextMailbox
|
|
|
|
# This demo makes your PC talk to an EV3 over Bluetooth.
|
|
#
|
|
# This is identical to the EV3 client example in ../bluetooth_client
|
|
#
|
|
# The only difference is that it runs in Python3 on your computer, thanks to
|
|
# the Python3 implementation of the messaging module that is included here.
|
|
# As far as the EV3 is concerned, it thinks it just talks to an EV3 client.
|
|
#
|
|
# So, the EV3 server example needs no further modifications. The connection
|
|
# procedure is also the same as documented in the messaging module docs:
|
|
# https://docs.pybricks.com/en/latest/messaging.html
|
|
#
|
|
# So, turn Bluetooth on on your PC and the EV3. You may need to make Bluetooth
|
|
# visible on the EV3. You can skip pairing if you already know the EV3 address.
|
|
|
|
# This is the address of the server EV3 we are connecting to.
|
|
SERVER = 'CC:78:AB:D8:4E:F6'
|
|
|
|
client = BluetoothMailboxClient()
|
|
mbox = TextMailbox('greeting', client)
|
|
|
|
print('establishing connection...')
|
|
client.connect(SERVER)
|
|
print('connected!')
|
|
|
|
# In this program, the client sends the first message and then waits for the
|
|
# server to reply.
|
|
mbox.send('hello!')
|
|
mbox.wait()
|
|
print(mbox.read())
|