Iot Based Air Quality Monitoring System using NodeMCU | IoT Project | DIY electronics | Node mcu esp 8266 | Aurduino IDE
Fig. Air quality monitoring System using NodeMCU
Materials Required
Before you start, make sure that you have following listed materials required for this project.
Hardware
Software
- Arduino IDE
- ThingSpeak
Block Diagram
The following block diagrams helps us to design our system and gives an idea about how all these things form to create a network which can be accessible throughout the internet.
Fig. Block diagram of Air quality monitoring system.
So, as we can see in the above figure, we have to make only two hardware connections, i.e. we have to connect DHT11 and MQ135 sensors to the NodeMCU. Along with this, we have to set up wifi connection and thingSpeak connection, so that we can save our data on ThingSpeak platform and access it whenever we need it.
When the system is turned ON by providing 5V power supply provided by micro USB cable, firstly the system will connect to the WIFI network. Then it will take the input values from the sensors and lastly the system will post this sensor data to the ThingSpeak.
Sensor Connections
It is very easy to connect the sensors to the NodeMCU. For secure connections make sure that you have jumper wires of good quality. You can take help of circuit diagram to connect the sensors to the NodeMCU.
DHT11
DHT11 is the three pin sensor module which gives temperature and humidity values as outputs in the digital format. It is commonly used temperature and humidity sensor and it is best for Arduino and NodeMCU platform based projects. This sensor operates on 3.5 V to 5 V. This sensor is factory calibrated and hence easy to interface with microcontrollers. It can measure temperature 0°C to 50°C and humidity from 20% to 90% with accuracy of ±1°C and ±1%.
Connections
- Connect VCC pin of DHT11 to Vin pin of NodeMCU
- Connect GND pin of DHT11 to GND pin of NodeMCU
- Connect DOUT pin of DHT11 to D3 pin of NodeMCU
MQ135
MQ135 is four pin sensor module which detects smoke, benzene, steam and other harmful gases. It is highly sensitive in ammonia, sulfide, benzene steam, smoke and alcohol. The operating voltage of MQ135 gas sensor is from 2.5 V to 5 V. This sensor is low cost and easy to interface with Arduino and NodeMCU like development boards.
Connections
- Connect VCC pin of MQ135 to 3V3 pin of NodeMCU
- Connect GND pin of MQ135 to GND pin of NodeMCU
- Connect AOUT pin of MQ135 to A0 pin of NodeMCU
Fig. Circuit Diagram
Connect the sensors to NodeMCU as shown in above circuit diagram.
Now, our hardware connection is ready. The next step is creating account and channel on ThingSpeak where the system's data will be stored.
Creating Channel on ThingSpeak
ThingSpeak is an open source IOT analytics platform service. It allows us to aggregate, visualize and analyze live data streams in cloud. It visualizes the sensor data in real time. It runs the Iot analytics automatically based on events or schedules. We are using ThingSpeak to store and visualize our system's data to ThingSpeak. Once, we interface ThingSpeak to our system, it will plot graphs of sensor values against time. For this purpose we will have three fields to display graphs of each temperature, humidity and air quality values.
Follow these steps to connect your system to ThingSpeak.
Step 1 Sign up on ThingSpeak
It is required to have an account on ThingSpeak to have its access to its services. Click on this link to navigate to ThingSpeak and open an account. https://thingspeak.com .
Clicking on the above link will navigate you here. Now, click on the Sign up button I have highlighted it for you so that you can easily find. If you already have an account on ThingSpeak, there is no need to create a new one.
Now the next page will look like this one. On this page you have to fill out all the required details. After this verify your Email address. Now your account is ready to set up a channel for our Air Quality Monitoring System.
Step 2 Creating a channel on ThingSpeak
Log in to your ThingSpeak account and create a new channel by clicking on "New Channel"
Now, bellow page will appear. Fill out all the required fields like name, description and click on check box of field 1, field 2 and field 3. Also, write the names of each field so that we can recognize a particular data field. Click on save channel. Now, your channel is ready.
Step 3 Getting the API Key
Click on "API keys" and copy the API key, we will use this key in our code to upload the sensor data on our channel. The unique API key is needed to upload the data on channel. It is unique for every different channel. So, for different devices there should be different channels.
Setting Arduino IDE to program NodeMCU
If you have not programmed NodeMCU using Arduino IDE before then you have to do few things before you start. First, you have to add NodeMCU board to Arduino IDE.
Open Arduino IDE. To add the board, go to files click on preference or press ctrl+comma. After doing this a window will pop up which will be like shown below. Now, at the bottom of this window you will find "Additional Board Manager URLs" click on it and paste the following link into this bar and click on OK.
If this link is already present in the Boards Manager URLs then no need to add it again. If there is/are another links previously present there, you must add a comma after last link and then paste this one.
Now, click on tools and go to Boards and then Boards Manager. As shown below.
After clicking on Boards Manager the following window will open, on which you have to enter ESP8266 in search bar. Now click on install button to install the board. The installing may take few minutes. Once it has installed, search the "NodeMCU" in board list.
As shown above select the "NodeMCU 1.0 (ESP-12E Module)" .
Now, you are ready to code the NodeMCU. I have given you the code for this project which is for interfacing of two sensors only. If you wish to add more air quality related sensors or change any one of sensor that I have used here. You can take reference of this code. Only one additional thing you will have to do will be adding the library for your newly used sensor(if required) . In the given code I have included "DHT library by Adafruit".
Uploading the code
To upload code to the board, open the code file and click on the "right arrow" icon and wait for few seconds. Once it is done you can see the results on your serial monitor.
/*
This sketch is for interfacing of DHT11 and MQ135 sensor modules.
You can add more sesors to your system by editing this code for perticuler sensor.
*/
#include <ESP8266WiFi.h>
#include <DHT.h> // library for dht
String apiKey = "Paste here ThingSpeak API key"; // API key from ThingSpeak
#ifndef STASSID
#define STASSID "Your WiFi Name"
#define STAPSK "Password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* server = "api.thingspeak.com";
const uint16_t port = 17;
#define DHTPIN 0 //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float a= analogRead(A0); // Read data from MQ135 through analog pin A0
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (isnan(a))
{
Serial.println("Failed to read from MQ-135 sensor!");
}
Serial.println("Temperature:");
Serial.println(t);
Serial.println("Humidity:");
Serial.println(h);
Serial.println("AirQuality:");
Serial.println(a/1023*100);
delay(2000);
return;
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(a/1023*100);
postStr += "\r\n\r\n";
//Uplad the postSting with temperature and Humidity information
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.println("Temperature: ");
Serial.println(t);
Serial.println(" degrees Celcius Humidity: ");
Serial.println(h);
Serial.println("AirQuality(%): ");
Serial.println(a/1023*100);
Serial.println("% send to Thingspeak");
}
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(30000);
}
This sketch is for interfacing of DHT11 and MQ135 sensor modules.
You can add more sesors to your system by editing this code for perticuler sensor.
*/
#include <ESP8266WiFi.h>
#include <DHT.h> // library for dht
String apiKey = "Paste here ThingSpeak API key"; // API key from ThingSpeak
#ifndef STASSID
#define STASSID "Your WiFi Name"
#define STAPSK "Password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* server = "api.thingspeak.com";
const uint16_t port = 17;
#define DHTPIN 0 //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float a= analogRead(A0); // Read data from MQ135 through analog pin A0
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (isnan(a))
{
Serial.println("Failed to read from MQ-135 sensor!");
}
Serial.println("Temperature:");
Serial.println(t);
Serial.println("Humidity:");
Serial.println(h);
Serial.println("AirQuality:");
Serial.println(a/1023*100);
delay(2000);
return;
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(a/1023*100);
postStr += "\r\n\r\n";
//Uplad the postSting with temperature and Humidity information
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.println("Temperature: ");
Serial.println(t);
Serial.println(" degrees Celcius Humidity: ");
Serial.println(h);
Serial.println("AirQuality(%): ");
Serial.println(a/1023*100);
Serial.println("% send to Thingspeak");
}
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(30000);
}
I hope you like this tutorial 'Iot Based Air Quality Monitoring System Using NodeMCU'. Let me know your feedback in the comments.
Thank You!
Tags : Air Quality Monitoring using NodeMCU , Auiduno IDE, DHT11, MQ135, Iot.
Great jobs ��
ReplyDeleteThanks for share your project .
Thank You Constructionguruindia.com
DeleteGreat work but how do you interpret the air quality? When is the air quality good and when bad?
ReplyDelete