DreamLab
Published © Apache-2.0

Air Quality Monitoring System

Air Quality Monitoring System Using Wio Terminal and PM2.5 Sensor

IntermediateShowcase (no instructions)2 hours58
Air Quality Monitoring System

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
Seeed Studio Grove - Laser PM2.5 Dust Sensor - Arduino Compatible - HM3301
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Project Diagram

Diagram

Code

Code

C/C++
#include <Arduino.h>
#include <Wire.h>
#include "Seeed_HM330X.h"

// Create sensor object (I2C mode)
HM330X sensor;

uint8_t buffer[30];

void printError(HM330XErrorCode error)
{
  switch (error)
  {
    case NO_ERROR:
      Serial.println("No error");
      break;
    case ERROR_NOT_READY:
      Serial.println("Sensor not ready");
      break;
    case ERROR_DATA_OVERFLOW:
      Serial.println("Data overflow");
      break;
    case ERROR_SERIAL_NO_ACK:
      Serial.println("No ACK from sensor");
      break;
    default:
      Serial.println("Unknown error");
      break;
  }
}

void setup()
{
  Serial.begin(115200);
  delay(1000);

  Serial.println("=== Wio Terminal Air Quality Monitor (I2C) ===");

  // Initialize I2C
  Wire.begin();

  // Initialize sensor in I2C mode
  if (sensor.init())
  {
    Serial.println("HM3301 sensor initialized successfully");
  }
  else
  {
    Serial.println("Sensor initialization failed!");
  }
}

void loop()
{
  HM330XErrorCode error;

  error = sensor.read_sensor_value(buffer, 29);

  if (error == NO_ERROR)
  {
    // Data format: PM1.0, PM2.5, PM10 (standard positions)
    int pm1_0  = buffer[4]  << 8 | buffer[5];
    int pm2_5  = buffer[6]  << 8 | buffer[7];
    int pm10   = buffer[8]  << 8 | buffer[9];

    Serial.println("\n---------------------------");
    Serial.println("AIR QUALITY DATA (I2C)");
    Serial.println("---------------------------");

    Serial.print("PM1.0  : ");
    Serial.print(pm1_0);
    Serial.println(" µg/m³");

    Serial.print("PM2.5  : ");
    Serial.print(pm2_5);
    Serial.println(" µg/m³");

    Serial.print("PM10   : ");
    Serial.print(pm10);
    Serial.println(" µg/m³");

    // Air quality classification
    String status;

    if (pm2_5 <= 12)
      status = "Good";
    else if (pm2_5 <= 35)
      status = "Moderate";
    else if (pm2_5 <= 55)
      status = "Unhealthy";
    else
      status = "Hazardous";

    Serial.print("Status : ");
    Serial.println(status);
  }
  else
  {
    Serial.print("Sensor error: ");
    printError(error);
  }

  delay(2000);
}

MQTT code

C/C++
#include <PubSubClient.h>

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void reconnect() {
  while (!client.connected()) {
    client.connect("WioTerminalClient");
  }
}

void sendData(int pm25) {
  String payload = "{\"pm25\": " + String(pm25) + "}";
  client.publish("air/quality", payload.c_str());
}

Credits

DreamLab
8 projects • 9 followers

Comments