Envorimental Monitoring
Loading...
Searching...
No Matches
main.c File Reference

Main program body for the STM32F4 multi-sensor data acquisition system. More...

#include "main.h"
#include "i2c_core.h"

Macros

#define RING_BUFFER_SIZE   30
 Number of samples held in each ring buffer (equals the reporting period in seconds).

Functions

void SystemClock_Config (void)
 System Clock Configuration.
int8_t stm32_i2c_read_wrapper (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
 Platform I2C read wrapper for sensor drivers.
int8_t stm32_i2c_write_wrapper (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
 Platform I2C write wrapper for sensor drivers.
void stm32_delay_wrapper (uint32_t ms)
 Platform delay wrapper for sensor drivers.
int main (void)
 The application entry point.
void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim)
 TIM3 period-elapsed callback — fires every 1 second.
void calculate_statistics (buf_handle_t *p_handle, Sensor_Stats_t *stats)
 Computes descriptive statistics from a ring buffer.
void Error_Handler (void)
 This function is executed in case of error occurrence.

Variables

I2C_HandleTypeDef hi2c3
TIM_HandleTypeDef htim3
UART_HandleTypeDef huart2
BMP180_HandleTypeDef hbmp180
BH1750_HandleTypeDef hbh1750
AHT20_HandleTypeDef haht20
Filter_Handle_t hFiltHum
Filter_Handle_t hFiltTemp
Filter_Handle_t hFiltLight
buf_handle_t hBufHum
buf_handle_t hBufTemp
buf_handle_t hBufLight
Sensor_Stats_t hum_stats
Sensor_Stats_t temp_stats
Sensor_Stats_t light_stats
volatile uint8_t read_sensor_flag = 0

Detailed Description

Main program body for the STM32F4 multi-sensor data acquisition system.

This application reads humidity and temperature data from an AHT20 sensor, illuminance from a BH1750 sensor, and atmospheric pressure from a BMP180 sensor over I2C. Raw readings are filtered with a sliding-window median filter and stored in separate circular (ring) buffers. Every 30 seconds the system computes min, max, median and standard deviation for each sensor and transmits the results over USART2 at 115200 baud in the following format:

HUM:<min>,<max>,<median>,<std_dev>\r\n
TMP:<min>,<max>,<median>,<std_dev>\r\n
LUX:<min>,<max>,<median>,<std_dev>\r\n

A 1-second tick is generated by TIM3 configured on the APB1 bus (prescaler = 12499, period = 999, resulting in a 1 Hz interrupt at 12.5 MHz).

Author
Şule Nur Demirdaş
Date
April 2026
Attention
Copyright (c) 2026 STMicroelectronics. All rights reserved. This software is licensed under terms found in the LICENSE file in the root directory of this software component.

Function Documentation

◆ calculate_statistics()

void calculate_statistics ( buf_handle_t * p_handle,
Sensor_Stats_t * stats )

Computes descriptive statistics from a ring buffer.

Computes min, max, median, and standard deviation from a ring buffer.

Iterates over all valid samples in p_handle to find the minimum, maximum and arithmetic mean. A second pass computes the population standard deviation. The buffer is then copied to a temporary array, sorted with bubble_sort(), and the median is extracted.

Note
The function returns immediately if the buffer is empty.
Parameters
[in]p_handlePointer to the ring buffer handle containing the samples.
[out]statsPointer to the Sensor_Stats_t structure to populate.

◆ Error_Handler()

void Error_Handler ( void )

This function is executed in case of error occurrence.

Return values
None

◆ HAL_TIM_PeriodElapsedCallback()

void HAL_TIM_PeriodElapsedCallback ( TIM_HandleTypeDef * htim)

TIM3 period-elapsed callback — fires every 1 second.

Sets read_sensor_flag to notify the main loop that a new sample should be acquired, and toggles the green LED as a heartbeat indicator.

Parameters
[in]htimPointer to the TIM_HandleTypeDef that triggered the callback.

◆ main()

int main ( void )

The application entry point.

Return values
int

◆ stm32_delay_wrapper()

void stm32_delay_wrapper ( uint32_t ms)

Platform delay wrapper for sensor drivers.

Parameters
[in]msDelay duration in milliseconds.

◆ stm32_i2c_read_wrapper()

int8_t stm32_i2c_read_wrapper ( uint8_t dev_addr,
uint8_t reg_addr,
uint8_t * data,
uint16_t len )

Platform I2C read wrapper for sensor drivers.

Sensor drivers call this function pointer instead of HAL directly, keeping the drivers portable across different MCU platforms.

When reg_addr is 0 the function performs a raw master-receive (no register address phase). Otherwise it uses HAL_I2C_Mem_Read.

Parameters
[in]dev_addr8-bit I2C device address (already left-shifted by 1).
[in]reg_addrRegister address to read from; 0 for raw receive.
[out]dataPointer to the buffer that receives the data.
[in]lenNumber of bytes to read.
Returns
0 on success, -1 on I2C error.

◆ stm32_i2c_write_wrapper()

int8_t stm32_i2c_write_wrapper ( uint8_t dev_addr,
uint8_t reg_addr,
uint8_t * data,
uint16_t len )

Platform I2C write wrapper for sensor drivers.

When reg_addr is 0 the function performs a raw master-transmit (no register address phase). Otherwise it uses HAL_I2C_Mem_Write.

Parameters
[in]dev_addr8-bit I2C device address (already left-shifted by 1).
[in]reg_addrRegister address to write to; 0 for raw transmit.
[in]dataPointer to the data to transmit.
[in]lenNumber of bytes to write.
Returns
0 on success, -1 on I2C error.

◆ SystemClock_Config()

void SystemClock_Config ( void )

System Clock Configuration.

Return values
None

Configure the main internal regulator output voltage

Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure.

Initializes the CPU, AHB and APB buses clocks

Variable Documentation

◆ haht20

AHT20 humidity/temperature sensor handle.

◆ hbh1750

BH1750 ambient-light sensor handle.

◆ hbmp180

BMP180 pressure/temperature sensor handle.

◆ hBufHum

buf_handle_t hBufHum

Ring buffer storing filtered humidity samples.

◆ hBufLight

buf_handle_t hBufLight

Ring buffer storing filtered illuminance samples.

◆ hBufTemp

buf_handle_t hBufTemp

Ring buffer storing filtered temperature samples.

◆ hFiltHum

Filter_Handle_t hFiltHum

Sliding-window median filter for humidity.

◆ hFiltLight

Filter_Handle_t hFiltLight

Sliding-window median filter for illuminance.

◆ hFiltTemp

Filter_Handle_t hFiltTemp

Sliding-window median filter for temperature.

◆ hum_stats

Sensor_Stats_t hum_stats

Statistical results for humidity.

◆ light_stats

Sensor_Stats_t light_stats

Statistical results for illuminance.

◆ read_sensor_flag

volatile uint8_t read_sensor_flag = 0

Set to 1 by TIM3 ISR; cleared in main loop.

◆ temp_stats

Sensor_Stats_t temp_stats

Statistical results for temperature.