|
Envorimental Monitoring
|
Main program body for the STM32F4 multi-sensor data acquisition system. More...
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 |
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:
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).
| 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.
| [in] | p_handle | Pointer to the ring buffer handle containing the samples. |
| [out] | stats | Pointer to the Sensor_Stats_t structure to populate. |
| void Error_Handler | ( | void | ) |
This function is executed in case of error occurrence.
| None |
| 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.
| [in] | htim | Pointer to the TIM_HandleTypeDef that triggered the callback. |
| int main | ( | void | ) |
The application entry point.
| int |
| void stm32_delay_wrapper | ( | uint32_t | ms | ) |
Platform delay wrapper for sensor drivers.
| [in] | ms | Delay duration in milliseconds. |
| 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.
| [in] | dev_addr | 8-bit I2C device address (already left-shifted by 1). |
| [in] | reg_addr | Register address to read from; 0 for raw receive. |
| [out] | data | Pointer to the buffer that receives the data. |
| [in] | len | Number of bytes to read. |
| 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.
| [in] | dev_addr | 8-bit I2C device address (already left-shifted by 1). |
| [in] | reg_addr | Register address to write to; 0 for raw transmit. |
| [in] | data | Pointer to the data to transmit. |
| [in] | len | Number of bytes to write. |
| void SystemClock_Config | ( | void | ) |
System Clock Configuration.
| 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
| AHT20_HandleTypeDef haht20 |
AHT20 humidity/temperature sensor handle.
| BH1750_HandleTypeDef hbh1750 |
BH1750 ambient-light sensor handle.
| BMP180_HandleTypeDef hbmp180 |
BMP180 pressure/temperature sensor handle.
| buf_handle_t hBufHum |
Ring buffer storing filtered humidity samples.
| buf_handle_t hBufLight |
Ring buffer storing filtered illuminance samples.
| buf_handle_t hBufTemp |
Ring buffer storing filtered temperature samples.
| Filter_Handle_t hFiltHum |
Sliding-window median filter for humidity.
| Filter_Handle_t hFiltLight |
Sliding-window median filter for illuminance.
| Filter_Handle_t hFiltTemp |
Sliding-window median filter for temperature.
| Sensor_Stats_t hum_stats |
Statistical results for humidity.
| Sensor_Stats_t light_stats |
Statistical results for illuminance.
| volatile uint8_t read_sensor_flag = 0 |
Set to 1 by TIM3 ISR; cleared in main loop.
| Sensor_Stats_t temp_stats |
Statistical results for temperature.