Thư viện hỗ trợ DHT11 theo đoạn code bên dưới:
http://www.mediafire.com/file/kqv5fhsmo8vtdcd/dht11.rar
Nếu nhiệt độ lớn hơn 40 thì kích relay đóng.
Code:
#include <Wire.h>
#include "DHT.h" //Khai báo thư viện DHT11
#include <LiquidCrystal.h> //Khai báo thư viện LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTPIN 6 //Khai báo chân tín hiệu DHT11
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const byte DS1307 = 0x68;
const byte NumberOfFields = 7;
int second, minute, hour, day, wday, month, year;
void setup()
{
//LCD
lcd.begin(16, 2);
Serial.begin(9600);
//DS1307
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
Wire.begin();
setTime(21, 30, 45, 6, 04, 12, 17); // 21:30:45 T6 04-12-2017
//DHT11
Serial.begin(19200);
Serial.println("DHTxx test!");
dht.begin();
pinMode(7,OUTPUT);
}
void loop()
{
readDS1307();
digitalClockDisplay();
delay(1000);
// Đọc nhiệt độ, độ ẩm mất khoảng 250ms
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
lcd.setCursor(11, 0);// cot hang
lcd.print("H");
lcd.setCursor(12, 0);// cot hang
lcd.print("=");
lcd.setCursor(13, 0);// cot hang
lcd.print(round(h));
lcd.setCursor(15, 0);// cot hang
lcd.print("%");
lcd.setCursor(11, 1);// cot hang
lcd.print("T");
lcd.setCursor(12, 1);// cot hang
lcd.print("=");
lcd.setCursor(13, 1);// cot hang
lcd.print(round(t));
lcd.setCursor(15, 1);// cot hang
lcd.print("C");
if(round(t)>=40)
digitalWrite(7,HIGH);
else
digitalWrite(7,LOW);
}
}
void readDS1307()
{
Wire.beginTransmission(DS1307);
Wire.write((byte)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, NumberOfFields);
second = bcd2dec(Wire.read() & 0x7f);
minute = bcd2dec(Wire.read() );
hour = bcd2dec(Wire.read() & 0x3f); // chế độ 24h.
wday = bcd2dec(Wire.read() );
day = bcd2dec(Wire.read() );
month = bcd2dec(Wire.read() );
year = bcd2dec(Wire.read() );
year += 2000;
}
int bcd2dec(byte num)
{
return ((num/16 * 10) + (num % 16));
}
int dec2bcd(byte num)
{
return ((num/10 * 16) + (num % 10));
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour);
printDigits(minute);
printDigits(second);
Serial.print(" ");
Serial.print(day);
Serial.print(" ");
Serial.print(month);
Serial.print(" ");
Serial.print(year);
Serial.println();
lcd.setCursor(0, 0);// cot hang
lcd.print(hour);
lcd.setCursor(2, 0);// cot hang
lcd.print(":");
lcd.setCursor(3, 0);// cot hang
lcd.print(minute);
lcd.setCursor(5, 0);// cot hang
lcd.print(":");
lcd.setCursor(6, 0);// cot hang
lcd.print(second);
lcd.setCursor(0, 1);// cot hang
lcd.print(day);
lcd.setCursor(2, 1);// cot hang
lcd.print("/");
lcd.setCursor(3, 1);// cot hang
lcd.print(month);
lcd.setCursor(5, 1);// cot hang
lcd.print("/");
lcd.setCursor(6, 1);// cot hang
lcd.print(year);
}
void printDigits(int digits)
{
// các thành phần thời gian được ngăn chách bằng dấu :
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
//cài đặt thời gian cho DS1307
void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr)
{
Wire.beginTransmission(DS1307);
Wire.write(byte(0x00)); // đặt lại pointer
Wire.write(dec2bcd(sec));
Wire.write(dec2bcd(min));
Wire.write(dec2bcd(hr));
Wire.write(dec2bcd(wd)); // day of week: Sunday = 1, Saturday = 7
Wire.write(dec2bcd(d));
Wire.write(dec2bcd(mth));
Wire.write(dec2bcd(yr));
Wire.endTransmission();
}
http://www.mediafire.com/file/kqv5fhsmo8vtdcd/dht11.rar
Nếu nhiệt độ lớn hơn 40 thì kích relay đóng.
Code:
#include <Wire.h>
#include "DHT.h" //Khai báo thư viện DHT11
#include <LiquidCrystal.h> //Khai báo thư viện LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTPIN 6 //Khai báo chân tín hiệu DHT11
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const byte DS1307 = 0x68;
const byte NumberOfFields = 7;
int second, minute, hour, day, wday, month, year;
void setup()
{
//LCD
lcd.begin(16, 2);
Serial.begin(9600);
//DS1307
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
Wire.begin();
setTime(21, 30, 45, 6, 04, 12, 17); // 21:30:45 T6 04-12-2017
//DHT11
Serial.begin(19200);
Serial.println("DHTxx test!");
dht.begin();
pinMode(7,OUTPUT);
}
void loop()
{
readDS1307();
digitalClockDisplay();
delay(1000);
// Đọc nhiệt độ, độ ẩm mất khoảng 250ms
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
lcd.setCursor(11, 0);// cot hang
lcd.print("H");
lcd.setCursor(12, 0);// cot hang
lcd.print("=");
lcd.setCursor(13, 0);// cot hang
lcd.print(round(h));
lcd.setCursor(15, 0);// cot hang
lcd.print("%");
lcd.setCursor(11, 1);// cot hang
lcd.print("T");
lcd.setCursor(12, 1);// cot hang
lcd.print("=");
lcd.setCursor(13, 1);// cot hang
lcd.print(round(t));
lcd.setCursor(15, 1);// cot hang
lcd.print("C");
if(round(t)>=40)
digitalWrite(7,HIGH);
else
digitalWrite(7,LOW);
}
}
void readDS1307()
{
Wire.beginTransmission(DS1307);
Wire.write((byte)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, NumberOfFields);
second = bcd2dec(Wire.read() & 0x7f);
minute = bcd2dec(Wire.read() );
hour = bcd2dec(Wire.read() & 0x3f); // chế độ 24h.
wday = bcd2dec(Wire.read() );
day = bcd2dec(Wire.read() );
month = bcd2dec(Wire.read() );
year = bcd2dec(Wire.read() );
year += 2000;
}
int bcd2dec(byte num)
{
return ((num/16 * 10) + (num % 16));
}
int dec2bcd(byte num)
{
return ((num/10 * 16) + (num % 10));
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour);
printDigits(minute);
printDigits(second);
Serial.print(" ");
Serial.print(day);
Serial.print(" ");
Serial.print(month);
Serial.print(" ");
Serial.print(year);
Serial.println();
lcd.setCursor(0, 0);// cot hang
lcd.print(hour);
lcd.setCursor(2, 0);// cot hang
lcd.print(":");
lcd.setCursor(3, 0);// cot hang
lcd.print(minute);
lcd.setCursor(5, 0);// cot hang
lcd.print(":");
lcd.setCursor(6, 0);// cot hang
lcd.print(second);
lcd.setCursor(0, 1);// cot hang
lcd.print(day);
lcd.setCursor(2, 1);// cot hang
lcd.print("/");
lcd.setCursor(3, 1);// cot hang
lcd.print(month);
lcd.setCursor(5, 1);// cot hang
lcd.print("/");
lcd.setCursor(6, 1);// cot hang
lcd.print(year);
}
void printDigits(int digits)
{
// các thành phần thời gian được ngăn chách bằng dấu :
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
//cài đặt thời gian cho DS1307
void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr)
{
Wire.beginTransmission(DS1307);
Wire.write(byte(0x00)); // đặt lại pointer
Wire.write(dec2bcd(sec));
Wire.write(dec2bcd(min));
Wire.write(dec2bcd(hr));
Wire.write(dec2bcd(wd)); // day of week: Sunday = 1, Saturday = 7
Wire.write(dec2bcd(d));
Wire.write(dec2bcd(mth));
Wire.write(dec2bcd(yr));
Wire.endTransmission();
}