Pro

Air Quality Monitor (ESP32)

A dashboard that only reacts to new messages shows blank data on every single reconnect — and a browser refresh is a reconnect.

  • ESP32
  • C++
  • MQTT
  • Dashboard

About this project

An ESP32 reads PM2.5 from a Plantower PMS5003 and temperature/humidity from a DHT22, builds one small JSON reading, and publishes it over MQTT every few minutes — a real, low-power publish interval, not a demo shortcut. A small Flask dashboard subscribes to every sensor's topic and shows the latest reading per location, with a CPCB PM2.5 category badge.

A dashboard that only reacts to new MQTT messages shows blank or stale data every single time it connects or reconnects — and a browser refresh is a reconnect — until the next publish actually happens. That is not a bug in Flask or in this dashboard's own code; it is how a plain publish() with no retain flag works by design. The fix is one flag on the exact same publish call: MQTT's own retain flag, which asks the broker — not the publisher, not the subscriber — to remember the last value on a topic and hand it to any client that subscribes afterwards, immediately. Proven with a real, timed standalone script (a fresh subscriber times out after 3+ seconds with no retain, then receives the same reading in under 3 milliseconds with it), and again through the actual broker/dashboard/publisher pipeline: a dashboard restarted after publishing shows every reading instantly, tagged as retained, instead of sitting blank until the next scheduled reading arrives.

What you will learn

  • Understand MQTT publish/subscribe as a broker-mediated pattern, not a direct connection
  • Explain why a late-connecting subscriber gets nothing without the retain flag, and prove it with a timed measurement
  • Use the MQTT retain flag correctly on both an ESP32 publisher and a Python one
  • Read and apply India's CPCB PM2.5 breakpoints, with their real limitations honestly disclosed
  • Reuse the exact retain pattern in any pub/sub system where state changes slowly and clients reconnect

Before you start

  • Arduino/C++ basics and this catalogue's arduino-weather-station or iot-plant-watering-system
  • Basic Python — reading a Flask app and an MQTT client
  • An ESP32 dev board, a PMS5003 and a DHT22 for real hardware use — none needed to install, verify, or read this project

New to ESP32? The free ESP32 course covers everything this project assumes.

Academic integrity notice

These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.