Breev - End-to-End AI Air Quality System
Taming the NaN: A Physics-Informed AI system connecting low-cost IoT sensors to a deep learning brain.

The Story
The Invisible Enemy
We live in an era where the air we breathe is becoming increasingly invisible, yet increasingly dangerous. As an Information Systems student, I often wondered: Can we use technology not just to monitor this, but to predict it?
This curiosity turned into an obsession when I stumbled upon a research paper titled "AirPhyNet: Physics-Informed Deep Learning for Air Quality Modeling". I didn't just want to replicate this code; I wanted to bring it to life. I envisioned "Project Breev": a complete ecosystem connecting the physical world to a digital brain.
1. The Architecture: From Chaos to Order
Building an end-to-end IoT system is more than just stacking containers; it's about orchestrating a symphony of data. I designed a robust pipeline that moves sensor readings from the streets of Jakarta to our AI cloud in milliseconds.
Figure 1: High-Level Architecture. Data flows from ESP32 sensors via MQTT, processed by a Python worker, stored in MongoDB, and ultimately fed into the AirPhyNet Model.
The architecture relies on MQTT—the heartbeat of the system. Unlike HTTP, which requires a handshake for every request, MQTT keeps a persistent connection, allowing our ESP32 sensors to stream data with minimal overhead. It’s the difference between sending a letter and having a conversation.
2. The Hardware: "The Frankenstein" Setup
Theory is clean; hardware is messy. We built our sensing node using the MQ-135 gas sensor and DHT22 for temperature/humidity.
Figure 2: The Physical Prototype. An ESP32 microcontroller wired to MQ-135 and DHT22 sensors, transmitting vital air quality data every 5 seconds.
The Hardware Betrayal: Real-world data is dirty. I discovered the sensor drifted by 2-3% whenever humidity exceeded 80%—it was "lying" to me due to physical properties. To combat this, I wrote a software calibration algorithm in the preprocessing pipeline that neutralizes this drift using real-time humidity data, ensuring reliability even on humid nights.
3. The AI: Taming the NaN
The biggest battle wasn't coding; it was math. My first training runs were a disaster, resulting in NaN (Not a Number) losses. The model wasn't learning; it was exploding. I treated these failures not as dead ends, but as data points.
Strategy 1: Anti-Exploding Gradient
I implemented torch.nn.utils.clip_grad_norm_ to cap extreme errors before they shattered the model weights.
Strategy 2: The "Brain" Upgrade
I tuned the architecture, increasing hidden_size to 128 and adding Dropout (0.3) layers. To prevent "memorizing" (overfitting), I added Weight Decay (1e-4) to the Adam optimizer—a form of L2 regularization that penalizes the model for being too complex.
Strategy 3: Scientific ML
I didn't just trust the data; I trusted physics. I integrated a custom loss function: total_loss = loss + 0.01 * p_loss. This forces the model to respect fluid dynamics (advection/diffusion) rather than just minimizing error.
Figure 3: Training History. The struggle (left) vs. convergence (right). After 200 epochs of fine-tuning learning rates (1e-4), the loss curve finally stabilized.
4. The Breakthrough: Analyzing the "Good Fit"
After weeks of iteration, the numbers finally turned green.
- Test R² Score: 0.537 – 0.569. (Explaining ~57% of variance).
- The "Gap" Analysis:
- Train Loss: 0.433
- Test MSE: 0.466
- Gap: 0.033
This tiny gap is the holy grail of Machine Learning: a "Good Fit". It means the model isn't just memorizing the answer key (overfitting); it actually understands the patterns.
5. The Result: Actionable Intelligence
All this engineering culminates in the Dashboard—a Next.js interface that empowers users to see the invisible. We didn't just want raw data; we wanted clarity.
The Command Center The main dashboard serves as the central hub for real-time monitoring. It aggregates data from multiple sensor nodes, presenting a live AQI reading that updates every 2.3 seconds. Users can instantly see the current air quality status (Healthy, Moderate, Unhealthy) colour-coded for immediate decision making.
Figure 4: The Breev Dashboard. Real-time AQI visualization with historical trends and a 72-hour forecast engine.
Deep Dive Analytics Beyond simple gauges, the system offers deep analytical capabilities. Users can drill down into specific pollutants—tracking Carbon Monoxide (CO), Nitrogen Oxides (NOx), and Ozone (O3) individually. This granularity helps identify specific pollution sources, such as vehicle emissions vs. industrial waste.
Figure 5: Detailed Analytics View. Granular data drill-down showing specific pollutant levels (CO, NOx, O3) over time.
Validating the Future The true test of the system is its predictive power. The plot below visualizes the model's performance on unseen test data. The Blue Line represents the ground truth (actual sensor values), while the Yellow Line is the AI's prediction. The close alignment (R² ~0.57) proves that the Physics-Guided approach successfully captures the complex, non-linear dynamics of air pollution.
Figure 6: Prediction vs Actual. The yellow line (Prediction) closely follows the blue line (Actual Ground Truth), proving that Physics-Guided AI can work on low-cost hardware.
6. References & Scientific Foundation
This project was heavily inspired by the research paper "AirPhyNet: Physics-Informed Deep Learning for Air Quality Modeling". The core innovation lies in treating pollution not just as a time-series problem, but as a fluid dynamics phenomenon governed by advection and diffusion equations.
- Paper: AirPhyNet: Physics-Informed Deep Learning for Air Quality Modeling
- Documentation: MLflow for Experiment Tracking
- Dataset: Jakarta Air Quality Index (2013-2024) on Kaggle.
Conclusion
Project Breev taught me that Machine Learning is 20% modeling and 80% engineering. A fancy model cannot fix broken data. MLOps saved me—tracking every "Run A" vs "Run B" in MLflow allowed me to debug the math behind the code. Resilience isn't just a soft skill; in AI, it's the ability to look at a NaN loss and see a math problem waiting to be solved.
The Problem
The "Exploding Gradient" Nightmare
My initial AI model failed. Not just "low accuracy" failed, but NaN (Not a Number) failed. I stared at the console, seeing loss values explode into infinity, realizing my model wasn't learning—it was broken. The R² score was -13.48, meaning my AI was performing worse than a random guess.
Simultaneously, the physical world betrayed me. The low-cost MQ-135 sensors were drifting by 2-3% purely due to humidity changes, effectively sending "lying" data to the cloud. To top it off, I had to deploy this heavy stack (Database, API, AI Model) on a single resource-constrained VPS, leading to constant "Out of Memory" crashes.
The Goal
Operationalizing Research
The goal wasn't just to write code, but to build a "Good Fit" model that is not a Black Box. I aimed to translate the theoretical "AirPhyNet" research paper into a working, end-to-end production system.
- Latency Target: < 3 seconds (Sensor to Dashboard). with MQTT.
- Accuracy Target: R² Score > 0.5 (Good Fit).
- Resilience: Automated drift compensation for sensor hardware.
Ultimately, I wanted to prove that advanced Physics-Informed AI could run on affordable, democratized IoT infrastructure.
Key Features
AirPhyNet Implementation
Physics-informed neural network capturing advection and diffusion.
Robust Data Pipeline
Automated calibration algorithm compensating for sensor humidity drift.
MLOps Automation
CI/CD with GitHub Actions and model tracking via MLflow/DagsHub.
Real-time Dashboard
Live AQI visualization with 2.3s latency using MQTT & WebSockets.
Infrastructure Monitoring
Grafana dashboards tracking CPU/RAM to manage VPS resources.
Lessons Learned
80% Engineering, 20% Modeling
Project Breev taught me that in the real world, a "perfect" model is useless if the data pipeline is broken. I learned that MLOps is not a luxury, but a necessity—without MLflow to track my experiments, I would have been lost in a sea of log files.
I also learned Mathematical Resilience. Seeing "NaN" is terrifying, but it's usually just a math problem (like unscaled inputs or high learning rates) waiting to be solved. This project shifted my mindset from "Student" to "Engineer"—someone who treats failures not as dead ends, but as data points to be debugged.
💡 Spotlight
From NaN to R² 0.569: Capturing 53% of air quality variance with a $5 sensor setup.
Tech Used
Team
- Ageng Putra PratamaAI & IoT Engineer
- Pak Subhan & Pak AndikThesis Advisors