Using BLE to set up Wi-Fi for ESP32
Created May 5, 2020. Last modified May 5, 2020.
Becoming familiar with BLE Wi-Fi Config support
- Build the
ayla_demo
application and flash it to your ESP32 following the steps on the Ayla ESP32 Solution page. - Tap the reset button on the ESP32 board.
- If you’ve previously provisioned the ESP32 to a Wi-Fi network, type
reset factory
at the ESP32 CLI to clear the Wi-Fi configuration. - Proceed with the steps for iPhone or Android, depending on which device you are using.
Provisioning using Aura on iPhone
- Launch the Aura app on an iPhone.
- If you had previously provisioned and registered the ESP32 device, unregister it:
a. Swipe left on the device’s entry.
b. TapUnregister
.
c. When prompted, confirm by tappingUnregister
.
d. TapOK
. - Tap the
+
icon on the upper right. - Tap the list expander icon at the right end of the
Advanced
header. - Tap
BLE Wi-Fi Setup
. - Tap
Proceed
. - Tap
Setup Device
. - Tap
Scan for devices
. - Tap the on the device you are going to provision. There will normally be only one with a name like
Ayla1234
. - When prompted to enter a key to BLE pair, enter
123456
. - Tap
Pair
. - Tap
Scan for APs
. - Wait 15 seconds, then tap
Get scan result
. - Tap the entry for the Wi-Fi network you want to connect your ESP32 to.
- Enter the Wi-Fi password for the selected network.
- Tap
Connect
. - Wait for the message
In demo_cloud_up
on the ESP32 console. - Tap
Confirm device connection
. - Tap
Register device
. - Tap
Exit setup
. - Tap
< Add Device
at the top left. - Tap `Cancel at the top right.
- Observe the device you just added is displayed in the Devices list.
Provisioning using Aura on Android
- Launch the Aura app on an Android.
- If you had previously provisioned and registered the ESP32 device, unregister it:
a. Tap on the device’s entry in the Device List.
b. Tap the vertical ellipsis icon at the upper right.
c. TapUnregister Device
.
d. TapOK
to confirm. - Tap the
+
icon. - Tap the list expander icon at the right of the
Advanced
header. - Tap
Wifi setup using BLE
. - Tap
OK
. - Tap
SCAN FOR DEVICES
. - Tap on the device you are going to provision. There will normally be only one with a name like
Ayla1234
. - Swipe down from the top of the screen to reveal pending notifications.
- Tap
Pairing request
. - Enter
123456
. - Tap
OK
. - Tap
START DEVICE SCAN FOR APS
. - Tap the entry for the Wi-Fi network you want to connect your ESP32 to.
- Tap the
Show
check box. - Tap the
Wi-Fi Password
entry field. - Enter the Wi-Fi password for the selected network.
- Tap the enter key on the keyboard.
- Tap
CONNECT TO …
. - Wait for the message
In demo_cloud_up
on the ESP32 console. - Tap
CONFIRM DEVICE CONNECTION
. - Tap
REGISTER
. - Tap
EXIT SETUP
. - Observe the device you just added is displayed in the Device List.
Understanding how to integrate BLE Wi-Fi Config support
- Locate and open
examples/ayla_demo/demo_bt.c
, which provides an example of integrating BLE Wi-Fi configuration with an application. - Review
demo_bt_init
. This function performs the initialization to start the BLE service. It does the following:
a. Determines if Wi-Fi has already been configured and returns without starting BLE service, if it has
b. Initializes the ESP32 hardware and NimBLE stack
c. Initializes the NimBLE host task configuration structure
d. Initializes the NimBLE GAP, GATT and config subsystems
e. Initializes the Ayla Bluetooth abstraction layer
f. Sets an identify callback function, which, for example, might blink an LED when the identify BLE attribute is written
g. Initializes the generic Ayla BLE service
h. Initializes the Ayla connection BLE service
i. Initializes the Ayla Wi-Fi configuration BLE service
j. Starts the NimBLE host task
k. Generates and sets a BLE device name based on the BLE device address
l. Starts BLE advertisements - Review other functions in
demo_bt.c
. Your application will need to implement similar functionality to orchestrate BLE services.
a.demo_bt_host_task
implements the host task thread, which executes nimble_port_run to execute the BLE host stack.
b.demo_bt_advertise
configures and (re)starts BLE advertisements.
c.demo_bt_display_passkey
displays the code the user must enter when pairing. By default the passkey is 123456 for demo purposes. This should be changed to use a random number or other hard to predict value for greater security in production products.
d.demo_bt_gap_event_handler
implements handling for BLE GAP events. It orchestrates management of BLE connections. See ESP32 and NimBLE documentation for more information on handling GAP events.
e.demo_bt_gatt_register_cb
handles BLE GATT registration events. See ESP32 and NimBLE documentation for more information on handling GATT registration events. - Review the BLE configuration of the ESP IDF configured by the Ayla demo application.
a.cd $IDF_PATH/examples/ayla_demo
.
b.make menuconfig
.
c. Browse toComponent config > Bluetooth
.
d. Browse through all of the Bluetooth configuration, noting option settings, which you will need to make in your application’s IDF configuration later.
Enabling BLE Wi-Fi support in your application
- Configure the ESP IDF BLE options to enable the NimBLE Bluetooth stack based on setting used by the Ayla demo application
- Integrate similar functionality to demo_bt.c into your application to orchestrate BLE operations
BLE Wi-Fi Configuration Implementation
The Ayla agent implements three BLE GATT services that realize the Wi-Fi configuration and device registration features:
The Ayla GATT services are built on top of generic GATT services provided by the ESP32 NimBLE stack.
The application implements the code to orchestrate BLE services (see demo_bt.c
), initializing and starting the NimBLE host thread, as well as registering each of the Ayla GATT services. The application manages BLE advertisements, which enable the device to be discovered and connected to. The application manages BLE connections, while the Ayla GATT services implement BLE attributes that provide for Wi-Fi provisioning and registering the device to a specific user account.
A mobile application uses BLE to discover the device, securely connect to it, instruct the device to scan for Wi-Fi networks, configure the device to connect to a particular Wi-Fi network, and register the device to a specific user.
Files
lib/adb/adb_ayla_svc.*
implements the Ayla generic GATT service.lib/adb/adb_conn_svc.*
implements the Ayla connectivity GATT service.lib/adb/adb_wifi_cfg_svc.*
implements the Ayla Wi-Fi configuration GATT service.lib/adb/adb.*
implements a framework for Ayla’s GATT services.lib/adb/al_bt_esp32.*
implements adaptation layer to ESP32 NimBLE stack.examples/ayla_demo/main/demo_bt.c
implements application orchestration of BLE services.
Build Switches
The define symbol AYLA_BLUETOOTH_SUPPORT is used as a compile time switch to enable or disable inclusion of Bluetooth features. Defining this symbol enables compilation of Bluetooth support. This symbol is defined by default. It is defined in the following make files:
components/ayla/component.mk
examples/ayla_demo/Makefile
Updated about 4 years ago