Skip to content

Instantly share code, notes, and snippets.

@Conplug
Last active January 23, 2019 08:13
Show Gist options
  • Save Conplug/2747fae7480db5ad4d196d249f0e8205 to your computer and use it in GitHub Desktop.
Save Conplug/2747fae7480db5ad4d196d249f0e8205 to your computer and use it in GitHub Desktop.
//
// Copyright (c) 2019 Conplug (https://conplug.com.tw)
// Author: Hartman Hsieh
//
// Description :
// Display date, time, temperature and humidity on LCD1602.
//
// Connections :
// Plug "DFRobot Gravity I2C LCD1602" module at "IIC0" of "NANO_EXP V1.0".
// Plug "DFRobot Gravity I2C DS1307 RTC" at "IIC2" of "NANO_EXP V1.0".
// Plug "DFRobot DHT11" at "JD3" of "NANO_EXP V1.0".
//
// Required Libraries :
// https://github.com/adafruit/DHT-sensor-library
// https://github.com/bearwaterfall/DFRobot_LCD-master
// https://github.com/DFRobot/DS1307_RTC
//
//
// Include RTC header files.
//
#include <Wire.h>
#include <DS1307.h>
char* WeekDays[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"}; // Days Of The Week
int Rtc[7];
//
// Include DHT header file.
//
#include "DHT.h"
#define DHTPIN 3 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
#include "DFRobot_LCD.h"
DFRobot_LCD Lcd(16, 2); //16 characters and 2 lines of show
int Index = 0;
void setup() {
Serial.begin(9600);
Lcd.init();
Lcd.display();
Lcd.clear();
dht.begin();
}
void loop() {
RTC.get(Rtc,true);
//
// Wait a few seconds between measurements.
//
delay(900);
//
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
//
float h = dht.readHumidity();
//
// Read temperature as Celsius (the default)
//
float t = dht.readTemperature();
//
// Check if any reads failed and exit early (to try again).
//
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
Lcd.setCursor(0, 0);
//
// Exchange display every 5 seconds.
//
if ((Index / 5) % 2) {
//
// Print YEAR-MONTH-DAY
//
Lcd.print(" ");
Lcd.print(Rtc[6]);
Lcd.print("-");
Lcd.print(Rtc[5]);
Lcd.print("-");
Lcd.print(Rtc[4]);
Lcd.print(" ");
}
else {
//
// Print Temperature and Humidity
//
Lcd.print(" ");
Lcd.print(t, 1);
Lcd.print("C ");
Lcd.print(h, 1);
Lcd.print("% ");
}
Lcd.setCursor(0, 1);
//
// Print HOUR:MIN:SEC WEEK
//
Lcd.print(" ");
Lcd.print(Rtc[2]);
Lcd.print(":");
Lcd.print(Rtc[1]);
Lcd.print(":");
Lcd.print(Rtc[0]);
Lcd.print(" ");
Lcd.print(WeekDays[Rtc[3]-1]);
Lcd.print(" ");
Index++;
if (Index >= 100)
Index = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment