Overview:A real-time room environment monitoring system built on the STM32 Blue Pill (STM32F103C8T6) using FreeRTOS and PlatformIO. The system monitors temperature, humidity, ambient light, and motion, displaying data on an SSD1306 OLED with rotary encoder navigation. Features a power-saving ACTIVE/INACTIVE state machine and temperature alarm with buzzer.Features:- Multi-sensor monitoring β Temperature, humidity, light, and motion simultaneously
- FreeRTOS architecture β 5 concurrent tasks with defined priorities
- State machine β ACTIVE/INACTIVE modes with 15-second inactivity timeout
- Rotary encoder navigation β Intuitive page switching between sensor displays
- SSD1306 OLED display β Real-time sensor data visualization
- Temperature alarm β Configurable buzzer alerts for out-of-range temperatures
- Modular design β Clean separation of HAL drivers, logic, and tasks
- Unit tested β 33 automated tests with 100% pass rate
- Static analysis verified β Zero HIGH/MEDIUM findingsHow It Wroks:Step 1: System Initialization
On power-up, HAL initializes clocks, GPIO, I2C, ADC, and UART peripherals. FreeRTOS creates all 5 tasks, queues, event groups, and mutexes, then starts the scheduler.
Step 2: Sensor Data Acquisition
SensorTask reads DHT22 (temperature + humidity) via one-wire protocol, samples LDR through ADC (12-bit), and checks PIR digital output. Data is sent to AlarmTask and DisplayTask via queue.
Step 3: State Machine
StateMachine_Update() checks PIR sensor. If motion detected β ACTIVE mode. After 15 seconds without motion β INACTIVE mode (OLED blank, reduced operations). Motion immediately restores ACTIVE.
Step 4: Display Navigation
DisplayTask renders 4 pages based on encoder input:
- Page 1: Temperature with status (LOW/NORMAL/HIGH)
- Page 2: Humidity with progress bar
- Page 3: Light level with progress bar
- Page 4: Motion detection status
Step 5: Temperature Alarm
AlarmTask evaluates temperature against thresholds:
- Temperature < 18Β°C β LOW alarm (buzzer at 1000Hz)
- Temperature > 30Β°C β HIGH alarm (buzzer at 2000Hz)
- 18Β°C β€ Temperature β€ 30Β°C β Normal (buzzer off)Challenges:1. DHT22 Timing β Using HAL_Delay() inside critical section caused hang. Fixed with DWT cycle-counting.
2. False Alarm on Sensor Failure β DHT22 failure set temp to 0Β°C, triggering false alarm. Fixed by sending NAN and checking isnan().
3. Encoder Race Condition β Reading position without synchronization lost ticks. Fixed with taskENTER_CRITICAL().
4. Wokwi Limitation β STM32Cube HAL UART doesn't output in Wokwi. Verified via unit tests and GPIO execution.
5. Stack Overflow β Initial task stacks too small. Fixed with configCHECK_FOR_STACK_OVERFLOW and 2x safety margins.

















Comments