Shrinking down code to upload to an ESP32 devkitC board

Howdy,
I dont know if this is the correct page spesifically to post this in but...

I am currently trying to get an ESP32 devkitC to communicate with my phone over an app I made but anyways, when I tried to upload the code to the ESP32, it says that there is not enough storage on the ESP32 module and I was wondering if you guys have any tips for shrinking down this code.

Here is the error I am currently getting:

Sketch uses 1392705 bytes (106%) of program storage space. Maximum is 1310720 bytes.
text section exceeds available space in boardGlobal variables use 46152 bytes (14%) of dynamic memory, leaving 281528 bytes for local variables. Maximum is 327680 bytes.

and here is the code:

#include <WiFi.h>
#include <HTTPClient.h>
#include "BluetoothSerial.h"
#include <AccelStepper.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;

AccelStepper LBWheel(1, 14, 12);
AccelStepper LFWheel(1, 26, 27);
AccelStepper RBWheel(1, 25, 33);
AccelStepper RFWheel(1, 32, 13);

const char* ssid = "##############";
const char* password = "###################";
int wheelSpeed = 1500;
int dataIn, m;
int lbw[50], lfw[50], rbw[50], rfw[50]; 
int hotdog = 0;

void setup() {
  LFWheel.setMaxSpeed(3000);
  LBWheel.setMaxSpeed(3000);
  RFWheel.setMaxSpeed(3000);
  RBWheel.setMaxSpeed(3000);
  Serial.begin(115200);
  SerialBT.begin("ESP32");
  delay(4000);
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
}

void loop() {
  if (SerialBT.available() > 0) {
    dataIn = SerialBT.read();  
    if (dataIn == 0) {
      m = 0;
    }
    if (dataIn == 2) {
      m = 2;
    }
    if (dataIn == 7) {
      m = 7;
    }
    if (dataIn == 9) {
      m = 9;
    }
    if (dataIn == 10) {
      m = 10;
    }
    if (dataIn == 5) {
      m = 5;
    }
    if (dataIn == 4) {
      m = 4;
    } 
    if (dataIn >= 16) {
      wheelSpeed = dataIn * 10;
    } 
  }
  if (m == 2) {
    moveForward();
  }
  if (m == 7) {
    moveBackward();
  }
  if (m == 9) {
    rotateLeft();
  }
  if (m == 10) {
    rotateRight();
  }
  if (m == 0) {
    stopMoving();
  }
  if (m == 5) {
    moveSidewaysRight();
  }
  if (m == 4) {
    moveSidewaysLeft();
  }
  if (m == 12) {
    if (hotdog == 0) {
      LBWheel.setCurrentPosition(0);
      LFWheel.setCurrentPosition(0);
      RBWheel.setCurrentPosition(0);
      RFWheel.setCurrentPosition(0);
    }
    lbw[hotdog] = LBWheel.currentPosition();  
    lfw[hotdog] = LFWheel.currentPosition();
    rbw[hotdog] = RBWheel.currentPosition();
    rfw[hotdog] = RFWheel.currentPosition();
    hotdog++;                     
    m = 0;
  }
  if (m == 14) {
    runSteps();
    if (dataIn != 14) {
      stopMoving();
      memset(lbw, 0, sizeof(lbw));
      memset(lfw, 0, sizeof(lfw));
      memset(rbw, 0, sizeof(rbw));
      memset(rfw, 0, sizeof(rfw));
      hotdog = 0;
    }
  }
  LFWheel.runSpeed();
  LBWheel.runSpeed();
  RFWheel.runSpeed();
  RBWheel.runSpeed();
  if(WiFi.status()== WL_CONNECTED){
    HTTPClient http;   
    http.begin("https://afternoon-headland-39577.herokuapp.com/");
    http.addHeader("Content-Type", "text/plain");      
    int httpResponseCode = http.POST("POSTING from ESP32"); 
    if(httpResponseCode>0){
      String response = http.getString();                    
    }
    else{
    }
  http.end();
  }
  else{  
  }
  delay(10000); 
}

void runSteps() {
  for (int i = hotdog - 1; i >= 0; i--) { 
    LFWheel.moveTo(lfw[i]);
    LFWheel.setSpeed(wheelSpeed);
    LBWheel.moveTo(lbw[i]);
    LBWheel.setSpeed(wheelSpeed);
    RFWheel.moveTo(rfw[i]);
    RFWheel.setSpeed(wheelSpeed);
    RBWheel.moveTo(rbw[i]);
    RBWheel.setSpeed(wheelSpeed);
    while (LBWheel.currentPosition() != lbw[i] & LFWheel.currentPosition() != lfw[i] & RFWheel.currentPosition() != rfw[i] & RBWheel.currentPosition() != rbw[i]) {
      LFWheel.runSpeedToPosition();
      LBWheel.runSpeedToPosition();
      RFWheel.runSpeedToPosition();
      RBWheel.runSpeedToPosition();
      if (SerialBT.available() > 0) {     
        dataIn = SerialBT.read();
        if ( dataIn == 15) {       
          while (dataIn != 14) {        
            if (SerialBT.available() > 0) {
              dataIn = SerialBT.read();
              if ( dataIn == 13) {
                stopMoving();
                break;
              }
            }
          }
        }
        if (dataIn >= 16) {
          wheelSpeed = dataIn * 10;
          dataIn = 14;
        }
        if ( dataIn == 13) {
          break;
        }
      }
    }
  }
  
  for (int i = 1; i <= hotdog - 1; i++) { 
    LFWheel.moveTo(lfw[i]);
    LFWheel.setSpeed(wheelSpeed);
    LBWheel.moveTo(lbw[i]);
    LBWheel.setSpeed(wheelSpeed);
    RFWheel.moveTo(rfw[i]);
    RFWheel.setSpeed(wheelSpeed);
    RBWheel.moveTo(rbw[i]);
    RBWheel.setSpeed(wheelSpeed);
    while (LBWheel.currentPosition() != lbw[i]& LFWheel.currentPosition() != lfw[i] & RFWheel.currentPosition() != rfw[i] & RBWheel.currentPosition() != rbw[i]) {
      LFWheel.runSpeedToPosition();
      LBWheel.runSpeedToPosition();
      RFWheel.runSpeedToPosition();
      RBWheel.runSpeedToPosition();
      if (SerialBT.available() > 0) {     
        dataIn = SerialBT.read();
        if ( dataIn == 15) {           
          while (dataIn != 14) {         
            if (SerialBT.available() > 0) {
              dataIn = SerialBT.read();
              if ( dataIn == 13) {
                stopMoving();
                break;
              }
            }
          }
        }
        if (dataIn >= 16) {
          wheelSpeed = dataIn * 10;
          dataIn = 14;
        }
        if ( dataIn == 13) {
        }
      }
    }
  }
}

void moveForward() {
  LFWheel.setSpeed(-wheelSpeed);
  LBWheel.setSpeed(-wheelSpeed);
  RFWheel.setSpeed(wheelSpeed);
  RBWheel.setSpeed(wheelSpeed);
}
void moveBackward() {
  LFWheel.setSpeed(wheelSpeed);
  LBWheel.setSpeed(wheelSpeed);
  RFWheel.setSpeed(-wheelSpeed);
  RBWheel.setSpeed(-wheelSpeed);
}
void moveSidewaysRight() {
  LFWheel.setSpeed(-wheelSpeed);
  LBWheel.setSpeed(wheelSpeed);
  RFWheel.setSpeed(-wheelSpeed);
  RBWheel.setSpeed(wheelSpeed);
}
void moveSidewaysLeft() {
  LFWheel.setSpeed(wheelSpeed);
  LBWheel.setSpeed(-wheelSpeed);
  RFWheel.setSpeed(wheelSpeed);
  RBWheel.setSpeed(-wheelSpeed);
}
void rotateLeft() {
  LFWheel.setSpeed(wheelSpeed);
  LBWheel.setSpeed(wheelSpeed);
  RFWheel.setSpeed(wheelSpeed);
  RBWheel.setSpeed(wheelSpeed);
}
void rotateRight() {
  LFWheel.setSpeed(-wheelSpeed);
  LBWheel.setSpeed(-wheelSpeed);
  RFWheel.setSpeed(-wheelSpeed);
  RBWheel.setSpeed(-wheelSpeed);
}
void stopMoving() {
  LFWheel.setSpeed(0);
  LBWheel.setSpeed(0);
  RFWheel.setSpeed(0);
  RBWheel.setSpeed(0);
}

any suggestions will help me a lot.

324Hz
win21H2
he/him

I suspect that what you've posted by itself is the excess 6%.

You can use a Partition Scheme without (or with less) space for filesystem and OTA

cool I will try that right now

soo i tried that and i these are my exact settings and it still does not fit:

324Hz
win21H2
he/him

You need to set a different from default!
The image is just for reference...

Try with Huge App or Minimal SPIFFS for example.

1 Like

Thanks it worked! it was able to uplaod without any errors :tada: would it affect the performance of both the wifi and bluetooth module? or would nothing be affected

324Hz
win21H2
he/him

No, it's only how the onboard flash is partitioned in order to store user program and other stuffs.

Ok. Thanks for the help!

324Hz
win21H2
he/him

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