Using Mq135 Sensor with InfluxDB
Data AnalyticsHow ToSensorsTutorials/DIY

MQ135 Sensor & InfluxDB -based Air Quality Monitoring System

Monitoring air quality is crucial for ensuring healthy living environments. In this project, we’ll build an MQ135 Sensor-based Air Quality Monitoring System using ESP8266 NodeMCU and store the collected data in InfluxDB for analysis.

Components Required

  • ESP8266 NodeMCU
  • MQ135 Air Quality Sensor
  • Breadboard and Jumper Wires
  • Power Supply (5V)
  • InfluxDB (for data storage)
  • Grafana (for visualizing data – optional but recommended)

Step 1: Circuit Diagram and Connections

MQ135 PinNodeMCU Pin
VCC3.3V
GNDGND
A0A0

Ensure that your power connections are stable to prevent data inconsistencies.

Step 2: Setting Up InfluxDB

  1. Install InfluxDB:
    • On a Raspberry Pi or server, run: sudo apt update sudo apt install influxdb sudo systemctl start influxdb sudo systemctl enable influxdb
  2. Create Database and Credentials:
    • Open InfluxDB interface at http://localhost:8086.
    • Create a Bucket, Organization, and Token.
    • Note your URL, Token, and Organization for later use.

Step 3: ESP8266 Code for MQ135 and InfluxDB Integration

Here’s a sample code to read data from the MQ135 sensor and send it to InfluxDB.

C
#include <ESP8266WiFi.h>
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>

#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

#define INFLUXDB_URL "http://<YOUR_INFLUXDB_IP>:8086"
#define INFLUXDB_TOKEN "YOUR_INFLUXDB_TOKEN"
#define INFLUXDB_ORG "YOUR_ORG_NAME"
#define INFLUXDB_BUCKET "YOUR_BUCKET_NAME"

#define SENSOR_PIN A0  // MQ135 Analog Pin

InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
Point sensor("air_quality");

void setup() {
    Serial.begin(115200);
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }

    Serial.println("Connected to WiFi");

    if (client.validateConnection()) {
        Serial.println("Connected to InfluxDB");
    } else {
        Serial.println("InfluxDB connection failed");
    }

    sensor.addTag("device", "ESP8266_NodeMCU");
}

void loop() {
    int mq135Value = analogRead(SENSOR_PIN);
    float ppmValue = mq135Value * 0.1; // Sample PPM Conversion (Adjust as required)

    sensor.clearFields();
    sensor.addField("ppm", ppmValue);

    if (!client.writePoint(sensor)) {
        Serial.print("InfluxDB write failed: ");
        Serial.println(client.getLastErrorMessage());
    } else {
        Serial.print("MQ135 Value Sent: ");
        Serial.println(ppmValue);
    }

    delay(10000); // Send data every 10 seconds
}

Step 4: Configuring InfluxDB Dashboard

  1. Open your InfluxDB interface.
  2. Go to Explore and select your created Bucket.
  3. Select ppm as the field and visualize data using the chart options.

Step 5: Visualizing Data Using Grafana (Optional but Recommended)

  1. Install Grafana on your Raspberry Pi or Server: sudo apt-get install -y software-properties-common sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main" sudo apt-get update sudo apt-get install grafana
  2. Access Grafana at http://localhost:3000.
  3. Add InfluxDB as a data source and configure it with your URL, Token, and Bucket.
  4. Create a dashboard with visual graphs representing air quality data.

Step 6: Testing and Troubleshooting

  • Ensure your Wi-Fi credentials are correct.
  • Verify that InfluxDB is running and accessible.
  • Check sensor connections if data seems inconsistent.

Step 7: Project Enhancements (Optional Improvements)

  • Add Temperature and Humidity Sensors for multi-sensor environmental monitoring.
  • Implement alerts in Grafana for high pollution levels.
  • Use a OLED display to show real-time air quality readings.

Conclusion

This project effectively demonstrates how to build an MQ135-based Air Quality Monitoring System integrated with ESP8266 NodeMCU and InfluxDB for data logging and analysis. By visualizing data using Grafana, you gain valuable insights to ensure your indoor or outdoor environment remains safe and healthy.

If you have any questions or want to explore additional features, feel free to ask!

Recommended:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

7 thoughts on “MQ135 Sensor & InfluxDB -based Air Quality Monitoring System

Leave a Reply

Your email address will not be published. Required fields are marked *