37 lines
529 B
C++
37 lines
529 B
C++
#include "DHT.h"
|
|
|
|
#define DHTTYPE DHT11
|
|
#define DHTPIN D3
|
|
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
dht.begin();
|
|
}
|
|
|
|
void loop() {
|
|
|
|
delay(2000);
|
|
|
|
// 湿度
|
|
float h = dht.readHumidity();
|
|
// 摄氏温度温度
|
|
float t = dht.readTemperature();
|
|
|
|
|
|
if (isnan(h) || isnan(t)) {
|
|
Serial.println(F("Failed to read from DHT sensor!"));
|
|
return;
|
|
}
|
|
|
|
Serial.print(F("Humidity: "));
|
|
Serial.print(h);
|
|
|
|
Serial.print(F("% Temperature: "));
|
|
Serial.print(t);
|
|
|
|
Serial.println(F("°C "));
|
|
}
|