#include <Wire.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// =======================
// DHT22
// =======================
#define DHT_PIN 4
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// =======================
// LDR
// =======================
#define LDR_PIN 34
// =======================
// Relay
// =======================
#define RELAY_PIN 5
// =======================
// LEDs
// =======================
#define GREEN_LED 18
#define BLUE_LED 19
#define RED_LED 23
// =======================
// Buzzer
// =======================
#define BUZZER_PIN 32
// =======================
// OLED
// =======================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_ADDRESS 0x3C
Adafruit_SSD1306 display(
SCREEN_WIDTH,
SCREEN_HEIGHT,
&Wire,
-1
);
// =======================
// EcoSense settings
// =======================
float HIGH_TEMP = 30.0;
float LIGHT_THRESHOLD = 5000.0;
// =======================
// Setup
// =======================
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(
SSD1306_SWITCHCAPVCC,
OLED_ADDRESS
)) {
Serial.println("OLED ERROR");
while (true) {
delay(100);
}
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(15, 10);
display.println("EcoSense");
display.setTextSize(1);
display.setCursor(25, 40);
display.println("Starting...");
display.display();
delay(2000);
// CSV header: column names
Serial.println(
"Time_ms,Temperature_C,Humidity_pct,"
"LightRaw,LightLevel_approx,Fan,Alert,LightState"
);
}
// =======================
// Loop
// =======================
void loop() {
// Read sensors
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int lightRaw = analogRead(LDR_PIN);
// Approximate light index
float lightLevel = map(
lightRaw,
0,
4095,
10000,
0
);
// Check DHT22
if (isnan(temperature) || isnan(humidity)) {
Serial.println("ERROR: DHT22 reading failed");
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
delay(2000);
return;
}
// =======================
// Decisions
// =======================
bool highTemperature = temperature >= HIGH_TEMP;
bool lightON = lightLevel >= LIGHT_THRESHOLD;
bool fanON = highTemperature;
bool alert = highTemperature || lightON;
// =======================
// Relay
// =======================
digitalWrite(RELAY_PIN, fanON ? HIGH : LOW);
// =======================
// LEDs
// =======================
digitalWrite(RED_LED, alert ? HIGH : LOW);
digitalWrite(GREEN_LED, alert ? LOW : HIGH);
digitalWrite(BLUE_LED, lightON ? HIGH : LOW);
// =======================
// Buzzer
// =======================
if (alert) {
tone(BUZZER_PIN, 1000);
} else {
noTone(BUZZER_PIN);
}
// =======================
// OLED display
// =======================
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("EcoSense");
display.setCursor(0, 14);
display.print("Temp: ");
display.print(temperature, 1);
display.println(" C");
display.setCursor(0, 27);
display.print("Humidity: ");
display.print(humidity, 1);
display.println("%");
display.setCursor(0, 40);
display.print("Light: ");
display.print(lightLevel, 0);
display.setCursor(0, 53);
display.print("State: ");
display.print(lightON ? "LIGHT" : "DARK");
display.display();
// =======================
// CSV data logging
// One row every 2 seconds
// =======================
Serial.print(millis());
Serial.print(",");
Serial.print(temperature, 2);
Serial.print(",");
Serial.print(humidity, 2);
Serial.print(",");
Serial.print(lightRaw);
Serial.print(",");
Serial.print(lightLevel, 2);
Serial.print(",");
Serial.print(fanON ? "ON" : "OFF");
Serial.print(",");
Serial.print(alert ? "YES" : "NO");
Serial.print(",");
Serial.println(lightON ? "LIGHT" : "DARK");
delay(2000);
}Loading
ssd1306
ssd1306