#include <Wire.h>
#include <Adafruit_SSD1306.h> // library for OLED
#include <Adafruit_GFX.h> // library to provides a common syntax and set of graphics
functions
#include <DHT.h> // library for DHT sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//// what pin we're connected to
#define DHTPIN 26
#define DHTTYPE DHT11
#define BUZZER_PIN 12
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Declaration
for an SSD1306 display connected to I2C (SDA, SCL pins)
const float TEMP_THRESHOLD = 32.0; // Temperature threshold for alarm (in Celsius)
const float HUMIDITY_THRESHOLD = 73.0; // Humidity threshold for alarm (in
percentage)
void setup() {
Serial.begin(9600); //initialize the Serial debugging at a baud rate of 9600
dht.begin();
pinMode(BUZZER_PIN, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Temperature &");
display.println("Humidity");
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
// Read temperature and humidity values from the sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display temperature and humidity on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.print("Temperature: ");
display.print(temperature);
display.print("C");
display.setCursor(0, 30);
display.print("Humidity: ");
display.print(humidity);
display.print(" %");
display.display();
// Check if temperature or humidity exceeds the thresholds
if (temperature > TEMP_THRESHOLD || humidity > HUMIDITY_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH); // Sound the buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
}
delay(2000); // Delay between readings
}