At the end of Part 7 and Hardware as Code, Stuffy Meter was running firmware v1.0. It worked well on my desk, mostly because I had a CLI on my Mac, knew every IP on my Wi-Fi, and didn’t mind plugging in a USB-C cable whenever something broke.
Since then, the firmware has grown from v1.0 to v2.1.2. The project now has a proper product page at cmwang.dev/stuffy-meter and a bilingual Quickstart Guide on GitHub. If you want a clean tour of what Stuffy Meter does—7-day history, air-quality insights, Telegram alerts, Home Assistant integration, and automatic updates—start there.
This post is about the development journey and tradeoffs behind those features: how to fit a real, everyday product onto a single-core ESP32-C3 (4 MB flash, ~400 KB RAM) while keeping everything 100% local—no cloud server, no accounts, and no mobile app.
The box we had to build inside
The ESP32-C3 is a great little chip, but its limits show up fast once a hobby firmware turns into a real product:
- 4 MB of flash, almost all of it spoken for by two 1.875 MB OTA slots (so updates can roll back safely), plus settings, history storage, and a crash-dump partition.
- ~400 KB of RAM, shared by Wi-Fi, TCP/IP, TLS encryption, the LVGL display driver, an HTTP server, MQTT, and two sensor drivers.
- A frozen partition table. Over-the-air updates can swap firmware slots, not repartition flash—so any storage upgrade had to fit inside the partitions already flashed onto existing boards.
Almost every interesting software change over the past few weeks came from trying to add something users genuinely wanted without breaking those three walls.
Supporting Traditional Chinese—and any room name—without a CJK font
Translating the web dashboard and Telegram alerts into Traditional Chinese was mostly a matter of organizing strings cleanly. Putting Traditional Chinese onto the 240×135 colour screen was much harder: a full Noto Sans CJK font is several megabytes, larger than our entire firmware partition.
For fixed UI labels—things like “溫度”, “濕度”, “良好”, and status messages—we know every character at build time. A Python script scans the UI translation table, collects only the unique Chinese characters used at each font size, and generates three tiny 4-bit LVGL fonts:
- 12 px: 93 characters
- 14 px: 90 characters
- 20 px: 42 characters
All three sizes together take only ~25 KB of flash and zero extra runtime RAM. Each font falls back to Montserrat for English letters, digits, degree signs, and icons (with matched line heights and baselines so mixed lines don’t jump up and down), and CI fails if anyone adds a new Chinese string without regenerating the fonts.
The catch: what if a user names their monitor “客廳”?
Build-time subsetting breaks the moment a user types their own room name—客廳 (Living Room), 主臥 (Master Bedroom), or anything in Japanese or accented Latin. You can’t ship every possible character in advance.
The workaround was to let the device that already has every font in the world do the rendering: the user’s browser.
When you save a room name in the web UI (or during initial Wi-Fi setup), the browser draws the name onto an invisible 112×14 <canvas> using your phone or computer’s system font, trims the empty edges, packs the anti-aliased pixels into a tiny 4-bit alpha bitmap, and uploads it to the monitor (PUT /api/device-name-glyph). The format is capped at 972 bytes (a short name like 客廳 is only a couple hundred bytes) so it drops straight into NVS flash:
[0..3] magic "AQG1"
[4] pixel depth (1-bit or 4-bit alpha)
[5..6] width & height (up to 120 x 16 px)
[8..11] FNV-1a hash of the UTF-8 room name
[12..] packed alpha pixels
When the monitor draws the top status bar, it checks that the hash matches the current room name and blits the bitmap straight onto the screen, tinted to match the UI text colour:

The tradeoff: if you rename the monitor from the serial console or MQTT instead of a browser, the stored hash won’t match and the status bar quietly falls back to plain ASCII text. That’s an easy trade for letting anyone put arbitrary Unicode room names on a 4 MB microcontroller.
(Bonus bug caught in code review: when you changed settings and reloaded the bitmap, the firmware freed the old pixel buffer while LVGL’s image widget was still pointing at it—a classic use-after-free, fixed by detaching the widget before freeing.)
Fighting OOMs, heap fragmentation, and TLS crashes on 400 KB RAM
In Hardware as Code, QEMU caught plenty of logic and state-machine bugs before the real boards arrived. Once real ESP32-C3 hardware ran 24/7 with HTTPS firmware updates, Telegram HTTPS calls, the colour screen, and the web dashboard all alive at once, heap fragmentation found every weak spot QEMU couldn’t see.
At the worst point, idle free heap hovered around 52 KB, and during an outbound HTTPS handshake minimum free heap dipped to 88 bytes. Opening the Settings page could crash the monitor, and GitHub firmware downloads died halfway through.
Four concrete failures drove the fixes:
| What broke on real hardware | Why it happened | Fix & tradeoff |
|---|---|---|
Opening Settings triggered out of memory and crashed the display task |
/api/config/schema and /api/config built a giant cJSON tree + serialized string on the heap (~57 KB peak spike) |
Rewrote /api/config, /api/config/schema, and /api/history to stream directly from a 512-byte stack buffer—zero heap spike, at the cost of writing small custom JSON serializers in C |
| GitHub HTTPS OTA died mid-download (after 15 KB or 569 KB) | Default 16 KB TLS receive buffers couldn’t find contiguous free DRAM, and GitHub’s CDN dropped keep-alive mid-file | Switched to dynamic mbedTLS buffers (cutting manifest-check peak heap from ~75 KB to ~48 KB) and a resumable 8 KB HTTP Range loop that picks up from the exact unwritten byte on drops (slightly slower download, rock-solid recovery) |
| Random HTTPS connection failures | Telegram polling and OTA update checks occasionally opened two TLS sessions at the same time | Added a global aq_tls_lock() so only one outbound HTTPS request runs at a time, and replaced permanent polling threads with a timer + short-lived task (0 bytes of idle task stack) |
| Too little headroom (~52 KB idle heap) for day-to-day spikes | Oversized Wi-Fi buffers, a 24 KB LVGL layer buffer, duplicate static UI buffers, and app_main() keeping its 6 KB stack forever |
Two full memory audits reclaimed 50+ KB of permanent DRAM—trimming Wi-Fi RX buffers, shrinking the LVGL layer buffer to 4 KB, moving supervision to a smaller task so app_main() exits, and chunking 7-day queries (16 KB → 3 KB scratch) |
After those two passes, idle free heap went from ~52 KB to over 80 KB, and peak TLS minimum free heap jumped from 88 bytes to ~17 KB. While auditing the web server, I also enabled task-watchdog panics (so a hung task reboots instead of silently freezing) and added a quick pre-parse guard that rejects deeply nested or oversized JSON payloads before cJSON ever touches them.
Fitting 7-day history and air-quality insights into 32 KB flash and ~40 KiB gzipped
Once monitors were sitting in real rooms, 24 hours of on-device history turned out to be too short—you want to compare weekday work-from-home days against the weekend, without having to set up Grafana first.
Two-tier history inside the same 32 KB partition
Because we couldn’t resize the flash partition table over OTA, a full week had to fit inside the exact same 32 KB history partition.
I replaced the original log format with compact 11-byte records (each 4 KB sector stores a base timestamp and every record carries a CRC-16 check, with automatic first-boot migration from v1) and split the 8 sectors into two tiers:
- Tier 1 (20 KB): 1-minute averages — always at least 24 hours of fine-grained data, even right after a sector wraps.
- Tier 2 (12 KB): 15-minute rollups — 7.7 to 11.6 days of history, read directly from flash on demand with zero extra idle RAM.
The tradeoff is coarser resolution beyond 24 hours—barely noticeable on a weekly chart—in exchange for keeping every deployed board 100% OTA-compatible.
Offloading insights to the browser—and keeping the web app under ~40 KiB
Charts are nice, but raw curves still make you interpret them yourself: is CO₂ climbing fast enough that I should open a window soon? Did cooking spike VOCs even though CO₂ looks fine? Is this room humid enough for mold? How many stuffy hours did we have this week?
Running trend regressions, comfort/mold calculations, and 7-day daily breakdowns in C on the ESP32 would have eaten precious RAM and complicated the firmware. Doing all of that in client-side JavaScript directly on top of /api/history costs zero firmware RAM.

The tradeoff is keeping the embedded web bundle small in flash. As the built-in web UI grew to include bilingual English/Chinese text, Light/Dark themes (added in v2.1.2), installable PWA support, synchronized charts, Insights, guided Wi-Fi setup, and firmware management, editing one monolithic web/index.html became painful. I split it into modular source files under web/src/ plus Markdown translation tables, compiled by ./aq web with strict CI size checks. Even with all of those features packed in, the entire web app still compiles down to 107.2 KiB raw — 40.3 KiB gzipped.
Auto-discovery without a cloud server
Commercial Wi-Fi sensors make setup easy by phoning home to a vendor cloud and binding to your account in a mobile app. If you refuse to run a cloud backend or maintain iOS and Android apps, you run straight into the hardest UX problem in local IoT: once the monitor joins your Wi-Fi, how do you actually open it without digging through your router’s DHCP table?
Relying on mDNS (stuffymeter-xxxxxx.local) alone wasn’t good enough—it works well on macOS and iOS, unevenly on Android and Windows, and fails outright on routers that block multicast. Instead of betting on one trick, I layered three ways to reach the device so each one covers where another falls short:
1. Browser-based LAN finder (cmwang.dev/stuffy-meter/discovery/)
Open cmwang.dev/stuffy-meter/discovery/ on any phone or computer on the same Wi-Fi and click Scan Network. It guesses your active subnet by probing common router gateways, scans for Stuffy Meter units, shows live readings and whether newer firmware is available on GitHub, and lets you blink a monitor’s LED and screen with Identify so you know which box is in which room. Everything runs in your browser; nothing leaves your LAN.

Getting a public static website to talk to plain-HTTP ESP32s on a private LAN forced two tricky tradeoffs:
- Security vs. discoverability: Turning on permissive CORS across the whole device would let any webpage you visit quietly reconfigure your monitor. So the firmware draws a hard line: only
GET /api/statusandPOST /api/identifysendAccess-Control-Allow-Origin: *andAccess-Control-Allow-Private-Network: true. Every settings, Wi-Fi, and OTA endpoint sends zero CORS headers and rejects HTML-formPOSTcontent types (text/plain,application/x-www-form-urlencoded,multipart/form-data) with HTTP415. Even before an admin password is set, another site cannot change settings with a hidden form. (First-boot Wi-Fi setup now also lets you set an admin password, room name, language, and time zone on one screen.) - The iOS WebKit wall: Desktop and Android Chrome/Edge allow an HTTPS site to scan local HTTP IPs after a Local Network Access prompt (
targetAddressSpace: 'local'). On iOS and iPadOS, however, every browser uses Apple’s WebKit engine, which blocks an HTTPS site from fetching plain-HTTP LAN addresses as mixed content. Rather than downgradingcmwang.devto HTTP, a GitHub Actions workflow mirrors the discovery page to a port-80-only AWS S3 HTTP address that iOS users can switch to in one tap. You can also type a monitor’s 6-character ID directly, or discover a USB-connected board over Web Serial.
2. The zero-magic escape hatch: an on-device QR code
If a network blocks mDNS and you don’t want to scan the subnet, pressing the MODE button on the monitor brings up a Dashboard QR code pointing straight to http://<ip>/. Point your phone camera at the screen and you’re in—no DNS, no mDNS, no browser permissions.

3. Native Home Assistant discovery (without requiring MQTT)
Stuffy Meter already supported MQTT with Home Assistant auto-discovery back in v1.0. In practice, asking someone to install and secure an MQTT broker just to add one CO₂ monitor is way too much friction.
I built a dedicated HACS integration, ha-stuffy-meter, that discovers monitors automatically via mDNS (_stuffymeter._tcp) and polls the local REST API directly—zero broker, zero cloud, one-click setup. The tradeoff is maintaining another small repository, while keeping MQTT and the standalone Docker Prometheus/Grafana stack (which also uses mDNS discovery) intact for users who already run them.
Zero-server alerts and safe unattended updates
Two final tradeoffs came from thinking about monitors running outside my own house:
- Telegram instead of a push-notification server: Sending alerts directly from the ESP32 to
api.telegram.orgover HTTPS gives phone notifications (and an opt-in/statusbot command) with zero backend infrastructure to host or pay for. The UX tradeoff is asking users to spend two minutes creating a bot with@BotFather, so I put a step-by-step guide and a test button right inside Settings. - Guardrails for automatic nightly updates: Once boards leave my desk, flashing by hand stops scaling—but a bad unattended OTA update can brick a remote device. Automatic nightly installs (skipped while running on battery) therefore arm an early rollback timer at the very start of
app_main(), require a 10-minute probation that only confirms after Wi-Fi connects and a live manifest check succeeds (so a broken build that breaks TLS can never strand a monitor), and remember rolled-back versions in NVS so a board won’t boot-loop on a bad release.

One small migration trick that paid off: when v2.0.0 renamed all leftover smart_aq identifiers (including the ESP-IDF application name) to stuffymeter, existing 1.x monitors would normally reject the renamed firmware over OTA. Shipping a transitional v1.12.0 bridge firmware that briefly accepted both names for the same board family kept the entire upgrade 100% wireless.
I also went back and forth on telemetry. Without any diagnostics, a firmware crash on someone else’s Wi-Fi is completely invisible. The compromise was a minimal daily health heartbeat and crash summary identified only by a locally hashed eFuse ID—never sending Wi-Fi SSIDs, room names, or sensor readings—with an explicit opt-out checkbox right in the first-boot setup screen and Settings.
What I learned
- Tight hardware budgets force cleaner architecture. A frozen partition table pushed us to a two-tier 7-day flash ring; no room for a CJK font led to browser-rendered status-bar bitmaps; 400 KB of RAM forced zero-heap streaming, serialized TLS, and client-side Insights.
- Zero-cloud doesn’t remove complexity—it relocates it. You trade running a backend server for CORS/Private Network Access boundaries, iOS WebKit mixed-content quirks, mDNS edge cases, and QR fallbacks. I’d still make that trade every time.
- Real hardware and real networks still catch the hardest bugs. QEMU is great for logic and fault-recovery tests, but it couldn’t reproduce fragmented ESP32-C3 DRAM colliding with GitHub’s CDN TLS keep-alive. Nothing replaces leaving real boards running 24/7.
Explore Stuffy Meter & what’s next
For the full feature overview, setup walkthroughs, and downloads:
- Product page & features: cmwang.dev/stuffy-meter
- Quickstart Guide (EN / 繁體中文): github.com/chiehmin/stuffy-meter-releases/wiki/Quickstart
- Browser LAN auto-discovery: cmwang.dev/stuffy-meter/discovery/
- Prebuilt firmware, 3D enclosure STLs & Docker monitoring stack: github.com/chiehmin/stuffy-meter-releases
- Home Assistant HACS integration: github.com/chiehmin/ha-stuffy-meter
With the software in good shape, the next post heads back to hardware: putting the Battery edition boards through real-world testing once they arrive and seeing how long an 18650 cell lasts with the SCD41 in the loop.