FreeRTOS tasks not running when using BLE and WiFi simultaneously

I've four FreeRTOS tasks that after after WiFi connects to a AP (then it's turned off) and BLE is started. I don't know what is going wrong.

Minimal not working sketch

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

const char* ssid       = "my wifi ssid";
const char* password   = "my wifi password";

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
		void onResult(BLEAdvertisedDevice advertisedDevice)
		{
			Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
		}
};

void task_1( void * parameter )
{
	/* loop forever */
	for (;;) {
		Serial.println("this is Task 1");
		delay(1000);
	}
	/*  delete a task when finish,
	    this will never happen because this is infinity loop */
	vTaskDelete( NULL );
}

void task_2( void * parameter )
{
	/* loop forever */
	for (;;) {
		Serial.println("this is Task 2");
		delay(1000);
	}
	/*  delete a task when finish,
	    this will never happen because this is infinity loop */
	vTaskDelete( NULL );
}

void task_3( void * parameter )
{
	/* loop forever */
	for (;;) {
		Serial.println("this is Task 3");
		delay(1000);
	}
	/*  delete a task when finish,
	    this will never happen because this is infinity loop */
	vTaskDelete( NULL );
}

void task_4( void * parameter )
{
	/* loop forever */
	for (;;) {
		Serial.println("this is Task 4");
		delay(1000);
	}
	/*  delete a task when finish,
	    this will never happen because this is infinity loop */
	vTaskDelete( NULL );
}

void setup()
{
	Serial.begin(115200);

	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}
	Serial.println(" CONNECTED");
	//	WiFi.disconnect(true);
	//	WiFi.mode(WIFI_OFF);

	Serial.println("Scanning...");

	BLEDevice::init("");
	pBLEScan = BLEDevice::getScan(); //create new scan
	pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
	pBLEScan->setActiveScan(
	    true); //active scan uses more power, but get results faster
	pBLEScan->setInterval(100);
	pBLEScan->setWindow(99);  // less or equal setInterval value

	Serial.println("Starting task");
	xTaskCreate(task_1, /* Task function. */
	            "Task 1", /* name of task. */
	            10000, /* Stack size of task */
	            NULL, /* parameter of the task */
	            1, /* priority of the task */
	            NULL); /* Task handle to keep track of created task */
	xTaskCreate(task_2, /* Task function. */
	            "Task 2", /* name of task. */
	            10000, /* Stack size of task */
	            NULL, /* parameter of the task */
	            1, /* priority of the task */
	            NULL); /* Task handle to keep track of created task */
	xTaskCreate(task_3, /* Task function. */
	            "Task 3", /* name of task. */
	            10000, /* Stack size of task */
	            NULL, /* parameter of the task */
	            1, /* priority of the task */
	            NULL); /* Task handle to keep track of created task */
	xTaskCreate(task_4, /* Task function. */
	            "Task 4", /* name of task. */
	            10000, /* Stack size of task */
	            NULL, /* parameter of the task */
	            1, /* priority of the task */
	            NULL); /* Task handle to keep track of created task */

    Serial.println("Tasks initiated");
}

void loop()
{
	// put your main code here, to run repeatedly:
	BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
	Serial.print("Devices found: ");
	Serial.println(foundDevices.getCount());
	Serial.println("Scan done!");
	pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
	delay(2000);
}

I get to see the "Task Initiated" statement on Serial monitor but no prints from the tasks are printed.

I do not have a conclusive answer. Under the Arduino implementation on ESP32: (default)
cpu0 handles the Espressif RF and housekeeping tasks,
cpu1 handle tasks on the Arduino side.

When we create tasks on the Arduino side and insert them into cpu0's queue, there is a risk at starving some RF protocol or critical timing unknown by the Arduino programmer.

The code I have seen and used for fun has always been prefixed with the understanding that the Arduino programmer can "break" stuff on the Espressif radios.

I have attached my play project which is a multitab-IDE project that separates the code into tabs... easily extendable and may be useful to your experiments (and maybe not :laughing: )

Ray
DualCore.zip (2.2 KB)

Hi @brahmajit

You haven't said which board or port FreeRTOS you're using.

On some FreeRTOS versions, it's necessary to start the scheduler with the command:

vTaskStartScheduler();              // Start the scheduler

This is usually called after the tasks have been created.

Hi @MartinL , sorry for the late reply.

After a little bit debugging I've found that the issue is with either BLE or WiFi. Whenever I remove either of those from my code, the tasks functions as intended.

Don't know what's going wrong.

Found the issue. The WiFi.disconnect() and WiFi.mode(WIFI_OFF) didn't deinit the wifi properly. So I had to call the esp_wifi_deinit() idf function.