Rishabh Banga
Published © CC BY-NC-SA

Full Home Automation System with Azure and Voice Assistance

Using Rasberry Pi 2, Azure, and Voice Assistance (Windows and Android) observe how your home not only gets automated, but also self aware.

AdvancedFull instructions provided17,595
Full Home Automation System with Azure and Voice Assistance

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Ethernet Cable
×1
MicroSD Card and Card Reader
×1
HDMI cable and monitor
×1
Breadboard (generic)
Breadboard (generic)
×1
10k Ohm resistors
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LED (generic)
LED (generic)
×1
Router
×1
Seeed GrovePi+
×1
Camera Module
Raspberry Pi Camera Module
×1
Grove Starter Kit for LinkIt ONE
Seeed Grove Starter Kit for LinkIt ONE
Sensors I will be using from this Kit, are compatible with Raspberry Pi 2.
×1
Reed Switch
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Visual Studio 2015
Microsoft Visual Studio 2015
Microsoft Azure
Microsoft Azure

Hand tools and fabrication machines

PC running Windows 10
Android Phone

Story

Read more

Schematics

Light connections schematics

Step 2
Schem1

PIR Sensor Schematics

Step 3
Schem2

Temperature and Humidity Connections

Step 1
Temp%26humidity

Code

Motion Detection

Python
Code for Motion Detection
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)

def MOTION(PIR_PIN):
               print Motion Detected!

print PIR Module Test (CTRL+C to exit)
time.sleep(2)
print Ready

try:
               GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
               while 1:
                              time.sleep(100)
except KeyboardInterrupt:
               print  Quit
               GPIO.cleanup()

Temperature and Humidity Logger

Python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

GPIO.output(18, GPIO.HIGH)

while True:
	if GPIO.input(17):
		break

while GPIO.input(17):
	pass

GPIO.output(18, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)

datafile = open("temperaturedata.log", "w")

while True:
	tfile = open("/sys/bus/w1/devices/w1_bus_master1/10-000802824e58/w1_slave")
	text = tfile.read()
	tfile.close()
	temperature_data = text.split()[-1]
	temperature = float(temperature_data[2:])
	temperature = temperature / 1000
	datafile.write(str(temperature) + "\n")
	if GPIO.input(17):
		break

datafile.close()
GPIO.output(25, GPIO.LOW)

Lights Switching

Python
export_file = open("/sys/class/gpio/export", "w")
export_file.write("18")
export_file.close()

direction_file = open("/sys/class/gpio/gpio18/direction", "w")
direction_file.write("out")
direction_file.close()

value_file = open("/sys/class/gpio/gpio18/value", "w")
value_file.write("1")
value_file.close()

value_file = open("/sys/class/gpio/gpio18/value", "w")
value_file.write("0")
value_file.close()

unexport_file = open("/sys/class/gpio/unexport", "w")
unexport_file.write("18")
unexport_file.close()

Who's at home?

C/C++
Deploy this after you're done with connecting PIR Motion Sensor, Ultrasonic Sensor and LED.
#include <Servo.h>
#include <Ultrasonic.h>
#include "TM1637.h"

/* Macro Define */
//#define PIR_MOTION_SENSOR     23            /* sig pin of the PIR sensor */
#define IR80                                 23
//#define LIGHT_SENSOR              9            /* pin of grove light sensor */
#define ULTRASONIC_PIN            27           /* pin of the Ultrasonic Ranger */
//#define BUZZER_PIN                   26          /* sig pin of the buzzer */
#define SERVO                             26
#define BLUELED                        24

#define LED                      RED_LED      /* led */
#define ON                       HIGH                    /* led on */
#define OFF                      LOW                     /* led off */
#define _handle_led(x)           digitalWrite(LED, x)    /* handle led */ 
#define _handle_blueled(x)     digitalWrite(BLUELED, x)   /* handle blue led */ 


#define CLK 9//pins definitions for TM1637 and can be changed to other ports
#define DIO 10
TM1637 tm1637(CLK,DIO);

int light_analog_value = 0;                     /* varible to store the value coming from rotary angle sensor */
Ultrasonic ultrasonic(ULTRASONIC_PIN);    /* Ultrasonic Ranger object */
int distance = 0;                         /* varible to store the distance to obstacles in front */
int8_t bits[4] = {0};                     /* array to store the single bits of the value */
bool occupy = true;
int IRdis;

//int length = 10;         /* the number of notes */
//char notes[] = "ccggaaffeeddc ";
//int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int length = 3;         /* the number of notes */
char notes[] = "ad ";
int beats[] = { 1, 2 };
int tempo = 300;

Servo myservo;
int PeopleCount = 2;

int sensor_val;
float voltFromRaw;

void setup()
{
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  
  //pinMode(BUZZER_PIN, OUTPUT);   /* set buzzer pin as output */
  //pinMode(PIR_MOTION_SENSOR, INPUT);   /* declare the sig pin as an INPUT */
  pinMode(RED_LED, OUTPUT);            /* declare the red_led pin as an OUTPUT */
  _handle_led(OFF);
  
  pinMode(BLUELED, OUTPUT);            /* declare the red_led pin as an OUTPUT */
  _handle_blueled(OFF);
  
  myservo.attach(SERVO);
  delay(100);
  myservo.write(80); // center position
  myservo.detach();
  
  Serial.begin(9600);
}

/* judge if there is a people around */
boolean isPeopleDetected() {
  
  int sensor_val = analogRead(IR80);
  float voltFromRaw=map(sensor_val, 0, 1023, 0, 5000);
  
 //IRdis = sensor_val;
  IRdis=27.728*pow(voltFromRaw/1000, -1.2045); // 10-80 model
  //IRdis=61.573*pow(voltFromRaw/1000, -1.1068); // 20-150 model
  //IRdis = 4800 / (sensor_val - 20);
  //d = 5461 / (ADC - 17) - 2 
 //IRdis = 5461 / ( sensor_val - 17 ) - 2;

 if ( IRdis < 6) {return true;}
 else {return false;}
  //return false;
  
/*  
  int sensor_val = digitalRead(PIR_MOTION_SENSOR);        // read sig pin 
    if(HIGH == sensor_val) {
        return true;                                        // people detected 
    } else {
        return false;                                       // people un-detected 
    }
 */
}

/* play tone */
/*
void playTone(int _tone, int duration) {
   
    for(long i = 0; i < duration * 1000L; i += _tone*2 ) {
        digitalWrite(BUZZER_PIN, HIGH);
        delayMicroseconds(_tone);
        digitalWrite(BUZZER_PIN, LOW);
        delayMicroseconds(_tone);
    }
}

void playNote(char note, int duration) {
    
    char name[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c'};
    int _tone[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
    
    // play the tone corresponding to the tone name
    for(int i = 0; i < 8; i++) {
        if(name[i] == note) {
            playTone(_tone[i], duration);
        }
    }
}
*/

void loop()
{
  int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
  int8_t ListDisp[4];
  unsigned char i = 0;
  
  sensor_val = analogRead(IR80);
  voltFromRaw=map(sensor_val, 0, 1023, 0, 5000);
  IRdis=27.728*pow(voltFromRaw/1000, -1.2045); // 10-80 model
   if ( IRdis < 5 )
   {
       _handle_blueled(ON);
       tm1637.display(2, IRdis);
       delay(10); // a little delay to wait Ultrasonic trigger
       
       distance = ultrasonic.MeasureInCentimeters();
       if(distance <=40)
       {
         Serial.print("After IR Trigger - distance : ");
         Serial.println(distance);
         memset(bits, 0, 4);                             // reset array when we use it 
        for(int i = 1; i >= 0; i--) {
        // get single bits of the analog value 
        bits[i] = distance % 10;
        distance = distance / 10;  
        tm1637.display(i, bits[i]);}
         
         PeopleCount++;
         if(PeopleCount == 1) 
         {
           myservo.attach(SERVO);
           delay(100);
           myservo.write(125); // on
           delay(100);
           myservo.write(80); // center position
           delay(100);
           myservo.detach();
         }
         
         delay(1000); // delay for PIR trigger duration
       }
   }
   else
   {
       _handle_blueled(OFF);
       
       distance = ultrasonic.MeasureInCentimeters();
       if(distance <=40)
       {
         Serial.print("Ultrasonic Trigger - distance : ");
         Serial.println(distance);
         memset(bits, 0, 4);                             // reset array when we use it 
        for(int i = 1; i >= 0; i--) {
        // get single bits of the analog value 
        bits[i] = distance % 10;
        distance = distance / 10;  
        tm1637.display(i, bits[i]);}
         delay(100); 
         sensor_val = analogRead(IR80);
         voltFromRaw=map(sensor_val, 0, 1023, 0, 5000);
         IRdis=27.728*pow(voltFromRaw/1000, -1.2045); // 10-80 model
         if ( IRdis < 5 )
         {
           _handle_blueled(ON);
           tm1637.display(2, IRdis);
           PeopleCount--;
           if(PeopleCount == 0) 
           {
             myservo.attach(SERVO);
             delay(100);
             myservo.write(50); // off
             delay(100);
             myservo.write(80); // center position
             delay(100);
             myservo.detach();
           }
           
           delay(1000); 
         }
        
         
       }
   }
  /*
  memset(bits, 0, 4);                             // reset array when we use it 
  for(int i = 1; i >= 0; i--) {
  // get single bits of the analog value 
  bits[i] = distance % 10;
  distance = distance / 10;  
  tm1637.display(i, bits[i]);}                 // display by 4-digital display }

    /*
    memset(bits, 0, 4);                             // reset array when we use it 
    for(int i = 3; i >= 2; i--) {
    // get single bits of the analog value 
    bits[i] = IRdis % 10;
    IRdis = IRdis / 10;  
    tm1637.display(i, bits[i]);}                 // display by 4-digital display }
   */
   //tm1637.display(2, IRdis);
   tm1637.display(0, 0xff);
   tm1637.display(1, 0xff);
   tm1637.display(2, 0xff);
   tm1637.display(3, PeopleCount);      
      
} // end loop

ControlCommands.xml

XML
<?xml version="1.0" encoding="utf-8"?>

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
  <CommandSet xml:lang="en-US">
    <CommandPrefix>HomeAuto</CommandPrefix>
    <Example> turn on my desk lights</Example>

    <Command Name="DeskLightsOn">
      <Example> turn on my desk lights </Example>
      <ListenFor> turn on my desk lights </ListenFor>
      <Feedback> Turning On Your Desk Lights... </Feedback>
      <Navigate />
    </Command>

    <Command Name="DeskLightsOff">
      <Example> turn off my desk lights </Example>
      <ListenFor> turn off my desk lights </ListenFor>
      <Feedback> Turning Off Your Desk Lights...</Feedback>
      <Navigate />
    </Command>

  </CommandSet>

  <CommandSet xml:lang="en-US">
    <CommandPrefix>HomeAuto</CommandPrefix>
    <Example> turn on fans</Example>

    <Command Name="FansOn">
      <Example> turn on my fans </Example>
      <ListenFor> turn on my fans</ListenFor>
      <Feedback> Turning On Your Fans... </Feedback>
      <Navigate />
    </Command>

    <Command Name="FansOff">
      <Example> turn off my fans </Example>
      <ListenFor> turn off my fans </ListenFor>
      <Feedback> Turning Off Your Fans...</Feedback>
      <Navigate />
    </Command>

  </CommandSet>

<CommandSet xml:lang="en-US">
    <CommandPrefix>HomeAuto</CommandPrefix>
    <Example> Activate Camera</Example>

    <Command Name="CameraOn">
      <Example> turn on my camera </Example>
      <ListenFor> turn on my camera </ListenFor>
      <Feedback> Turning On Your Camera... </Feedback>
      <Navigate />
    </Command>

    <Command Name="CameraOff">
      <Example> turn off my camera </Example>
      <ListenFor> turn off my camera </ListenFor>
      <Feedback> Turning Off Your Camera...</Feedback>
      <Navigate />
    </Command>

  </CommandSet>

<CommandSet xml:lang="en-US">
    <CommandPrefix>HomeAuto</CommandPrefix>
    <Example> Check Weather</Example>

    <Command Name="CheckWeather">
      <Example>Check Weather </Example>
      <ListenFor> Check Weather </ListenFor>
      <Feedback> Checking Temperature and Humidity... </Feedback>
      <Navigate />
    </Command>

  </CommandSet>
</VoiceCommands>

Azure SDK for Python

Clone the repo down and install it on your machine

Credits

Rishabh Banga

Rishabh Banga

11 projects • 113 followers
Intel Software Innovator | Microsoft Certified Professional | IoT Evangelist
Contact
Thanks to Matthew Kirk, talk2bruce, jckelley, and JackRose.

Comments

Add projectSign up / Login