/*
Smart Weather Monitoring System
Components: DHT22, BMP180, LCD1602 , Red LED, Green LED, Buzzer, Potentiometer
Board: Arduino Uno
*/
#include <LiquidCrystal.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085.h> // Also supports BMP180
// ---------- Pin Definitions ----------
#define DHTPIN 6
#define DHTTYPE DHT22
const int lcdRS = 12, lcdE = 11, lcdD4 = 10, lcdD5 = 9, lcdD6 = 8, lcdD7 = 7;
const int redLED = 5;
const int greenLED = 4;
const int buzzerPin = 3;
const int potPin = A0;
// ---------- Thresholds ----------
const float TEMP_HIGH = 35.0;
const float HUMIDITY_HIGH = 80.0;
// ---------- Objects ----------
LiquidCrystal lcd(lcdRS, lcdE, lcdD4, lcdD5, lcdD6, lcdD7);
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
unsigned long lastRead = 0;
const unsigned long interval = 2000;
void setup() {
Serial.begin(9600);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.begin(16, 2);
lcd.print("Weather Monitor");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
dht.begin();
if (!bmp.begin()) {
lcd.clear();
lcd.print("BMP180 error!");
Serial.println("Could not find BMP180 sensor, check wiring!");
while (1) {}
}
delay(1500);
lcd.clear();
}
void loop() {
if (millis() - lastRead >= interval) {
lastRead = millis();
float humidity = dht.readHumidity();
float tempDHT = dht.readTemperature();
float pressure = bmp.readPressure() / 100.0F;
float tempBMP = bmp.readTemperature();
int potValue = analogRead(potPin);
if (isnan(humidity) || isnan(tempDHT)) {
lcd.clear();
lcd.print("DHT read error");
Serial.println("Failed to read from DHT sensor!");
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(tempDHT, 1);
lcd.print("C H:");
lcd.print(humidity, 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(pressure, 0);
lcd.print("hPa");
Serial.print("DHT Temp: "); Serial.print(tempDHT);
Serial.print(" C, Humidity: "); Serial.print(humidity);
Serial.print(" %, BMP Temp: "); Serial.print(tempBMP);
Serial.print(" C, Pressure: "); Serial.print(pressure);
Serial.print(" hPa, Pot: "); Serial.println(potValue);
if (tempDHT >= TEMP_HIGH) {
digitalWrite(redLED, HIGH);
tone(buzzerPin, 1000);
} else {
digitalWrite(redLED, LOW);
noTone(buzzerPin);
}
if (humidity >= HUMIDITY_HIGH) {
digitalWrite(greenLED, HIGH);
} else {
digitalWrite(greenLED, LOW);
}
}
}