TIMBRE PARA COLEGIO CON ESP32
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#include <SPI.h> // incluye libreria bus SPI
#include <RTClib.h> // for the RTC
#include <LiquidCrystal_I2C.h> //Downlaod the library: http://www.electronoobs.com/eng_arduino_liq_crystal.php
LiquidCrystal_I2C lcd(0x27,16,2);
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProSwitch.h"
#include <map>
#define WIFI_SSID "Jroyero"
#define WIFI_PASS "@*8642329*@"
#define APP_KEY "d5dc9adc-91d7-4e49-a3b0-9f5a17e19e45"
#define APP_SECRET "cf5f0225-2825-48f5-a442-6bb90a99929a-e8571a4f-360d-4ba9-8f4b-478533c8803c"
// comment the following line if you use a toggle switches instead of tactile buttons
#define BAUD_RATE 115200
#define DEBOUNCE_TIME 250
#define RELAYPIN_1 16
#define RELAYPIN_2 4
#define led 2 //G2
//#define triac 16 //G16
#define boton1 14//G14
#define boton2 12//G12
RTC_DS3231 rtc; // crea objeto del tipo RTC_DS3231
#include <Wire.h>
#define I2CADDR 0x20
byte INDICE = 0; // indice del array
const int hora[] = {
6, 7, 7, 8, 9, 10, 10, 11, 11, 12, 14};
const int minuto[] = {
45, 0, 45, 30, 15, 0, 20, 5, 50, 35, 55};
//****************************************
//HORARIO 2
//****************************************
const int hora2[] = {
6, 7, 7, 8, 9, 10, 10, 11, 11, 12, 15};
const int minuto2[] = {
45, 0, 45, 30, 15, 0, 20, 5, 50, 35, 59};
//*****************************************
int segundosTimbre = 5;
int estado_boton = LOW;
int estado_boton2 = LOW;
int Horario = 0;
int Estadoledvirtual = 0;
int Estadoledreal = 0;
typedef struct { // struct for the std::map below
int relayPIN;
int flipSwitchPIN;
bool activeLow;
} deviceConfig_t;
// this is the main configuration
// please put in your deviceId, the PIN for Relay and PIN for flipSwitch
// this can be up to N devices...depending on how much pin's available on your device ;)
// right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually
std::map<String, deviceConfig_t> devices = {
//{deviceId, {relayPIN, flipSwitchPIN, activeLow}}
{"63c5b1ad22e49e3cb5e671d9", { 16, 14, false }},
{"63c5b20e07e1833ecb428ccb", { 4, 12, false }},
};
typedef struct { // struct for the std::map below
String deviceId;
bool lastFlipSwitchState;
unsigned long lastFlipSwitchChange;
bool activeLow;
} flipSwitchConfig_t;
std::map<int, flipSwitchConfig_t> flipSwitches; // this map is used to map flipSwitch PINs to deviceId and handling debounce and last flipSwitch state checks
// it will be setup in "setupFlipSwitches" function, using informations from devices map
void setupRelays() {
for (auto &device : devices) { // for each device (relay, flipSwitch combination)
int relayPIN = device.second.relayPIN; // get the relay pin
pinMode(relayPIN, OUTPUT); // set relay pin to OUTPUT
}
}
void setupFlipSwitches() {
for (auto &device : devices) { // for each device (relay / flipSwitch combination)
flipSwitchConfig_t flipSwitchConfig; // create a new flipSwitch configuration
flipSwitchConfig.deviceId = device.first; // set the deviceId
flipSwitchConfig.lastFlipSwitchChange = 0; // set debounce time
flipSwitchConfig.lastFlipSwitchState = false; // set lastFlipSwitchState to false (LOW)
int flipSwitchPIN = device.second.flipSwitchPIN; // get the flipSwitchPIN
bool activeLow = device.second.activeLow; // set the activeLow
flipSwitchConfig.activeLow = activeLow;
flipSwitches[flipSwitchPIN] = flipSwitchConfig; // save the flipSwitch config to flipSwitches map
if(activeLow) {
pinMode(flipSwitchPIN, INPUT_PULLUP); // set the flipSwitch pin to INPUT_PULLUP
}
else {
pinMode(flipSwitchPIN, INPUT); // set the flipSwitch pin to INPUT
}
}
}
bool onPowerState(String deviceId, bool &state) {
Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off");
int relayPIN = devices[deviceId].relayPIN; // get the relay pin for corresponding device
digitalWrite(relayPIN, state); // set the new relay state
Estadoledvirtual = state;
return true;
}
void handleFlipSwitches() {
unsigned long actualMillis = millis(); // get actual millis
for (auto &flipSwitch : flipSwitches) { // for each flipSwitch in flipSwitches map
unsigned long lastFlipSwitchChange = flipSwitch.second.lastFlipSwitchChange; // get the timestamp when flipSwitch was pressed last time (used to debounce / limit events)
if (actualMillis - lastFlipSwitchChange > DEBOUNCE_TIME) { // if time is > debounce time...
int flipSwitchPIN = flipSwitch.first; // get the flipSwitch pin from configuration
bool lastFlipSwitchState = flipSwitch.second.lastFlipSwitchState; // get the lastFlipSwitchState
bool activeLow = flipSwitch.second.activeLow;
bool flipSwitchState = digitalRead(flipSwitchPIN); // read the current flipSwitch state
if(activeLow) flipSwitchState = !flipSwitchState;
if (flipSwitchState != lastFlipSwitchState) { // if the flipSwitchState has changed...
#ifdef TACTILE_BUTTON
if (flipSwitchState) { // if the tactile button is pressed
#endif
flipSwitch.second.lastFlipSwitchChange = actualMillis; // update lastFlipSwitchChange time
String deviceId = flipSwitch.second.deviceId; // get the deviceId from config
int relayPIN = devices[deviceId].relayPIN; // get the relayPIN from config
bool newRelayState = !digitalRead(relayPIN); // set the new relay State
digitalWrite(relayPIN, newRelayState); // set the trelay to the new state
SinricProSwitch &mySwitch = SinricPro[deviceId]; // get Switch device from SinricPro
mySwitch.sendPowerStateEvent(newRelayState); // send the event
#ifdef TACTILE_BUTTON
}
#endif
flipSwitch.second.lastFlipSwitchState = flipSwitchState; // update lastFlipSwitchState
}
}
}
}
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}
void setupSinricPro() {
for (auto &device : devices) {
const char *deviceId = device.first.c_str();
SinricProSwitch &mySwitch = SinricPro[deviceId];
mySwitch.onPowerState(onPowerState);
}
SinricPro.begin(APP_KEY, APP_SECRET);
}
void setup() {
Serial.begin(BAUD_RATE);
setupRelays();
setupFlipSwitches();
setupWiFi();
setupSinricPro();
lcd.init();
lcd.backlight();
pinMode(led, OUTPUT);
//pinMode(triac, OUTPUT);
//pinMode(boton1, INPUT);
pinMode(boton2, INPUT);
digitalWrite (led, LOW);
//digitalWrite (triac, LOW);
//Serial.println("initialization done.");
if(!rtc.begin()) {
//Serial.println(F("Couldn't find RTC"));
while(1);
}
}
void loop() {
estado_boton = digitalRead(boton1);
estado_boton2 = digitalRead(boton2);
if(estado_boton == LOW){
Estadoledreal = 0;
}
if(estado_boton == HIGH){
Estadoledreal = 1;
}
if(Estadoledvirtual == 0 && Estadoledreal == 0){
digitalWrite(led, LOW);
lcd.setCursor(11,1);
lcd.print("(OFF)");
}
if(Estadoledvirtual == 1 || Estadoledreal == 1){
digitalWrite(led, HIGH);
lcd.setCursor(12,1);
lcd.print("(ON)");
}
if(estado_boton2 == LOW){
Horario = 1;
//Serial.println("HORARIO 1");
}
if(estado_boton2 == HIGH){
Horario = 2;
//Serial.println("HORARIO 2");
}
SinricPro.handle();
handleFlipSwitches();
DateTime fecha = rtc.now(); // funcion que devuelve fecha y horario en formato
lcd.setCursor(14,0);
lcd.print("H");
lcd.setCursor(15,0);
lcd.print(Horario);
lcd.setCursor(2,0);
lcd.print(fecha.day()); // funcion que obtiene el dia de la fecha completa
lcd.print("/"); // caracter barra como separador
lcd.print(fecha.month()); // funcion que obtiene el mes de la fecha completa
lcd.print("/"); // caracter barra como separador
lcd.print(fecha.year()); // funcion que obtiene el año de la fecha completa
lcd.setCursor(2,1);
lcd.print(fecha.hour()); // funcion que obtiene la hora de la fecha completa
lcd.print(":"); // caracter dos puntos como separador
lcd.print(fecha.minute()); // funcion que obtiene los minutos de la fecha completa
lcd.print(":"); // caracter dos puntos como separador
lcd.print(fecha.second()); // funcion que obtiene los segundos de la fecha completa
if (fecha.second() == 59){
lcd.clear();
}
if(Horario == 1){
for(int i = 0; i<11; i++){
if (fecha.hour() == pgm_read_word(&(hora[i])) && fecha.minute() == pgm_read_word(&(minuto[i])) && fecha.second() <= segundosTimbre - 1){
Timbre();
}}
}
if(Horario == 2){
for(int i = 0; i<11; i++){
if (fecha.hour() == pgm_read_word(&(hora2[i])) && fecha.minute() == pgm_read_word(&(minuto2[i])) && fecha.second() <= segundosTimbre - 1){
Timbre();
}}
}
if(WiFi.status() != WL_CONNECTED){
WiFi.begin(WIFI_SSID, WIFI_PASS);
lcd.setCursor(0,0);
lcd.print("D");
}
}
void Timbre(){
//Serial.println("TRIAC ENCENDIDO");
lcd.clear();
lcd.print("TIMBRE ON");
digitalWrite (led, HIGH);
delay(segundosTimbre*1000);
digitalWrite (led, LOW);
//Serial.println("TRIAC APAGADO");
lcd.clear();
}
CODIGO SIN CONECTIVIDAD WIFI
#include <SPI.h> // incluye libreria bus SPI
#include <RTClib.h> // for the RTC
#include <LiquidCrystal_I2C.h> //Downlaod the library: http://www.electronoobs.com/eng_arduino_liq_crystal.php
LiquidCrystal_I2C lcd(0x27,16,2);
#define led 2 //G2
#define triac 16 //G16
#define boton1 14//G14
#define boton2 12//G12
RTC_DS3231 rtc; // crea objeto del tipo RTC_DS3231
#include <Wire.h>
#define I2CADDR 0x20
byte INDICE = 0; // indice del array
//****************************************
//HORARIO 1
//****************************************
const int hora[] = {
6, 7, 7, 8, 9, 10, 10, 11, 11, 12, 15};
const int minuto[] = {
45, 0, 45, 30, 15, 0, 20, 5, 50, 35, 58};
//****************************************
//HORARIO 2
//****************************************
const int hora2[] = {
6, 7, 7, 8, 9, 10, 10, 11, 11, 12, 15};
const int minuto2[] = {
45, 0, 45, 30, 15, 0, 20, 5, 50, 35, 59};
//*****************************************
int segundosTimbre = 5;
int estado_boton = LOW;
int estado_boton2 = LOW;
int Horario = 0;
void setup() {
//Serial.begin(57600); // inicializa comunicacion por monitor serie a 9600 bps
lcd.init();
lcd.backlight();
pinMode(led, OUTPUT);
pinMode(triac, OUTPUT);
pinMode(boton1, INPUT);
pinMode(boton2, INPUT);
digitalWrite (led, LOW);
digitalWrite (triac, LOW);
//Serial.println("initialization done.");
if(!rtc.begin()) {
//Serial.println(F("Couldn't find RTC"));
while(1);
}
}
void loop() {
estado_boton = digitalRead(boton1);
estado_boton2 = digitalRead(boton2);
if(estado_boton == HIGH){
digitalWrite(triac, HIGH);
digitalWrite(led, HIGH);
//Serial.println("TRIAC ENCENDIDO");
lcd.setCursor(12,1);
lcd.print("(ON)");
}
if(estado_boton == LOW){
digitalWrite(triac, LOW);
digitalWrite(led, LOW);
//Serial.println("TRIAC APAGADO");
lcd.setCursor(11,1);
lcd.print("(OFF)");
}
if(estado_boton2 == LOW){
Horario = 1;
//Serial.println("HORARIO 1");
}
if(estado_boton2 == HIGH){
Horario = 2;
//Serial.println("HORARIO 2");
}
DateTime fecha = rtc.now(); // funcion que devuelve fecha y horario en formato
lcd.setCursor(14,0);
lcd.print("H");
lcd.setCursor(15,0);
lcd.print(Horario);
lcd.setCursor(2,0);
lcd.print(fecha.day()); // funcion que obtiene el dia de la fecha completa
lcd.print("/"); // caracter barra como separador
lcd.print(fecha.month()); // funcion que obtiene el mes de la fecha completa
lcd.print("/"); // caracter barra como separador
lcd.print(fecha.year()); // funcion que obtiene el año de la fecha completa
lcd.setCursor(2,1);
lcd.print(fecha.hour()); // funcion que obtiene la hora de la fecha completa
lcd.print(":"); // caracter dos puntos como separador
lcd.print(fecha.minute()); // funcion que obtiene los minutos de la fecha completa
lcd.print(":"); // caracter dos puntos como separador
lcd.print(fecha.second()); // funcion que obtiene los segundos de la fecha completa
if (fecha.second() == 59){
lcd.clear();
}
if(Horario == 1){
for(int i = 0; i<11; i++){
if (fecha.hour() == pgm_read_word(&(hora[i])) && fecha.minute() == pgm_read_word(&(minuto[i])) && fecha.second() <= segundosTimbre - 1){
Timbre();
}}
}
if(Horario == 2){
for(int i = 0; i<11; i++){
if (fecha.hour() == pgm_read_word(&(hora2[i])) && fecha.minute() == pgm_read_word(&(minuto2[i])) && fecha.second() <= segundosTimbre - 1){
Timbre();
}}
}
} //AQUI FINALIZA EL LOOP
void Timbre(){
//Serial.println("TRIAC ENCENDIDO");
lcd.clear();
lcd.print("TIMBRE ON");
digitalWrite (triac, HIGH);
digitalWrite (led, HIGH);
delay(segundosTimbre*1000);
digitalWrite (triac, LOW);
digitalWrite (led, LOW);
//Serial.println("TRIAC APAGADO");
lcd.clear();
}
Comentarios
Publicar un comentario