#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define REED_SWITCH_PIN 2 // Pin where the reed switch is connected
#define DHTPIN 3 // Pin where the DHT22 sensor is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
// Initialize the reed switch pin as an input
pinMode(REED_SWITCH_PIN, INPUT_PULLUP);
// Initialize the SSD1306 display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Pass 0x3C as the I2C address
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setTextSize(2); // Set the text size to 1
display.setTextColor(SSD1306_WHITE);
display.setFont(NULL); // Use the default 5x7 font
}
void loop() {
// Read the state of the reed switch
int reedState = digitalRead(REED_SWITCH_PIN);
// Clear the display buffer
display.clearDisplay();
if (reedState == HIGH) {
// Garage door is open
display.setCursor(0, 10);
display.println(" Garage open");
} else {
// Garage door is shut
// Read temperature and humidity from DHT22
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Display the temperature and humidity
display.setCursor(0, 10);
display.print("T: ");
display.print(t);
display.println(" C");
display.setCursor(0, 30);
display.print("H: ");
display.print(h);
display.println(" %");
}
// Update the display with the new information
display.display();
// Small delay to avoid rapid updates
delay(2000);
}