Page 1 of 1

ESP32-C6 - Sketch size too large when using both Wifi & BLE libraries

Posted: Mon Apr 22, 2024 6:31 pm
by a.foss
I started attempting to get my C6 boards using both BLE & Wifi by first mixing together some of ESP's provided examples.

Using the basic wifi connect example it uses about 64% of program storage space, and the BLE client example uses 81%.

when combined I get an error: `Sketch uses 1350620 bytes (103%) of program storage space. Maximum is 1310720 bytes.`

I thought the C6 was supposed to have 8MB of flash. Clearly I'm misunderstanding something. I've provided the code I'm using below. Is there a way to somehow squeeze both BLE and Wifi out of the ESP32-C6 simultaneously?

[Codebox]
#include <WiFi.h>

#include "BLEDevice.h"

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

// The remote service we wish to connect to.
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
// Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
// Serial.print(" of data length ");
// Serial.println(length);
// Serial.print("data: ");
// Serial.write(pData, length);
// Serial.println();
}

class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
}

void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};

bool connectToServer() {
Serial.print(F("Forming a connection to "));
Serial.println(myDevice->getAddress().toString().c_str());

BLEClient* pClient = BLEDevice::createClient();
Serial.println(F(" - Created client"));

pClient->setClientCallbacks(new MyClientCallback());

// Connect to the remove BLE Server.
pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(F(" - Connected to server"));
pClient->setMTU(517); //set client to request maximum MTU from server (default is 23 otherwise)

// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
// Serial.print(F("Failed to find our service UUID: "));
// Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(F(F(" - Found our service")));


// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print(F("Failed to find our characteristic UUID: "));
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(F(" - Found our characteristic"));

// Read the value of the characteristic.
if(pRemoteCharacteristic->canRead()) {
String value = pRemoteCharacteristic->readValue();
Serial.print(F("The characteristic value was: "));
Serial.println(value.c_str());
}

if(pRemoteCharacteristic->canNotify())
pRemoteCharacteristic->registerForNotify(notifyCallback);

connected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
/**
* Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print(F("BLE Advertised Device found: "));
Serial.println(advertisedDevice.toString().c_str());

// We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;

} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks


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

pinMode(LED_BUILTIN, OUTPUT);

Serial.println();
Serial.print("[WiFi] Connecting to Wifi");

WiFi.begin(ssid, password);

// Will try for about 10 seconds (20x 500ms)
int tryDelay = 500;
int numberOfTries = 20;

// Wait for the WiFi event
while (true) {

switch(WiFi.status()) {
case WL_NO_SSID_AVAIL:
Serial.println(F("[WiFi] SSID not found"));
break;
case WL_CONNECT_FAILED:
Serial.print(F("[WiFi] Failed - WiFi not connected! Reason: "));
return;
break;
case WL_CONNECTION_LOST:
Serial.println(F("[WiFi] Connection was lost"));
break;
case WL_SCAN_COMPLETED:
Serial.println(F("[WiFi] Scan is completed"));
break;
case WL_DISCONNECTED:
Serial.println(F("[WiFi] WiFi is disconnected"));
break;
case WL_CONNECTED:
Serial.println(F("[WiFi] WiFi is connected!"));
Serial.print(F("[WiFi] IP address: "));
Serial.println(WiFi.localIP());
return;
break;
default:
Serial.print(F("[WiFi] WiFi Status: "));
Serial.println(WiFi.status());
break;
}
delay(tryDelay);

if(numberOfTries <= 0){
Serial.print(F("[WiFi] Failed to connect to WiFi!"));
// Use disconnect function to force stop trying to connect
WiFi.disconnect();
return;
} else {
numberOfTries--;
}
}

BLEDevice::init("");

// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds.
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);

}

void loop()
{

// If the flag "doConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true) {
if (connectToServer()) {
Serial.println(F("We are now connected to the BLE Server."));
} else {
Serial.println(F("We have failed to connect to the server;"));
}
doConnect = false;
}

// If we are connected to a peer BLE Server, update the characteristic each time we are reached
// with the current time since boot.
if (connected) {
String newValue = "Time since boot: " + String(millis()/1000);
Serial.println("Setting new characteristic: \"" + newValue + "\"");

// Set the characteristic's value to be the array of bytes that is actually a string.
pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
}else if(doScan){
BLEDevice::getScan()->start(0); // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
}

delay(1000); // Delay a second between loops.

}
[/Codebox]

Re: ESP32-C6 - Sketch size too large when using both Wifi & BLE libraries

Posted: Tue Apr 23, 2024 12:49 am
by ESP_Sprite
You're likely runnning into issues wrt how the partitions on the chip are defined. In Arduino, you can probably change that under Tools > Partition scheme.