From 8907939639ed20f8e69749765f83db9248d2dbec Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Sat, 16 May 2020 17:01:36 +0200 Subject: [PATCH] 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. --- snippets/ev3/bluetooth_pc/.vscode/launch.json | 2 +- snippets/ev3/bluetooth_pc/pcclient.py | 33 +++++++++++++++++++ snippets/ev3/bluetooth_pc/pcserver.py | 31 ----------------- 3 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 snippets/ev3/bluetooth_pc/pcclient.py delete mode 100644 snippets/ev3/bluetooth_pc/pcserver.py 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!')