/*
 * Date:    04/10/2022
 * Title:   LCD 16x2 ESP32
 * Author:  Rubén Lozano
 * 
 * References: 
 *              
*/
/*
 *****************************************************
 *
 *    LIBRERIAS
 *
 *****************************************************
*/
#include <LiquidCrystal.h>
#include <ESP32Time.h>
/*
 *****************************************************
 *
 *    VARIABLES DEFINIDAS.
 *
 *****************************************************
*/
#define RS 19 //PIN D19 o D5
#define EN 18 //PIN D18 o TX2 GPIO17
#define DB4 5 //PIN D5 o RX2 GPIO16
#define DB5 4 //PIN D4
#define DB6 2 //PIN D2
#define DB7 15 //PIN D15
LiquidCrystal lcd(RS, EN, DB4, DB5, DB6, DB7); //INICIALIZACION LCD 16x2
//LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); ////INICIALIZACION LCD 16x2 I²C
ESP32Time rtc;
/*
 *****************************************************
 *
 *    SETUP
 *
 *****************************************************
*/
void setup() {
  Serial.begin(115200);
  //Formato setTime(seg, min, hora, dia, mes, año) sin usar ceros
  rtc.setTime(30, 33, 12, 9, 4, 2021);  // 17th Jan 2021 15:24:30
  //rtc.setTime(1609459200);  // 1st Jan 2021 00:00:00 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("HOLA MUNDO");
}
/*
 *****************************************************
 *
 *    LOOP
 *
 *****************************************************
*/ 
void loop() {
    String now=rtc.getTime("%A,%B %d %Y %H:%M:%S"); //Devuelve la hora con el formato especificado
    String horario=rtc.getAmPm(true);               // Devuelve el horario si es AM o PM
    int segundos=rtc.getSecond();                   //Devuelve los segundos entre (0-59)
    int minutos=rtc.getMinute();                    //Devuelve los segundos entre (0-59)
    int hora=rtc.getHour();                         //Devuelve los horas entre (0-12)
    int horas_24=rtc.getHour(true);                 //Devuelve las hora en formato de 24 horas (0-23)
    int dia=rtc.getDay();                           //Devuelve el dia en formato entero (1-31)
    int mes=rtc.getMonth();                         //Devuelve el mes en representación de un formato entero (0-11)
    int anio=rtc.getYear();                         //Devuelve el año en formato entero. 
    Serial.println("Hora actual");
    Serial.print("Forma 1: ");
    Serial.println(now);
    Serial.print("Forma 2: ");
    Serial.print(dia);
    Serial.print("/");
    Serial.print(mes+1);
    Serial.print("/");
    Serial.print(anio);
    Serial.print(" ");
    Serial.print(horas_24);
    Serial.print(":");
    Serial.print(minutos);
    Serial.print(":");
    Serial.print(segundos);
    Serial.print("");
    Serial.print(horario);
    Serial.print("\n");
    delay(1000);
  
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}