arduino

[arduino] 溫溼度DHT11 with d1 mini

 


// 開發版選Wemos D1 R1

// 你可能需要安裝下列的函式庫 Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN D6 // D1 mini的接口請接D6

//切換你的溫溼度感應模組
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

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

dht.begin();
}

void loop() {
// 等待幾秒讓感測器感測一下溫溼度
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// 感測器的感測秒數需要等待超過2秒才會有新的數值 (DHT系列是很慢的感測元件)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
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(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F("濕度Humidity: "));
Serial.print(h);
Serial.print(F("% 溫度Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F 體感溫度Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}

如果你想要把溫溼度顯示在LED版上的話 就改裝一下就好

</pre>
// 開發版選Wemos D1 R1

//溫溼度感測器相關
// 你可能需要安裝下列的函式庫 Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"

#define DHTPIN D6 // D1 mini的接口請接D6

//切換你的溫溼度感應模組
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
//溫溼度感測器結束

//LED字幕相關
#include <Wire.h> // Arduino IDE 內建
// LCD I2C Library,從這裡可以下載:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
#include <LiquidCrystal_I2C.h>

// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 設定 LCD I2C 位址
//LED字幕結束

void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();

lcd.begin(16, 2); // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光

// 閃爍三次
for(int i = 0; i < 3; i++) {
lcd.backlight(); // 開啟背光
delay(250);
lcd.noBacklight(); // 關閉背光
delay(250);
}
lcd.backlight();

// 輸出初始化文字
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(0, 1); // 設定游標位置在第二行行首
lcd.print("davidou.org");
delay(8000);

// 告知使用者可以開始手動輸入訊息
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System boot");
lcd.setCursor(0, 1);
lcd.print("Wait a sec..");
}

void loop() {
// 等待幾秒讓感測器感測一下溫溼度
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// 感測器的感測秒數需要等待超過2秒才會有新的數值 (DHT系列是很慢的感測元件)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
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(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F("濕度Humidity: "));
Serial.print(h);
Serial.print(F("% 溫度Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F 體感溫度Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));

lcd.setCursor(0, 0);
lcd.print("Humidity: " );
lcd.print( h,1);//顯示小數點後一位就好
lcd.print("%" );
lcd.setCursor(0, 1);
lcd.print("Temper: " );
lcd.print( t,1);
lcd.print(" C" );
}

Be the First to comment.

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。

(若看不到驗證碼,請重新整理網頁。)