2026-05-06
STM32 Base Unit FirmwareSTM32 底座板固件整理笔记
A base-unit firmware note about DHT sensing, IR presence detection, fan PWM, OLED UI, Bluetooth reporting, and saving user settings through Flash.底座板固件笔记,记录 DHT 温湿度、红外在位检测、风扇 PWM、OLED UI、蓝牙上报,以及通过 Flash 保存用户设定。
Starting Point起点
底座板固件没有外机那么多“大件”,但它更像一个完整的小设备。它要读环境温湿度和出风口温湿度,要判断设备在不在位,要让用户调风扇,要在 OLED 上显示状态,还要通过蓝牙上报数据。每个功能单独看都不大,放进同一个主循环后就开始考验节奏。
The base-unit firmware has fewer large parts than the external-unit firmware, but it feels closer to a complete small device. It reads ambient and outlet temperature/humidity, checks whether the device is in position, lets the user adjust the fan, shows state on OLED, and reports data over Bluetooth. Each feature is small by itself. Together they need timing and state handling.
一开始最担心的是它变成“能跑但很乱”的工程。风扇能转、OLED 能亮只是第一步,真实使用还要考虑关机确认、掉电记忆、传感器读取失败、按键误触发、蓝牙发送会不会卡住主循环。
The part that made it feel like a device was persistence. Fan setting, shutdown confirmation, sensor failure handling, and communication timing all ask what should happen after the user stops looking at the screen or after power is lost.
Code Structure代码怎么分
底座固件按 App + BSP_drivers + Core 整理。Core 保留 CubeMX 生成的外设初始化,BSP_drivers 处理 DHT、IR、风扇、OLED、蓝牙、Flash,App 负责状态机和任务调度。这样后续看代码时,至少知道该去哪里找问题。
The firmware is organized as App + BSP_drivers + Core. Core keeps CubeMX peripheral initialization, BSP_drivers handles DHT, IR, fan, OLED, Bluetooth, and Flash, and App owns the state machine and task scheduling.
风扇设定值也要做掉电记忆,是因为真实设备会被反复使用。每次断电再上电都回到默认值,体验会很粗糙。Flash 保存看起来只是一个小功能,却会把擦写次数、延迟写入、CRC 和最近一次有效记录恢复都带出来。
Saving a fan setting in Flash sounds tiny, but it brings real firmware questions: erase count, delayed writes, CRC, and recovery of the most recent valid record. That is a much better exercise than only lighting the OLED once.
Key Functions关键功能
The useful part of this note is the boundary between small functions and product behavior.
DHT11reads ambient temperature and humidity;DHT22reads outlet temperature and humidity.- Infrared detection judges whether the device is in position.
- Fan PWM is set within a 0 to 100% range.
- OLED displays
ENV / OUT / FAN, and the shutdown confirmation page shows a countdown. - Bluetooth messages use a shape like
DATA:<envT>,<outT>,<fan>. - Flash persistence uses a header, record, CRC32, and commit mark to recover the most recent valid setting.
DHT11负责环境温湿度,DHT22负责出风口温湿度。- 红外检测用于判断设备是否在位。
- 风扇 PWM 设定范围为 0 到 100%。
- OLED 显示
ENV / OUT / FAN,关机确认页显示倒计时。 - 蓝牙报文形如
DATA:<envT>,<outT>,<fan>。 - Flash 参数区用页头、记录、CRC32 和 commit 标记恢复最近一次有效设定。
File Handling文件处理
This note keeps the firmware-structure explanation. The base-unit source code and device constraint lists stay in the private project archive for now.
页面只保留固件结构说明。底座板源码和器件约束清单暂时留在私有项目归档里。
Debugging Reflection调试感受
This project made me pay more attention to what a device should remember. Earlier, I cared more about whether a feature could run. Here I had to think about power loss, accidental key presses, failed sensor reads, and Bluetooth sending inside the main loop.
这个工程让我开始重视“设备应该记得什么”。以前更关注功能能不能跑,这里会多想一步:断电以后怎么办,用户误触怎么办,传感器失败怎么办,通信会不会阻塞。
The base unit is small, but that is why it is useful. A small device gives enough room to practice writing features as things people may use repeatedly: read, display, adjust, remember, report, and recover.
底座板很小,也正因为小,它很适合练习把功能写得像会被人反复使用的设备:读取、显示、调节、记住、上报、恢复。