#include<LiquidCrystal.h>
#include<dht11.h>
#include<SPI.h>
#include<SD.h>
dht11DHT11;
//设置 DHT 引脚 为 Pin8
#defineDHT11PIN 8
//定义液晶
constint numrows=2;
constint numcols=16;
constint chipselect=4;
LiquidCrystallcd(2,3,5,6,7,9);
voidsetup() {
//串口输出
Serial.begin(9600);
Serial.println("DHT11 TESTPROGRAM");
// output, even if you don't use it:
pinMode(10, OUTPUT);
//液晶输出
lcd.begin(numcols,numrows);
// 输出 DHT 库的版本号
Serial.println(DHT11LIB_VERSION);
Serial.println();
Serial.println("Initializing SDcard");
if(!SD.begin(chipselect))
{
Serial.println("initializationfailed!");
while(1);
}
Serial.println("initializationdone");
}
voidloop() {
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
// 测试 DHT 是否正确连接
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
// 获取测量数据
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
//液晶定位到第一行第一列,输出湿度
lcd.setCursor(0,0);
lcd.print("Hum ");
lcd.print((float)DHT11.humidity, 2);
Serial.print("Temperature ): ");
Serial.println((float)DHT11.temperature, 2);
//液晶定位到第二行第一列,输出温度
lcd.setCursor(0,1);
lcd.print("T ");
lcd.print((float)DHT11.temperature, 2);
//save data to TF
Serial.println("open file and writedata");
FiledataFile=SD.open("data.txt",FILE_WRITE);
if (dataFile)
{
dataFile.print(DHT11.humidity);
dataFile.print(",");
dataFile.print(DHT11.temperature);
dataFile.close();
Serial.println(DHT11.humidity);
Serial.println(DHT11.temperature);
}
else
{
Serial.println("error openingdatal.txt");
}
Serial.println("Wait for nextloop");
delay(10000);
}