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 Pin | NodeMCU Pin |
---|---|
VCC | 3.3V |
GND | GND |
A0 | A0 |
Ensure that your power connections are stable to prevent data inconsistencies.
Step 2: Setting Up InfluxDB
- Install InfluxDB:
- On a Raspberry Pi or server, run:
sudo apt update sudo apt install influxdb sudo systemctl start influxdb sudo systemctl enable influxdb
- On a Raspberry Pi or server, run:
- 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.
- Open InfluxDB interface at
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.
#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
- Open your InfluxDB interface.
- Go to Explore and select your created Bucket.
- Select
ppm
as the field and visualize data using the chart options.
Step 5: Visualizing Data Using Grafana (Optional but Recommended)
- 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
- Access Grafana at
http://localhost:3000
. - Add InfluxDB as a data source and configure it with your URL, Token, and Bucket.
- 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:
Pingback: Home automation | IoT Products for Home Automation
Pingback: How To Setup Static IP Address on Raspberry Pi - Raspberry Pi
Pingback: Best Arduino IDE alternatives to start programming
Pingback: Raspberry Pi GPIO Basics - IoTbyHVM - Explore TechBytes
Pingback: VerneMQ - Clustering MQTT for high availability and scalability
Pingback: MQTT | What is MQTT | MQTT in Depth | QoS | FAQs | MQTT Introduction
Pingback: Interface MQ135 (Gas Sensor) with NodeMCU - IoTbyHVM