Python Tutorial 5: Connect WiFi
This document will provide a walk-through tutorial to use bleak to implement the Open GoPro Interface to enable the GoPro’s WiFi Access Point (AP) so that it can be connected to. It will also provide an example of connecting to the WiFi AP.
Requirements
It is assumed that the hardware and software requirements from the connect tutorial are present and configured correctly.
The scripts that will be used for this tutorial can be found in the Tutorial 5 Folder.
Just Show me the Demo(s)!!
If you just want to run the demo, you can find Python scripts for each of the concepts in this tutorial in the Open GoPro GitHub repo.
demos/python/tutorial/tutorial_modules/tutorial_5_connect_wifi/
You can test querying the current Resolution on your camera through BLE using the following script:
$ python wifi_enable.py
See the help for parameter definitions:
$ python wifi_enable.py --help
usage: wifi_enable.py [-h] [-i IDENTIFIER] [-t TIMEOUT]
Connect to a GoPro camera via BLE, get WiFi info, and enable WiFi.
optional arguments:
-h, --help show this help message and exit
-i IDENTIFIER, --identifier IDENTIFIER
Last 4 digits of GoPro serial number, which is the last 4 digits of the
default camera SSID. If not used, first discovered GoPro will be connected to
-t TIMEOUT, --timeout TIMEOUT
time in seconds to maintain connection before disconnecting. If not set, will
maintain connection indefinitely
Setup
We must first connect to BLE as was discussed in the connect tutorial. We are also using the same notification handler as was used in the sending commands tutorial
Connecting to WiFi AP
Now that we are connected via BLE, paired, and have enabled notifications, we can send the command to enable the WiFi AP.
Here is an outline of the steps to do so:
Essentially we will be finding the WiFi AP information (SSID and password) via BLE, enabling the WiFi AP via BLE, then connecting to the WiFi AP.
Find WiFi Information
Note that the process to get this information is different than all procedures described up to this point. Whereas the previous command, setting, and query procedures all followed the Write Request-Notification Response pattern, the WiFi Information is retrieved via direct Read Requests to BLE characteristics.
Get WiFi SSID
The WiFi SSID can be found by reading from the WiFi AP SSID characteristic of the WiFi Access Point service.
First, we need to define the attribute to read from:
WIFI_AP_SSID_UUID = GOPRO_BASE_UUID.format("0002")
Now, let’s send the read request to get the SSID (and decode it into a string).
ssid = await client.read_gatt_char(WIFI_AP_SSID_UUID)
ssid = ssid.decode()
read_gatt_char
method
returns.
In the demo, this information is logged as such:
INFO:root:Reading the WiFi AP SSID
INFO:root:SSID is GP24500456
Get WiFi Password
The WiFi password can be found by reading from the WiFi AP password characteristic of the WiFi Access Point service.
First, we need to define the attribute to read from:
WIFI_AP_PASSWORD_UUID = GOPRO_BASE_UUID.format("0003")
Now, let’s send the read request to get the password (and decode it into a string).
password = await client.read_gatt_char(WIFI_AP_PASSWORD_UUID)
password = password.decode()
read_gatt_char
method
returns.
In the demo, this information is logged as such:
INFO:root:Reading the WiFi AP password
INFO:root:Password is g@6-Tj9-C7K
Enable WiFi AP
Before we can connect to the WiFi AP, we have to make sure it is enabled. This is accomplished by using the “AP Control” command:
Command | Bytes |
---|---|
Ap Control Enable | 0x03 0x17 0x01 0x01 |
Ap Control Disable | 0x03 0x17 0x01 0x00 |
This is done in the same manner that we did in the sending commands tutorial.
Now, let’s write the bytes to the “Command Request UUID” to enable the WiFi AP!
event.clear()
await client.write_gatt_char(COMMAND_REQ_UUID, bytearray([0x03, 0x17, 0x01, 0x01]))
await event.wait() # Wait to receive the notification response
Note that we have received the “Command Status” notification response from the Command Response characteristic since we enabled it’s notifications in Enable Notifications. This can be seen in the demo log:
INFO:root:Enabling the WiFi AP
INFO:root:Received response at handle=52: b'02:17:00'
INFO:root:Command sent successfully
INFO:root:WiFi AP is enabled
As expected, the response was received on the correct handle and the status was “success”.
Establish Connection to WiFi AP
If you have been following through the ble_enable_wifi.py
script, you will notice that it ends here such that
we know the WiFi SSID and password and the WiFi AP is enabled and ready to connect to. This is because there
are many different methods of connecting to the WiFi AP depending on your OS and the framework you are
using to develop. You could, for example, simply use your OS’s WiFi GUI to connect.
WiFi Demo
from the Open GoPro Python SDK.
Quiz time! 📚 ✏️
Troubleshooting
See the first tutorial’s troubleshooting section.
Good Job!
You are now connected to the GoPro’s Wifi AP and can send any of the HTTP commands defined in the Open GoPro Interface. Proceed to the next tutorial.