#include <DHT.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// Definimos el pin digital donde se conecta el sensor DHT11
#define DHTPIN 2
// Dependiendo del tipo de sensor DHT que estés utilizando OJO ACA¡¡¡¡
#define DHTTYPE DHT22
//DEFINIENDO LOS PULSADORES
int button1= 5;
int button2= 6;
int button3= 7;
//DEFINIENDO PARA EL TFT
#define _sclk 13
#define _miso 12
#define _mosi 11
#define _cs 10
#define _dc 9
#define _rst 8
//CREAMOS EL OBJETO tft
Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _rst);
//CREAMOS EL OBJETO DHT QUE ACEPTA EL PIN Y EL TIPO
// Inicializamos el sensor DHT11
DHT dht(DHTPIN, DHTTYPE);
// Definimos el pin analógico donde se conecta el sensor de calidad del aire
#define ANALOG_PIN A0
void setup() {
// Inicializamos comunicación serie
Serial.begin(9600);
// Comenzamos el TFT
tft.begin();
tft.setRotation(1);
testText();
// Comenzamos el sensor DHT
dht.begin();
//DEFINIMOS COMO ENTRADA AL BOTON
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
}
void loop() {
//DEFINIREMOS VARIABLES QUE LEAN LOS BOTONES
bool estado1 = digitalRead(button1);
bool estado2 = digitalRead(button2);
bool estado3 = digitalRead(button3);
delay(10); //ANTIREBOTES
if(estado1==HIGH){
// Leemos la lectura analógica del sensor de calidad del aire
int sensorValue = analogRead(ANALOG_PIN);
// Convertimos la lectura analógica a voltaje
float voltage = sensorValue * (5.0 / 1023.0);
// Calculamos la resistencia del sensor en el aire
float sensorResistance = (5.0 - voltage) / voltage;
// Calculamos la calidad del aire basada en la resistencia del sensor
int airQuality = map(sensorResistance, 3.3, 30, 0, 100);
// Mostramos la calidad del aire
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 20);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(4);
tft.print("Calidad del aire: ");
tft.setTextColor(ILI9341_GREEN);
tft.println(airQuality);
}
delay(10); //ANTIREBOTES
if (estado2==HIGH){ //SI SE PRESIONA EL BOTON ENTONCES
// Leemos la humedad y la temperatura
float Humedity = dht.readHumidity();
float Temc = dht.readTemperature();
// Comprobamos si no ha habido algún error en la lectura del sensor DHT11
if (!isnan(Temc) || !isnan(Humedity)) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 20);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(4);
tft.println("Humedad: "+String(Humedity,1)+"% ");
//Serial.println(Humedity);
tft.setTextColor(ILI9341_GREEN);
tft.println("Temperature: "+String(Temc,1)+"C");
tft.setTextColor(ILI9341_WHITE);
if (Temc > 20) {
tft.println("CALOR");
} else if (Temc <= 20 && Temc > 12) {
tft.println("TEMPLADO");
} else {
tft.println("FRIO");
}
} else {
Serial.println("Error obteniendo los datos del sensor DHT11");
return;
}
} if(estado3==HIGH){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 20);
tft.setTextColor(ILI9341_NAVY);
tft.setTextSize(4);
tft.print("PROYECTO 1 - IOT- GRUPO 5 ");
tft.setTextColor(ILI9341_DARKGREEN);
tft.println("Bienvenidos a nuestro proyecto");
}
// Esperamos 1 segundos entre medidas
delay(10);
}
unsigned long testText() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 20);
tft.setTextColor(ILI9341_NAVY);
tft.setTextSize(4);
tft.print("PROYECTO 1 - IOT- GRUPO 5 ");
tft.setTextColor(ILI9341_DARKGREEN);
tft.println("Bienvenidos a nuestro proyecto");
}