#include <AdafruitSensor.h>
#include <DHT.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define DHTPIN 4 // Pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // Change to DHT11 if you're using a DHT11 sensor
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int greenLedPin = 23; // GPIO23 for the green LED
const int redLedPin = 19; // GPIO19 for the red LED
void setup() {
// Initialize DHT sensor
dht.begin();
// Initialize the display
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
// Initialize LEDs
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}
void loop() {
// Read temperature and humidity from DHT22
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Display data on the OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.print(" C");
display.setCursor(0, 10);
display.print("Humidity: ");
display.print(humidity);
display.print(" %");
display.display();
// Control LEDs based on temperature
if (temperature > 30.0) {
digitalWrite(greenLedPin, HIGH); // Turn on green LED
digitalWrite(redLedPin, LOW); // Turn off red LED
} else {
digitalWrite(greenLedPin, LOW); // Turn off green LED
digitalWrite(redLedPin, HIGH); // Turn on red LED
}
delay(2000); // Update every 2 seconds
}