498 lines
12 KiB
Markdown
498 lines
12 KiB
Markdown
# RTC
|
|
## Real Time Clock
|
|
|
|
DS3231
|
|
|
|
- [Ejm Básico](#funciones-para-hora-y-fecha)
|
|
- [Ajuste Hora y Fecha](#ajuste-hora-y-fecha)
|
|
- [Ajuste Hora y Fecha según tiempo de compilación](#set-hora-y-fecha-segun-datos-de-compilacion)
|
|
- [Conexiones](#conexiones)
|
|
- [RTC LCD y Bluetooth](#rtc-lcd-y-bluetooth)
|
|
|
|
|
|
|
|
### Funciones para hora y fecha
|
|
**RTC DS3231** conectado por **I2C** y libreria **Wire**
|
|
```c
|
|
#include <Wire.h>
|
|
#include "RTClib.h"
|
|
|
|
RTC_DS3231 rtc;
|
|
|
|
char daysOfTheWeek[7][12] = {
|
|
"Domingo", "Lunes", "Martes",
|
|
"Miercoles", "Jueves", "Viernes",
|
|
"Sabado"};
|
|
|
|
void setup () {
|
|
#ifndef ESP8266
|
|
while (!Serial); // for Leonardo/Micro/Zero
|
|
#endif
|
|
Serial.begin(9600);
|
|
delay(3000); // wait for console opening
|
|
|
|
if (! rtc.begin()) {
|
|
Serial.println("Couldn't find RTC");
|
|
while (1);
|
|
}
|
|
if (rtc.lostPower()) {
|
|
Serial.println("RTC lost power, lets set the time!");
|
|
// 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, 1, 21, 3, 0, 0));
|
|
}
|
|
}
|
|
|
|
void loop () {
|
|
DateTime now = rtc.now();
|
|
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
|
|
Serial.print(" ");
|
|
Serial.print(now.day(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(now.month(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(now.year(), DEC);
|
|
Serial.print(" ");
|
|
Serial.print(now.hour(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(now.minute(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(now.second(), DEC);
|
|
Serial.println();
|
|
|
|
Serial.print(" desde medianoche del 1/1/1970 = ");
|
|
Serial.print(now.unixtime());
|
|
Serial.print("s = ");
|
|
Serial.print(now.unixtime() / 86400L);
|
|
Serial.println("d");
|
|
/*
|
|
// calculate a date which is 7 days and 30 seconds into the future
|
|
DateTime future (now + TimeSpan(7,12,30,6));
|
|
|
|
Serial.print(" hoy +7d +30s: ");
|
|
Serial.print(future.year(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(future.month(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(future.day(), DEC);
|
|
Serial.print(' ');
|
|
Serial.print(future.hour(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(future.minute(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(future.second(), DEC);
|
|
Serial.println();
|
|
*/
|
|
Serial.println();
|
|
delay(3000);
|
|
}
|
|
```
|
|
|
|
----
|
|
|
|
### Ajuste Hora y Fecha
|
|
Usando un RX8025 RTC conectado via I2C y libreria Wire
|
|
```c
|
|
#include <Wire.h>
|
|
#include "RTClib.h"
|
|
|
|
RTC_DS3231 RTC; //Create the DS3231 object
|
|
|
|
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
|
|
|
//year, month, date, hour, min, sec and week-day(starts from 0 and goes to 6)
|
|
//writing any non-existent time-data may interfere with normal operation of the RTC.
|
|
//Take care of week-day also.
|
|
DateTime dt(2018, 3, 20, 21, 51, 0);
|
|
|
|
void setup(){
|
|
Serial.begin(57600);
|
|
Wire.begin();
|
|
RTC.begin();
|
|
RTC.adjust(dt); //Adjust date-time as defined 'dt' above
|
|
}
|
|
|
|
void loop(){
|
|
DateTime now = RTC.now(); //get the current date-time
|
|
Serial.print(now.year(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(now.month(), DEC);
|
|
Serial.print('/');
|
|
//Serial.print(now.date(), DEC);
|
|
Serial.print(' ');
|
|
Serial.print(now.hour(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(now.minute(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(now.second(), DEC);
|
|
Serial.println();
|
|
//Serial.print(weekDay[now.dayOfWeek()]);
|
|
Serial.println();
|
|
delay(1000);
|
|
}
|
|
```
|
|
|
|
### Set hora y fecha segun datos de compilacion
|
|
|
|
```c
|
|
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
// Pines del chip I2C al LCD:
|
|
// (addr,en,rw,rs,d4,d5,d6,d7,BackLight, BLstatus)
|
|
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address [0x27 o 0x3F]
|
|
RTC_DS3231 reloj;
|
|
bool alarma = true;
|
|
DateTime fecha;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
lcd.begin(20,4);
|
|
if(!reloj.begin()) {
|
|
Serial.println("Modulo RTC no encontrado!!");
|
|
while(1); // 1 = true - blucle infito si no hay modulo
|
|
}
|
|
//reloj.adjust(DateTime(__DATE__, __TIME__)); // Descomentar para ajustar fecha y hora
|
|
}
|
|
|
|
void loop() {
|
|
fecha = reloj.now();
|
|
getFecha();
|
|
lcd.clear();
|
|
lcd.home();
|
|
mostrarHora();
|
|
delay(1000);
|
|
}
|
|
|
|
void getFecha() {
|
|
Serial.print(fecha.day());
|
|
Serial.print("/");
|
|
Serial.print(fecha.month());
|
|
Serial.print("/");
|
|
Serial.print(fecha.year());
|
|
Serial.print(" ");
|
|
Serial.print(fecha.hour());
|
|
Serial.print(":");
|
|
Serial.print(fecha.minute());
|
|
Serial.print(":");
|
|
Serial.println(fecha.second());
|
|
}
|
|
|
|
String getHora(char var) {
|
|
switch (var) {
|
|
case 'h':
|
|
if(fecha.hour() < 10) {
|
|
return ('0'+(String)fecha.hour()); break;
|
|
} else {
|
|
return (String)fecha.hour();break;
|
|
}
|
|
case 'm':
|
|
if(fecha.minute() <10) {
|
|
return ('0'+(String)fecha.minute()); break;
|
|
} else {
|
|
return (String)fecha.minute();break;
|
|
}
|
|
case 's':
|
|
if(fecha.second() <10) {
|
|
return ('0'+(String)fecha.second()); break;
|
|
} else {
|
|
return (String)fecha.second();break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void mostrarHora() {
|
|
lcd.print("La hora es: ");
|
|
lcd.print(getHora('h'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('m'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('s'));
|
|
lcd.setCursor(0, 1);
|
|
}
|
|
```
|
|
|
|
### Set hora y fecha segun datos de compilacion
|
|
|
|
`reloj.adjust(DateTime(__DATE__, __TIME__));`
|
|
```c
|
|
#include <RTClib.h>
|
|
|
|
RTC_DS3231 reloj;
|
|
bool alarma = true;
|
|
DateTime fecha;
|
|
|
|
void setup(){
|
|
Serial.begin(9600);
|
|
if(!reloj.begin()){
|
|
Serial.println("Modulo RTC no encontrado!!");
|
|
while(1); // 1 = true - blucle infito si no hay modulo
|
|
}
|
|
// Ajustes fecha y hora según tiempo de compilación
|
|
reloj.adjust(DateTime(__DATE__, __TIME__));
|
|
}
|
|
|
|
void loop() {
|
|
fecha = reloj.now();
|
|
getFecha();
|
|
delay(1000);
|
|
}
|
|
/*
|
|
if(fecha.hour() == 13 && fecha.minute() == 00) {
|
|
if(alarma == true) {
|
|
Serial.println("Alarma 1 PM");
|
|
alarma = false;
|
|
}
|
|
}
|
|
getFecha();
|
|
delay(1000);
|
|
if(fecha.hour() == 12 && fecha.minute() ==30) {
|
|
alarma = true;
|
|
}
|
|
}
|
|
*/
|
|
|
|
void getFecha() {
|
|
Serial.print(fecha.day());
|
|
Serial.print("/");
|
|
Serial.print(fecha.month());
|
|
Serial.print("/");
|
|
Serial.print(fecha.year());
|
|
Serial.print(" ");
|
|
Serial.print(fecha.hour());
|
|
Serial.print(":");
|
|
Serial.print(fecha.minute());
|
|
Serial.print(":");
|
|
Serial.println(fecha.second());
|
|
}
|
|
|
|
String getHora(char var) {
|
|
switch (var) {
|
|
case 'h':
|
|
if(fecha.hour() < 10) {
|
|
return ('0'+(String)fecha.hour()); break;
|
|
} else {
|
|
return (String)fecha.hour();break;
|
|
}
|
|
case 'm':
|
|
if(fecha.minute() <10) {
|
|
return ('0'+(String)fecha.minute()); break;
|
|
} else {
|
|
return (String)fecha.minute();break;
|
|
}
|
|
case 's':
|
|
if(fecha.second() <10) {
|
|
return ('0'+(String)fecha.second()); break;
|
|
} else {
|
|
return (String)fecha.second();break;
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
----
|
|
|
|
## RTC LCD y Bluetooth
|
|
|
|
### Conexiones
|
|
|
|
|HC - 06|ARDUINO|
|
|
|-|-|
|
|
|**`VCC`** | **`5V`**|
|
|
|**`GND`** | **`GND`**|
|
|
|**`TXD`** | **`10`**|
|
|
|**`RXD`** | **`11`**|
|
|
|
|
> Pines del chip I2C al LCD:
|
|
> (addr,en,rw,rs,d4,d5,d6,d7,BackLight, BLstatus)
|
|
|
|
```c
|
|
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
#define relLuz 13
|
|
#define relVent 12
|
|
#define sHum1 A0
|
|
#define sHum2 A1
|
|
#define sHum3 A2
|
|
#define sHum4 A3
|
|
byte hum1, hum2, hum3;
|
|
// (addr,en,rw,rs,d4,d5,d6,d7,BackLight, BLstatus)
|
|
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address [0x27 o 0x3F]
|
|
RTC_DS3231 reloj;
|
|
bool alarma = true;
|
|
DateTime fecha;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
lcd.begin(20,4);
|
|
if(!reloj.begin()) {
|
|
Serial.println("Modulo RTC no encontrado!!");
|
|
while(1); // 1 = true - blucle infito si no hay modulo
|
|
}
|
|
//reloj.adjust(DateTime(__DATE__, __TIME__)); // Descomentar para ajustar fecha y hora
|
|
}
|
|
|
|
void loop() {
|
|
lcd.clear();
|
|
lcd.home();
|
|
fecha = reloj.now();
|
|
mostrarFechaSerie();
|
|
horaLCD();
|
|
delay(1000);
|
|
}
|
|
|
|
//En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10
|
|
String getHora(char var) {
|
|
switch (var) {
|
|
case 'h':
|
|
if(fecha.hour() < 10) {
|
|
return ('0'+(String)fecha.hour()); break;
|
|
} else {
|
|
return (String)fecha.hour();break;
|
|
}
|
|
case 'm':
|
|
if(fecha.minute() <10) {
|
|
return ('0'+(String)fecha.minute()); break;
|
|
} else {
|
|
return (String)fecha.minute();break;
|
|
}
|
|
case 's':
|
|
if(fecha.second() <10) {
|
|
return ('0'+(String)fecha.second()); break;
|
|
} else {
|
|
return (String)fecha.second();break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Hora en LCD
|
|
void horaLCD() {
|
|
lcd.print("La hora es: ");
|
|
lcd.print(getHora('h'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('m'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('s'));
|
|
}
|
|
|
|
// Enviar hora y fecha por puerto serie
|
|
void mostrarFechaSerie() {
|
|
Serial.print(fecha.day());
|
|
Serial.print("/");
|
|
Serial.print(fecha.month());
|
|
Serial.print("/");
|
|
Serial.print(fecha.year());
|
|
Serial.print(" ");
|
|
Serial.print(getHora('h'));
|
|
Serial.print(":");
|
|
Serial.print(getHora('m'));
|
|
Serial.print(":");
|
|
Serial.println(getHora('s'));
|
|
}
|
|
```
|
|
----
|
|
|
|
```c
|
|
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
#include <SoftwareSerial.h>
|
|
SoftwareSerial miBT(10,11);
|
|
// Pines del chip I2C al LCD:
|
|
// (addr,en,rw,rs,d4,d5,d6,d7,BackLight, BLstatus)
|
|
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address [0x27 o 0x3F]
|
|
RTC_DS3231 reloj;
|
|
bool alarma = true;
|
|
DateTime fecha;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
lcd.begin(20,4);
|
|
miBT.begin(9600);
|
|
if(!reloj.begin()) {
|
|
Serial.println("Modulo RTC no encontrado!!");
|
|
while(1); // 1 = true - blucle infito si no hay modulo
|
|
}
|
|
//reloj.adjust(DateTime(__DATE__, __TIME__)); // Descomentar para ajustar fecha y hora
|
|
}
|
|
|
|
void loop(){
|
|
fecha = reloj.now();
|
|
getFecha();
|
|
lcd.clear();
|
|
lcd.home();
|
|
mostrarHora();
|
|
comBT();
|
|
delay(1000);
|
|
}
|
|
|
|
//En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10
|
|
String getHora(char var) {
|
|
switch (var) {
|
|
case 'h':
|
|
if(fecha.hour() < 10) {
|
|
return ('0'+(String)fecha.hour()); break;
|
|
} else {
|
|
return (String)fecha.hour(); break;
|
|
}
|
|
case 'm':
|
|
if(fecha.minute() <10) {
|
|
return ('0'+(String)fecha.minute()); break;
|
|
} else {
|
|
return (String)fecha.minute();break;
|
|
}
|
|
case 's':
|
|
if(fecha.second() <10) {
|
|
return ('0'+(String)fecha.second()); break;
|
|
} else {
|
|
return (String)fecha.second();break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Hora en LCD
|
|
void mostrarHora() {
|
|
lcd.print("La hora es: ");
|
|
lcd.print(getHora('h'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('m'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('s'));
|
|
}
|
|
|
|
// Enviar hora y fecha por puerto serie
|
|
void getFecha() {
|
|
Serial.print(fecha.day());
|
|
Serial.print("/");
|
|
Serial.print(fecha.month());
|
|
Serial.print("/");
|
|
Serial.print(fecha.year());
|
|
Serial.print(" ");
|
|
Serial.print(fecha.hour());
|
|
Serial.print(":");
|
|
Serial.print(fecha.minute());
|
|
Serial.print(":");
|
|
Serial.println(fecha.second());
|
|
}
|
|
|
|
void comBT() {
|
|
if(miBT.available()) {
|
|
lcd.setCursor(0, 1);
|
|
while(miBT.available() > 0) {
|
|
lcd.print((char)miBT.read()); //lee BT y envia a Arduino
|
|
}
|
|
delay(1000);
|
|
}
|
|
if(Serial.available()) {
|
|
while(Serial.available() > 0) {
|
|
miBT.write(Serial.read()); // lee Arduino y envia a BT
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|