#include "DHT.h" #include #include #define DHTPIN 4 // Pin al que se conecta la salida del arduino. #define DHTTYPE DHT22 // Tipo de sensor que vamos usar (DHT 22). Recordar poner rsistencia entre salida y +Vcc #define RxD 10 #define TxD 11 const int ledStatus = 13; int ledStatus1 = 12; //Luz const int pin = 8; //Ventilador float h = 0; float t = 0; float hic = 0; const float thresholdLOW = 25.0; const float thresholdHIGH = 30.0; int intervaloMedidas = 1000; int auxMillis = 0; SoftwareSerial nextion(RxD, TxD); Nextion myNextion(nextion, 9600); DHT dht(DHTPIN, DHTTYPE); // Inicializa el sensor. bool state = 0; // ventilador activo o inactivo float GetTemperature() { return 20.0; //sustituir en funciĆ³n del sensor empleado } void setup() { digitalWrite(ledStatus, LOW); pinMode(ledStatus1, OUTPUT); pinMode(pin, OUTPUT); //definir pin como salida digitalWrite(ledStatus1, LOW); digitalWrite(pin, LOW); Serial.begin(9600); // Puerto serial a 9600bps. Serial.println("DHT22 test!"); // Inprimimos dht.begin(); // Iniciamos sensor. myNextion.init(); imprimeNextion(); myNextion.sendCommand("page 0"); myNextion.setComponentText("page0.t2", "SMS ALARMAS"); } void loop() { int botonMarcha = myNextion.getComponentValue("page0.bt2"); if (botonMarcha == true ) digitalWrite(ledStatus1, HIGH); else digitalWrite(ledStatus1, LOW); if ( millis() - auxMillis > intervaloMedidas) { // delay(1000); // Tiempo entre mediciones. h = dht.readHumidity(); // Leemos Humedad. t = dht.readTemperature(); // Leemos temperatura en grados celsuis. imprimeNextion(); float currentTemperature = GetTemperature(); if (state == 0 && currentTemperature > thresholdHIGH) { state = 1; digitalWrite(pin, HIGH); // encender ventilador } if (state == 1 && currentTemperature < thresholdLOW) { state == 0; digitalWrite(pin, LOW); // apagar ventilador } delay(4000); // esperar 4 segundos entre medicioens } } void imprimeNextion () { int auxhumedad = int(h); String temperatura = String(t); String humedad = String (auxhumedad); //myNextion.setComponentValue("page1.z0",presionAnalogicaNextion); myNextion.setComponentText("page0.t1", temperatura); myNextion.setComponentText("page0.t5", humedad); } void imprimeserial () { if (isnan(h) || isnan(t)) { // verifica fallo de lectura y si existe, inicia una nueva lectura sin esperar. Serial.println("Fallo leyendo datos del sensor DHT!"); return; } hic = dht.computeHeatIndex(t, h, false); // Calcula la sensacion de frio. Serial.print("Humedad: "); // Maquetamos datos de salida por el monitor serial. Serial.print(h); Serial.print(" %\t"); Serial.print("Temperatura: "); Serial.print(t); Serial.print(" *C "); Serial.print("Heat index: "); Serial.print(hic); Serial.println(" *C "); }