Measuring Air Quality with OpenSensors: An Arduino & Python Guide
Learn how to monitor indoor air quality using an Arduino, a Shinyei PPD‑42 particle sensor, and the OpenSensors MQTT broker. This step‑by‑step tutorial demonstrates how to collect real‑time particulate data and publish it securely to a public data stream.
Introduction
For this project we wanted a simple yet powerful way to assess the air quality in our workplace. We chose the Shinyei PPD‑42 sensor for its affordability and ease of use, and paired it with an Arduino UNO and a Linux‑based host (e.g., Raspberry Pi). The sensor streams the concentration of small particulates that can pose health risks, and we publish that data continuously to the OpenSensors messaging broker.
What you’ll need:
- Shinyei PPD‑42 sensor
- Arduino UNO
- Linux computer or Raspberry Pi
This guide follows the DustDuino project, which already integrates the PPD‑42 with Arduino and Wi‑Fi. We adapted its wiring diagram and code to work with our OpenSensors setup.
First, connect the sensor to the Arduino as described in step 2 of the DustDuino instructions. Then download the sketch DustDuinoSerial.ino from the GitHub repository, open it in the Arduino IDE, and upload it to the UNO.
Verify the sensor is sending data by opening the Arduino IDE’s Serial Monitor.
Next, we’ll bridge the Arduino’s serial output to the OpenSensors broker using a lightweight Python script that leverages the Mosquitto client library.
Hello Python World
To confirm Python is ready on your machine, create a file named hi.py with the following content:
print("Hello World")
Run it from a terminal:
python hi.py
You should see Hello World printed to the console.
Hello OpenSensors
Install the Mosquitto Python client if you haven’t already:
sudo apt-get install python-pip
sudo pip install paho-mqtt
Test publishing to OpenSensors with this minimal script (replace Louis and the password with your credentials):
import paho.mqtt.client as mqtt
mqttc = mqtt.Client(client_id="939")
mqttc.username_pw_set("Louis", password="AbcDEFgH")
mqttc.connect("mqtt.opensensors.io")
mqttc.publish("/users/Louis/test2", payload="Hello Opensensors!", qos=0, retain=False)
mqttc.disconnect()
Upon successful execution you’ll see the message appear in the OpenSensors console.
To connect to the Arduino’s serial port, identify the device with:
dmesg | grep tty
Typical output looks like:
[ 0.000000] console [tty0] enabled
[ 3522.192687] cdc_acm 7-1:1.0: ttyACM0: USB ACM device
Here /dev/ttyACM0 is the serial port to use.
Read serial data with Python:
import serial
ser = serial.Serial('/dev/ttyACM0')
while True:
print(ser.readline())
Integrating Sensor Output with MQTT
Combine the serial reader and MQTT publisher in a single script. The example below publishes each line of sensor data to the topic /users/Louis/ODI/airquality every second:
import serial
import paho.mqtt.client as mqtt
import time
mqttc = mqtt.Client(client_id="939")
mqttc.username_pw_set("Louis", password="AbcDEFgH")
mqttc.connect("mqtt.opensensors.io")
ser = serial.Serial('/dev/ttyACM0') # adjust if your port differs
while True:
message = ser.readline()
print(message)
mqttc.publish("/users/Louis/ODI/airquality", payload=message, qos=0, retain=False)
time.sleep(1)
For easier downstream processing, consider formatting the Arduino output as JSON before publishing.
Explore the resulting data stream on OpenSensors by visiting the topic you created here.
Full Code Example
import serial
import paho.mqtt.client as mqtt
import time
mqttc = mqtt.Client(client_id="939")
mqttc.username_pw_set("Louis", password="AbcDEFgH")
mqttc.connect("opensensors.io")
ser = serial.Serial('/dev/ttyACM0') # open first serial port
while True:
message= ser.readline()
print(message)
mqttc.publish("/users/Louis/ODI/airquality", payload=message, qos=0, retain=False)
time.sleep(1);
Source: How to Measure Air Quality on OpenSensors
Manufacturing process
- The Hidden Health Risks of Poor Indoor Air Quality
- Build a Universal IR Remote with Arduino: Step‑by‑Step Guide
- Measure Earth's Mass with Arduino: A Step‑by‑Step DIY Guide
- Build a Precise Air Quality Monitor with the Sharp GP2Y1010AU0F Sensor
- Build an Arduino‑Based Air Quality Monitor Using the DSM501A Dust Sensor
- Arduino-Based Indoor Air Quality & Comfort Sensor Kit
- Advanced Indoor Air Quality Monitoring System with Arduino & IoT
- Leveraging IoT to Build Advanced Indoor Air Quality Monitoring Systems
- Silicone Wristbands: Affordable Passive Samplers for Accurate Air Quality Monitoring
- Measuring Quality in Metal Fabrication: Standards & Best Practices