diff --git a/snippets/ev3/bluetooth_pc/.vscode/launch.json b/snippets/ev3/bluetooth_pc/.vscode/launch.json index 7190ee2..2c05c3c 100644 --- a/snippets/ev3/bluetooth_pc/.vscode/launch.json +++ b/snippets/ev3/bluetooth_pc/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "PC Bluetooth Example", "type": "python", "request": "launch", - "program": "pcserver.py", + "program": "pcclient.py", "console": "integratedTerminal" } ] diff --git a/snippets/ev3/bluetooth_pc/pcclient.py b/snippets/ev3/bluetooth_pc/pcclient.py new file mode 100644 index 0000000..ffc1ee2 --- /dev/null +++ b/snippets/ev3/bluetooth_pc/pcclient.py @@ -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()) diff --git a/snippets/ev3/bluetooth_pc/pcserver.py b/snippets/ev3/bluetooth_pc/pcserver.py deleted file mode 100644 index 83095f7..0000000 --- a/snippets/ev3/bluetooth_pc/pcserver.py +++ /dev/null @@ -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!')