编号:DHT11
[code]
#include "DHT.h"
#define dhtPin 12
DHT dht(dhtPin, DHT11);
float hmData; //湿度变量
float tmpData; //温度变量
float htIndex; //热指数
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
Serial.println("DHT11 initialed:");
Serial.println("********************");
}
void loop() {
// put your main code here, to run repeatedly:
hmData = dht.readHumidity(); //读取湿度值
tmpData = dht.readTemperature(); //读取温度值
//humidity data printed
Serial.print("当前湿度值为:");
Serial.print(hmData);
Serial.println("%");
//temperature data printed
Serial.print("当前温度值为:");
Serial.println(tmpData);
htIndex = dht.computeHeatIndex(); //读取当前环境的热指数
if(htIndex <= 80){ //安全区域
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境较为凉爽,可以长期停留其中。");
}
else if(htIndex>80 && htIndex <= 88 ){ //环境偏热
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境偏热,应注意通风和补充水分。");
}
else if(htIndex>88 && htIndex<=101){ //环境酷热
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境酷热,因多补充水分,加强通风,防止直接日晒,不宜久待。");
}
else if(htIndex>101 && htIndex<=124){ //环境极热,容易中暑
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境过热,久待会有中暑的危险,应注意通风散热,积极补充水分,完成必须工作后尽快离开。");
}
else { //环境过热,很危险
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境过热,可能会导致人中暑、休克,甚至死亡,应尽快离开。");
}
delay(5000);
}
[/code]
=========================================================================
编号:DHT11_1
[code]
#include "DHT.h"
#define dhtPin 12
#define alarmLed 13
DHT dht(dhtPin, DHT11);
float hmData; //湿度变量
float tmpData; //温度变量
float htIndex; //热指数
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
Serial.println("DHT11 initialed:");
Serial.println("********************");
pinMode(alarmLed, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
hmData = dht.readHumidity(); //读取湿度值
tmpData = dht.readTemperature(); //读取温度值
//humidity data printed
Serial.print("当前湿度值为:");
Serial.print(hmData);
Serial.println("%");
//temperature data printed
Serial.print("当前温度值为:");
Serial.println(tmpData);
//输出热指数相关的信息
htIndex = dht.computeHeatIndex(); //读取当前环境的热指数
if(htIndex <= 80){ //安全区域
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境较为凉爽,可以长期停留其中。");
digitalWrite(alarmLed, LOW);
delay(5000);
}
else if(htIndex>80 && htIndex <= 88 ){ //环境偏热
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境偏热,应注意通风和补充水分。");
digitalWrite(alarmLed, HIGH);
delay(5000);
}
else if(htIndex>88 && htIndex<=101){ //环境酷热
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境酷热,因多补充水分,加强通风,防止直接日晒,不宜久待。");
for(int i=0;i<5;i++){ //闪烁频率为一秒
digitalWrite(alarmLed, LOW);
delay(500);
digitalWrite(alarmLed, HIGH);
delay(500);
}
}
else if(htIndex>101 && htIndex<=124){ //环境极热,容易中暑
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境过热,久待会有中暑的危险,应注意通风散热,积极补充水分,完成必须工作后尽快离开。");
for(int i=0;i<10;i++){ //闪烁频率为半秒
digitalWrite(alarmLed, LOW);
delay(250);
digitalWrite(alarmLed, HIGH);
delay(250);
}
}
else { //环境过热,很危险
Serial.print("当前热指数为:");
Serial.println(htIndex);
Serial.println("当前环境过热,可能会导致人中暑、休克,甚至死亡,应尽快离开。");
for(int i=0;i<20;i++){ //闪烁频率为四分之一秒
digitalWrite(alarmLed, LOW);
delay(125);
digitalWrite(alarmLed, HIGH);
delay(125);
}
}
}
[/code]