/*
______________________________________________________________________________
| Smart Appliance Manager |
|-> This device utilizes DHT22 sensor to measure temperature and LDR sensor |
| for monitoring light intensity in a room. |
|-> It adjusts the brightness of an LED depending on ambient light intensity.|
|-> It controls a fan and an air conditioner (AC) based on the temparature. |
|-> The current temparature, LED Brightness, fan and AC status are displayed |
| on a 20x4 I2C LCD screen. |
|-> Further, the fan and AC status are indicated using an LED. |
|-> Intgration with node red for remote monitoring all stats. |
|____________________________________________________________________________|
*/
#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <WiFi.h>
#include <PubSubClient.h>
LiquidCrystal lcd(0, 1, 2, 3, 4, 5); // Initialize LCD object
const int dhtPin = 8;
#define dhtType DHT22
DHT dht(dhtPin, dhtType); // Initialize DHT sensor object
const int ledPin = 9; // LED pin
const int ldrPin = 28; // LDR pin
const int fanRelayPin = 22; // Fan relay pin
const int acPin = 17; // AC relay pin
int ledBrightness = 0;
bool isFanOn = false;
bool isACOn = false;
// WiFi setup
const char* ssid = "OnePlus 11R 5G"; // Setting SSID
const char* password = "1234567890"; // Setting Password
// MQTT Credentials
const char* mqttServer = "broker.hivemq.com";
const char* clientID = "pi-Wokwi";
const char* topic1 = "TempData";
const char* topic2 = "FAN";
const char* topic3 = "AC";
const char* topic4 = "Brightness";
// Setting up WiFi and MQTT client
WiFiClient piClient;
PubSubClient client(piClient);
void setup() {
Serial1.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
pinMode(fanRelayPin, OUTPUT);
pinMode(acPin, OUTPUT);
setup_wifi();
client.setServer(mqttServer, 1883); // Setting MQTT server
dht.begin();
lcd.begin(20, 4);
lcd.setCursor(2, 2);
lcd.print("LED");
lcd.setCursor(8, 2);
lcd.print("FAN");
lcd.setCursor(14, 2);
lcd.print("AC");
}
// Connect to the Wi-Fi network using the provided credentials
void setup_wifi() {
delay(10);
Serial1.print("Connecting to WiFi");
WiFi.begin(ssid, password); //Initialize WiFi
while (WiFi.status() != WL_CONNECTED) {
Serial1.println(".");
delay(500);
}
Serial1.println("WiFi connected");
}
void loop() {
nodeRed();
readSensorsAndPublishData();
controlDevices();
displayStatus();
delay(1);
}
// Attempt to reconnect to the MQTT broker if the connection is lost
void reconnect() {
Serial1.print("Attempting MQTT connection...");
if (client.connect("PiClient")) {
Serial1.println("connected");
client.subscribe(topic1);
client.subscribe(topic2);
client.subscribe(topic3);
client.subscribe(topic4);
}
else {
Serial1.print("failed, rc=");
Serial1.print(client.state());
Serial1.println(" trying again in 5 seconds");
delay(5000);
}
}
void nodeRed(){
// Check MQTT connection and process incoming messages
if (!client.connected()) {
reconnect();
}
client.loop();
delay(1000); // Add a delay to avoid flooding the MQTT broker with messages
}
void readSensorsAndPublishData() {
// Reading LDR value
int ldrValue = analogRead(ldrPin);
ledBrightness = map(ldrValue, 170, 1023, 0, 255);
int ledPercentage = map(ledBrightness, 0, 255, 0, 100);
if (ledPercentage<0){
ledPercentage=0;
}
// Reading temperature sensor value
int tempValue = dht.readTemperature();
lcd.setCursor(2,3);
lcd.print(ledPercentage);
lcd.print(" %");
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("TEMP : ");
lcd.print(tempValue);
lcd.print(" C");
// Controlling fan and AC based on temperature
if (tempValue >= 30) {
isFanOn = false;
isACOn = true;
} else if (tempValue < 30 && tempValue >= 20) {
isFanOn = true;
isACOn = false;
} else if (tempValue < 20) {
isFanOn = false;
isACOn = false;
}
// Publishing data to node red
String tempStr = String(tempValue);
String LEDStr = String(ledPercentage);
String FANStr = String(isFanOn ? "ON" : "OFF");
String ACStr = String(isACOn ? "ON" : "OFF");
client.publish(topic1, tempStr.c_str());
client.publish(topic2, FANStr.c_str());
client.publish(topic3, ACStr.c_str());
client.publish(topic4, LEDStr.c_str());
}
// Function to control devices based on sensor readings
void controlDevices() {
analogWrite(ledPin, ledBrightness);
digitalWrite(fanRelayPin, isFanOn ? HIGH : LOW);
digitalWrite(acPin, isACOn ? HIGH : LOW);
}
// Function to display device status on LCD
void displayStatus() {
// Displaying status on LCD
lcd.setCursor(8, 3);
lcd.print(isFanOn ? "ON " : "OFF");
lcd.setCursor(14, 3);
lcd.print(isACOn ? "ON " : "OFF");
}