#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN 14 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor type
DHT dht(DHTPIN, DHTTYPE);
#define TRIGGER_PIN 15 // Pin for the ultrasound motion sensor
#define LED_LIGHTS 5 // Pin for the lights LED
#define LED_AC 18 // Pin for the AC LED
#define LIGHTS_SWITCH_PIN 26 // Pin for the lights switch
#define AC_SWITCH_PIN 25 // Pin for the AC switch
#define POTENTIOMETER_PIN 34 // Pin for the potentiometer
// OLED configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
bool motionDetected = false;
bool lightsOn = false;
bool acOn = false;
float thermostatSetting = 0.0;
float temperature = 0.0;
void setup() {
Serial.begin(115200);
// Initialize OLED display with default I2C address (0x3D)
if(!display.begin()) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
pinMode(TRIGGER_PIN, INPUT);
pinMode(LED_LIGHTS, OUTPUT);
pinMode(LED_AC, OUTPUT);
pinMode(LIGHTS_SWITCH_PIN, INPUT_PULLUP);
pinMode(AC_SWITCH_PIN, INPUT_PULLUP);
}
void loop() {
// Read temperature and humidity from DHT sensor
temperature = dht.readTemperature();
// Read motion status from ultrasound sensor
motionDetected = digitalRead(TRIGGER_PIN) == HIGH;
// Read potentiometer value for thermostat setting
int potValue = analogRead(POTENTIOMETER_PIN);
thermostatSetting = map(potValue, 0, 4095, 16.0, 30.0);
// Read the state of the lights and AC switches
bool lightsSwitchState = digitalRead(LIGHTS_SWITCH_PIN) == LOW;
bool acSwitchState = digitalRead(AC_SWITCH_PIN) == LOW;
// Display information on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temperature: ");
display.println(temperature);
display.print("AC: ");
display.print(acOn || acSwitchState ? "On" : "Off");
display.print(" Thermostat: ");
display.print(thermostatSetting);
display.println("C");
display.print("Lights: ");
display.print(lightsOn || lightsSwitchState ? "On" : "Off");
display.print(" Motion: ");
display.println(motionDetected ? "Detected" : "Not Detected");
display.display();
// Check and control AC and lights
controlAC(temperature, acSwitchState);
controlLights(motionDetected, lightsSwitchState);
delay(1000); // Update every second
}
void controlAC(float temperature, bool acSwitchState) {
// Simulate AC control using an LED
if ((temperature > thermostatSetting || acSwitchState) && !acOn) {
acOn = true;
digitalWrite(LED_AC, HIGH); // Turn on AC LED
} else if ((temperature <= thermostatSetting && !acSwitchState) && acOn) {
acOn = false;
digitalWrite(LED_AC, LOW); // Turn off AC LED
}
}
void controlLights(bool motionDet, bool lightsSwitchState) {
// Simulate lights control using an LED
if ((motionDet || lightsSwitchState) && !lightsOn) {
lightsOn = true;
digitalWrite(LED_LIGHTS, HIGH); // Turn on lights LED
} else if ((!motionDet && !lightsSwitchState) && lightsOn) {
lightsOn = false;
digitalWrite(LED_LIGHTS, LOW); // Turn off lights LED
}
}