mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-15 02:52:41 +00:00
ev3/snippets/bluetooth_pc: PC is client
Technically it does not matter much who is the server or the client. However, if Windows PCs are involved, we found it to be easier if Windows is the client and EV3 is the server. That way, it is the Windows PC initiating the connection. It doesn't seem to like it when other devices take the initiative. So, we update the example accordingly to make it accessible for most users without loss of generality. You can still make the PC the server by just running the server example on your PC.
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@
|
||||
"name": "PC Bluetooth Example",
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"program": "pcserver.py",
|
||||
"program": "pcclient.py",
|
||||
"console": "integratedTerminal"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/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())
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
from pybricks.messaging import BluetoothMailboxServer, TextMailbox
|
||||
|
||||
# This demo makes your PC talk to an EV3 over Bluetooth.
|
||||
#
|
||||
# Note that is identical to the EV3 server example in ../bluetooth_server
|
||||
#
|
||||
# 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 is just talking to an EV3.
|
||||
#
|
||||
# So, the EV3 client 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 and pair them.
|
||||
#
|
||||
# Of course, you'll need to enter your PC Bluetooth name in the EV3 client
|
||||
# example so the EV3 knows which paired device to connect to. For example,
|
||||
# change it to say: SERVER = 'work-laptop' if that is the device name.
|
||||
|
||||
server = BluetoothMailboxServer()
|
||||
mbox = TextMailbox('greeting', server)
|
||||
|
||||
print('waiting for connection...')
|
||||
server.wait_for_connection()
|
||||
print('connected!')
|
||||
|
||||
mbox.wait()
|
||||
print(mbox.read())
|
||||
mbox.send('hello to you!')
|
||||
Reference in New Issue
Block a user