#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
#define DHTPIN D5 // 与OLED使用时要修改DHT11的数据引脚为D5,因为OLED的D3和D4
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 初始化I2C
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
display.clearDisplay(); // 清屏
// OLED显示字符
displaychar(h,t);
}
/**
* 显示字符
*/
void displaychar(float h, float t) {
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Humi:");
display.setCursor(50,0);
display.println(h);
display.print("Temp:");
display.setCursor(50,10);
display.println(t);
display.display();
delay(1);
}
|