BLE and MQTT sketch on ESP32 too big

I'm writing a sketch to use on an ESP32 WROOM 32D and am getting the error;

Sketch uses 1499609 bytes (114%) of program storage space. Maximum is 1310720 bytes. text section exceeds available space in board
Global variables use 56780 bytes (17%) of dynamic memory, leaving 270900 bytes for local variables. Maximum is 327680 bytes.
Sketch too big; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing it.

The sketch is to read the data over BLE from my fitness tracker and then publish it over MQTT. It doesn't seem that big so where have I gone wrong?

(I have tried a similar sketch I found online and that was the same. ESP32/ESP32_OLED_HRM at master · CuriousTimo/ESP32 · GitHub)

#include "BLEDevice.h"
//#include "BLEScan.h"
//#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <NewPing.h>

// The remote service we wish to connect to.
static BLEUUID serviceUUID("XXXXXXXXXXXXXXXXXXXXXXXX");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID(BLEUUID((uint16_t)0x2A37));
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

// Update these with values suitable for your network.

const char* ssid = "XXXXX";
const char* password = "XXXXXXXXX";
const char* mqtt_server = "XXXXXXXXXX";

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (20)
char msg[MSG_BUFFER_SIZE];
int value = 0;
String temp_str;
char temp[20];
int HRM;

void setup_wifi()
{
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "HRMnick";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("HRM/nick", "Running");
      // ... and resubscribe
      client.subscribe("HRM/nick/in");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

static void notifyCallback
(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify)
{

  if (length == 2)
  {
    HRM = pData[1];
    Serial.print("Heart Rate ");
    Serial.print(HRM, DEC);
    Serial.println("bpm");

    temp_str = String(HRM);
    temp_str.toCharArray(temp, temp_str.length() + 1);
    client.publish("HRM/nick/out", temp, false);
  }
}

class MyClientCallback : public BLEClientCallbacks
{
    void onConnect(BLEClient* pclient)
    {
      connected = true;
      Serial.println("onConnect");
    }

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

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

  BLEClient*  pClient  = BLEDevice::createClient();
  Serial.println(" - 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(" - 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("Failed to find our service UUID: ");
    Serial.println(serviceUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - 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("Failed to find our characteristic UUID: ");
    Serial.println(charUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - Found our characteristic");

  // Read the value of the characteristic.
  if (pRemoteCharacteristic->canRead())
  {
    std::string value = pRemoteCharacteristic->readValue();
    Serial.print("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("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);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  Serial.println("Starting Arduino BLE Client application...");
  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);
} // End of setup.


// This is the Arduino main loop function.
void loop()
{

  if (!client.connected())
  {
    reconnect();
  }
  client.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("We are now connected to the BLE Server.");
    }
    else
    {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    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 value to \"" + newValue + "\"");
    //Serial.println("\"" + 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.
} // End of loop

Try changing the partitions schema as pointed out by this old topic:
https://forum.arduino.cc/t/cant-fit-bluetoothserial-h-on-to-esp32/1175504/3

I've managed to preserve also OTA by using the following settings:

# Name,   Type, SubType,  Offset,  Size, Flags
nvs,      data, nvs,      36K,     20K,
otadata,  data, ota,      56K,     8K,
app0,     app,  ota_0,    64K,     1984K,
app1,     app,  ota_1,    2048K,   1984K,
spiffs,   data, spiffs,   4032K,   64K,

If your selected board has different partition schemes available, definitely try using one that allows a larger sketch, as @genemars1 indicated.

Just for fun, I tried out your sketch with the arduino-esp32 core and selecting my Firebeetle 32 board, which also uses an ESP32 WROOM 32D, I got the same "sketch too big" result. And there weren't any partition schemes available for that board.

Switching to the DFRobot firebeetle core, I could select the Firebeetle 32 board, set the flash size to 16M, and set the partition scheme to 'app3M_fat9M_16MB`, which allowed for a 3MB app. And then your sketch compiled.

I'm a little surprised that the apparently outdated DFRobot core had more options than Espressif's arduino-esp32 core. Maybe it'd be possible to copy some of the options out of the DFRobot boards.txt file and transplant them? I might give that a try if I ever run into the sketch size limit like you have.

Thank you, that was perfect. :grinning: :grinning:

I'll have to look into what makes a sketch large as I use other sketches that look much longer but are smaller!

From the very superficial reading I did earlier, it's the BLE library that's humongous.

1 Like

There's a solution, but does it work out?
As I understand it - BLE and WiFi, together, are not possible because it's the same transceiver (radio).

Yes it's working. :grinning:
I suffer really badly, and have done for 4 years, with Long covid and my pulse is all over the place so I wanted an Alexa notification system telling when I have to stop what I'm doing. (A shower will send my pulse into the 150's)

I do seem to be getting one problem where the program seems to stop/hangs. The serial print is as follows;

18:37:03.546 -> Connecting to Home
18:37:04.155 -> .
18:37:04.155 -> WiFi connected
18:37:04.155 -> IP address:
18:37:04.155 -> 192.168.3.10
18:37:04.155 -> Ready
18:37:04.155 -> IP address: 192.168.3.10
18:37:04.155 -> Starting Arduino BLE Client application...
18:37:04.858 -> BLE Advertised Device found: Name: , Address: 40:95:ee:d9:41:72, manufacturer data: 060001092002bee355bd016fedd69f88b28bdab98205b225364276e820, rssi: -48
18:37:04.858 -> BLE Advertised Device found: Name: , Address: 50:e9:8f:23:69:9e, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -77, serviceData: J#1R9F4⸮j!)⸮⸮⸮R⸮o⸮⸮S߰(
18:37:04.952 -> BLE Advertised Device found: Name: WHOOP 4C1194911, Address: dc:ae:6b:ea:85:35, serviceUUID: 0000180d-0000-1000-8000-00805f9b34fb, rssi: -80
18:37:04.999 -> Attempting MQTT connection...connected
18:37:04.999 -> Forming a connection to dc:ae:6b:ea:85:35
18:37:04.999 -> - Created client
18:37:06.967 -> - Connected to server

STOPS HERE

It seems as if its some communication problem with the connection to the BLE device that causes it but it's only occasionally and possibly only when I'm moving around and not so close to the ESP32.

So I suspect it's here-

  // 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(" - 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("Failed to find our service UUID: ");
    Serial.println(serviceUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - Found our service");

But as I don't do that much coding I'm not sure why.

Maybe it's not a coding issue but rather that the device needs more power expecially if you're feeding it with only 500mA (a common PC USB port) and other devices are using the same power source.

Pretty sure it's not that as they have dedicated PSU and I've tried a Pi5 PSU.

I've put a watchdog in the code for now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.