Disabling WiFi sniffing in LoRa32 v2.1

I’m using paxcounter and I would like to disable WiFi to solely scan BLE if I request it to do it.

In the following script (rcommand.cpp), I added a couple of functionalities, but the main one is the function of enabling/disabling WiFi given the command received:

void set_wifiscan(uint8_t val[]) {
ESP_LOGI(TAG, “Remote command: set WiFi scanner to %s”, val[0] ? “on” : “off”);
cfg.wifiscan = val[0] ? 1 : 0;
if (cfg.wifiscan)
wifi_sniffer_init();
else {
macs_wifi = 0; // clear BLE counter
ESP_LOGI(WiFi_TAG, “WiFi closing down…”);
//ESP_ERROR_CHECK( esp_wifi_disconnect());
esp_wifi_disconnect();
vTaskDelay(100 / portTICK_RATE_MS);
//ESP_ERROR_CHECK( esp_wifi_stop());
esp_wifi_stop();
vTaskDelay(100 / portTICK_RATE_MS);
//ESP_ERROR_CHECK( esp_wifi_deinit());
esp_wifi_deinit();
vTaskDelay(100 / portTICK_RATE_MS);
}
}

As suggested in these 3 websites:

  1. https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html#wi-fi-disconnect-phase
  2. https://github.com/espressif/esp-idf/issues/2050
  3. https://esp32.com/viewtopic.php?t=2512

But, the approach doesn’t work. When I implement this, the device gets restarted and I’m not sure what else should I try. I’ve seen others with my same problem, as well as other people reporting success in completing such task… but I can’t seem to get around it.

As the title suggests I’m using LoRa32 v2.1, but since these are all based on ESP32, getting this done on one of these boards is doing it for the rest.

Any help is appreciated, thanks for reading!