Preparation
Before we begin, you will have to obtain:
Arduino Software.
MQ135 Library by GeorgeK
Import Library
Arduino > Sketch > Include Library > Add .ZIP library (Select the MQ135 Library zip file that you have downloaded from above)
Connect the NodeMCU to MQ135 sensor
Connect NodeMCU A0 to MQ135 AO
Connect NodeMCU GND to MQ135 GND
Connect NodeMCU 3.3V to MQ135 VCC
Burnout
It is necessary to "burnout" the sensor for first use to calibrate proper rzero value, to do so paste following code to your arduino window :
#include "MQ135.h"
const int ANALOGPIN=0;
MQ135 gasSensor = MQ135(ANALOGPIN);
void setup(){
Serial.begin(115200); // sets the serial port to 115200
}
void loop(){
float rzero = gasSensor.getRZero();
Serial.println(rzero);
delay(1000);
}
Upload the code to NodeMCU
Open the serial monitor : Tools > Serial Monitor
Let it running for an hour or two until you get constant rzero value. Write down that RZERO value as you will need it for later use.
If you made it up to here, you almost got to the end.
ThingSpeak
This is the platform that we are going to use to store our measures and display them in a nice graph that we can embed to any website or similar. (This tutorial might expand in future to a selfhosted variant of storing and displaying data)
Sign up
Make new channel on [https://thingspeak.com/channels/new] (Name: Anything, Field1: PPM) - Save and obtain write API key
Measure Air Quality
Adapt following code to reflect your RZERO value, API key, SSID and password.
Note: RZERO is the value from the burnout section.
#include <ESP8266WiFi.h>
#include "MQ135.h"
#define RZERO 76.63
const int ANALOGPIN=0;
MQ135 gasSensor = MQ135(ANALOGPIN);
String apiKey = "WRITE-API-KEY"; // Enter your Write API key from ThingSpeak
const char *ssid = "SSIDNAME"; // replace with your wifi ssid
const char *pass = "PASSWORDHERE"; // replace this field with your password
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
float ppm = gasSensor.getPPM();
Serial.println(ppm);
delay(1000);
if (client.connect(server, 80))
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(ppm);
postStr += "r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Gas Level: ");
Serial.println(ppm);
Serial.println("Data Send to Thingspeak");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates.
delay(1500);
}
That is it, from here you are sending values to ThingSpeak platform, you got yourself a nice graph, and you are monitoring air quality
Here is the image of values to know when the air is bad :