Ishmael Ryu
Created January 24, 2018

Complete Alexa Smart Home Package

A smart home model that is controlled by Alexa.

598
Complete Alexa Smart Home Package

Things used in this project

Story

Read more

Custom parts and enclosures

Smart Home Drawing

Schematics

Smart Home Schematics

Code

Raspberry Pi Python 3 (IDLE)

Python
from flask import Flask, render_template

from flask_ask import Ask,statement

import smbus

import serial

from picamera import PiCamera

import time

import plivo, plivoxml

from ubidots import ApiClient

from twython import Twython

from auth3 import (

    consumer_key,

    consumer_secret,

    access_token,

    access_token_secret

    )

twitter = Twython(

    consumer_key,

    consumer_secret,

    access_token,

    access_token_secret

    )

 

api = ApiClient(token = "A1E-esutDEn2fFKzH7cIAJ31kLn9agQsoP")

ubi_pir = api.get_variable("5a572831c03f9731148ff03e")

 

bus = smbus.SMBus(1)

address = 0x04

 

ser = serial.Serial("/dev/ttyACM0", 9600)

 

 

app = Flask(__name__)

ask = Ask(app, '/')

 

@app.route('/')

def page():

    return " welcome to Smart Home"

 

@ask.launch

def start():

    return statement("Hello,welcome to smart home.")

 

@ask.intent("LivingRoomIntent")

def living_room_lights_on():

    bus.write_byte(address,97)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s living room lights are on." % (f)

    twitter.update_status(status=message)

    return statement("OK, living room lights are on.")

 

 

@ask.intent("LivingRoomOffIntent")

def living_room_lights_off():

    bus.write_byte(address,65)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s living room lights are off." % (f)

    twitter.update_status(status=message)

    return statement("OK, living room lights are off.")

 

 

@ask.intent("KitchenIntent")

def kitchen_lights_on():

    bus.write_byte(address,98)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s kitchen lights are on." % (f)

    twitter.update_status(status=message)

    return statement("OK, kitchen lights are on.")

 

@ask.intent("KitchenOffIntent")

def kitchen_lights_off():

    bus.write_byte(address,66)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s kitchen lights are off." % (f)

    twitter.update_status(status=message)

    return statement("OK, kitchen lights are off.")

 

@ask.intent("BedroomIntent")

def bedroom_lights_on():

    bus.write_byte(address,99)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s bedroom lights are on." % (f)

    twitter.update_status(status=message)

    return statement("OK, bedroom lights are on.")

 

@ask.intent("BedroomOffIntent")

def bedroom_lights_off():

    bus.write_byte(address,67)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s bedroom lights are off." % (f)

    twitter.update_status(status=message)

    return statement("OK, bedroom lights are off.")

 

@ask.intent("BathroomIntent")

def bathroom_lights_on():

    bus.write_byte(address,100)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s bathroom lights are on." % (f)

    twitter.update_status(status=message)

    return statement("OK, bathroom lights are on.")

 

@ask.intent("BathroomOffIntent")

def bathroom_lights_off():

    bus.write_byte(address,68)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message ="%s bathroom lights are on." % (f)

    twitter.update_status(status=message)

    return statement("OK, bathroom lights are off.")

 

@ask.intent("HumidityIntent")

def what_is_the_humidity():

    bus.write_byte(address,101)

    time.sleep(1)

    data = ser.readline()

    h = str(data,'utf-8')

    hum_text = "humidity is %s percent." % (h)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s %s'%(f,hum_text)

    twitter.update_status(status=message)

    return statement(hum_text)

 

@ask.intent("TemperatureIntent")

def what_is_the_temperature():

    bus.write_byte(address,102)

    time.sleep(1)

    data = ser.readline()

    t = str(data,'utf-8')

    temp_text = "temperature is %s celsius ." % (t)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s %s'%(f,temp_text)

    twitter.update_status(status=message)

    return statement(temp_text)

 

@ask.intent("SecurityOnIntent")

def security_on():

    bus.write_byte(address,103)

    data = ser.readline()

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s INTRUDER'%(f)

    if (data ==b'h\r\n'):

        ubi_pir.save_value({'value':1})

        time.sleep(2)

        ubi_pir.save_value({'value':0})

        camera = PiCamera()

        camera.capture('/home/pi/Desktop/intruder.jpg')

        

        with open('/home/pi/Desktop/intruder.jpg','rb') as photo:

            twitter.update_status_with_media(status=message, media=photo)

    return statement("OK, security systems are on.")  

 

@ask.intent("SecurityOffIntent")

def security_off_intent():

    bus.write_byte(address,71)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s security systems are off.'%(f)

    twitter.update_status(status=message)

    return statement("OK,security systems are off.")

 

@ask.intent("OpenDoorIntent")

def open_the_door():

    bus.write_byte(address, 105)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s door is opened.'%(f)

    twitter.update_status(status=message)

    return statement("OK,the door is opened.")

 

@ask.intent("CloseDoorIntent")

def close_the_door():

    bus.write_byte(address, 106)

    i = time.asctime( time.localtime(time.time()) )

    f=str(i)

    message = '%s door is closed.'%(f)

    twitter.update_status(status=message)

    return statement("OK,the door is closed.")

 

@ask.intent("WhereIsMyPhoneIntent")

def where_is_my_phone():

    auth_id = "Your auth_id"

    auth_token = "Your auth_token"

 

    p = plivo.RestAPI(auth_id, auth_token)

 

    params = {

        'to': 'Your phone number',    # The phone numer to which the call will be placed

        'from' : 'any phone number', # The phone number to be used as the caller id

 

        # answer_url is the URL invoked by Plivo when the outbound call is answered

        # and contains instructions telling Plivo what to do with the call

        'answer_url' : "https://s3.amazonaws.com/static.plivo.com/answer.xml",

        'answer_method' : "GET", # The method used to call the answer_url

 

        # Example for asynchronous request

        # callback_url is the URL to which the API response is sent.

        #'callback_url' => "http://myvoiceapp.com/callback/",

        #'callback_method' => "GET" # The method used to notify the callback_url.

    }

 

    # Make an outbound call and print the response

    response = p.make_call(params)

    return statement("Here is your phone.")

 

@ask.intent("HelpIntent")

def help():

    auth_id = "Your auth_id"

    auth_token = "Your auth_token"

 

    p = plivo.RestAPI(auth_id, auth_token)

 

    params = {

        'to': '911',    # The phone numer to which the call will be placed

        'from' : 'Your phone number', # The phone number to be used as the caller id

 

        # answer_url is the URL invoked by Plivo when the outbound call is answered

        # and contains instructions telling Plivo what to do with the call

        'answer_url' : "https://s3.amazonaws.com/static.plivo.com/answer.xml",

        'answer_method' : "GET", # The method used to call the answer_url

 

        # Example for asynchronous request

        # callback_url is the URL to which the API response is sent.

        #'callback_url' => "http://myvoiceapp.com/callback/",

        #'callback_method' => "GET" # The method used to notify the callback_url.

    }

 

    # Make an outbound call and print the response

    response = p.make_call(params)

    return statement("Calling 911")

 

 

if __name__=='__main__':

    app.run(debug = True);

Raspberry Pi Python 3 (IDLE) auth3

Python
save it as auth3.py
consumer_key = "your consumer_key"
consumer_secret = "your consumer_secret"
access_token = "your access_token"
access_token_secret = "your access_token_secret"

Arduino IDE

C/C++
#include <Wire.h>
#define my_i2c_addr 0x04

#include <Servo.h> // servo library 
Servo myservo; // servo name

#include "DHT.h"

#define DHTPIN 13    // what digital pin we're connected to
int pirPin = 7;

#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
 Wire.begin(my_i2c_addr);
 Wire.onReceive(loop);
  
  myservo.attach(9); // attach servo signal wire to pin 9
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  //bluetooth.begin(9600);
  pinMode(12,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(pirPin, INPUT);
  //Serial.println("DHTxx test!");

  dht.begin();
  
}

void loop()
{
 
  
   int servopos = Wire.read();//input from raspbery pi through I2C

  
    if(servopos=='i')//opening the door
    {
      myservo.write(0);
      tone(12,261,500);
      delay(100);
      tone(12,330,500);
      delay(100);
      tone(12,392,500);
      delay(1000);
      
    }
    if(servopos=='j')//closing the door
    {
      myservo.write(180);
     tone(12,392,500);
      delay(100);
      tone(12,330,500);
      delay(100);
      tone(12,261,500);
      delay(1000);
    }

   
    if(servopos== 'a')//led 1 on
    {
      digitalWrite(2,HIGH);
    }
    if(servopos=='b')//led 2 on
    {
      digitalWrite(3,HIGH);
    }
    if(servopos=='c')//led 3 on
    {
      digitalWrite(4,HIGH);
    }
    if(servopos=='d')//led 1 on
    {
      digitalWrite(5,HIGH);
    }
       if(servopos=='A')//led 1 off
    {
      digitalWrite(2,LOW);
    }
       if(servopos=='B')//led 2 off
    {
      digitalWrite(3,LOW);
    }
       if(servopos=='C')//led 3 off
    {
      digitalWrite(4,LOW);
    }
       if(servopos=='D')//led 1 off
    {
      digitalWrite(5,LOW);
    }


 
 int t = dht.readTemperature();
  
  
 int h = dht.readHumidity();

 
 if(servopos == 'e'){ //print humidity to raspberry pi
  Serial.println(h);
 }
  
  if(servopos == 'f'){ //print temperature to raspberry pi
  Serial.println(t);
  }

int  pir = digitalRead(pirPin);
 if(servopos == 'g'){//activte pir senor
  
  if (servopos == 'G'){//deactivate pir sensor
  pir = 0;
 }
 if (pir>0){
  Serial.println("h");
  delay(500);
 }
} 


 
}

Credits

Ishmael Ryu

Ishmael Ryu

1 project • 1 follower

Comments