Introduction
A small IoT node that monitors PM2.5, PM10, NO2, CO(ppm) along with Temperature and Humidity in real-time and predicts the next AQI value using Machine Learning Techniques. Sensor data and AQI are displayed on an app running on the local machine.
Block Diagram
The Idea
Having tinkered with Arduino in the past, I wanted to have a serious go at an IoT project. I came across a number of projects online that used a single gas sensor or PM sensors with ESP32 or Raspberry Pi, but only a few that monitored multiple pollutants and a handful that integrated some kind of machine learning.
Since ESP32 doesn’t have a dedicated module for neural network processing, I was left with statistical models like Linear Regression. Which provided a satisfactory level of accurac…
Introduction
A small IoT node that monitors PM2.5, PM10, NO2, CO(ppm) along with Temperature and Humidity in real-time and predicts the next AQI value using Machine Learning Techniques. Sensor data and AQI are displayed on an app running on the local machine.
Block Diagram
The Idea
Having tinkered with Arduino in the past, I wanted to have a serious go at an IoT project. I came across a number of projects online that used a single gas sensor or PM sensors with ESP32 or Raspberry Pi, but only a few that monitored multiple pollutants and a handful that integrated some kind of machine learning.
Since ESP32 doesn’t have a dedicated module for neural network processing, I was left with statistical models like Linear Regression. Which provided a satisfactory level of accuracy, given the relatively small dataset that I used to train the model (~2000 values).
Sensors
- PMS7003: Plantower’s PMS7003 measures PM1.0, PM2.5 and PM10, serial communication, constant 5V supply,PMS library used.
- MQ135: measures NO2, requires calibration and a long burn-in period. MQUnifiedsensors library used to calculate ppm value ( requires a few tweaks to get a semi-accurate reading). 5V supply.
- MQ7: measures CO concentration, 5V supply
- DHT11: Temperature and Humidity, 3.3V from ESP32
NOTE: If you wish to replicate this project:
- Download the latest "node_v4.ino" file and "app_v1.py" from the repository.
- Make sure to set the value of "RL" according to the sensor that you are using and calibrate the sensors properly to get a stable "R0" value, which is the sensor’s resistance in baseline concentration of a gas/clean air, this value is used by the library to calculate concentration in ppm. You can adjust the (RS/R0) ratio, which is passed as arguments in these functions "calcR0_CO2 += co2Sensor.calibrate(5.5);" and "calcR0 += coSensor.calibrate(22.0);" accordingly.
Code and Data Acquisition
- "node_v4.ino" in the "main" folder is the latest version of the code, containing an AQI prediction function that uses weights and bias obtained from the Model.
//AQI Prediction//float predictAQI(float pm25_raw, float pm10_raw){//StandardScaler transformationfloat pm25_scaled = (data.PM_AE_UG_2_5 - 197.25513906) / 50.92380164;float pm10_scaled = (data.PM_AE_UG_10_0 - 227.17230955) / 60.14131832;//Linear Regression Formulafloat aqi = (133.59694591 * pm25_scaled) + (-86.0035514 * pm10_scaled) + 349.86281917;return aqi;}
- Sensor data and predicted AQI is sent via UDP packet; it can be accessed via USB as well.
char ssid[] = "YourSSID";char pass[] = "YourPassword";const char* pc_ip="10.115.108.48";const intpc_port=8000;WiFiUDP udp;
- "app_v1.py" listens to the port specified in the.ino file, and uses a simple UI to display the received data.
Data was recorded using "data_logger.py" scripts into.csv file
Which was then processed using "data_process.py" script that cleans up and scales the data into a usable form for model training.
Processing output
StandardScaler was used; 80/20 train test split.
Model Training
Model training was done in colab, the notebooks are in the "ML" folder in the repository.
Mean Absolute Error=19.704
Linear Regression Model performance
Despite the limited amount of data, the model can predict with reasonable accuracy as can be seen from the following screenshots from the dashboard.
Dashboard
Dashboard Screenshot
Dashboard Screenshot
Dashboard receives the data packet and calculates the AQI based on CPCB’s breakpoints