Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE provisioning sample doesn't work on release/v2.x branch #8176

Open
1 task done
sidwarkd opened this issue May 9, 2023 · 46 comments · May be fixed by #11167
Open
1 task done

BLE provisioning sample doesn't work on release/v2.x branch #8176

sidwarkd opened this issue May 9, 2023 · 46 comments · May be fixed by #11167
Assignees
Labels
Area: BLE Issues related to BLE bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Type: Bug 🐛 All bugs
Milestone

Comments

@sidwarkd
Copy link

sidwarkd commented May 9, 2023

Board

ESP32 Dev Module

Device Description

DevKitC plain

Hardware Configuration

No additional hardware connected.

Version

latest master (checkout manually)

IDE Name

VSCode

Operating System

Linux

Flash frequency

40MHz

PSRAM enabled

no

Upload speed

2000000

Description

The use of esp_bt_controller_mem_release does not seem correct in esp32-hal-misc.c. In the function initArduino() if you have set BT_ENABLED in sdkconfig, it will call btInUse to determine whether to release the BT memory. There are two problems.

First, in using the sample sketch below (a slightly modified version of the provisioning sample included with this library) it does not appear to link correctly. The sample will fail to run properly as the BT memory is released in initArduino so trying to enable BT later for provisioning fails. The weak linking is not working as expected.

Secondly, this pattern seems wrong as well. The code in initArduino() would imply that you could have CONFIG_BT_ENABLED but have btInUse() return false which, per the code, would never be possible. If CONFIG_BT_ENABLED is yes then esp32-hal-bt.c declares btInUse() to return true always. So there is no scenario where if the config is enabled btInUse() should return false (although it does per the first problem mentioned above).

This makes the following block of code in esp32-hal-misc.c puzzling:

#ifdef CONFIG_BT_ENABLED
    if(!btInUse()){
        esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
    }
#endif

To get the provisioning example code to work with BLE the call to esp_bt_controller_mem_release must NOT be called in initArduino.

Sketch

#include "WiFiProv.h"
#include "WiFi.h"
#include "nvs_flash.h"
#include "nvs.h"

bool is_provisioned = false;
void SysProvEvent(arduino_event_t *sys_event)
{
    switch (sys_event->event_id)
    {
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
        Serial.print("\nConnected IP address : ");
        Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
        is_provisioned = true;
        break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
        Serial.println("\nDisconnected. Connecting to the AP again... ");
        break;
    case ARDUINO_EVENT_PROV_START:
        Serial.println("\nProvisioning started\nGive Credentials of your access point using \" Android app \"");
        break;
    case ARDUINO_EVENT_PROV_CRED_RECV:
    {
        Serial.println("\nReceived Wi-Fi credentials");
        Serial.print("\tSSID : ");
        Serial.println((const char *)sys_event->event_info.prov_cred_recv.ssid);
        Serial.print("\tPassword : ");
        Serial.println((char const *)sys_event->event_info.prov_cred_recv.password);
        break;
    }
    case ARDUINO_EVENT_PROV_CRED_FAIL:
    {
        Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
        if (sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR)
            Serial.println("\nWi-Fi AP password incorrect");
        else
            Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()");
        break;
    }
    case ARDUINO_EVENT_PROV_CRED_SUCCESS:
        Serial.println("\nProvisioning Successful");
        break;
    case ARDUINO_EVENT_PROV_END:
        Serial.println("\nProvisioning Ends");
        break;
    default:
        break;
    }
}

void setup()
{
    Serial.begin(115200);
    WiFi.onEvent(SysProvEvent);
    WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, "abcd1234", "Prov_123");
}

void loop()
{
    if(is_provisioned)
    {
        Serial.println("Connected to Wi-Fi and ready to run main application");
        delay(5000);
    }
    else
    {
        Serial.println("Waiting for Wi-Fi credentials. Open app to get started.");
        delay(5000);
    }
}


### Debug Message

```plain
E (912) wifi_prov_scheme_ble: bt_mem_release of classic BT failed 259
I (920) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1022) wifi:mode : sta (ac:0b:fb:6c:f4:8c)
I (1023) wifi:enable tsf
I (1024) wifi:Set ps type: 1

E (1027) simple_ble: simple_ble_start enable controller failed 259
E (1028) protocomm_ble: simple_ble_start failed w/ error code 0x103
E (1035) wifi_prov_scheme_ble: Failed to start protocomm BLE service
E (1041) wifi_prov_mgr: Failed to start service

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@sidwarkd sidwarkd added the Status: Awaiting triage Issue is waiting for triage label May 9, 2023
@sidwarkd sidwarkd changed the title esp_bt_controller_mem_release being called in initArduino() when CONFIG_BT_ENABLED=y BLE provisioning sample doesn't work May 9, 2023
@mrengineer7777 mrengineer7777 added Status: Needs investigation We need to do some research before taking next steps on this issue Type: Example Issue is related to specific example. Area: BLE Issues related to BLE and removed Status: Awaiting triage Issue is waiting for triage labels May 10, 2023
@MingyaoLiu
Copy link

Same issue on ESP32-C3 and 2.0.9.

@MingyaoLiu
Copy link

@sidwarkd do you have a temporary fix for this? I tried disable the memory release, however when I try to connect to it through rainmaker app, it always fails and no error in console.

@VojtechBartoska
Copy link
Contributor

can you please help with triage @PilnyTomas? Thanks

@sidwarkd
Copy link
Author

@MingyaoLiu Yes, the fix which worked for me was to simply comment out the call to esp_bt_controller_mem_release since, once you release that memory, it cannot be reclaimed per the docs. Here is a link to my fork with the change on line 261 of esp32-hal-misc.c. https://github.com/deploythefleet/arduino-esp32/blob/master/cores/esp32/esp32-hal-misc.c#L261. No other changes were required to get the sample working.

@PilnyTomas
Copy link
Contributor

PilnyTomas commented Aug 11, 2023

Using the provided sketch, I was not able to reproduce the mentioned Error.
Using ESP32, latest arduino-esp32 master branch 2.0.11
Could you please retest on current version?

@PilnyTomas PilnyTomas added the Resolution: Awaiting response Waiting for response of author label Aug 11, 2023
@VojtechBartoska VojtechBartoska added Resolution: Unable to reproduce With given information issue is unable to reproduce and removed Status: Needs investigation We need to do some research before taking next steps on this issue labels Aug 15, 2023
@VojtechBartoska VojtechBartoska self-assigned this Aug 15, 2023
@VojtechBartoska
Copy link
Contributor

Closing this issue as expired, if needed you can reopen.

@VojtechBartoska VojtechBartoska added Resolution: Expired More info wasn't provided and removed Resolution: Awaiting response Waiting for response of author labels Oct 2, 2023
@sidwarkd
Copy link
Author

@PilnyTomas I am not able to use the master branch as it requires v5.1 of the IDF. When I check out the 2.0.11 tag I know longer get the error but I also do not get any WiFi networks returned in the provisioning app. If I roll back to my fixed branch I see them appear again. Then I tried different tags:

2.0.11 - No error but no WiFi networks returned in the app
2.0.12 - Back to getting the error again
2.0.13 - Same error
2.0.14 - Same error

So at least on the 2.x release it seems like this was kind of fixed and then regressed again. Is there some other commit I should try?

@sidwarkd
Copy link
Author

Researching this more and definitely a regression. The following shows 2.0.9 on the left, 2.0.10 in the middle and then 2.0.12 on the right which essentially puts it back to the way it was. So definitely broken again in 2.x. Will this be fixed in 2.x?

image

@me-no-dev
Copy link
Member

@SuGlider PTAL.

@SuGlider
Copy link
Collaborator

@sidwarkd / @me-no-dev - I have made it work using the sketch presented in this issue.
Important details:

  • Upload the Sketch using the Erase All Flash Before Sketch Upload: "Enabled" in order to erase previous WiFi/BLE sertup.
  • In the ESP BLE Prov APP don't forget to change the Prefix to the one the sketch is using with Lower Case Prov_

These are the step that I used to make it work:
1- Downloaded the ESP BLE Prov APP in my Android phone.
2- Uploaded the sketch to the ESP32 - USING the option Erase All Flash Before Sketch Upload: "Enabled"
3- Turn the Android phone Bluetooth ON and open the ESP BLE Prov APP.
4- Click on the "Provision New Device" button
5- Ignore the QR Scan and click on "I don't have a QR code" button. Change the Prefix to lower case "Prov_"
6- Android will scan BLE device. It shall list "Prov_123" in the Devices List. If necesary click on "Scan Again"
7- Click the device "Prov_123"
8- Confirm the proof of possession PIN for Prov_123 as "abcd1234" and click the "Next ->" button
9- Click on "Join Other Network" and enter your SSID/Password for your local WiFi network. Attention to Lower/Upper case.
10- Press the "PROVISION" button and wait for the process to complete.

This is the output:

ts Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1416
load:0x40078000,len:14804
load:0x40080400,len:4
load:0x40080404,len:3356
entry 0x4008059c
Waiting for Wi-Fi credentials. Open app to get started.
Provisioning started
Give Credentials of your access point using " Android app "

Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.
Waiting for Wi-Fi credentials. Open app to get started.

Received Wi-Fi credentials
	SSID : MyWiFiSSID
	Password : MyWiFiSSID_PWD
Waiting for Wi-Fi credentials. Open app to get started.

Connected IP address : 192.168.3.128

Provisioning Successful

Provisioning Ends
Connected to Wi-Fi and ready to run main application
Connected to Wi-Fi and ready to run main application
Connected to Wi-Fi and ready to run main application

@SuGlider
Copy link
Collaborator

I have used Arduino Core 3.0.0-alpha2 ...

@SuGlider
Copy link
Collaborator

@sidwarkd

2.0.11 - No error but no WiFi networks returned in the app

I also get no list of WiFi networks in the APP, but I can set it manually with "Join Other Network" button.

@sidwarkd
Copy link
Author

@SuGlider On older versions the scan actually returns a list of WiFi networks but the later versions do not. I was able to "Join Other Network" but the flow is supposed to return a list of available WiFi networks. That is a bug, right?

@SuGlider
Copy link
Collaborator

SuGlider commented Nov 1, 2023

The quesiton would be if this is an APP issue or ESP32 Software issue. Both could supply the list of WiFi SSIDs, given that both shall be close to each other for a BLE connection.

@SuGlider
Copy link
Collaborator

SuGlider commented Nov 1, 2023

@sidwarkd - I have just run all over again here with Arduino Core 3.0.0-Alpha2 and it is running perfectly fine, including listing the WiFi SSIDs in my Android APP.

Sometime it doesn't list it immediately, but it seems to be running fine with no bugs.

@SuGlider
Copy link
Collaborator

SuGlider commented Nov 1, 2023

@sidwarkd - Please try it again from scratch. It shall run fine.

@VojtechBartoska
Copy link
Contributor

@azmi-plavaga can you please retest on version 3.1.0-RC1?

@azmi-plavaga
Copy link

@VojtechBartoska E (127) network_prov_scheme_ble: bt_mem_release of classic BT failed 259 with 3.1.0-RC1 as well.

[env:development]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10+rc1/platform-espressif32.zip
board = esp32dev

PLATFORM: Espressif 32 (53.3.10+rc1) > Espressif ESP32 Dev Module

PACKAGES: 
 - framework-arduinoespressif32 @ 3.1.0 
 - framework-arduinoespressif32-libs @ 5.3.0+sha.466a392a76 
 - ...
 
Dependency Graph
|-- WiFi @ 3.1.0
|-- WiFiProv @ 3.1.0

Hard resetting via RTS pin...
--- Terminal on /dev/ttyUSB0 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
[   123][E][STA.cpp:343] connect(): STA connect failed! 0x300a: ESP_ERR_WIFI_SSID

WiFi not provisioned. Starting BLE provisioning...
E (127) network_prov_scheme_ble: bt_mem_release of classic BT failed 259
E (131) simple_ble: simple_ble_start enable controller failed 259
E (131) protocomm_ble: simple_ble_start failed w/ error code 0x103
E (138) network_prov_scheme_ble: Failed to start protocomm BLE service
E (144) network_prov_mgr: Failed to start service
[   154][E][WiFiProv.cpp:155] beginProvision(): network_prov_mgr_start_provisioning failed!

@VojtechBartoska
Copy link
Contributor

@lucasssvaz can you please retest this on RC2? Thanks a lot

@VojtechBartoska VojtechBartoska modified the milestones: 2.0.15, 3.1.0 Nov 4, 2024
@lucasssvaz
Copy link
Collaborator

@VojtechBartoska The sketches from this issue and the examples both with ESP32 and ESP32-C3. Everything worked as expected.

@rftafas rftafas modified the milestones: 3.1.0, 3.1.1 Jan 6, 2025
@slhddn
Copy link

slhddn commented Jan 12, 2025

I am having the exact same problem. It was working fine on Arduino IDE. I migrated the project to PlatformIO. After migration, WiFi BLE Provisioning stopped working. Please see the log below:

E (10920) simple_ble: simple_ble_start enable controller failed 259
E (10921) protocomm_ble: simple_ble_start failed w/ error code 0x103
E (10922) wifi_prov_scheme_ble: Failed to start protocomm BLE service
E (10928) wifi_prov_mgr: Failed to start service
[ 5284][E][WiFiProv.cpp:151] beginProvision(): wifi_prov_mgr_start_provisioning failed!

Thanks

Edit:
As mentioned above, I made it work by commenting the line below:

esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);

FYI

@rftafas rftafas modified the milestones: 3.1.1, 3.2.0 Jan 14, 2025
@lucasssvaz
Copy link
Collaborator

@Jason2866 Have you ever seen this issue ?

@Jason2866
Copy link
Collaborator

Without an example sketch and the used platformio.ini can't say anything.
Can't imagine there is an issue, since Arduino code is same as when using ArduinoIDE.

@rftafas
Copy link
Collaborator

rftafas commented Feb 18, 2025

Closing this. Seems to be a migration issue or something else, using old release.

If tested against current/updated software and problem persists, open new issue.

@kumardeo
Copy link

kumardeo commented Mar 21, 2025

Facing same issue (runtime error) when using PlatformIO IDE (platform: pioarduino | core: v3.1.3) and ESP32 (nodemcu-32s) board.
The same code works as expected when using Arduino IDE (core: v3.1.3).

The runtime error:

E (247) simple_ble: simple_ble_start enable controller failed 259
E (248) protocomm_ble: simple_ble_start failed w/ error code 0x103
E (249) network_prov_scheme_ble: Failed to start protocomm BLE service
E (255) network_prov_mgr: Failed to start service
[   265][E][WiFiProv.cpp:165] beginProvision(): network_prov_mgr_start_provisioning failed!

src/main.cpp
An example from https://github.com/espressif/arduino-esp32/tree/master/libraries/RainMaker/examples/RMakerSwitch
Or:

#include <Arduino.h>
#include <WiFiProv.h>

const uint8_t LED_PIN = 2;
const char *pop = "12345678";                // Proof of possession - otherwise called a PIN - string provided by the device, entered by the user in the phone app
const char *service_name = "PROV_MYAPP";  // Name of your device (the Espressif apps expects by default device name starting with "Prov_")
const char *service_key = NULL;              // Password used for SofAP method (NULL = no password needed)
bool reset_provisioned = true;               // When true the library will automatically delete previously provisioned data.

void setup() {
  pinMode(2, OUTPUT);

  Serial.begin(115200);

  Serial.println("Begin Provisioning using BLE");
  // Sample uuid that user can pass during provisioning using BLE
  uint8_t uuid[16] = { 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02 };
  WiFiProv.beginProvision(
    NETWORK_PROV_SCHEME_BLE, NETWORK_PROV_SCHEME_HANDLER_FREE_BLE, NETWORK_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned);
  Serial.println("You may use this BLE QRCode:");
  WiFiProv.printQR(service_name, pop, "ble");
}

void loop() {
  bool isConnected = WiFi.isConnected();
  if (isConnected) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
    delay(200);
    digitalWrite(LED_PIN, HIGH);
    delay(200);
  }
}

platformio.ini

[env:esp32dev]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.13/platform-espressif32.zip
board = esp32dev
framework = arduino
board_build.partitions = rainmaker_4MB_no_ota.csv
monitor_speed = 115200

@Jason2866 Sorry to disturb, can you please take a look at this?

@soile1991
Copy link

Hey @kumardeo ,

I had the same problem when I was working with ESP32.

I added this to my platformio.ini and it worked.


build_flags = 
	-D SOC_WIFI_SUPPORTED=1
	-D BLUEDROID_ENABLED=1
	-D CONFIG_BT_ENABLED=1
	-D CONFIG_BT_BLE_ENABLED=1

Hope it helps!

@kumardeo
Copy link

I had the same problem when I was working with ESP32.

I added this to my platformio.ini and it worked.

Didn't work for me :(

@Jason2866
Copy link
Collaborator

Issue can be reproduced with this example https://github.com/pioarduino/test-rainmaker/tree/wifiprov

Fix as suggested here is working #8176 (comment) to solve the issue

@Jason2866 Jason2866 added Type: Bug 🐛 All bugs bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. and removed Resolution: Unable to reproduce With given information issue is unable to reproduce Type: Example Issue is related to specific example. labels Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BLE Issues related to BLE bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Type: Bug 🐛 All bugs
Projects
Development

Successfully merging a pull request may close this issue.

17 participants