Project/Arduino

ESP32로 CO2와 미세먼지 측정하기(MH-Z14A)

Juzero 2020. 6. 18. 13:28

이산화탄소와 미세먼지를 동시에 측정하는 코드이다. 

 

이 전 게시글에 비해 미세먼지를 측정하는 코드 약 10줄? 정도만 추가되어서, 바뀐 부분만 보면 될 듯하다.

 

일반 아날로그값을 읽는 것과는 다르게 Setup() 부분에서 미세먼지 센서를 pinMode(pinnumber, INPUT)으로 읽는다.

 

#include <WiFi.h>
#include <HTTPClient.h>
#include <HttpClient.h>

//#include <Wire.h>

 
const char* ssid = "A";
const char* password =  "A!";
const char* serverName = "http://00.000.00.000:0000/";
int value_co2;
int value_dust;
int sensor_number = 1;
int analog = 34;

IPAddress hostIp(00, 000, 00, 000);
int SERVER_PORT = 0000;
WiFiClient client;

void setup() {
 
  Serial.begin(115200);
  pinMode(25, INPUT);


  WiFi.begin(ssid, password); 
 
  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
 
}
 
void loop() {
  // 미세먼지 측정 부분
  float value_dust = analogRead(25);
  value_dust= value_dust*(22.0 / 1023.0);
  Serial.print("Dust : ");
  Serial.println(value_dust);

 
 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

     for(int i=0; i<8; i++){
      value_co2+=analogRead(analog);
      delay(200);
     }
     value_co2/=8;
     int adcVal = value_co2;
     float voltage = adcVal*(3.3/4095.0);
    if(voltage == 0)
    {
    Serial.println("A problem has occurred with the sensor.");
    }
    else if(voltage < 0.4)
    {
    Serial.println("Pre-heating the sensor...");
    }
    else
    {
 
    float voltageDiference=voltage-0.4;
    float value_co2 =(voltageDiference*5000.0)/1.6;
    Serial.print("voltageDiference : ");
    Serial.println(voltageDiference);
  
    Serial.print("value_co2 :");
    Serial.println(value_co2);

    }
     HTTPClient http;
   
     http.begin("http://00.000.00.000:0000/");  //Specify destination for HTTP request
     http.addHeader("Content-Type",  "application/x-www-form-urlencoded");             //Specify content-type header
  
     String httpRequestData = "sensor_number_co2="+String(sensor_number)+"&sensor_number_dust="+String(sensor_number)+"&value_co2="+String(value_co2)+"&value_dust="+String(value_dust);
     int httpResponseCode = http.POST(httpRequestData);   //Send the actual POST request
   
     if(httpResponseCode>0){
  
      String response = http.getString();                       //Get the response to the request
   
      Serial.println(httpResponseCode);   //Print return code
      Serial.println(response);           //Print request answer
      Serial.println("");
   
     }else{
   
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
   
     }
   
     http.end();  //Free resources
   
 }else{

    Serial.println("Error in WiFi connection");   
 }
 
    delay(2000);  //Send a request every 10 seconds
    //10minute = 600seconds = 600000

}