You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32程序编译报错:代码段超出存储,求体积优化方案

ESP32程序存储超量(text section溢出)的解决方法

编译报错text section exceeds available space in board,程序占用1480309字节(112%),超出ESP32 1310720字节的程序存储上限,已尝试寄存器优化和移除无用函数仍未解决,以下是针对性优化方案:

核心优化步骤

  • 移除未使用的库与代码:你的代码引入了esp_now.h并初始化了ESP-NOW,但后续未使用任何ESP-NOW功能。直接删除#include <esp_now.h>头文件以及esp_now_init()相关的判断代码块,这能砍掉蓝牙+WiFi之外的冗余代码。
  • 精简蓝牙配置:在Arduino IDE的ESP32工具选项中,进入Tools > Bluetooth,选择Classic Bluetooth而非默认选项——默认可能同时启用BLE和经典蓝牙,而你的代码仅用到经典蓝牙(BluetoothSerial),关闭BLE支持能节省大量Flash空间。
  • 升级编译优化等级:将已有的#pragma GCC optimize ("Os")改为#pragma GCC optimize ("Oz")(更侧重代码尺寸的激进优化),同时在IDE配置里开启Compile-time optimization为Optimize for size。
  • 简化浮点格式化逻辑:浮点格式化是代码体积大户,若精度允许,把%.2f改为%.1f;或者手动构造输出字符串,完全替代snprintf_P(比如直接拼接数值转字符串的结果)。
  • 调整分区表:在PlatformIO或Arduino IDE中,使用minimal.csv最小分区表(注意:此配置会取消OTA升级空间,无需OTA时可使用)。
  • 降级核心版本:若使用最新版Arduino ESP32核心,尝试降级到2.0.6等稳定旧版本——新版本功能更全但体积更大。

修改后的示例代码

#include <WiFi.h>
#include <pgmspace.h>
#include "BluetoothSerial.h"
#pragma GCC optimize ("Oz")

const char deviceName[] PROGMEM = "ESPMaster";
const unsigned long bluetoothTimeout = 5000;
unsigned long lastActivityTime = 0;

BluetoothSerial SerialBT;

void setup() {
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);

    SerialBT.begin(deviceName);
    Serial.println(F("The device with name \"ESPMaster\" is started.\nNow you can pair it with Bluetooth!"));
}

void loop() {
    while (Serial.available()) {
        SerialBT.write(Serial.read());
        lastActivityTime = millis();
    }

    if (millis() - lastActivityTime >= bluetoothTimeout) {
        lastActivityTime = millis();
        float accelerationX = *((float*)0x3FF00000);
        float accelerationY = *((float*)0x3FF00004);
        float accelerationZ = *((float*)0x3FF00008);

        char buffer[20];
        snprintf_P(buffer, sizeof(buffer), PSTR("X:%.1f,Y:%.1f,Z:%.1f\n"), accelerationX, accelerationY, accelerationZ);

        SerialBT.print(buffer);
    }
}

内容的提问来源于stack exchange,提问作者user25032913

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:23:09