L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
sps30.h
Go to the documentation of this file.
1/*
2 * sps30.h
3 *
4 * Created on: 2. 1. 2026
5 * Author: Milan
6 *
7 * sensor SPS30 dust - particles SPS30Datasheet.pdf
8 * https://sensirion.com/products/catalog/SPS30
9 * https://cdn.sparkfun.com/assets/2/d/2/a/6/Sensirion_SPS30_Particulate_Matter_Sensor_v0.9_D1__1_.pdf
10 *
11 * SPS30 can be directly connected to SDA/SCL (I2C) or through HVAC click module
12 * this sensor unlike others runs on 5V
13 *
14 * gemini: https://gemini.google.com/share/24ed9f3930ce
15 */
16
17#ifndef INC_SPS30_H_
18#define INC_SPS30_H_
19
20#include "mysensors_def.h"
21
22#ifdef SENSOR_SPS30
23
24#include "stm32wlxx_hal.h"
25
26/*
27 1. Mass Concentration ([μg/m3])
28
29 These values represent the total weight of particles in a cubic meter of air. This is the standard metric used by government air quality indexes (AQI).
30 mass_pm1_0: Ultra-fine particles smaller than 1.0 micrometers.
31 mass_pm2_5: Fine particles smaller than 2.5 micrometers. This is the most critical health metric as these particles can enter the lungs and bloodstream.
32 mass_pm4_0: Particles smaller than 4.0 micrometers.
33 mass_pm10_0: Coarse particles smaller than 10 micrometers (e.g., dust, pollen).
34
35 2. Number Concentration ([n/cm3])
36
37 These values represent the "count" or quantity of individual particles found in a cubic centimeter of air.
38 This is often more sensitive than mass for detecting the start of a fire or very fine smoke.
39 num_pm0_5: Count of particles between 0.3 and 0.5 micrometers.
40 num_pm1_0: Count of particles up to 1.0 micrometers.
41 num_pm2_5: Count of particles up to 2.5 micrometers.
42 num_pm4_0: Count of particles up to 4.0 micrometers.
43 num_pm10_0: Count of particles up to 10 micrometers.
44
45 3. Particle Size ([\mu m])
46 typical_particle_size: This is the average diameter of the particles currently being detected.
47 Low values (~0.4 - 1.0) usually indicate combustion smoke or fumes.
48 High values (>2.0) usually indicate heavy dust or pollen.
49
50 Particle Type, Typical Size (μm), Struct Variable
51 Wildfire Smoke 0.4 – 0.7 "num_pm0_5, mass_pm1_0"
52 Bacteria 0.3 – 10 "mass_pm2_5, num_pm2_5"
53 Dust/Pollen 10 – 100 mass_pm10_0
54
55
56 Summary of PM2.5 Brackets (μg/m3)
57 Level Range Health Implications
58 Good 0.0 – 12.0 Little to no risk.
59 Moderate 12.1 – 35.4 Risk for very sensitive people.
60 Unhealthy (Sens.) 35.5 – 55.4 General public not likely affected; sensitive groups at risk.
61 Unhealthy 55.5 – 150.4 Everyone may begin to experience health effects.
62 Very Unhealthy 150.5 – 250.4 Health alert: everyone may experience serious effects.
63 */
64// don't change order !!!
65#pragma pack(1)
66/**
67 * @brief Measurement data produced by the SPS30 particulate matter sensor.
68 * Populated by sps30_Read(); check IsDataValid before using the values.
69 *
70 * Mass concentrations (µg/m³) reflect the total mass of particles per unit
71 * volume, which is the metric used by government AQI indices.
72 * Number concentrations (n/cm³) count individual particles and are more
73 * sensitive for detecting early-stage smoke or combustion products.
74 */
75typedef struct
76{
77 float Mass_pm1_0; /**< Mass concentration of PM1.0 particles (< 1 µm) in µg/m³ */
78 float Mass_pm2_5; /**< Mass concentration of PM2.5 particles (< 2.5 µm) in µg/m³ – primary health metric */
79 float Mass_pm4_0; /**< Mass concentration of PM4.0 particles (< 4 µm) in µg/m³ */
80 float Mass_pm10_0; /**< Mass concentration of PM10 particles (< 10 µm) in µg/m³ */
81 float Num_pm0_5; /**< Number concentration of particles 0.3–0.5 µm in n/cm³ */
82 float Num_pm1_0; /**< Number concentration of particles up to 1.0 µm in n/cm³ */
83 float Num_pm2_5; /**< Number concentration of particles up to 2.5 µm in n/cm³ */
84 float Num_pm4_0; /**< Number concentration of particles up to 4.0 µm in n/cm³ */
85 float Num_pm10_0; /**< Number concentration of particles up to 10 µm in n/cm³ */
86 float Typical_particle_size; /**< Mean particle diameter in µm (low ≈ smoke/combustion, high ≈ dust/pollen) */
87 int8_t IsDataValid; /**< 1 – values are valid; 0 – last read failed or data not yet available */
88} sps30_t;
89#pragma pack()
90
91/**
92 * @brief Air Quality Index (AQI) level based on PM2.5 mass concentration.
93 * Follows US EPA / WHO PM2.5 breakpoints (µg/m³).
94 */
95typedef enum
96{
97 AQI_GOOD = 0, /**< 0.0 – 12.0 µg/m³ – little or no health risk */
98 AQI_MODERATE, /**< 12.1 – 35.4 µg/m³ – acceptable; some risk for very sensitive individuals */
99 AQI_UNHEALTHY_SENSITIVE, /**< 35.5 – 55.4 µg/m³ – sensitive groups (asthma, heart disease) may be affected */
100 AQI_UNHEALTHY, /**< 55.5 – 150.4 µg/m³ – everyone may begin to experience health effects */
101 AQI_VERY_UNHEALTHY, /**< 150.5 – 250.4 µg/m³ – health alert; serious effects possible for all */
102 AQI_HAZARDOUS /**< > 250.4 µg/m³ – emergency conditions; entire population at risk */
104
105/** @brief Live measurement data from the SPS30 sensor; updated by sps30_Read(). */
106extern sps30_t _sps30Data;
107/** @brief Snapshot copy of the last completed SPS30 measurement; used for LoRaWAN transmission. */
109
110/**
111 * @brief Classify the current PM2.5 mass concentration according to the AQI scale.
112 * @param label Optional output pointer; if non-NULL, *label is set to a static
113 * human-readable string (e.g. "Good", "Moderate", "Hazardous").
114 * @return The AQI_Level_t value corresponding to the PM2.5 concentration in
115 * _sps30Data.Mass_pm2_5.
116 */
117AQI_Level_t sps30_ClassifyPM25(char** label);
118
119/**
120 * @brief - check if SPS30 sensor is present
121 * @param tryInit - in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no
122 * @retval 1 - is present, 0 - is not
123 */
124int8_t sps30_Is(I2C_HandleTypeDef *hi2c, int8_t tryInit);
125
126/**
127 * @brief initialization of sensor sps30
128 * @retval HAL_OK - sensor is present, HAL_ERROR - error
129 */
130HAL_StatusTypeDef sps30_Init(I2C_HandleTypeDef *hi2c);
131
132/**
133 * @brief Turn on laser and fan to allow measurements
134 * @retval HAL_OK, HAL_ERROR
135 */
136HAL_StatusTypeDef sps30_On(I2C_HandleTypeDef *hi2c);
137
138/**
139 * @brief Turn off laser and fan to stop measurements
140 * @retval HAL_OK, HAL_ERROR
141 */
142HAL_StatusTypeDef sps30_Off(I2C_HandleTypeDef *hi2c);
143
144/**
145 * @brief Check if data is available
146 * @retval HAL_OK, HAL_ERROR, HAL_BUSY
147 */
148HAL_StatusTypeDef sps30_IsDataReady(I2C_HandleTypeDef *hi2c);
149
150/**
151 * @brief Read data if data is available
152 */
153HAL_StatusTypeDef sps30_Read(I2C_HandleTypeDef *hi2c);
154
155/**
156 * @brief - helper to check if sensor is on or not
157 */
158HAL_StatusTypeDef sps30_IsOnOff(I2C_HandleTypeDef *hi2c, uint8_t *onOff);
159
160/**
161 * @brief manually start fan for cleaning, runs for about 10s
162 * Note: The sensor will be busy cleaning for 10 seconds.
163 * Data read during this time might be inconsistent due to high airflow.
164 */
165HAL_StatusTypeDef sps30_StartCleaning(I2C_HandleTypeDef *hi2c);
166
167/**
168 * @brief interval for auto cleaning
169 *
170 */
171HAL_StatusTypeDef sps30_GetAutoCleanInterval(I2C_HandleTypeDef *hi2c, uint32_t *interval_sec);
172
173/**
174 * @brief set interval for auto cleaning
175 */
176HAL_StatusTypeDef sps30_SetAutoCleanInterval(I2C_HandleTypeDef *hi2c, uint32_t interval_sec);
177
178/**
179 * @brief the sensor need to be cleaning
180 * @retval 0 - no need, 1 - sensor is clearing
181 */
182int8_t sps30_Service(I2C_HandleTypeDef *hi2c);
183
184/**
185 * @brief log data to buffer
186 */
187void sps30_LogData(char* buf);
188
189#endif
190
191#endif /* INC_SPS30_H_ */
AQI_Level_t sps30_ClassifyPM25(char **label)
Classify the current PM2.5 mass concentration according to the AQI scale.
Definition sps30.c:47
sps30_t _bck_sps30Data
Snapshot copy of the last completed SPS30 measurement; used for LoRaWAN transmission.
Definition sps30.c:39
HAL_StatusTypeDef sps30_Read(I2C_HandleTypeDef *hi2c)
Read data if data is available.
Definition sps30.c:224
HAL_StatusTypeDef sps30_StartCleaning(I2C_HandleTypeDef *hi2c)
manually start fan for cleaning, runs for about 10s Note: The sensor will be busy cleaning for 10 sec...
Definition sps30.c:288
void sps30_LogData(char *buf)
log data to buffer
Definition sps30.c:356
HAL_StatusTypeDef sps30_SetAutoCleanInterval(I2C_HandleTypeDef *hi2c, uint32_t interval_sec)
set interval for auto cleaning
Definition sps30.c:329
HAL_StatusTypeDef sps30_IsOnOff(I2C_HandleTypeDef *hi2c, uint8_t *onOff)
helper to check if sensor is on or not
Definition sps30.c:271
HAL_StatusTypeDef sps30_Off(I2C_HandleTypeDef *hi2c)
Turn off laser and fan to stop measurements.
Definition sps30.c:175
AQI_Level_t
Air Quality Index (AQI) level based on PM2.5 mass concentration. Follows US EPA / WHO PM2....
Definition sps30.h:96
@ AQI_HAZARDOUS
Definition sps30.h:102
@ AQI_GOOD
Definition sps30.h:97
@ AQI_UNHEALTHY
Definition sps30.h:100
@ AQI_VERY_UNHEALTHY
Definition sps30.h:101
@ AQI_MODERATE
Definition sps30.h:98
@ AQI_UNHEALTHY_SENSITIVE
Definition sps30.h:99
HAL_StatusTypeDef sps30_GetAutoCleanInterval(I2C_HandleTypeDef *hi2c, uint32_t *interval_sec)
interval for auto cleaning
Definition sps30.c:300
int8_t sps30_Is(I2C_HandleTypeDef *hi2c, int8_t tryInit)
check if SPS30 sensor is present
Definition sps30.c:83
int8_t sps30_Service(I2C_HandleTypeDef *hi2c)
the sensor need to be cleaning
Definition sps30.c:367
HAL_StatusTypeDef sps30_Init(I2C_HandleTypeDef *hi2c)
initialization of sensor sps30
Definition sps30.c:90
HAL_StatusTypeDef sps30_IsDataReady(I2C_HandleTypeDef *hi2c)
Check if data is available.
Definition sps30.c:201
HAL_StatusTypeDef sps30_On(I2C_HandleTypeDef *hi2c)
Turn on laser and fan to allow measurements.
Definition sps30.c:128
sps30_t _sps30Data
Live measurement data from the SPS30 sensor; updated by sps30_Read().
Definition sps30.c:38
Measurement data produced by the SPS30 particulate matter sensor. Populated by sps30_Read(); check Is...
Definition sps30.h:76
float Mass_pm1_0
Definition sps30.h:77
float Num_pm10_0
Definition sps30.h:85
float Num_pm4_0
Definition sps30.h:84
float Mass_pm10_0
Definition sps30.h:80
float Typical_particle_size
Definition sps30.h:86
float Mass_pm2_5
Definition sps30.h:78
float Mass_pm4_0
Definition sps30.h:79
float Num_pm2_5
Definition sps30.h:83
int8_t IsDataValid
Definition sps30.h:87
float Num_pm1_0
Definition sps30.h:82
float Num_pm0_5
Definition sps30.h:81