0
0
Drone-programmingHow-ToBeginner · 4 min read

IoT Project for Air Quality Monitoring: Setup and Code Example

An IoT project for air quality monitoring uses sensors like MQ-135 connected to a microcontroller (e.g., ESP32) to measure pollutants. Data is sent via MQTT protocol to a server or cloud for real-time monitoring and alerts.
📐

Syntax

This project involves three main parts: sensor reading, data transmission, and data reception.

  • Sensor reading: Use analog or digital pins to get air quality data from sensors like MQ-135.
  • Data transmission: Use MQTT protocol to send sensor data over Wi-Fi.
  • Data reception: A server or cloud service subscribes to the MQTT topic to receive and display data.
cpp
#include <WiFi.h>
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient mqttClient(espClient);

void setup() {
  Serial.begin(115200); // Start serial communication
  WiFi.begin("SSID", "PASSWORD"); // Connect to Wi-Fi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  mqttClient.setServer("broker.hivemq.com", 1883); // MQTT broker
  while (!mqttClient.connected()) {
    mqttClient.connect("ESP32Client");
  }
}

void loop() {
  int airQualityValue = analogRead(A0); // Read sensor
  char payload[10];
  sprintf(payload, "%d", airQualityValue); // Convert to string
  mqttClient.publish("air/quality", payload); // Publish data
  delay(2000); // Wait 2 seconds
}
💻

Example

This example shows how to read air quality data from an MQ-135 sensor connected to an ESP32, then send it via MQTT to a public broker.

cpp
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) {
    if (client.connect("ESP32AirQuality")) {
      Serial.println("MQTT connected");
    } else {
      delay(1000);
    }
  }
}

void loop() {
  int airQuality = analogRead(34); // MQ-135 sensor connected to GPIO34
  char msg[10];
  snprintf(msg, 10, "%d", airQuality);
  client.publish("home/airquality", msg);
  Serial.print("Published air quality: ");
  Serial.println(msg);
  delay(5000);
}
Output
... WiFi connected MQTT connected Published air quality: 512 Published air quality: 520 Published air quality: 515 ...
⚠️

Common Pitfalls

  • Incorrect sensor wiring: Make sure the sensor's analog output pin connects to the correct microcontroller pin.
  • Wi-Fi connection failures: Double-check SSID and password; add retries and timeouts.
  • MQTT broker issues: Use a reliable broker and ensure the client ID is unique.
  • Data format errors: Convert sensor readings to strings before publishing.
cpp
/* Wrong: Publishing integer directly without conversion */
int airQuality = analogRead(34);
mqttClient.publish("topic", String(airQuality).c_str()); // This will work

/* Right: Convert integer to string before publishing */
char msg[10];
sprintf(msg, "%d", airQuality);
mqttClient.publish("topic", msg);
📊

Quick Reference

  • Sensor: MQ-135 or similar air quality sensor
  • Microcontroller: ESP32 or ESP8266
  • Protocol: MQTT over Wi-Fi
  • Broker: Public (e.g., broker.hivemq.com) or private MQTT broker
  • Data: Analog sensor value converted to string before publishing

Key Takeaways

Use an air quality sensor like MQ-135 connected to a microcontroller to measure pollutants.
Send sensor data over Wi-Fi using MQTT protocol to a broker for real-time monitoring.
Always convert sensor readings to strings before publishing via MQTT.
Ensure stable Wi-Fi and MQTT connections with proper error handling.
Test sensor wiring and data flow step-by-step to avoid common mistakes.