윤원호Parkmoonsugledel정다연
Published © GPL3+

Air Meter Making #4: Complete

It can measure fine dust, humidity, temperature and CO2 concentration in the air and check it on the internet in real time.

AdvancedFull instructions providedOver 4 days9,557
Air Meter Making #4: Complete

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Aduino fine dust sensor (PPD42NS)
×1
Co2 sensor v1.2
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
20x4 lcd
×1

Story

Read more

Custom parts and enclosures

3d

Schematics

Arduino Air Meter (iot) Schematic

Arduino Air Meter (iot) Schematic

Code

Arduino Air Meter (iot) code

Arduino
Arduino Air Meter (iot) code
(2017.10.29 Minor error correction)
(2017.10.30 Fixed fine dust code error)
(2017.10.31 Modify fine dust renewal interval)
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <DHT.h>
 
boolean state_led1 = 0;
const int duration2 = 16000;
 
#define         MG_PIN                       (0)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (2)
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interval(in milisecond) between each samples in 
                                                     //normal operation
#define         ZERO_POINT_VOLTAGE           (0.37) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define         REACTION_VOLTGAE             (0.020) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
float           CO2Curve[3]  =  {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};   
 
                                                     
#define DHTPIN 4        // Setting the SDA pin
#define DHTTYPE DHT11   // DHT22 (AM2302) Sensor type setting
int pin = 3; //Dust sensor
unsigned long duration;   //duration
unsigned long starttime;  //Start time
unsigned long sampletime_ms = 30000;   //Updated every 30 seconds of sample time
unsigned long lowpulseoccupancy = 0;   //Low Initializes the time the signal lasted
float ratio = 0;  //ratio
float concentration = 0;  //Initialized to zero particle concentration
float pcsPerCF = 0;  //Initialize CF to 0 per particle
float ugm3 = 0;  //The final value, in micrograms per cubic meter(㎍/㎥)
 
unsigned long pre_time = 0;
unsigned long cur_time = 0;
 
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);  
// Upload Notification LED Setting (Adunono On Board LED)
int ledPin = 13;
 
// Enter Write API key for your own thingspeak channel
String apiKey = "Your API Key";
 
SoftwareSerial ser(6,5 ); //RX / TX configuration, serial object creation
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
    pre_time = millis();
    pinMode(3,INPUT);
   pinMode(4,INPUT);
   pinMode(BOOL_PIN, INPUT);                        //set pin to input
    digitalWrite(BOOL_PIN, HIGH);                    //turn on pullup resistors
    lcd.begin(20, 4); 
  dht.begin();
  //Notification LED Output Settings
  pinMode(ledPin, OUTPUT);
  //Serial communication speed 9600 baud rate setting  
  Serial.begin(9600);
  //Start the software serial
  ser.begin(9600);
  //ESP8266 Reset
  ser.println("AT+RST");
}
 
void loop() {
 delay(1800);
    cur_time = millis();//Assign millis to cur_time (for parallel operations)
    int percentage;
    float volts;
  Serial.println( pre_time );
Serial.println( cur_time);
 
 //co2 Measurement Codes--------------------------------------------------------
    volts = MGRead(MG_PIN);
    Serial.print( "SEN-00007:" );
    Serial.print(volts); 
    Serial.print( "V           " );
    Serial.println(volts/DC_GAIN);
 
 
    percentage = MGGetPercentage(volts,CO2Curve);
 
    Serial.print("CO2:");
    if (percentage == -1) {
        Serial.print( "<400" );
    } else {
        Serial.print(percentage);
    }
 
    Serial.print( "ppm" );  
    Serial.print("\n");
 
    if (digitalRead(BOOL_PIN) ){
        Serial.print( "=====BOOL is HIGH======" );
    } else {
        Serial.print( "=====BOOL is LOW======" );
    }
 
    Serial.print("\n");
   //---------------------------------------------------------------------------
   
   //Fine dust measuring section code-------------------------------------------
  
  duration = pulseIn(pin, LOW); 
  lowpulseoccupancy = lowpulseoccupancy+duration;
  
  if ((millis()-starttime) >= sampletime_ms)  {   //If the sample time is 5 seconds (the sample time specified above)
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percent
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // Fine dust sensor specification Sheet curve use
    pcsPerCF = concentration * 100;  // When the particle concentration is multiplied by 100, the CF value per particle
    ugm3 = pcsPerCF / 13000;  //When CF per particle is divided by 13000, the fine dust measurement of micrograms per cubic meter
    Serial.print(ugm3);
    lcd.setCursor(0, 2);
    lcd.print("                    ");
    lcd.setCursor(0, 2);
    lcd.print("Dust:");
    lcd.setCursor(6, 2);
    lcd.print(ugm3);
    lcd.print("ug/m3");
    lowpulseoccupancy = 0;
    starttime = millis();
    }
 
  // blink LED on board
  digitalWrite(ledPin, HIGH);   
  delay(200);               
  digitalWrite(ledPin, LOW);
 
  //Read DHT11 value
  float temp = dht.readTemperature();
  float humi = dht.readHumidity();
 
  // String Conversion
  char buf[16];
  String strTemp = dtostrf(temp, 4, 1, buf);
  String strHumi = dtostrf(humi, 4, 1, buf);
  
  Serial.println(strTemp);
  Serial.println(strHumi);
  Serial.print("Dust density(ug/m3) = ");
  Serial.println(ugm3);
  //Measured value lcd display section------------------------------------------
 
 
 lcd.setCursor(0, 3);
 lcd.print("             "); 
       lcd.setCursor(0, 3);
    lcd.print("Co2:");
    if (percentage == -1) {
         lcd.print( "<400" );
    } else {
        lcd.print(percentage);
         lcd.print("ppm");
    }
 
 
    
 lcd.setCursor(0, 0);
 lcd.print("                    ");
 lcd.setCursor(0, 1);
 lcd.print("                    ");
 lcd.setCursor(0, 0);
 lcd.print("Temperature:");
 lcd.setCursor(13, 0);
 lcd.print(strTemp);
 lcd.setCursor(0, 1);
 lcd.print("Humidity:");
 lcd.setCursor(10, 1);
 lcd.print(strHumi);
 
 //-----------------------------------------------------------------------------
 
 
   if(cur_time - pre_time >= duration2){//Sensor measurement and measurement lcd Independent from the display section Transfer measurements to the thingspeak server once every 16 seconds
 
 pre_time = cur_time;
 
  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com Access IP
  cmd += "\",80";           // api.thingspeak.com access port, 80
  ser.println(cmd);
   
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
     lcd.setCursor(13, 3);
      lcd.print("Wifioff");
    return;
  }
  
  // Set String, Data to send by GET method
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr +="&field2=";
  getStr += String(strHumi);
  getStr +="&field3=";
  getStr += String(ugm3);
  getStr +="&field4=";
  getStr += String(percentage);
  getStr += "\r\n\r\n";
 
  // Send Data
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ser.println(cmd);
 
  if(ser.find(">")){
    ser.print(getStr);
     lcd.setCursor(14, 3);
      lcd.print("Wifion");
  
  }
  else{
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
     lcd.setCursor(13, 3);
      lcd.print("Wifioff");
 
  }
   }
}
//co2 measurement sensor--------------------------------------------------------
float MGRead(int mg_pin)
{
    int i;
    float v=0;
 
    for (i=0;i<READ_SAMPLE_TIMES;i++) {
        v += analogRead(mg_pin);
        delay(READ_SAMPLE_INTERVAL);
    }
    v = (v/READ_SAMPLE_TIMES) *5/1024 ;
    return v;  
}
 
int  MGGetPercentage(float volts, float *pcurve)
{
   if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) {
      return -1;
   } else { 
      return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
    }
}
 
//--------------------------------------------

Credits

윤원호

윤원호

1 project • 7 followers
Ordinary high school student who wants to enter science and engineering college
Parkmoonsu

Parkmoonsu

1 project • 6 followers
A Korean high school student who wants to learn about Arduino!
gledel

gledel

100 projects • 115 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"
정다연

정다연

1 project • 4 followers

Comments