#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// These are for the clock
#define DS1307_ADDRESS 0x68 //địa chỉ của DS1307 Slave Address (SLA)
// These are for the temperature
#define DHTPIN 2 //Đọc dữ liệu từ DHT11 ở chân 2 trên mạch Arduino
#define DHTTYPE DHT22 //Khai báo loại cảm biến DHT22
#define TIMEDHT 1000
// Global variables
uint8_t wday, mday, month, year;
uint8_t hours, minutes, seconds;
char szTime[9]; // mm:ss\0
float humidity, celsius;
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
uint8_t clear = 0x00;
uint32_t timerDHT = TIMEDHT;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27,20,4); // Define LCD 2004 with Address
bool flasher = true; // nhấp nhay dau 2 cham
void beginDS1307()
{
// Read the values (date and time) of the DS1307 module
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(clear);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 0x07);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0xff);
wday = bcdToDec(Wire.read());
mday = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
uint8_t decToBcd(uint8_t value)
{
return ((value / 10 * 16) + (value % 10));
}
uint8_t bcdToDec(uint8_t value)
{
return ((value / 16 * 10) + (value % 16));
}
// Code for get Temperature
void getTemperature()
{
// Wait for a time between measurements
if ((millis() - timerDHT) > TIMEDHT) {
// Update the timer
timerDHT = millis();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
celsius = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(celsius) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
// Get a label from PROGMEM into a char array
char *mon2str(uint8_t mon, char *psz, uint8_t len)
{
static const __FlashStringHelper* str[] =
{
F("Jan"), F("Feb"), F("Mar"), F("Apr"),
F("May"), F("Jun"), F("Jul"), F("Aug"),
F("Sep"), F("Oct"), F("Nov"), F("Dec")
};
strncpy_P(psz, (const char PROGMEM *)str[mon - 1], len);
psz[len] = '\0';
return (psz);
}
char *dow2str(uint8_t code, char *psz, uint8_t len)
{
static const __FlashStringHelper* str[] =
{
F("Sunday"), F("Monday"), F("Tuesday"),
F("Wednesday"), F("Thursday"), F("Friday"),
F("Saturday")
};
strncpy_P(psz, (const char PROGMEM *)str[code - 1], len);
psz[len] = '\0';
return (psz);
}
void setup(void)
{
Wire.begin();
lcd.init();
dht.begin();
}
void lcdClock(void) //
{
char szBuf[10];
lcd.backlight();
lcd.setCursor(7,0);
lcd.print(hours); lcd.print((flasher ? ':' : ' '));
lcd.print(minutes);
flasher = !flasher;
lcd.setCursor(1,1);
lcd.print(dow2str(wday,szBuf,sizeof(szBuf) - 1));lcd.print(" ");
lcd.print(mday);lcd.print("-"); lcd.print(mon2str(month, szBuf, sizeof(szBuf) - 1));
lcd.print("-"); lcd.print(year +2000);
lcd.setCursor(0,2);
lcd.print("--------------------");
lcd.setCursor(0,3);
lcd.print("T:");lcd.print(celsius);lcd.print(" C");
lcd.setCursor(11,3);
lcd.print("H:");lcd.print(humidity); lcd.print("\%");
}
void loop(void)
{
static uint32_t lastTime = 0; // Memory (ms)
static uint8_t display = 0; // Current display mode Được dùng để tạo ngắt Timer
beginDS1307();
getTemperature();
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
lcdClock();
}
}