Skip to content

BLE dependencies seem to be included when using WiFi Provisioning #4125

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

Closed
jwktje opened this issue Jun 29, 2020 · 7 comments
Closed

BLE dependencies seem to be included when using WiFi Provisioning #4125

jwktje opened this issue Jun 29, 2020 · 7 comments

Comments

@jwktje
Copy link

jwktje commented Jun 29, 2020

Hardware:

Board: Wemos Lolin32
Core Installation version: Git master
IDE name: Arduino IDE
Flash Frequency: 40Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Mac OSX

Description:

When running the example for WiFi provisioning I noticed I had to change the partitions to something non-default to even allow the sketch to fit. This surprised me as I had only experienced this before when working with BLE.

However I wanted to use the SoftAP transport method for provisioning. I modified the example sketch for this. I will put my code below.

Expected behaviour:

When I use SoftAP and don't explicitly use the BLE transport for provisioning the compiled binary shouldn't be so massive as it currently is.

Actual behaviour:

Just excluding the "WiFi.beginProvision" excludes all dependencies it seems.
But as soon as this line is uncommented he Sketch jumps from 724.780 to 1.472.272 bytes.

Sketch:

#include "WiFi.h"

void SysProvEvent(system_event_t *sys_event,wifi_prov_event_t *prov_event)
{
    if(sys_event) {
      switch (sys_event->event_id) {
      case SYSTEM_EVENT_STA_GOT_IP:
          Serial.print("\nConnected IP address : ");
          Serial.println(ip4addr_ntoa(&sys_event->event_info.got_ip.ip_info.ip));
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("\nDisconnected. Connecting to the AP again... ");
          break;
      default:
          break;
      }      
    }

    if(prov_event) {
        switch (prov_event->event) {
        case WIFI_PROV_START:
            Serial.println("\nProvisioning started\nGive Credentials of your access point using \" Android app \"");
            break;
        case WIFI_PROV_CRED_RECV: { 
            Serial.println("\nReceived Wi-Fi credentials");
            wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)prov_event->event_data;
            Serial.print("\tSSID : ");
            Serial.println((const char *) wifi_sta_cfg->ssid);
            Serial.print("\tPassword : ");
            Serial.println((char const *) wifi_sta_cfg->password);
            break;
        }
        case WIFI_PROV_CRED_FAIL: { 
            wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)prov_event->event_data;
            Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
            if(*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 WIFI_PROV_CRED_SUCCESS:
            Serial.println("\nProvisioning Successful");
            break;
        case WIFI_PROV_END:
            Serial.println("\nProvisioning Ends");
            break;
        default:
            break;
        }      
    }
}

void setup() {
  Serial.begin(115200);
  
  //Delete saved credentials every time for debugging reasons
  WiFi.enableSTA(true); // must be sta to disconnect erase
  WiFi.disconnect(true,true);
  
  WiFi.onEvent(SysProvEvent);
  WiFi.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_EVENT_HANDLER_NONE, WIFI_PROV_SECURITY_1, "abcd1234", "PROV_ARDUINO_TEST");
}

void loop() {
  
}
@jwktje
Copy link
Author

jwktje commented Jun 30, 2020

Additionally I've found that after provisioning the Info messages suggest that the device will connect to the saved credentials. This doesn't actually occur however. I consider this a bug. Is it possible to make it actually start to connect to the saved credentials upon calling beginProvision?

The line that logs this is here

Below is the way I had to implement it in Arduino to actually have it conditionally connect or provision;

void provision_reset() {
  WiFi.enableSTA(true); // must be sta to disconnect erase
  WiFi.disconnect(true,true);

  delay(1000);
  
  Serial.println("Reset the thing");
}

void provision_start() {
  WiFi.onEvent(SysProvEvent);
  WiFi.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_EVENT_HANDLER_NONE, WIFI_PROV_SECURITY_1, "abcd1234", "SETUP_MORNING_WINNER");
}

void provision_init() {
  int con_status = WL_IDLE_STATUS;
  int retryCount = 0;
  while ( con_status != WL_CONNECTED) { // attempt to connect to Wifi network:
    if(retryCount < 6) {
      Serial.println("Attempting to connect to Network");
      con_status = WiFi.begin();
      delay(10000);// wait 10 seconds for connection:
      retryCount++;
    } else {
      Serial.println("Retry limit reached! Connection failed. Resetting");
      provision_reset();
      provision_start();
    }
    
  }
  Serial.println("You're connected to the network");
  Serial.println(WiFi.localIP());
}

@lbernstone
Copy link
Contributor

I don't know anything about the provisioning stuff, but when you connect to an AP, it should save your configuration. You can do something like this in your setup to check:

#include <WiFi.h>
#include <esp_wifi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_MODE_STA);
  wifi_config_t conf;
  esp_wifi_get_config(WIFI_IF_STA, &conf);
  String saved_ssid = String(reinterpret_cast<char*>(conf.sta.ssid));
  if(saved_ssid=="") {
    Serial.println("No saved SSID");
    // do provisioning
  } else {
    Serial.printf("Saved SSID: %s\n", saved_ssid);
    WiFi.begin();
  }
}

@sweetymhaiske
Copy link
Contributor

@jwktje Few fixes are made for BLE dependencies. While using SoftAP it won't be pulled.

@jwktje
Copy link
Author

jwktje commented Sep 3, 2020

@sweetymhaiske I had time to test it today and this seems to fix the size issue! The compiled size is now way lower. I will still have to functionally test it later, but I will close this issue for now. If anything comes up I will reopen.

Thanks you and @me-no-dev for the help on this!

@jwktje jwktje closed this as completed Sep 3, 2020
@ZXZ1126
Copy link

ZXZ1126 commented Oct 8, 2021

Hi @jwktje
When I try to verify the code, for both example code and the one u posted , I have this error on arduino
I did not change the code
'class WiFiClass' has no member named 'beginProvision'

Is it bcz the provision method is no longer supported by esp32 ? I cannot find the sample code from their github page alr.
If I still want to use, should I change to old version of WIFI.h?

Do you face similar issue?

big big thanks for your reply

Below is full error code
C:\Users\Shiyang\Desktop\WORK\Learning\ESP32_SOFTAP_PROVISIONGING\ESP32_SOFTAP_PROVISIONGING.ino: In function 'void setup()':
ESP32_SOFTAP_PROVISIONGING:62:8: error: 'class WiFiClass' has no member named 'beginProvision'
WiFi.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_EVENT_HANDLER_NONE, WIFI_PROV_SECURITY_1, "abcd1234", "PROV_ARDUINO_TEST");
^
ESP32_SOFTAP_PROVISIONGING:62:23: error: 'WIFI_PROV_SCHEME_SOFTAP' was not declared in this scope
WiFi.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_EVENT_HANDLER_NONE, WIFI_PROV_SECURITY_1, "abcd1234", "PROV_ARDUINO_TEST");
^
Multiple libraries were found for "WiFi.h"
Used: C:\Users\Shiyang\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi
Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
Not used: C:\Users\Shiyang\Documents\Arduino\libraries\WiFiNINA
Using library WiFi at version 1.0 in folder: C:\Users\Shiyang\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi
exit status 1
'class WiFiClass' has no member named 'beginProvision'

@sweetymhaiske
Copy link
Contributor

sweetymhaiske commented Oct 11, 2021

Hi @ZXZ1126,
beginProvision method has been shifted under WiFiProv object.
Here it is - https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFiProv/examples/WiFiProv/WiFiProv.ino#L49-L53
Make sure you include this library #include "WiFiProv.h"
Thanks!

@ZXZ1126
Copy link

ZXZ1126 commented Oct 11, 2021

@sweetymhaiske wah thanks have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants