본문 바로가기
아두이노

아두이노 온도 습도 값을 읽어 불쾌지수 표시

by ADELA_J 2023. 7. 24.

<온도 습도 값을 읽어 불쾌지수를 계산하여 시리얼통신으로 전송하기>

더보기

 

#include <DHT.h>
 
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
 
void setup(){
  Serial.begin(9600);
 
  dht.begin();
}
 
void loop(){
  float di = discomfortIndex();
  if (di != 0)
  {
    Serial.print("불쾌지수: ");
    Serial.println(di);
  }
}
 
int discomfortIndex()
{
  static unsigned long currTime = 0;
  static unsigned long prevTime = 0;
 
  currTime = millis();
  if (currTime - prevTime >= 2000)
  {
    prevTime = currTime;
    float humi = dht.readHumidity();
    float temp = dht.readTemperature();
 
    if (!isnan(humi)|| !isnan(temp))
    {
      float di = (1.8 * temp) - (0.55 * (1 - humi/100.0)*(1.8 *temp - 26)) + 32;
      return di;
    }
  }
  return 0;
}

 

<온도 습도 값을 읽어 불쾌지수 계산해서 LCD에 표시>

더보기
#include <DHT.h>
#include <DHT_U.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 11
#define DHTTYPE DHT11
 
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
 
int count=0;
 
void setup(){
  Serial.begin(9600);
  dht.begin();    //DHT초기화
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print('230724_DHT');
  delay(2000);
  lcd.clear();
}
 
void loop(){
  //DHT
  delay(2000);QD
  int di = discomfortIndex();
  if (di != 0)
  {
    Serial.print("discomfortIndex: ");
    Serial.println(di);
    lcd.setCursor(0,0);
    lcd.print("discomfortIndex:");
    lcd.setCursor(0,1);
    lcd.print(di);
  }
}
  int discomfortIndex()
  {
    static unsigned long currTime = 0;
    static unsigned long prevTime = 0;
 
    currTime = millis();
    if (currTime - prevTime >= 2000)
    {
      prevTime = currTime;
      float humi = dht.readHumidity();
      float temp = dht.readTemperature();
 
      if (!isnan(humi)|| !isnan(temp))
      {
        float di = (1.8 * temp) - (0.55 * (1 - humi/100.0)*(1.8 *temp - 26)) + 32;
        return di;
      }
    }
    return 0;
  }