#include "DHT.h" //inteface with DHT sensor
#include <Wire.h> //interface with l2C device
#include <LiquidCrystal_I2C.h> //control LCD display cia l2C protocol
#include <RTClib.h> // interface real-time clock
#include <Adafruit_GFX.h> // for graph
#include <Adafruit_SSD1306.h> // oled display
#define DHTPIN 4
#define DHTTYPE DHT22
#define REDLEDPIN 13 // Red LED pin
#define GREENLEDPIN 12 // Green LED pin
#define BUZZER_PIN 11 // Buzzer pin
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
const int numReadings = 128;
float temperatureReadings[numReadings];
int currentReadingIndex = 0;
void setup() {
Serial.begin(115200);
Serial.println("Start...");
dht.begin();
lcd.init();
lcd.backlight();
pinMode(REDLEDPIN, OUTPUT); // Set Red LED pin as output
pinMode(GREENLEDPIN, OUTPUT); // Set Green LED pin as output
pinMode(BUZZER_PIN, OUTPUT); // Set Buzzer pin as output
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Display "Welcome to Smart Home" on the LCD
lcd.setCursor(0, 0);
lcd.print("Welcome to");
lcd.setCursor(0, 1);
lcd.print("Smart Home");
delay(3000); // Display for 3 seconds
lcd.clear();
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
display.clearDisplay();
display.display();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Check for failed reads
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
DateTime now = rtc.now();
// Update readings array
temperatureReadings[currentReadingIndex] = t;
currentReadingIndex = (currentReadingIndex + 1) % numReadings;
// OLED display (Graph)
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temperature Graph");
drawGraph();
display.display();
// LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t);
lcd.print("C H:");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
lcd.print(' ');
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
// Print data to Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" ºC\t");
Serial.print("Fahrenheit: ");
Serial.print(f);
Serial.println(" F");
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.println(now.second());
// LED logic and buzzer
if (h > 70 || t > 30) {
digitalWrite(REDLEDPIN, HIGH); // Turn on the red LED
digitalWrite(GREENLEDPIN, LOW); // Turn off the green LED
tone(BUZZER_PIN, 1000); // Sound the buzzer
} else {
digitalWrite(REDLEDPIN, LOW); // Turn off the red LED
digitalWrite(GREENLEDPIN, HIGH); // Turn on the green LED
noTone(BUZZER_PIN); // Stop the buzzer
}
}
void drawGraph() {
int graphHeight = 40;
int graphWidth = SCREEN_WIDTH;
int graphY = SCREEN_HEIGHT - graphHeight - 1;
int maxValue = 40; // Max temperature value for the graph
int minValue = 0; // Min temperature value for the graph
for (int i = 0; i < numReadings - 1; i++) {
int x0 = i;
int y0 = graphY + graphHeight
- map(temperatureReadings[(currentReadingIndex + i) % numReadings],
minValue, maxValue, 0, graphHeight);
int x1 = i + 1;
int y1 = graphY + graphHeight
- map(temperatureReadings[(currentReadingIndex + i + 1) % numReadings],
minValue, maxValue, 0, graphHeight);
display.drawLine(x0, y0, x1, y1, SSD1306_WHITE);
}
}