Dhrumil Makadia
Created November 22, 2019 © CC BY

Smart Home Solution

Smart home automation system controllable by smart phones and automation system.

AdvancedProtip20 hours94
Smart Home Solution

Things used in this project

Hardware components

PSoC 4 Prototyping Kit
Cypress PSoC 4 Prototyping Kit
×1
FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
DFRobot FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
×1
Relay
×3
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
AC motor 2.5-3 rpm
×1

Software apps and online services

Android Studio
Android Studio
Arduino IDE
Arduino IDE
Firebase
Google Firebase
MQTT
MQTT

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering
PCB Holder, Soldering Iron
PCB Holder, Soldering Iron

Story

Read more

Schematics

Auto Light

Auto Door

Code

Auto Door

Arduino
#define switch1 D4 
#define switch2 D7

const int trigPin = D5;
const int echoPin = D6;

long duration;
int distance,d1;

void setup() 
{
  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 
}

void loop() 
{
  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) //car is near to the gate 
  {
    digitalWrite(switch1,HIGH);
    digitalWrite(switch2,LOW);
    Serial.println("Door Opening Soon");
    delay(10000);
    digitalWrite(switch1, LOW);
  }
  else if(distance>200) //car is far to the gate
  {
    digitalWrite(switch2,HIGH);
    digitalWrite(switch1,LOW);
    Serial.println("Door Closing Soon");
    delay(10000);
    digitalWrite(switch2, LOW);
  } 
}

Auto Light

Arduino
#define Light D5  
#define PIR D6
#define LDR A0

void setup()
{    
  Serial.begin(115200);
  pinMode(Light, OUTPUT);
  pinMode(PIR, INPUT);
  pinMode(LDR, INPUT);
}

void loop() 
{  
  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)  
  }
}

Home Appliances

Arduino
Control by physical touch and smart android phones
#include <WiFi.h>
#include <PubSubClient.h>
#include <IOXhop_FirebaseESP32.h>
 
#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 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

#define FIREBASE_HOST "xxxxxxxxxxxxxxxxx.firebaseio.com"           //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"       //Your Firebase Database Secret goes here

int val1,val2,val3,val4,val5,val6,val7,val8;

int touch11, touch22, touch33, touch44, touch55, touch66, touch77, touch88;

boolean a = true; //two way operation
boolean b = true;
boolean c = true;
boolean d = true;
boolean e = true;
boolean f = true;
boolean g = true;
boolean h = true;

const char* ssid = "xxxxxxxxxxx";  
const char* password =  "xxxxxxxxxxx";
 
//Enter your mqtt server configurations
const char* mqttServer = "xxxxxxxx.cloudmqtt.com";    //Enter Your mqttServer address
const int mqttPort =   xxxxx;       //Port number
const char* mqttUser = "xxxxxxx"; //User
const char* mqttPassword = "xxxxxxxxxxx"; //Password
 
WiFiClient espClient;
PubSubClient client(espClient);
 
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 configuration
      delay(2000);
    }
  }
  client.publish("esp/test", "Hello From Wemos D1");
  client.subscribe("esp/test");

  Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
}

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();
  */
} 
 
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("-----------------------");  
}
 
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();
}

Credits

Dhrumil Makadia

Dhrumil Makadia

40 projects • 43 followers

Comments