Skip to content

Instantly share code, notes, and snippets.

@carlosguerrac
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save carlosguerrac/3a75a08be2a5bfedbf55 to your computer and use it in GitHub Desktop.

Select an option

Save carlosguerrac/3a75a08be2a5bfedbf55 to your computer and use it in GitHub Desktop.
Clock + Countdown + DHT22
//
// FILE: dht22v02.ino
// AUTHOR: Carlos Guerra
// VERSION: 0.2
// PURPOSE: lectura sensor DHT22 y visualizacion mediante LCD
// lectura RTC y visualizacion de LCD
// countdown a Dia D
// URL:
//
// Released to the public domain
//
#include <dht.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
// VARIABLES:
dht DHT;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 rtc;
int i=0;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
//String strDateTime;
#define DHT22_PIN 5
void informacionRTC()
{
Serial.println("Conexiones RTC1307 (from LEFT to RIGHT): ");
Serial.println("1 : DS N/D");
Serial.println("2 : SCL N/C");
Serial.println("3 : SDA N/C");
Serial.println("4 : VCC (5V)");
Serial.println("5 : GND ");
Serial.println("Conexion Arduino:");
Serial.println("A4: SDA");
Serial.println("A5: SCL");
Serial.println("");
}
void informacionDHT22()
{
Serial.println("Conexiones DHT22 (from LEFT to RIGHT): ");
Serial.println("1 : VCC (5V)");
Serial.println("2 : SIGnal ");
Serial.println("3 : NC Not connected ");
Serial.println("4 : GND ");
Serial.println("Conexion Arduino PIN-5 ");
}
void informacionLCD()
{
Serial.println("Conexiones LCD I2C: ");
Serial.println("1 : GND ");
Serial.println("2 : VCC (5V)");
Serial.println("3 : SDA ");
Serial.println("4 : SCL ");
Serial.println("Conexiones Arduino: ");
Serial.println("A4 : SDA ");
Serial.println("A5 : SCL ");
}
void informacionTimeConfig()
{
Serial.println("Informacion Setup: ");
Serial.println(" SET YYYYMMDDhhmmss");
}
void SetupLCD()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
informacionLCD();
// lcd.print("Hum % Temp C");
}
void SetupDHT22()
{
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void SetupRTC()
{
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 7, 19, 19, 23, 0));
}
}
void SetupSerial()
{
Serial.begin(115200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void setup()
{
SetupSerial();
informacionDHT22();
SetupDHT22();
informacionLCD();
SetupLCD();
SetupRTC();
informacionRTC();
informacionTimeConfig();
}
void readRTC()
{
char buffer[20];
char TempBuf[4] = " ";
DateTime now = rtc.now();
sprintf(buffer,"%02i/%02i %02i:%02i:%02i %02c", now.day(), now.month(), now.hour(), now.minute(), now.second(), TempBuf);
lcd.setCursor(0, 0);
lcd.print(buffer);
Serial.println(buffer);
}
void readDHT22()
{
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22(DHT22_PIN);
uint32_t stop = micros();
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(DHT.humidity);
lcd.setCursor(8, 1);
lcd.print(DHT.temperature);
}
void showTimeSpan(const char* txt, const TimeSpan& ts)
{
/*
Serial.print(txt);
Serial.print(" ");
Serial.print(ts.days(), DEC);
Serial.print(" days ");
Serial.print(ts.hours(), DEC);
Serial.print(" hours ");
Serial.print(ts.minutes(), DEC);
Serial.print(" minutes ");
Serial.print(ts.seconds(), DEC);
Serial.print(" seconds (");
Serial.print(ts.totalseconds(), DEC);
Serial.print(" total seconds)");
Serial.println();
*/
char buffer[50];
char TempBuf[4] = " ";
sprintf(buffer,"%03iD %02ih %02im %02is %02c", ts.days(), ts.hours(), ts.minutes(), ts.seconds(), TempBuf );
//12 34 56 78 90 12 34 56
lcd.setCursor(0, 1);
lcd.print(buffer);
Serial.println(buffer);
}
void countdown()
{
DateTime dt (2015, 06, 27, 18, 0, 0);
DateTime now = rtc.now();
TimeSpan ts1 = dt - now;
showTimeSpan("countdown ", ts1);
}
void serialMenuOption()
{
// print the string when a newline arrives:
if (stringComplete)
{
Serial.println(inputString);
if (inputString.substring(0,4) == "SET ")
{
Serial.println("configuracion");
Serial.println("año:\t"+inputString.substring(4,8));
Serial.println("mes:\t"+inputString.substring(8,10));
Serial.println("dia:\t"+inputString.substring(10,12));
Serial.println("hora:\t"+inputString.substring(12,14));
Serial.println("min:\t"+inputString.substring(14,16));
Serial.println("seg:\t"+inputString.substring(16,18));
rtc.adjust(DateTime(inputString.substring(4,8).toInt(), inputString.substring(8,10).toInt(), inputString.substring(10,12).toInt(), inputString.substring(12,14).toInt(), inputString.substring(14,16).toInt(), inputString.substring(16,18).toInt()));
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
void loop()
{
// READ TIME
//DateTime now = rtc.now();
readDHT22();
// READ DATA
readRTC();
// show countdown or humidity and temperature
if (i>=40)
{
countdown();
}
else if (i>=60)
{
i=0;
}
delay(1000);
i++;
serialMenuOption();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
//
// END OF FILE
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment