Using Mq135 Sensor with InfluxDB

Today in this tutorial i am telling to you How to measure air quality using MQ135 Sensor (formally known as Gas Sensor) and stored data into InfluxDB.

So You need :

  • MQ135 Senor
  • Esp8266
  • Arduino IDE
  • Influx DB
  • NodeJS
  • MQTT Broker

Visit this article for install Nodejs – How To Install Node.js on Ubuntu

Visit this article for install InFluxDb –How to Install InfluxDB on Ubuntu and Getting Started with InfluxDB

We use test.osquitto.org for MQTT broker but if you are intrested in your own secure mqtt broker, visit this article – How To Create Secure MQTT Broker

Using Mq135 Sensor with InfluxDB

So i provide codes given below. Modify code and use

Data.js code (this code run on System where you want to stored data)

**********************************data.js*****************************************

var mqtt = require('mqtt');
const fs = require('fs');
//var obj = { 'username': 'user', 'password': 'password' }
var client = mqtt.connect('mqtt://test.mosquitto.org:1883');
//var client = mqtt.connect('mqtt://test.mosquitto.org:1883', obj);
const Influx = require('influxdb-nodejs');
const clientInflux = new Influx('http://127.0.0.1:8086/aqDB');
const fieldSchema = {
aq: 'i',
ppm:'f'
};
const tagSchema = {
deviceId:'*'
};
clientInflux.schema('sendData', fieldSchema, tagSchema, {
// default is false
stripUnknown: true,
});

client.on('connect', () => {
console.log('Connected to server');
client.subscribe('sendData');
});
client.on('close', () => {
console.log('Disconnected from server');
});

client.on('message', (topic, message) => {
console.log("mqtt msg : " + message.toString());
data = JSON.parse(message)
if (data.deviceId && data.data) {
clientInflux.write('airData')
.tag({
deviceId: data.deviceId,
})
.field({
aq: data.data.rzero,
ppm:data.data.ppm
})
.then(() => {
console.log("Success")
})
.catch((err)=>{
console.log(err)
});
}
else
{
console.log("")
}
});

*****************************data.js***************************************

Node Sensor Code “mq135.ino”

Flash this code In ESP8266 using Arduino IDE. If you don’t know visit this article – Arduino Support for ESP8266 with simple test code

*********************************Mq135.js*********************************

#include <ESP8266WiFi.h>
#include <MQ135.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define ANALOGPIN A0

const char* ssid = "enter ssid";
const char* password = "enter password";

const char* mqttServer = "test.mosquitto.org";
const int mqttPort = 1883;
//const char* mqttUser = "user";
//const char* mqttPassword = "password";

WiFiClient espClient;
PubSubClient client(espClient);


MQ135 gasSensor = MQ135(ANALOGPIN);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(100);
pinMode(2, OUTPUT);
// WiFi.softAP(hostssid, hostpassword);/
Serial.println();
WiFi.begin(ssid, password);
int i = 0;
while ( (i <= 10) && (WiFi.status() != WL_CONNECTED) )
{
delay(500);
Serial.print(".");
i++;
}
Serial.println();
// WiFi.config(ip,gateway,subnet,dns);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

client.setServer(mqttServer, mqttPort);
client.setCallback(callback);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

if (client.connect("ESP8266Client1")) {

Serial.println("connected");

} else {

Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);

}
}

client.publish("sendData", "Hello from ESP8266");
client.subscribe("sendData");

}

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived in topic: ");
Serial.println(topic);

Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}

Serial.println();
Serial.println("-----------------------");

}

void loop() {
// put your main code here, to run repeatedly:
float rzero = gasSensor.getRZero(); //this to get the rzero value, uncomment this to get ppm value
Serial.print("RZero=");
Serial.println(rzero); // this to display the rzero value continuously, uncomment this to get ppm value

float ppm = gasSensor.getPPM(); // this to get ppm value, uncomment this to get rzero value
Serial.print("PPM=");
Serial.println(ppm); // this to display the ppm value continuously, uncomment this to get rzero value
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["ppm"] = ppm;
String d;
json.printTo(d);
client.publish("sendData", d.c_str());
Serial.println(d);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}

*********************************mq135******************************

Pin Diagram for Mq135 sensor

mq135                                esp82266

VCC       ============> 3.3v

GND     ============>GND

DATA   ============>A0

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm

6 thoughts on “Using Mq135 Sensor with InfluxDB

Leave a Reply

%d bloggers like this: