// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
// 生成 DHT 对象,参数是PIN和DHT类型
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// initialize serial communication @ 9600 baud:
Serial.begin(9600);
Serial.println("DHTxx test!");
// 必须使用的begin()函数
dht.begin();
}
void loop() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // Sensor getting wet
Serial.println("機房幹管破裂,請立即回大樓查看!!");
break;
case 1: // Sensor getting wet
Serial.println("機房幹管可能滲漏或誤報,請提高警覺或查看");
break;
case 2: // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
Serial.println("機房幹管正常,無滲漏");
break;
}
delay(5000); // delay between reads
// Wait a few seconds between measurements.
// 每次等待2秒后再输出(必须等、大于1秒,1秒等於1000)
delay(5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// readHumidity() 这里是读取当前的湿度
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
// readTemperature() 读取当前的温度,单位C
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//readTemperature(true) 读取当前的温度,单位F
float f = dht.readTemperature(true);
// 如果读取失败则退出,再读取一次
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("自DHT sensor讀取數值失敗!");
return;
}
// Compute heat index in Fahrenheit (the default)
// 读取体感温度,单位F
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
// 读取体感温度,单位C
float hic = dht.computeHeatIndex(t, h, false);
- If the Sensor Board is completely soaked; "case 0" will be activated and " Flood " will be sent to the serial monitor.
- If the Sensor Board has water droplets on it; "case 1" will be activated and " Rain Warning " will be sent to the serial monitor.
- If the Sensor Board is dry; "case 2" will be activated and " Not Raining " will be sent to the serial monitor.
*/
5、麻煩前輩給新手指導,謝謝!!
作者: 超新手 時間: 2019-2-19 10:35
用 if else if 去判斷 hic 的值
輸出資料用 Serial.println
也就是
如果 hic 小於 40
印出"正常"
否則如果 hic 小於 45
印出 "目前溫度高於40度,小於45度,請注意"、
否則
印出"目前溫度高於49度,請立即查看"