Using BLE to set up Wi-Fi for ESP32

By Steve Grau. Created May 5, 2020. Last modified May 5, 2020.

Becoming familiar with BLE Wi-Fi Config support

  1. Build the ayla_demo application and flash it to your ESP32 following the steps on the Ayla ESP32 Solution page.
  2. Tap the reset button on the ESP32 board.
  3. 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.
  4. Proceed with the steps for iPhone or Android, depending on which device you are using.

Provisioning using Aura on iPhone

  1. Launch the Aura app on an iPhone.
  2. If you had previously provisioned and registered the ESP32 device, unregister it:
    1. Swipe left on the device’s entry.
    2. Tap Unregister.
    3. When prompted, confirm by tapping Unregister.
    4. Tap OK.
  3. Tap the + icon on the upper right.
  4. Tap the list expander icon at the right end of the Advanced header.
  5. Tap BLE Wi-Fi Setup.
  6. Tap Proceed.
  7. Tap Setup Device.
  8. Tap Scan for devices.
  9. Tap the on the device you are going to provision. There will normally be only one with a name like Ayla1234.
  10. When prompted to enter a key to BLE pair, enter 123456.
  11. Tap Pair.
  12. Tap Scan for APs.
  13. Wait 15 seconds, then tap Get scan result.
  14. Tap the entry for the Wi-Fi network you want to connect your ESP32 to.
  15. Enter the Wi-Fi password for the selected network.
  16. Tap Connect.
  17. Wait for the message In demo_cloud_up on the ESP32 console.
  18. Tap Confirm device connection.
  19. Tap Register device.
  20. Tap Exit setup.
  21. Tap < Add Device at the top left.
  22. Tap `Cancel at the top right.
  23. Observe the device you just added is displayed in the Devices list.

Provisioning using Aura on Android

  1. Launch the Aura app on an Android.
  2. If you had previously provisioned and registered the ESP32 device, unregister it:
    1. Tap on the device’s entry in the Device List.
    2. Tap the vertical ellipsis icon at the upper right.
    3. Tap Unregister Device.
    4. Tap OK to confirm.
  3. Tap the + icon.
  4. Tap the list expander icon at the right of the Advanced header.
  5. Tap Wifi setup using BLE.
  6. Tap OK.
  7. Tap SCAN FOR DEVICES.
  8. Tap on the device you are going to provision. There will normally be only one with a name like Ayla1234.
  9. Swipe down from the top of the screen to reveal pending notifications.
  10. Tap Pairing request.
  11. Enter 123456.
  12. Tap OK.
  13. Tap START DEVICE SCAN FOR APS.
  14. Tap the entry for the Wi-Fi network you want to connect your ESP32 to.
  15. Tap the Show check box.
  16. Tap the Wi-Fi Password entry field.
  17. Enter the Wi-Fi password for the selected network.
  18. Tap the enter key on the keyboard.
  19. Tap CONNECT TO ….
  20. Wait for the message In demo_cloud_up on the ESP32 console.
  21. Tap CONFIRM DEVICE CONNECTION.
  22. Tap REGISTER.
  23. Tap EXIT SETUP.
  24. Observe the device you just added is displayed in the Device List.

Understanding how to integrate BLE Wi-Fi Config support

  1. Locate and open examples/ayla_demo/demo_bt.c, which provides an example of integrating BLE Wi-Fi configuration with an application.
  2. Review demo_bt_init. This function performs the initialization to start the BLE service. It does the following:
    1. Determines if Wi-Fi has already been configured and returns without starting BLE service, if it has
    2. Initializes the ESP32 hardware and NimBLE stack
    3. Initializes the NimBLE host task configuration structure
    4. Initializes the NimBLE GAP, GATT and config subsystems
    5. Initializes the Ayla Bluetooth abstraction layer
    6. Sets an identify callback function, which, for example, might blink an LED when the identify BLE attribute is written
    7. Initializes the generic Ayla BLE service
    8. Initializes the Ayla connection BLE service
    9. Initializes the Ayla Wi-Fi configuration BLE service
    10. Starts the NimBLE host task
    11. Generates and sets a BLE device name based on the BLE device address
    12. Starts BLE advertisements
  3. Review other functions in demo_bt.c. Your application will need to implement similar functionality to orchestrate BLE services.
    1. demo_bt_host_task implements the host task thread, which executes nimble_port_run to execute the BLE host stack.
    2. demo_bt_advertise configures and (re)starts BLE advertisements.
    3. 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.
    4. 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.
    5. demo_bt_gatt_register_cb handles BLE GATT registration events. See ESP32 and NimBLE documentation for more information on handling GATT registration events.
  4. Review the BLE configuration of the ESP IDF configured by the Ayla demo application.
    1. cd $IDF_PATH/examples/ayla_demo.
    2. make menuconfig.
    3. Browse to Component config > Bluetooth.
    4. 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

  1. Configure the ESP IDF BLE options to enable the NimBLE Bluetooth stack based on setting used by the Ayla demo application
  2. 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