[Suggestions] Optimizations for ESP8266#9
Conversation
|
I've pushed some more changes and alignment to reduce duplication. |
|
It looks like I might have broken other OTA upload methods now that I went over the readme again. |
Major memory optimization targeting the ESP8266 reboot loop (issue #8): - Refactor all MQTT discovery/state publishing from String concatenation to snprintf with static buffers, eliminating ~70 temporary heap allocations per MQTT connect cycle - Convert RFID scent lookup from 39+ String objects to char array comparison with zero heap allocations - Add heap guards for logger JSON export and update checker - Limit GitHub API response to 4KB on ESP8266, reject chunked responses Cherry-picked bug fixes from PR #9 (FredericMa): - WiFi manager String-to-char[] conversion to reduce fragmentation - RPM calculation overflow fix with 64-bit intermediate - Web server action processing race condition fix - MQTT input validation and race condition fix - Button press time initialization to prevent false long-press at boot - ESP8266 RFID minimal page read (page 4 only vs full 45-page dump) - Browser tab visibility polling pause in web UI Additional fixes: - Fix boot path where OTA and NTP were skipped after SDK auto-reconnect - Fix ESP8266 GPIO16 INPUT_PULLUP (not supported, use INPUT) - Fix duplicate scent hex code "0426C6" (Black Oudh vs Cotton Blossom) - Reduce RFID PCD_Init frequency from every 1s to every ~3s - Enable update checker on ESP8266 with proper heap guards - Add null-dereference checks for LED and RFID allocations - Fix CSS :has() selector crash on older browsers - Night mode start==end edge case handling
- Add v1.9.5 changelog with heap fragmentation fixes - Credit @FredericMa for ESP8266 optimization contributions (PR #9) - Update version badge to 1.9.5 - Update ESP8266 stability notice - Trim older changelog entries for readability - Rebuild gzipped script.js with :has() try-catch fix
|
Thanks for the optimization suggestions @FredericMa! Several of your ideas have been incorporated into v1.9.5, including the WiFi manager String-to-char conversion, fan RPM overflow fix, and web server race condition fix. The MQTT handler has also been fully rewritten with snprintf and static buffers following the same approach. Release: https://github.com/martijnrenkema/Rituals-diffuser/releases/tag/v1.9.5 |
|
Hi Martijn, This is the log: |
|
Hi @martijnrenkema, |
|
Hi @FredericMa, good suggestion on disabling ArduinoOTA for ESP8266 - this is now implemented in v1.9.7! The ArduinoOTA background UDP service has been removed on ESP8266 to free up RAM. OTA updates still work via the web-based Safe Update mode. ESP32 and ESP32-C3 keep ArduinoOTA since they have plenty of RAM. Regarding the minimal LED driver: I appreciate the idea and the RAM savings it brings, but I'm a bit cautious about switching to a custom implementation since NeoPixelBus is well-tested across many ESP8266 setups and edge cases. For now I'd rather stick with the proven library to keep things stable, especially since ESP8266 stability is already a challenge. Thanks again for all your contributions and ideas - really appreciate you helping improve this project! Release: https://github.com/martijnrenkema/Rituals-diffuser/releases/tag/v1.9.7 |
|
Hi Martjin, I've tested the latest version but it still crashes the ESP8266. I think you have now implemented almost all changes except the LED driver in your repo so I don't know what you can do to further save RAM. I guess the only solution is to disable the RFID on the ESP8266. |
Hi,
I did some vibe coding with Claude and this is a PR with the changes it made.
I've no ESP32 version available so I can't check if it still works correctly but the esp32dev environment still builds.
This is the memory consumption with these changes:
NOTE: I did need to reconfigure the diffuser via the AP after uploading the firmware. Probably because of some memory address changes. I also needed to reconfigure MQTT afterwards.
Feel free to include anything you like in your own codebase.
This is the Copilot generated summary:
This pull request introduces significant optimizations for ESP8266 builds, refactors LED handling on ESP8266 to use a minimal WS2812 driver, and removes OTA handler support from the codebase. It also improves resource management, adds MQTT uptime reporting, and fixes potential issues in button handling and fan RPM calculations.
ESP8266 optimizations and refactoring:
platformio.ini,src/logger.h,src/logger.cpp: Optimize build flags for ESP8266, reduces RAM usage (smaller log buffer), and removes unused libraries. Also switches ESP8266 LED handling to a minimal WS2812 driver, replacing NeoPixelBus for improved stability and lower memory usage.data_src/script.js: Adds logic to pause polling when the browser tab is hidden, reducing unnecessary network usage and memory consumption.OTA handler removal:
src/main.cpp: Removes all references and callbacks related to the OTA handler, including initialization, loop, and state change handlers. [1] [2] [3] [4] [5]MQTT improvements:
src/mqtt_handler.cpp: Adds uptime sensor discovery and reporting to MQTT, with a new state machine step and periodic uptime counter updates. Also fixes a race condition in state publishing. [1] [2] [3] [4] [5]Fan controller and button handler fixes:
src/fan_controller.cpp: Fixes potential overflow in RPM calculation by using 64-bit arithmetic, and ensures LED status is updated after fan calibration events. [1] [2] [3] [4] [5]src/button_handler.cpp: Initializes button press times on startup to prevent false long-press detection if a button is held during boot.General improvements:
src/main.cpp: Ensures night mode brightness is always set correctly on the first call after NTP sync.These changes collectively improve memory usage, stability, and functionality, especially for ESP8266 builds.