#include <LiquidCrystal.h>
#include <DHT.h>
#include <ESP32Servo.h>
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT sensor type (DHT11)
hw_timer_t * timer0 = NULL;
hw_timer_t * timer1 = NULL;
hw_timer_t * timer2 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
LiquidCrystal lcd(17, 5, 22, 21, 19, 18);
const int LedOn = 23;
const int botonON = 15; // Pin del botón 1
const int in1 = 0;
//in 2 conectado a tierra ya que nunca se prende.
int servo = 4;
int speed = 0; // velocidad actual del motor
const int maxSpeed = 255; // velocidad máxima para PWM
const int speedStep = 51; // incremento de velocidad
int s = 0;
int t=0;
int pos = 0;
const int H1 = 65;
const int H2 = 70;
int cont1 = 0;
int cont2 = 0;
void IRAM_ATTR onTimer0()
{
portENTER_CRITICAL_ISR(&timerMux0);
digitalWrite(LedOn, HIGH);
if (digitalRead(botonON) == HIGH)
{
s=0;
}
float temperature = dht.readTemperature(); // Read the temperature value in Celsius.
lcd.setCursor(8, 0);
lcd.print("T:");
lcd.setCursor(10, 0);
lcd.print(temperature);
lcd.setCursor(14, 0);
lcd.print(".C");
if (t==0)
{
digitalWrite(in1, LOW);
lcd.setCursor(0, 1);
lcd.print("MT OFF");
if (temperature >= 28) {
t=1;
}
}
if (t==1)
{
digitalWrite(in1, HIGH);
lcd.setCursor(0, 1);
lcd.print("MT ON ");
if (temperature <= 27) {
t=0;
}
}
portEXIT_CRITICAL_ISR(&timerMux0);
Serial.println("start timer 0");
}
void IRAM_ATTR onTimer1()
{
portENTER_CRITICAL_ISR(&timerMux1);
digitalWrite(LedOn, HIGH);
if (digitalRead(botonON) == HIGH)
{
s=0;
}
float humidity = dht.readHumidity(); // Read humidity value
lcd.setCursor(0, 0);
lcd.print("H:");
lcd.setCursor(2, 0);
lcd.print(humidity);
lcd.setCursor(6, 0);
lcd.print("%");
if (humidity < H1)
{
pos=0;
lcd.setCursor(7, 1);
lcd.print("SM 0% ");
}
else
{
if ((humidity < H2 ))
{
pos=50;
lcd.setCursor(7, 1);
lcd.print("SM 50% ");
}
else
{
pos=100;
lcd.setCursor(7, 1);
lcd.print("SM 100%");
}
myservo.write(pos);
}
portEXIT_CRITICAL_ISR(&timerMux1);
Serial.println("start timer 1");
}
void IRAM_ATTR onTimer2()
{
portENTER_CRITICAL_ISR(&timerMux2);
digitalWrite(LedOn, LOW);
if (digitalRead(botonON) == HIGH) {
s=1;
}
lcd.clear();
digitalWrite(in1, LOW);
pos=0;
myservo.write(pos);
portEXIT_CRITICAL_ISR(&timerMux2);
Serial.println("start timer 2");
}
void setup()
{
// Configurar pines
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(LedOn, OUTPUT);
pinMode(in1, OUTPUT);
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);
myservo.attach(servo, 500, 2400);
dht.begin(); // Initialize the DHT sensor.
timer0 = timerBegin(0, 80, true);
timerAttachInterrupt(timer0, &onTimer0, true);
timerAlarmWrite(timer0, 100000, true);
timer1 = timerBegin(1, 80, true);
timerAttachInterrupt(timer1, &onTimer1, true);
timerAlarmWrite(timer1, 100000, true);
timer2 = timerBegin(1, 80, true);
timerAttachInterrupt(timer2, &onTimer2, true);
timerAlarmWrite(timer2, 100000, true);
}
void loop()
{
vTaskDelay(portMAX_DELAY);
while (s == 0)
{
timerAlarmEnable(timer2);
}
while (s == 1)
{
timerAlarmEnable(timer0);
timerAlarmEnable(timer1);
}
}