/*
Primero que vamos a realizar es verificar las temperaturas
con el sensor de temperatura, posterior a esto verificamos la humedad
con el mismo sensor BME280
Luego escogemos que tipo de huevo se va a incubar
Teniedo los valores de la humedad y la presión en las variables
Verificamos los valores ideales
Luego conectamos el RTC cada 8 horas hace el movimiento del servo
*/
#include <RTClib.h> //Libreria RTC
#include <ESP32Servo.h> //Libreria servo
#include <Keypad.h> //Libreria teclado matricial
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
RTC_DS3231 rtc;
Servo miServo;
#define SEALEVELPRESSURE_HPA (1013.25)
float Temperatura, Humedad, Presion;
float TempIdeal1, HumIdeal1;
float TempIdeal2, HumIdeal2;
//Teclado
const byte FILAS = 4; // Número de filas en el teclado matricial
const byte COLUMNAS = 4; // Número de columnas en el teclado matricial
char teclas[FILAS][COLUMNAS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte filasPines[FILAS] = {42, 41, 40, 39}; // Pines de las filas
byte columnasPines[COLUMNAS] = {38, 37, 36, 35}; // Pines de las columnas
Keypad teclado = Keypad(makeKeymap(teclas), filasPines, columnasPines, FILAS, COLUMNAS);
////////////////////////////////////////////////
Adafruit_BME280 bme;
void setup() {
Serial.begin(9600);
myservo.attach(8); // attaches the servo on pin 8 to the servo object
miServo.write(45);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
if (!rtc.begin()) {
Serial.println("RTC no encontrado");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(2023,11,17,21,27,0 )); //Año. mes.dia.hora.min.seg
}
}
void loop() {
char tecla = teclado.getKey();
bme.read(Presion, Temperatura, Humedad);
DateTime now= rtc.now(); //Obtener hora
if (tecla) {
switch (tecla) {
case '1': //Gallina
// Acción para la tecla 1
Serial.println("Gallina");
TempIdeal1=37;
HumIdeal1=40;
TempIdeal2=38;
HumIdeal2=50;
break;
case '2':
// Acción para la tecla 2
Serial.println("Presionaste la tecla 2");
break;
case '3':
// Acción para la tecla 3
Serial.println("Presionaste la tecla 3");
break;
case '4':
// Acción para la tecla 4
Serial.println("Presionaste la tecla 4");
break;
case '5':
// Acción para la tecla 5
Serial.println("Presionaste la tecla 5");
break;
// Agrega más casos según sea necesario para las otras teclas
default:
// Acción por defecto
Serial.println("Tecla no reconocida");
break;
}
}
//Se mira temperatura
if(Temperatura<TempIdeal1){
//Prender bombillo
}else if(Temperatura>TempIdeal2){
//Prender ventilador techo y apagar bombillo
}
//Se mira humedad
if(Humedad<HumIdeal1){
//Prender humidificador
}else if(Humedad>HumIdeal2){
//Prender ventilador techo y apagar bombillo
}
//Se obtiene el tiempo que tardará la incubación en el dia debe activar el servo 3 veces
DateTime activationTime= now + TimeSpan(8*60*60)// Se añaden 8 horas
if(now>=activationTime){
ActivarServo();
}
Serial.print("Temperatura = ");
Serial.print(Temperatura);
Serial.println("*C");
Serial.print("Presión = ");
Serial.print(Presion / 100.0F);
Serial.println("hPa");
Serial.print("Humedad = ");
Serial.print(Humedad);
Serial.println("%");
Serial.println();
delay(1000);
}
void ActivarServo(){
miServo.write(90);
delay(5000);
miServo.write(0);
delay(5000);
miServo.write(45);
}
/*
void incubargallina{
// temperaura 37.7 °c menos los ultimos 3 dias donde baja a 33
// humedad entre 40 50% ultimos 3 dias 65%
// tiempo total de incubacion 21 dias
TempIdeal1=37;
HumIdeal1=40;
TempIdeal2=38;
HumIdeal2=50;
}
void incubarcodorniz{
//temperaura 37.5 y 39 °c menos los ultimos 3 dias donde baja 2 grados
// humedad 50 60% ultimos 3dias debe estar entre 70 80
// tiempo total de incubacion 16 dias
//se debe dar vuelta cada 6 o 7 horas
TempIdeal1=37.5;
HumIdeal1=50;
TempIdeal2=39;
HumIdeal2=60;
}
void incubaravestruz{
//temperaura 36.4 y
// humedad 25%
// tiempo total de incubacion 42 dias
serial.print("Se recomienda que la posición de los huevos tenga unos 45º de inclinación y la punta más redondeada sea la superior")
TempIdeal1=37.7;
HumIdeal1=25;
TempIdeal2=37.7;
HumIdeal2=26;
}
void incubarpato{
//temperaura 37.7 °c menos los ultimos 3 dias donde baja a 36.5 grados
// humedad 50 55% ultimos 3dias debe estar entre 70 85
// tiempo total de incubacion 28 dias
TempIdeal1=37.7;
HumIdeal1=50;
TempIdeal2=38;
HumIdeal2=55;
}
void incubarpavo{
//temperaura 37.5 -38 °c menos los ultimos 3 dias donde baja a 36.5 grados
// humedad 55% ultimos 3dias debe estar entre 75
// tiempo total de incubacion 28 dias
//se debe dar vuelta cada 6 o 7 horas menos los ultimos 3 dias
TempIdeal1=37.5;
HumIdeal1=55;
TempIdeal2=38;
HumIdeal2=60;
}*/