diy/아두이노

미세먼지 측정기(sds011) 2

do121 2022. 9. 29. 08:57
#include <SDS011.h>
#include "U8glib.h"
#include <Wire.h> //for nano sda scl a4 a5


String pm10, pm10_chr ;
String pm25, pm25_chr;
/*
   1 EXCELLENT
   2 GOOD
   3 ACCEPTABLE
   4 MODERATE
   5 HEAVY
   6 SEVERE
   7 HAZARDOUS
*/

#define EXCELLENT                     "Excellent"
#define GOOD                          "GOOD"
#define ACCEPTABLE                    "Acceptable"
#define MODERATE                      "Moderate"
#define HEAVY                         "Heavy"
#define SEVERE                        "Severe"
#define HAZARDOUS                     "Hazardous"

void aqi() {

  if (pm10.toInt() >= 101) {
    pm10_chr = HAZARDOUS;
  } else if (pm10.toInt() >= 76) {
    pm10_chr = SEVERE;
  } else if (pm10.toInt() >= 51) {
    pm10_chr = HEAVY;
  } else if (pm10.toInt() >= 41) {
    pm10_chr = MODERATE;
  } else if (pm10.toInt() >= 31) {
    pm10_chr = ACCEPTABLE;
  } else if (pm10.toInt() >= 16) {
    pm10_chr = GOOD;
  } else {
    pm10_chr = EXCELLENT;
  }


  if (pm25.toInt() >= 51) {
    pm25_chr = HAZARDOUS;
  } else if (pm25.toInt() >= 38) {
    pm25_chr = SEVERE;
  } else if (pm25.toInt() >= 26) {
    pm25_chr = HEAVY;
  } else if (pm25.toInt() >= 21) {
    pm25_chr = MODERATE;
  } else if (pm25.toInt() >= 16) {
    pm25_chr = ACCEPTABLE;
  } else if (pm25.toInt() >= 9) {
    pm25_chr = GOOD;
  } else {
    pm25_chr = EXCELLENT;
  }
}



U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

float p10, p25;

int error;


SDS011 my_sds;


void setup() {



  my_sds.begin(8, 7); //tx->8, rx->7

  Serial.begin(9600);



  Serial.println("Initialized complete");


  u8g.firstPage();

  do {

    u8g.setFont(u8g_font_unifont);

    u8g.setPrintPos(0, 10);


    u8g.print("init ... ");


  } while (u8g.nextPage());
 

}

void dis_oled3(String msg1, String msg2, String msg3, String msg4, String msg5) {
//FOR two color oled(yellow, blue)
  int idx[] = {10, 26, 39, 52, 65};

  String str[5];

  str[0] = msg1;

  str[1] = msg2;

  str[2] = msg3;

  str[3] = msg4;

  str[4] = msg5;



  u8g.firstPage();



  do {

    u8g.setFont(u8g_font_unifont);



    for (int i = 0; i < 5; i++) {

      u8g.setPrintPos(0, idx[i]);



      u8g.print(str[i]);

    }

  } while (u8g.nextPage());



}

void loop() {


  //Serial.println("read");
  error = my_sds.read(&p25, &p10);
  Serial.println("error:" + String(error));

  if (! error) {

    Serial.println("P2.5: " + String(p25));

    Serial.println("P10:  " + String(p10));

  }



pm10 = String(round(p10));
pm25 = String(round(p25));
aqi();
dis_oled3("PM25:" + String(p25), "     "+ String(pm10_chr) ,"PM10:" + String(p10), "     " + String(pm25_chr),"");

  delay(5000);


}