arduino简单string入门——处理 ESP32 和 ESP8266 上的内存不足问题 – 添加定期自动重启
ESP32 和 ESP8266 拥有更多可用的 SRAM,因此只要您遵循使用 Arduino 字符串的指南,您的代码就不太可能出现任何内存问题。但是,这些开发板的 Web 库大量使用字符串,因此如果您正在编写 Web 项目,最终可能会因为底层 Web 库的问题而耗尽内存。在这种情况下,开发板将锁定或重新启动。从代码中删除字符串不会改变这一点。您能做的最好的事情是定期安排开发板自动重启以清除内存。ESP32 和 EPS8266 都有一个看门狗定时器,因此启用看门狗定时器并将代码置于紧密循环中将导致重启。例如
// millisDelay included in SafeString library, from Arduino library manager or // download zip from https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html #include <millisDelay.h> millisDelay rebootDelay; unsigned long REBOOT_DELAY_MS = 24ul * 60 * 60 * 1000; // 1day in mS void setup() { // . . . rebootDelay.start(REBOOT_DELAY_MS); // start reboot timer #if defined(ARDUINO_ARCH_ESP32) enableLoopWDT(); // default appears to be 5sec #elif defined(ARDUINO_ARCH_ESP8266) ESP.wdtEnable(5000); // arg ignored :-( default appears to be 3sec #else #error Only ESP2866 and ESP32 reboot supported #endif } void loop() { if (rebootDelay.justFinished()) { while (1) {} // force watch dog timer reboot } // . . . rest of loop code }
摘要: ESP32 和 ESP8266 在其 Web 支持库中广泛使用字符串。因此,如果您遵循指南,则在代码中不使用字符串没有任何好处。如果您想长时间运行项目,请构建定期自动重启。
浙公网安备 33010602011771号