K. Sai Ashish
Published © GPL3+

Integrated Home Automation System

Experience the power of automation with your voice and touch. Feel the magic of automated light & also design multi-alert system for danger.

IntermediateFull instructions provided8 hours250
Integrated Home Automation System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LED (generic)
LED (generic)
×2
Buzzer
Buzzer
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Bolt IoT Android App
Bolt IoT Android App
SMS Messaging API
Twilio SMS Messaging API
Maker service
IFTTT Maker service
Snappy Ubuntu Core
Snappy Ubuntu Core

Story

Read more

Schematics

Final Schematic

Code

Interface Code

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Smart Home Interface</title>
		<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
        <script>
        setKey('{{ApiKey}}','{{Name}}');
        </script>
		<style>
		body
		{
		background:url("https://images.unsplash.com/photo-1522444195799-478538b28823?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80");
		background-size: cover;
		background-repeat:no-repeat;
		background-position:center;
                margin: 0px;
                padding: 0px;
        }
		.space { margin-top: 120px; }
		div 
		{ 
		background-color:#ffffff;
		width: 420px;
		display: flex;
		justify-content: center;
		align-items: center;
		opacity: .8;
		} 
		h3
		{
		font-size:22px;
		font-family: "Times New Roman", cursive, sans-serif;
		</style>
    </head>
<body>
<center>
<div class="space"></div>

<div><br> 
 <center>
<h3>Experience Automation <br> at your fingertips!</h3></center>
</div>

<br><br><br>
<input type="button" value="Light On" onClick="digitalWrite(1, 'HIGH');" /> 
<input type="button" value="Light Off" onClick="digitalWrite(1, 'LOW');" /> <br> <br>
<input type="button" value="Fan On" onClick="digitalWrite(2, 'HIGH');" />
<input type="button" value="Fan Off" onClick="digitalWrite(2, 'LOW');" /> <br> <br>
<input type="button" value="Alarm On" onClick="digitalWrite(3, 'HIGH');" />
<input type="button" value="Alarm off" onClick="digitalWrite(3, 'LOW');" /> <br> <br>
<input type="button" value="Automated Light" onClick="digitalWrite(1, 'LOW');" /><br><br>
<br/><br/></center>

</body> 
</html>

Configuration File (conf.py)

Python
SSID = 'You can find SSID in your Twilio Dashboard' 
AUTH_TOKEN = 'You can find  on your Twilio Dashboard' 
FROM_NUMBER = 'This is the no. generated by Twilio. You can find this on your Twilio Dashboard'
TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device'
FROM_WHATSAPP= 'whatsapp: followed by the whatsapp no. generated by Twilio. You can find this in the Programmable SMS's Whatasapp Beta tab'
TO_WHATSAPP= 'whatsapp: followed by your whatsapp number. Make sure you are adding +91 in beginning'
FRAME_SIZE = 10
MUL_FACTOR = 6

Automated Light (automated_led.py)

Python
#importing_the_necessary_packages
import conf, json, time, math, requests, statistics 
from boltiot import Sms, Bolt 

#defining function to calculate the upper and lower bounds by Z score analysis
def compute_bounds(history_data,frame_size,factor): 
    if len(history_data)<frame_size :
        return None

    if len(history_data)>frame_size :
        del history_data[0:len(history_data)-frame_size]
    Mn=statistics.mean(history_data)
    Variance=0
    for data in history_data :
        Variance += math.pow((data-Mn),2)
    Zn = factor * math.sqrt(Variance / frame_size)
    High_bound = history_data[frame_size-1]+Zn
    Low_bound = history_data[frame_size-1]-Zn
    return [High_bound,Low_bound] #returns the upper and lower bound

#importing configurations from the conf.py file
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID) 
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[] #list to store the data sensed by LDR


while True:
    response_ldr = mybolt.analogRead('A0') #reading response from analog pin connected to LDR
    ldr_data = json.loads(response_ldr) #response is decoded from the JSON format
    if ldr_data['success'] != 1: #for errors
        print("There was an error while retrieving the data.")
        print("This is the error:"+ldr_data['value'])
        time.sleep(10)
        continue

    try:
        sensor_value = int(ldr_data['value'])
    except Exception as e:
        print("There was an error while parsing the response: ",e)
        continue

    print("LDR sensor value is:" + str(sensor_value)) #prints LDR value on screen
    print(response_ldr)

    led_value_1 = int(sensor_value/4) 
    if led_value_1 > 255:
        led_value_1 = 255
    led_value = 255 - led_value_1 #calculating intensity of LED in accordance with LDR
    response_led = mybolt.analogWrite('0', led_value) #setting instensity as calculated from above"""
    print("Automated LED value is: "+str(led_value)) #printing value of LED on screen
    print(response_led)

#invoking function to calculated and return the bounds
    bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR) 
    if not bound:
        required_data_count=conf.FRAME_SIZE-len(history_data)
        if(required_data_count!=0 and required_data_count!=1):
            print("Not enough data to compute Z-score. Need ",required_data_count," more data points")
        history_data.append(int(ldr_data['value'])) #appends to list of input data
        time.sleep(10)
        continue

    try:
        if sensor_value > bound[0] : #if sensor value is greater than Higher bound
            print ("The light level increased suddenly. Sending an SMS.")
            #sends SMS as programmed
            response = sms.send_sms("Someone turned on the lights") 
            print("This is the response ",response)
        elif sensor_value < bound[1]: #if sensor value is lesser than Lesser Bound
            print ("The light level decreased suddenly. Sending an SMS.")                             
            #sends SMS as programmed
            response = sms.send_sms("Someone turned off the lights")
            print("This is the response ",response)
        history_data.append(sensor_value) #appends to list of input data
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Security Alert (intruder_alert_system.py)

Python
#importing the required packages
import conf, json, time, requests 
from boltiot import Sms, Bolt
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

#configuring the email settings
email = 'Your_email_id'
password = 'Your_email_password'
send_to_email = 'Recipient's_Email '
subject = 'Intrusion'
message = 'Someone Entered!'

msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject

msg.attach(MIMEText(message, 'plain'))

#SMS configuration
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
#Whatsapp configuration
sms_whatsapp= Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_WHATSAPP, conf.FROM_WHATSAPP)

while True:
    response = requests.get('http://cloud.boltiot.com/remote/your_api_key/digitalRead?pin=4&deviceName=your_device_id') #getting response from PIR sensor
    data=json.loads(response.text) #decoding the response obtained in JSON format
    print(data)
    sensor_value = int(data['value'])
    if sensor_value == 1: #Sensor picked up reading
        print("Motion Detected!")
        print("Sending SMS...")
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(email, password)
        text = msg.as_string()
        server.sendmail(email, send_to_email, text) #Email Delivered
        sms.send_sms("Intruder Alert!") #SMS sent
        sms_whatsapp.send_sms("Intruder Alert!") #Whatsapp Successful
        server.quit()
    elif sensor_value ==0:
        print("SENSOR IS READY")
    time.sleep(5)

Credits

K. Sai Ashish

K. Sai Ashish

1 project • 0 followers

Comments