下载库:SimpleDHT

github:https://github.com/winlinvip/SimpleDHT

连接D4引脚

#include <SimpleDHT.h>

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = D4;
SimpleDHT11 dht11(pinDHT11);

void setup() {
  Serial.begin(115200);
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); 
    Serial.println(err);
    delay(1000);
    return;
  }
  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" ℃, "); 
  Serial.print((int)humidity); Serial.println(" %");
  
  // DHT11 sampling rate is 1HZ.
  delay(1500);
}

================================
Sample DHT11...
Read DHT11 failed, err=5904
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
=================================
Sample DHT11...
Sample OK: 17 ℃, 46 %
 

oled版,温度传感器

 

#include <SimpleDHT.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ D2, /* data=*/ D1, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
int pinDHT11 = D4;
SimpleDHT11 dht11(pinDHT11);

void setup() {
  Serial.begin(115200);
  u8g2.begin();
  u8g2.enableUTF8Print();  
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); 
    Serial.println(err);
    delay(1000);
    return;
  }
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" ℃, "); 
  Serial.print((int)humidity); Serial.println(" %");
  u8g2.setFont(u8g2_font_unifont_t_chinese1);  // use chinese2 for all the glyphs of "你好世界"
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  u8g2.setCursor(0, 15);
  u8g2.print("室内温湿度:");
  u8g2.setCursor(0, 30);
  u8g2.print("温度:");
  u8g2.print((int)temperature);        // Chinese "Hello World" 
  u8g2.print("℃");
  u8g2.setCursor(0, 45);
   u8g2.print("湿度:");
  u8g2.print((int)humidity);  
  u8g2.print("%");
  u8g2.sendBuffer();
  
  // DHT11 sampling rate is 1HZ.
  delay(1500);
}