#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define pin numbers for LEDs, buzzer, DHT22, and OLED reset
const int ledPin1 = 12; // GPIO 12
const int ledPin2 = 13; // GPIO 13
const int buzzerPin = 14; // GPIO 14
const int dhtPin = 15; // GPIO 15
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin (or -1 if sharing Arduino reset pin)
// Define DHT type
#define DHTTYPE DHT22
// Initialize DHT sensor
DHT dht(dhtPin, DHTTYPE);
// Initialize RTC
RTC_DS3231 rtc;
// Initialize OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the LED pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize the buzzer pin as an output
pinMode(buzzerPin, OUTPUT);
// Start with everything turned off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(buzzerPin, LOW);
// Initialize DHT sensor
dht.begin();
// Initialize RTC and check if it is connected
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
// Set the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
}
void loop() {
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Get current date and time
DateTime now = rtc.now();
// Print temperature, humidity, date, and time to Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C\t");
Serial.print("Date/Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Display temperature, humidity, date, and time on OLED
display.clearDisplay();
display.setCursor(0, 0); // Start at top-left corner
display.print("Temp: ");
display.print(temperature);
display.println(" *C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.print("Date: ");
display.print(now.year(), DEC);
display.print('/');
display.print(now.month(), DEC);
display.print('/');
display.print(now.day(), DEC);
display.println();
display.print("Time: ");
display.print(now.hour(), DEC);
display.print(':');
display.print(now.minute(), DEC);
display.print(':');
display.print(now.second(), DEC);
display.display(); // Update the display with new data
// Turn on LED 1
digitalWrite(ledPin1, HIGH);
delay(1000); // Wait for 1 second
// Turn off LED 1
digitalWrite(ledPin1, LOW);
// Turn on LED 2
digitalWrite(ledPin2, HIGH);
delay(1000); // Wait for 1 second
// Turn off LED 2
digitalWrite(ledPin2, LOW);
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
delay(1000); // Wait for 1 second
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
delay(1000); // Wait for 1 second
}