We are going to make smart home appliances control and automatic stair case light and auto door open/close system.
1. Smart Home Appliances control
We are using relay and esp32, It has built-in touch pin and digital pin. So let's get started. For to control each home appliances, it's need one relay. Like 8 home appliances, we need 8 relay. Using google firebase and MQTT we made this whole product. For fast switching use MQTT and for live status use Google firebase.
Android application (This is just prototype, For each and every user it's need to be new account) : https://github.com/ddmakadia1699/HomeAutomation
Code Explanation :
Library
#include <WiFi.h>
#include <PubSubClient.h>
#include <IOXhop_FirebaseESP32.h>
Define relay pin
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
Define touch pin
#define Touch1 T0 //4
#define Touch2 T3 //15
#define Touch3 T4 //13
#define Touch4 T5 //12
#define Touch5 T6 //14
#define Touch6 T7 //27
#define Touch7 T8 //33
#define Touch8 T9 //32
Google firebase credentials
#define FIREBASE_HOST "home-automation-eda63.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "7UmOVKl30EHP1sJiBulS900j3Y1mosOMc4Pf95fX" //Your Firebase Database Secret goes here
Google firebase value where we can store data
int val1,val2,val3,val4,val5,val6,val7,val8;
Touch pin gives analog values so it will store it into this integer values (we are using 8 touch pin)
int touch11, touch22, touch33, touch44, touch55, touch66, touch77, touch88;
For doing two way operation (From touch pin and smart phone application)
boolean a = true;
boolean b = true;
boolean c = true;
boolean d = true;
boolean e = true;
boolean f = true;
boolean g = true;
boolean h = true;
Wi-Fi credentials and MQTT credentials
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
//Enter your mqtt server configurations
const char* mqttServer = "soldier.cloudmqtt.com"; //Enter Your mqttServer address
const int mqttPort = 18166; //Port number
const char* mqttUser = "dtsghhrn"; //User
const char* mqttPassword = "ycDpcGIFTmK4"; //Password
WiFiClient espClient;
PubSubClient client(espClient);
First time setup, Define pin modes, Establish connection between module and internet source, Establish connection for MQTT and send test message and subscribe topic which is use for controlling home appliances (here esp/test) and last begin google firebase.
void setup()
{
Serial.begin(115200);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
pinMode(LED6,OUTPUT);
pinMode(LED7,OUTPUT);
pinMode(LED8,OUTPUT);
/* //no need to define
pinMode(Touch1, INPUT);
pinMode(Touch2, INPUT);
pinMode(Touch3, INPUT);
pinMode(Touch4, INPUT);
pinMode(Touch5, INPUT);
pinMode(Touch6, INPUT);
pinMode(Touch7, INPUT);
pinMode(Touch8, INPUT);
*/
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.print("Connected to WiFi :");
Serial.println(WiFi.SSID());
client.setServer(mqttServer, mqttPort);
client.setCallback(MQTTcallback);
while (!client.connected())
{
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266", mqttUser, mqttPassword ))
{
Serial.println("connected");
}
else
{
Serial.print("failed with state ");
Serial.println(client.state()); //If you get state 5: mismatch in config
delay(2000);
}
}
client.publish("esp/test", "Hello From Wemos D1");
client.subscribe("esp/test");
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
}
If any error in connecting with firebase than this loop will excute.
void firebasereconnect()
{
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Serial.println("Trying to reconnect");
ESP.restart();
delay(1000);
/*
WiFi.forceSleepBegin();
wdt_reset();
while(1)
wdt_reset();
*/
}
MQTT loop which is send data to server and whatever received take action on that.
void MQTTcallback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
String message;
for (int i = 0; i < length; i++)
{
message = message + (char)payload[i]; //Conver *byte to String
}
Serial.print(message);
if(message == "#relay1on")
{
digitalWrite(LED1,LOW);
}
if(message == "#relay1off")
{
digitalWrite(LED1,HIGH);
}
if(message == "#relay2on")
{
digitalWrite(LED2,LOW);
}
if(message == "#relay2off")
{
digitalWrite(LED2,HIGH);
}
if(message == "#relay3on")
{
digitalWrite(LED3,LOW);
}
if(message == "#relay3off")
{
digitalWrite(LED3,HIGH);
}
if(message == "#relay4on")
{
digitalWrite(LED4,LOW);
}
if(message == "#relay4off")
{
digitalWrite(LED4,HIGH);
}
if(message == "#relay5on")
{
digitalWrite(LED5,LOW);
}
if(message == "#relay5off")
{
digitalWrite(LED5,HIGH);
}
if(message == "#relay6on")
{
digitalWrite(LED6,LOW);
}
if(message == "#relay6off")
{
digitalWrite(LED6,HIGH);
}
if(message == "#relay7on")
{
digitalWrite(LED7,LOW);
}
if(message == "#relay7off")
{
digitalWrite(LED7,HIGH);
}
if(message == "#relay8on")
{
digitalWrite(LED8,LOW);
}
if(message == "#relay8off")
{
digitalWrite(LED8,HIGH);
}
Serial.println();
Serial.println("-----------------------");
}
Main loop which is going on infinite times. Here it will read touch pin and as per condition it will work. So as per condition it will turn on and turn off home appliances. This also send real time live status to google firebase to know about home appliances live status on smart phone. This is for 8 relay and touch pin.
void loop()
{
if (Firebase.failed())
{
Serial.print("setting number failed:");
Serial.println(Firebase.error());
setup();
firebasereconnect();
return;
}
int touch11 = touchRead(Touch1);
Serial.println(touch11);
if(touch11 < 10)
{
a = !a;
if(a == false)
{
digitalWrite(LED1,HIGH);
Serial.println("light 1 OFF from touch sensor");
client.publish("esp/test", "#relay1off");
Firebase.setString("S1","D1 Off");
delay(1000);
}
else if (a==true)
{
digitalWrite(LED1,LOW);
Serial.println("light 1 ON from touch sensor");
client.publish("esp/test", "#relay1on");
Firebase.setString("S1","D1 On");
delay(1000);
}
}
int touch22 = touchRead(Touch2);
Serial.println(touch22);
if(touch22 < 10)
{
b = !b;
if(b == false)
{
digitalWrite(LED2,HIGH);
Serial.println("light 2 OFF from touch sensor");
client.publish("esp/test", "#relay2off");
Firebase.setString("S2","D2 Off");
delay(1000);
}
else if (b==true)
{
digitalWrite(LED2,LOW);
Serial.println("light 2 ON from touch sensor");
client.publish("esp/test", "#relay2on");
Firebase.setString("S2","D2 On");
delay(1000);
}
}
int touch33 = touchRead(Touch3);
Serial.println(touch33);
if(touch33 < 10)
{
c = !c;
if(c == false)
{
digitalWrite(LED3,HIGH);
Serial.println("light 3 OFF from touch sensor");
client.publish("esp/test", "#relay3off");
Firebase.setString("S3","D3 Off");
delay(1000);
}
else if (c==true)
{
digitalWrite(LED3,LOW);
Serial.println("light 3 ON from touch sensor");
client.publish("esp/test", "#relay3on");
Firebase.setString("S3","D3 On");
delay(1000);
}
}
int touch44 = touchRead(Touch4);
Serial.println(touch44);
if(touch44 < 10)
{
d = !d;
if(d == false)
{
digitalWrite(LED4,HIGH);
Serial.println("light 4 OFF from touch sensor");
client.publish("esp/test", "#relay4off");
Firebase.setString("S4","D4 Off");
delay(1000);
}
else if (d==true)
{
digitalWrite(LED4,LOW);
Serial.println("light 4 ON from touch sensor");
client.publish("esp/test", "#relay4on");
Firebase.setString("S4","D4 On");
delay(1000);
}
}
int touch55 = touchRead(Touch5);
Serial.println(touch55);
if(touch55 < 10)
{
e = !e;
if(e == false)
{
digitalWrite(LED5,HIGH);
Serial.println("light 5 OFF from touch sensor");
client.publish("esp/test", "#relay5off");
Firebase.setString("S5","D5 Off");
delay(1000);
}
else if (e==true)
{
digitalWrite(LED5,LOW);
Serial.println("light 5 ON from touch sensor");
client.publish("esp/test", "#relay5on");
Firebase.setString("S5","D5 On");
delay(1000);
}
}
int touch66 = touchRead(Touch6);
Serial.println(touch66);
if(touch66 < 10)
{
f = !f;
if(f == false)
{
digitalWrite(LED6,HIGH);
Serial.println("light 6 OFF from touch sensor");
client.publish("esp/test", "#relay6off");
Firebase.setString("S6","D6 Off");
delay(1000);
}
else if (f==true)
{
digitalWrite(LED6,LOW);
Serial.println("light 6 ON from touch sensor");
client.publish("esp/test", "#relay6on");
Firebase.setString("S6","D6 On");
delay(1000);
}
}
int touch77 = touchRead(Touch7);
Serial.println(touch77);
if(touch77 < 10)
{
g = !g;
if(g == false)
{
digitalWrite(LED7,HIGH);
Serial.println("light 7 OFF from touch sensor");
client.publish("esp/test", "#relay7off");
Firebase.setString("S7","D7 Off");
delay(1000);
}
else if (g==true)
{
digitalWrite(LED7,LOW);
Serial.println("light 7 ON from touch sensor");
client.publish("esp/test", "#relay7on");
Firebase.setString("S7","D7 On");
delay(1000);
}
}
int touch88 = touchRead(Touch8);
Serial.println(touch88);
if(touch88 < 10)
{
h = !h;
if(h == false)
{
digitalWrite(LED8,HIGH);
Serial.println("light 8 OFF from touch sensor");
client.publish("esp/test", "#relay8off");
Firebase.setString("S8","D8 Off");
delay(1000);
}
else if (h==true)
{
digitalWrite(LED8,LOW);
Serial.println("light 8 ON from touch sensor");
client.publish("esp/test", "#relay8on");
Firebase.setString("S8","D8 On");
delay(1000);
}
}
client.loop();
}
Google Firebase window
MQTT Window
2. Auto stair case light
Using PIR sensor, LDR sensor and Relay. We are gonna to make module. For auto stair light turn off and on. For auto stair light we need NoduMCU, LDR sensor, PIR sensor and Relay. Connect two sensor and relay with NodeMUC as shown in figure. We can use any light (I am using 12v LED strip).
Code Explanation :
Define 3 pins
#define Light D5
#define PIR D6
#define LDR A0
First time setup, Define pin modes and begin serial, here baud rate is 115200
Serial.begin(115200);
pinMode(Light, OUTPUT);
pinMode(PIR, INPUT);
pinMode(LDR, INPUT);
Main loop which is going on continuous
int valueLDR = analogRead(LDR); // read LDR value
int valuePIR = digitalRead(PIR); // read PIR value
Serial.print("Value of LDR : ");
Serial.println(valueLDR);
Serial.print("Value of PIR : ");
Serial.println(valuePIR);
if((300>valueLDR) && (valuePIR==HIGH))
{
digitalWrite(Light,LOW); // Turn ON the light (Relay is active low)
delay(10000); // Time as per your requirements (Here 10sec)
}
else
{
digitalWrite(Light,HIGH); // Turn OFF the light (Relay is active low)
}
3. Door opening and closing
Using this module we can control our doors. Using NodeMCU, Ultra Sonic Sensor, 2-channel relay and motor.
Code Explanation
Define Pins
#define switch1 D4
#define switch2 D7
const int trigPin = D5;
const int echoPin = D6;
To calculate distance take few variables
long duration;
int distance,d1;
First time setup loop where define pin modes and begin serial monitor to know status about all things.
pinMode(switch1,OUTPUT);
pinMode(switch2,OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); //(19200,SERIAL_8E1) - data size = 8 bits , parity = Even , stop bit = 1bit
Main loop which is going on continuously. Here it will first calculate distance by using ultra sonic sensor. Than take the action according to distance measure by the sensor. If distance is less than 200 than it will start opening motor and if more than 200 than it will start closing motor.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = int(duration*0.034/2);
Serial.print("Distance : ");
Serial.println(distance);
if(distance<200) //Object is near to the door
{
digitalWrite(switch1,HIGH);
digitalWrite(switch2,LOW);
Serial.println("Door Opening Soon");
delay(10000);
digitalWrite(switch1, LOW);
}
else if(distance>200) //Object is far to the door
{
digitalWrite(switch2,HIGH);
digitalWrite(switch1,LOW);
Serial.println("Door Closing Soon");
delay(10000);
digitalWrite(switch2, LOW);
}
Comments