메뉴 건너뛰기

XEDITION

아두이노

 

 

[sketch_mydust] - 미세멘지 측정기 작업 용

 

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domainu8g


#include <DHT.h>
#include <U8glib.h>
/* Include the DS1302 library */
#include <DS1302.h>

int f_APIN_LED_SDA = 4;     // SDA A4
int f_APIN_LED_SCL = 5;     // SCL A5

int f_DPIN_MOTION = 2; // 움직임 감지

int f_DPIN_SCK = 5;
int f_DPIN_IO = 4;
int f_DPIN_RST = 3;

int f_DPIN_RELAY1 = 6;     // what pin we're connected to
int f_DPIN_RELAY2 = 7;     // what pin we're connected to

int f_DPIN_DHT22 = 8;     // what pin we're connected to

int f_DPIN_DUST_LED_POWER = 9;   //Connect 3 led driver pins of dust sensor to Arduino D7
int f_APIN_DUST_SENSOR_A1 = 1; //Connect dust sensor to Arduino A0 pin

float f_h = 0;  //dht.readHumidity();
float f_t = 0;  //dht.readTemperature();

int f_motion = 0;  // 움직임 감지 값 ( 0 . 1 )

 /* Initialise the DS1302 library */
DS1302 rtc(f_DPIN_RST, f_DPIN_IO, f_DPIN_SCK); // Clock

unsigned long lastReadTime = 0;    

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(f_DPIN_DHT22, DHT22); // DHT 22  (AM2302)

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

float dustVal = 0;
float calcVoltage = 0;
float dustDensity = 0;

float voltage = 0;  // average voltage
float dustdensity = 0;  // average dust density
float ppmpercf = 0;  //
int f_readcount=0;
float f_ppmsum=0;    // sum of analog read


const unsigned long SENSING_INTERVAL = 3000;
const unsigned long POSTING_INTERVAL = 15*1000;  //delay between updates to Pachube.com
unsigned long lastSensingTime = 0;
unsigned long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds


//////////////////////////////////////////////////////
// Display
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI

// Draw screen
//const int DISP_CHAR_LEN = 13;
const int f_DISP_CHAR_LEN2 = 22;
char f_strTemp[f_DISP_CHAR_LEN2];
char str_line1[f_DISP_CHAR_LEN2] = "Cur TM : ";
char str_line2[f_DISP_CHAR_LEN2] = "Dust : 00.0000 mg/m3";
char str_line3[f_DISP_CHAR_LEN2] = "Initializing............";

void setup()
{
  Serial.begin(9600);
  Serial.println("DHTxx test! >>>>>>>>>>>>>>>>>>>>>>>>>>> ");
  Serial.println("Dust test!");

  //fn_setTime();

  pinMode(f_DPIN_RELAY1, OUTPUT); // Relay1 전기 1
  pinMode(f_DPIN_RELAY2, OUTPUT); // Relay2 전기 2

  digitalWrite(f_DPIN_RELAY1, LOW); // 전기 연결 OFF
  digitalWrite(f_DPIN_RELAY2, LOW); // 전기 연결 OFF

  pinMode(f_DPIN_DUST_LED_POWER, OUTPUT);

  pinMode(f_DPIN_MOTION, INPUT);

  dht.begin();

  fn_showInfo();
}

void loop()
{
  // Read dust sensor
  if(millis() - lastSensingTime > SENSING_INTERVAL) {
    fn_readDustSensor();
    lastSensingTime = millis();
  }

  if(millis() - lastConnectionTime > POSTING_INTERVAL)
  {
   
    // calculate average voltage, dust density
    voltage = f_ppmsum/f_readcount*0.0049;
    dustdensity = 0.17*voltage-0.1;
    ppmpercf = (voltage-0.0256)*120000;
    if (ppmpercf < 0)
      ppmpercf = 0;
    if (dustdensity < 0 )
      dustdensity = 0;
    if (dustdensity > 0.5)
      dustdensity = 0.5;

    //memset(f_strTemp, 0, sizeof(char) * 64);
   
    // for debug
    String dataString = "Dust : ";
    //dataString += dtostrf(voltage, 9, 4, s);
    //dataString += ",";
    //dataString += dtostrf(ppmpercf, 8, 0, s);
    //dataString += ",";
    //dataString += dtostrf(f_readcount, 2, 0, s);
    //dataString += ",";
    //....
    //dtostrf(f_readcount, 3, 0, s);
    //sprintf(dataString, "Count : %8s℃", s);
    //memset(f_strTemp, 0, size_t f_strTemp);
   
    //dataString += (String)dtostrf(dustdensity, 5, 4, f_strTemp);
    //dataString += " mg/m3";

    //  m     milli     밀리          10^   -3
    //  μ      micro   마이크로  10^  -6


  // 좋음 : ~30
  // 보통 : ~80
  // 나쁨 : ~150
  // 매우나쁨 ; 151~

    dustdensity = dustdensity * 1000;
    dataString += (String)dtostrf(dustdensity, 3, 0, f_strTemp);
    //dataString += " ㎍/m3";
    dataString += " ug/m3";

    if( dustdensity > 150)
    {
      dataString += " (VB)";
    } else if( dustdensity > 80)
    {
      dataString += " (B)";
    } else if( dustdensity > 30)
    {
      dataString += " (N)";
    } else if( dustdensity > 0)
    {
      dataString += " (G)";
    } else
    {
      dataString += " ";
    }
   
    fn_setInfo(dataString, 2);
   
    f_readcount=0;
    f_ppmsum=0;   
    lastConnectionTime = millis();
  }

  if(millis() - lastReadTime > 1000)
  {
    // Reading temperature or humidity takes about 250 milliseconds!
    fn_readTime();
   
    fn_readTemp();
   
    fn_readMotion();
   
    fn_showInfo();
   
    lastReadTime = millis();
  }

  //delay(2000);
  if ( f_t > 27)
  {
    digitalWrite(f_DPIN_RELAY1, HIGH); // 전기 연결
    digitalWrite(f_DPIN_RELAY2, HIGH); // 전기 연결
  } else
  {
    digitalWrite(f_DPIN_RELAY1, LOW); // 전기 햬제
    digitalWrite(f_DPIN_RELAY2, LOW); // 전기 연결
  }

}

void fn_readMotion()
{
  f_motion = digitalRead(f_DPIN_MOTION);
  //Serial.print("Motion : ");
  //Serial.println(f_motion);
}

void fn_setTime()
{
  // Clear the 1302's halt flag
  rtc.halt(false);
  // And disable write protection
  rtc.writeProtect(false);
 
  // Set the time and date
  rtc.setDOW(SUNDAY);
  rtc.setTime(13,36,0);
  rtc.setDate(12,2,2017);
}

void fn_readTime()
{
  //Serial.print("It is ");
  //Serial.print(rtc.getDOWStr());
  //Serial.print(" ");
  //Serial.print(rtc.getDateStr());
  //Serial.print(" ");
  //Serial.print("and the time is: ");
  //Serial.println(rtc.getTimeStr());
  String sTime = "Time : ";
  sTime += (String)rtc.getTimeStr();

  if( f_motion == 1 )
  {
    sTime += " (M)";
  }
  //Serial.println("Dust density : " + sTime);
  fn_setInfo(sTime, 1);
}

void fn_readDustSensor()
{
  digitalWrite(f_DPIN_DUST_LED_POWER,LOW); // power on the LED
  delayMicroseconds(samplingTime);
 
  dustVal = analogRead(f_APIN_DUST_SENSOR_A1); // read the dust value

  f_readcount = f_readcount + 1;
  f_ppmsum = f_ppmsum+dustVal;
 
  delayMicroseconds(deltaTime);
  digitalWrite(f_DPIN_DUST_LED_POWER,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
 
  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = dustVal * (5.0 / 1024.0);
 
  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  dustDensity = 0.17 * calcVoltage - 0.1;

 
  Serial.print("Raw Signal Value (0-1023): ");
  Serial.print(dustVal);
 
  Serial.print(" - Voltage: ");
  Serial.print(calcVoltage);
 
  Serial.print(" - Dust Density: ");
  Serial.print(dustDensity); // unit: mg/m3
  Serial.println(" mg/m3 ");

  //String strVoltage = "Voltage=";
  //strVoltage += dtostrf(dustVal, 5, 2, s);
  //fn_setInfo(strVoltage, 3);
}

void fn_readTemp()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  f_h = dht.readHumidity();
  f_t = dht.readTemperature();
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(f_t) || isnan(f_h)) {
    Serial.println("Failed to read from DHT");
  } else {
    //온도, 습도 표시 시리얼 모니터 출력
    //fn_printTemp(h, t);
  }
 
  //온도, 습도 표시 시리얼 모니터 출력
 
  String dataString;
  dataString = "H : ";
  dataString += dtostrf(f_h, 3, 1, f_strTemp);
  dataString += "  T : ";
  dataString += dtostrf(f_t, 3, 1, f_strTemp);

  fn_setInfo(dataString, 3);

  //Serial.print(">>>>>>>>. " );
  //Serial.println( str_line3);
}


void fn_setInfo(String strDisp, int line_index)
{
  if(strDisp == NULL) {
    return;
  }
 
  int posY = 45;
  if(line_index == 1) {
    for(int i=0; i<f_DISP_CHAR_LEN2; i++)
      str_line1[i] = 0x00;
    strDisp.toCharArray(str_line1, f_DISP_CHAR_LEN2 - 1);
  } else if(line_index == 2) {
    for(int i=0; i<f_DISP_CHAR_LEN2; i++)
      str_line2[i] = 0x00;
    strDisp.toCharArray(str_line2, f_DISP_CHAR_LEN2 - 1);
  } else {
    for(int i=0; i<f_DISP_CHAR_LEN2; i++)
      str_line3[i] = 0x00;
    strDisp.toCharArray(str_line3, f_DISP_CHAR_LEN2 - 1);
  }

  //Serial.print("fn_setInfo : " );
  //Serial.println(strDisp);
 
}

void fn_showInfo()

  // picture loop 
  u8g.firstPage(); 
  do {
    // draw icon
    //u8g.drawBitmapP( 13, 11, 3, 24, IMG_logo_24x24);
    // show text
    u8g.setFont(u8g_font_fixed_v0);
    u8g.setFontRefHeightExtendedText();
    u8g.setDefaultForegroundColor();
    u8g.setFontPosTop();
    u8g.drawStr(5, 0, str_line1);
    u8g.drawStr(5, 10, str_line2);
    u8g.drawStr(5, 20, str_line3);
  } while( u8g.nextPage() );
 
}

 

 

 

번호 제목 글쓴이 날짜 조회 수
공지 IR 리모콘 코드 따기 [1] 묵묵이 2017.02.10 12
공지 rc 카 작업용 [1] 묵묵이 2017.02.07 11
» [sketch_mydust] 미세멘지 측정기 작업 용 [2] 묵묵이 2017.02.07 15
42 RFID 정보를 읽고 쓸 수 있는 Proxmark3 묵묵이 2024.09.13 1
41 [Android,Java] 안드로이드 - AES256 암호화 및 복호화 예제 소스 다운로드 file 묵묵이 2022.06.12 0
40 교류모터 속도제어킷 묵묵이 2021.05.02 7
39 WiFiHTTPSServer test 묵묵이 2020.03.02 7
38 8GLIB 유저매뉴얼 구글번역 묵묵이 2020.02.29 5
37 아두이노 2.4G 통신 모듈 사용하기 묵묵이 2019.04.04 9
36 전기가용량 계산 방법 - 전압, 전류와 전력량 측정에 대한 내용 묵묵이 2017.07.27 9
35 초저렴 iot 모듈 묵묵이 2017.06.04 8
34 적정 습도 / 미세먼지 값보정 묵묵이 2017.05.11 8
33 5V동작 20A 전류 센서 모듈 측정하기 ... [6] 묵묵이 2017.04.17 16
32 먼지 센서 구매 하는곳 ... 묵묵이 2017.04.15 7
31 라즈베리파이 프로그램 자동 실행 묵묵이 2017.03.12 8
30 라즈베리파일 설정 묵묵이 2017.03.11 6
29 [라즈베리파이] C언어를 이용한 GPIO 입출력 제어 [3] 묵묵이 2017.03.07 5
28 와이파이 모듈(ESP8266) 공유기 연결 AT 명령어 [1] file 묵묵이 2017.02.26 6
27 아두이노 EEPROM 사용해보기 묵묵이 2017.02.20 6
26 [아두이노] 0.96인치 OLED LCD 모듈 텍스트 출력 [2] file 묵묵이 2017.02.20 9
25 HEX to DEC 묵묵이 2017.02.10 5
위로