#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <DHT.h>
#include "RTClib.h"
// Pin definitions for ESP32
#define TFT_DC 2
#define TFT_CS 15
#define TFT_RST 4
#define DHT_SENSOR_PIN 32
#define DHT_SENSOR_TYPE DHT22
#define BUZZ_PIN 5
#define POTPIN 25
// Initialize the display using the defined pins
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
RTC_DS1307 rtc;
int buzzLimit = 100;
int potRead = 0;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
tft.setTextSize(3);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
potRead = analogRead(POTPIN);
buzzLimit = map(potRead, 0, 4098, 0, 80);
float humi = dht_sensor.readHumidity(); // read humidity
float tempC = dht_sensor.readTemperature(); // read temperature
DateTime now = rtc.now();
if (isnan(tempC) || isnan(humi)) {
Serial.println("Failed to read DHT22");
}
else {
if(buzzLimit < tempC) tone(BUZZ_PIN, 262, 250);
tft.setCursor(0, 0);
tft.print("Temp: ");
tft.print(tempC); // display the temperature
tft.println("C");
tft.print("Humi: ");
tft.print(humi); // display the humidity
tft.println("%");
tft.print("Time: ");
tft.print(now.hour(), DEC);
tft.print(':');
tft.println(now.minute(), DEC);
tft.print("Buzz limit: ");
tft.print(buzzLimit);
delay(100);
}
}