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

  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:
    a. Swipe left on the device’s entry.
    b. Tap Unregister.
    c. When prompted, confirm by tapping Unregister.
    d. 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:
    a. Tap on the device’s entry in the Device List.
    b. Tap the vertical ellipsis icon at the upper right.
    c. Tap Unregister Device.
    d. 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:
    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
  3. 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.
  4. 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 to Component 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

  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