archit borkar
Published © GPL3+

Smart Switch Controller

Control wall mounted switches smartly using three different methods - android app, ok google voice command and hand gestures.

IntermediateFull instructions provided3 hours1,912
Smart Switch Controller

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Arduino UNO
Arduino UNO
×1
l293d motor driver
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
LED (generic)
LED (generic)
×1
Temperature Sensor
Temperature Sensor
×1
Resistor 1k ohm
Resistor 1k ohm
×1
9V battery (generic)
9V battery (generic)
×1
servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
DC Motor, 12 V
DC Motor, 12 V
×1

Software apps and online services

Arduino IDE
Arduino IDE
MIT App Inventor 2
MIT App Inventor 2
Bolt Cloud
Bolt IoT Bolt Cloud
IFTTT Web service

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Schematic diagram

Code

Arduino code for the project

Arduino
#include <Servo.h>             //Servo library
Servo servo;                   //initialize a servo object for the connected servo                  
int angle;                     //angle of servo
#define motp 3                 //movement motor
#define motn 4
#define eco 11                 //distance sensor
#define trig 12
#define led 7                  //indicator led
long duration;                 
int pos=1,temp,tempC,distance,tym=0,l=0,f=0;
float t=0;

void setup() 
{ 
  Serial.begin(9600);
  servo.attach(2);             //attach the signal pin of servo to pin2 of arduino
  servo.write(28);             //set the initial angle of servo to 28 degree  
  pinMode(3,OUTPUT);           
  pinMode(4,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(trig,OUTPUT);
  pinMode(eco,INPUT);
  pinMode(led,OUTPUT);
} 

  
void loop() 
{ 
  if(tym > 10800 && (l>0 || f>0))             //after the last control if 3 hrs are passed automatically turn off all apliances
  {
    if(pos == 1){
       tog_off();
       fanP();
       tog_off();
       lightP();
       t = 0;
       l=0;
       f=0;
       tym = 0;
      }
    else if(pos == 2)
      {
       tog_off();
       lightP();
       tog_off();
       t = 0;
       l=0;
       f=0;
       tym = 0;
      }
  }
  temp = analogRead(A1);       //measure the temperature of the room
  temp = (temp*500)/1023;
  tempC = temp;
  
  
  if (Serial.available()){
    char x = Serial.read();    //character read from bolt cloud
    if(x == 'a'){              // case 1 fan off
      digitalWrite(led,HIGH);
      delay(100);
      digitalWrite(led,LOW);
      if(pos == 2){
       tog_off();
       t = 0;
       f=0;
       tym = 0;
      }
      else if(pos == 1)
      {
       fanP();
       tog_off();
       t = 0;
       f=0;
       tym = 0;
      }
    }
    else if(x == 's'){         //case 2 fan on
      digitalWrite(led,HIGH);
      delay(100);
      digitalWrite(led,LOW);
      if(pos == 2)
      {
       tog_on();
       t = 0;
       f++;
       tym = 0;
      }
      else if(pos == 1)
      {
       fanP(); 
       tog_on();
       t = 0;
       f++;
       tym = 0;
      }
    }
    else if(x == 'd'){         //case 3 light off
      digitalWrite(led,HIGH);
      delay(100);
      digitalWrite(led,LOW);
      if(pos == 1){
       tog_off();
       t = 0;
       l=0;
       tym = 0;
      }
      else if(pos == 2)
      {
       lightP();
       tog_off();
       t = 0;
       l=0;
       tym = 0;
      }
    }
    else if(x == 'f'){         //case 4 light on
      digitalWrite(led,HIGH);
      delay(100);
      digitalWrite(led,LOW);
      if(pos == 1)
      {
       tog_on();
       t = 0;
       l++;
       tym = 0;
      }
      else if(pos == 2)
      {
       lightP(); 
       tog_on();  
       t = 0;
       l++;
       tym = 0;
      }
    }
  }

  if(tempC >= 35)              //temperature gone above 35 deg C turn on the fan
    {
      if(pos == 2)
      {
       tog_on();
       t = 0;
       f++;
       tym = 0;
      }
      else if(pos == 1)
      {
       fanP(); 
       tog_on();
       t = 0;
       f++;
       tym = 0;
      }
    }

  if(l>0 || f>0)
  {
    tym = tym + 0.4;
  }

  manual();                   // check gesture controls
    
  delay(100);                 // for auto positioning the servo after 60 sec of use
  t = t + 0.4;
  if(t >= 60 && pos == 2){
    lightP();
  }
}

void lightP()                 // function- light position
{
  digitalWrite(motp,HIGH);
  digitalWrite(motn,LOW);
  delay(13000);
  digitalWrite(motp,LOW);
  digitalWrite(motn,LOW);
  pos = 1;
}

void fanP()                   // function- fan position
{
  digitalWrite(motp,LOW);
  digitalWrite(motn,HIGH);
  delay(13000);
  digitalWrite(motp,LOW);
  digitalWrite(motn,LOW);
  pos = 2;
}

void tog_off()                //toggle off
{
  servo.write(0); //off
  delay(1000);
  servo.write(28);
}

void tog_on()                 //toggle on
{
  servo.write(50); //on 
  delay(1000);
  servo.write(28);
}

void manual()                //gesture control
{
  digitalWrite(trig,LOW);
  delayMicroseconds(2);
  digitalWrite(trig,HIGH);
  delay(200);
  digitalWrite(trig,LOW);
  duration=pulseIn(eco,HIGH);
  distance=duration*0.034/2;
  if(distance<=20)            //if distance less than 20cm turn off light and fan
  {
    digitalWrite(led,HIGH);
    delay(100);
    digitalWrite(led,LOW);
    if(pos == 2)
      {
       tog_off();
       lightP();
       tog_off();
       t = 0;
       l=0;
       tym = 0;
      }
    else if(pos == 1 && f>0 && l>0){
       fanP();
       tog_off();
       t = 0;
       l=0;
       f=0;
       tym = 0;
       lightP();
       tog_off();
      }  
    else if(pos == 1 && l>0){
        tog_off();
        t = 0;
        l = 0;
        tym = 0;
      }
    else if(pos == 1 && f>0){
       fanP();
       tog_off();
       t = 0;
       f=0;
       tym = 0;
       lightP();
    }
  }

  else if(distance>=20 && distance<=40)        //if distance is 20cm to 40cm turn on the light
  {
      digitalWrite(led,HIGH);
      delay(100);
      digitalWrite(led,LOW);
      if(pos == 1 )
      {
       tog_on();
       t = 0;
       l++;
       tym = 0;
      }
      else if(pos == 2)
      {
       lightP(); 
       tog_on();
       t = 0;
       l++;
       tym = 0;
      }
  }
}

Credits

archit borkar

archit borkar

2 projects • 6 followers
Mechanical Engineer Robotics enthusiast IoT and ML enthusiast Product Design and Animation

Comments