Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2336|回復: 4
打印 上一主題 下一主題

漏水感知&溫溼度組合問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2019-2-19 10:01:41 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 f1226 於 2019-2-19 10:03 編輯

1、目前使用DT-11、FC-37兩種元件組合打算針對機房做漏水偵測和溫度警示控管

2、從網路上找了兩種元件的程式組合也運行完成(如下圖)


3、FC-37的部分修改容易沒什問題,但是DT-11修改的時候遭遇問題,能否指導我如何將DT-11的偵測結果改為類似FC-37的內容?
例:"目前溫度低於40度,正常"、"目前溫度高於40度,小於45度,請注意"、"目前溫度高於49度,請立即查看"


4、目前程式內容如下:

// Written by ladyada, public domain

// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum

// 导入头文件
#include "DHT.h"
// 定义OUT接口的数字引脚
#define DHTPIN 2     // what digital pin we're connected to

// Uncomment whatever type you're using!
// 选择DHT类型为 DHT 11
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// 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);

  Serial.print("空氣濕度: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("溫度: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("酷熱指數: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");


}


/* Flame Sensor analog example.


- 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、麻煩前輩給新手指導,謝謝!!
2#
發表於 2019-2-19 10:35:44 | 只看該作者
用 if else if 去判斷 hic  的值
輸出資料用 Serial.println

也就是
如果 hic 小於 40
    印出"正常"
否則如果 hic 小於 45
   印出 "目前溫度高於40度,小於45度,請注意"、
否則
    印出"目前溫度高於49度,請立即查看"


3#
 樓主| 發表於 2019-2-19 14:55:30 | 只看該作者
按您提供的建議進行修改,在程式中加入下列內容

if (hic>40){
  Serial.print("機房溫度正常")
;  }
else if (hic>45){
  Serial.print("機房溫度大於40度,請注意")
;  }
else if (hic>49){
  Serial.print("機房溫異常,請盡速查")
; }


驗證(除錯)部分順利通過,但"序列埠監控視窗"運行卻仍然是舊的結果(即最初運行的結果圖片)??

請問這是哪邊需要修正??
4#
發表於 2019-2-19 15:45:29 | 只看該作者
本帖最後由 超新手 於 2019-2-19 15:54 編輯

因為你的判斷式寫錯了
你仔細看一下, 就會發現整個邏輯有問題
第一個判斷式是小於, 不是大於
第二, 三個判斷式則是要先判斷 49 度, 再判斷 45 度
否則 49 度就會大於 45 度,
根本沒辦法進到下一個(第三個)判斷式
5#
 樓主| 發表於 2019-2-20 14:50:53 | 只看該作者
謝謝您,已經解決,
接下來要嘗試加入網路和IFTTT部分,有問題再向您請教。
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-19 16:30 , Processed in 0.090316 second(s), 9 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表