01 Abstract
This project implements a distributed IoT sensor network for real-time environmental monitoring in a smart building. A collection of wireless sensor nodes (ESP32-based) measure temperature, humidity, CO₂, and occupancy, publishing data via MQTT to a central broker. A time-series database (InfluxDB) stores measurements with 30-second granularity over a 3-day rolling window. An interactive web dashboard provides live visualization, historical analysis, and rule-based anomaly detection for HVAC optimization.
The system is designed for scalability and resilience: nodes include local data buffering to tolerate network interruptions, edge-side data aggregation reduces backend load, and the MQTT pub/sub pattern decouples sensors from analysis systems.
This IoT architecture demonstrates core principles applicable to smart cities, industrial automation, and environmental monitoring — all areas where distributed sensing and real-time decision-making are critical.
02 System Architecture
Network Topology
The network follows a star topology centered on an MQTT broker (Mosquitto, on Raspberry Pi). Each sensor node is an independent device communicating wirelessly via WiFi (802.11b/g/n on 2.4 GHz). Nodes publish sensor readings to topic hierarchies (e.g., building/floor1/room_a/temperature), and a Python subscriber on the backend aggregates and persists data to InfluxDB.
Sensor Nodes
Each node hosts up to four sensors and publishes data on an independent cadence. Readings are timestamped locally and include metadata (node_id, location, battery level). If network connectivity is lost, data is buffered in flash memory (up to 1000 records) and replayed upon reconnection.
MQTT Protocol & Topics
Topic structure follows the convention building/{floor}/{room}/{parameter}. QoS level 1 (at-least-once delivery) ensures no loss during temporary disconnections. Retained messages on building/status/+/last_seen allow subscribers to detect stale data and trigger alerts.
building/floor1/living_room/temperature → 22.3 °C
building/floor1/living_room/humidity → 45.2 %RH
building/floor1/living_room/co2 → 580 ppm
building/floor1/living_room/occupancy → 2 persons
Each node publishes every 30 seconds. Broker stores retained messages for 60 minutes.
03 Hardware Implementation
ESP32 Module
The Espressif ESP32 is a dual-core 32-bit MCU with integrated WiFi and Bluetooth, ideal for battery-constrained IoT. Each node runs FreeRTOS and custom firmware in C++ (using the Arduino framework). Power consumption is ~80 mA during active transmission and ~2 mA in deep sleep.
Sensor Modules
All sensors communicate via I²C (address 0x60–0x77). Calibration is stored in EEPROM; auto-ranging is applied for CO₂. Noise filtering uses a 5-tap moving average.
| Sensor | Parameter | Range | Accuracy |
|---|---|---|---|
| DHT22 | Temperature, Humidity | −40 to +80 °C, 0–100 %RH | ±0.5 °C, ±2 %RH |
| CCS811 | CO₂ equivalent (eCO₂) | 400–8192 ppm | ±25 % (typ) |
| PIR + counter | Motion / Occupancy | 0–10 persons | ±1 person |
| ADC (ACS712) | Power consumption | 0–30 A | ±1.5 % FS |
04 Firmware & Software Architecture
Embedded Firmware (C++)
Firmware runs three concurrent FreeRTOS tasks: SensorTask (read sensors, apply filters), NetworkTask (WiFi/MQTT), and StorageTask (manage flash buffer). A watchdog timer resets the device if any task hangs.
Backend & Data Pipeline
A Python subscriber runs on a Raspberry Pi, consuming MQTT messages and inserting them into InfluxDB. Rules engine triggers alerts if temperature deviates >3 °C from setpoint or CO₂ exceeds 1000 ppm (indoor air quality threshold). Alerts are logged and published to a building/alerts topic.
The decoupled architecture (sensors → MQTT → backend) allows independent scaling: more sensors can be added without modifying the analysis pipeline, and the backend can be upgraded (switch DB, add ML) without touching firmware.
05 Dashboard & Visualization
An interactive web dashboard (built with plotly.js and fetch API) queries InfluxDB for historical data and displays:
- Live metrics: Current temperature, humidity, CO₂, occupancy for each room (updates every 10 seconds).
- Time-series plots: 24-hour temperature profile, cumulative energy (if power sensor present).
- Heatmaps: Room-by-room thermal distribution overlaid on floor plan.
- Alerts log: Time-stamped anomalies with acknowledgment buttons.
The dashboard is available in a dedicated HTML page (dashboard.html) and is fully responsive for mobile monitoring.
06 Data Analytics & Insights
Post-processing scripts (Python + pandas) extract weekly and monthly occupancy patterns, thermal inertia characterization, and correlation between internal temperature and outdoor conditions. Results feed back into HVAC scheduling algorithms.
07 Conclusions
This IoT system demonstrates a complete end-to-end architecture from embedded sensor nodes through cloud data ingestion to user-facing analytics. The MQTT-based design is inherently scalable and resilient, making it suitable for deployment in diverse environments (offices, homes, industrial facilities).
Progetto per l'area Smart Building e IoT — Politecnico di Milano, Dipartimento di Ingegneria Informatica.