#include <ArduinoJson.h>
#include <SD.h>
#include "RTC.h"
#include <LiquidCrystal_I2C.h>
#define CS 5
#define MOSI 23
#define MISO 19
#define SCK 18
DS1307_RTC RTC;
LiquidCrystal_I2C LCD(0x27, 20, 4);
String json; // String para la serialización y deserialización del JSON
void setup ( void ) {
doc["sec"] = 0;
doc["min"] = 0;
doc["hr"] = 0;
doc["day"] = 0;
doc["month"] = 0;
doc["year"] = 0;
Serial.begin ( 115200 ); /* Inicializar baudrate a 115200 */
Serial.print("Iniciando SD... ");
while (!SD.begin(CS))
{
}
Serial.println("Terminado");
LCD.init();
LCD.backlight();
RTC.RTC_init ( ); /* Inicializar configuración del reloj */
Serial.println ( F ( "El DS-1307 ha sido configurado exitosamente." ) );
}
void loop ( void ) {
RTC.get_time ( ); /* Actualizar fecha y hora en formato RAW */
serializeJson(doc, json);
writeFile(json);
//RTC.show_time ( ); /* Dar formato y mostrar por consola serial */
delay ( 1000 );
deserializeJson(doc, readFile());
String hora = "Hora: " + (String)doc["hr"] + ":" + (String)doc["min"] + ":" + (String)doc["sec"];
String fecha = "Fecha: " + (String)doc["day"] + "/" + (String)doc["month"] + "/" + (String)doc["year"];
LCD.setCursor(0, 0);
LCD.print(hora);
LCD.setCursor(0, 2);
LCD.print(fecha);
delay ( 1000 );
}
String readFile()
{
String text;
File textFile = SD.open("/prieto.txt");
if (textFile)
{
while (textFile.available())
{
text += (char)textFile.read();
}
textFile.close();
}
else
{
// Si no existe el archivo
serializeJson(doc, text);
writeFile(text);
}
Serial.println(text);
return text;
}
void writeFile(String text)
{
File textFile = SD.open("/prieto.txt", FILE_WRITE);
if (textFile)
{
textFile.print(text);
textFile.close();
Serial.println(readFile());
}
else
{
Serial.println("Caca");
}
}Requiere de una pila para poder conservar sus configuraciones.