Aniket Chavan
Published © GPL3+

Smart Home with Raspberry Pi

This project presents the overall design of the low-cost, wireless Home Automation System (HAS).

IntermediateFull instructions providedOver 1 day1,510
Smart Home with Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Breadboard (generic)
Breadboard (generic)
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1
Male Header 40 Position 1 Row (0.1")
Male Header 40 Position 1 Row (0.1")
×1
Grove - Gas Sensor(MQ2)
Seeed Grove - Gas Sensor(MQ2)
×1
8-Channel I2C MCP23008 Optically Isolated Digital Input with I2C Interface
National Control Devices 8-Channel I2C MCP23008 Optically Isolated Digital Input with I2C Interface
×1
Jumper wires (generic)
Jumper wires (generic)
×1
MAX30003 Ultra-Low Power, Single-Channel Integrated Biopotential (ECG, R to R Detection) AFE
Maxim Integrated MAX30003 Ultra-Low Power, Single-Channel Integrated Biopotential (ECG, R to R Detection) AFE
×3
Flash Memory Card, SD Card
Flash Memory Card, SD Card
×1
Buzzer
Buzzer
×1
LED Panel Mount Indicator, Green
LED Panel Mount Indicator, Green
×1
IC & Component Socket, 20 Contacts
IC & Component Socket, 20 Contacts
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
VS Code
Microsoft VS Code

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
PCB Holder, Soldering Iron
PCB Holder, Soldering Iron

Story

Read more

Code

file name c

Python
This file contain the command line it is use to give permission to write the file when ever the changes is done in file and to run the command when ever the raspberry pie is on or booted
sudo chmod 777 c
sudo chmod 777 ngrok
lxterminal --command="./ngrok http 5000"
lxterminal --command="python post_random_url.py"
lxterminal --command="sudo python control.py"

configfile.py

Python
This is an python file which is use for configuring the number on which the alert message should be send when ever the gas leakage is detected
number = 1234567890

file name :control.py

Python
This file contain the basic operation that should be perform every short time of interval, its check the state of the connected devices and send the status to server in json formate this file also listen the incoming request comming through the website.
def readGasReadings():

gasDetected = False

while True:

value = mcp.read_adc(0)

if value > 400:

if not gasDetected:

gasDetected = True

GPIO.output(alert, True)

print(value)

sms.send("Gas Leakage Detected",configfile.number)

pin_status['gas_leakage'] = 1

else:

if gasDetected:

GPIO.output(alert, False)

gasDetected = False

print(value)

pin_status['gas_leakage'] = 0



time.sleep(0.01)



def indicateStart(led):

GPIO.output(led, False)

time.sleep(2)

GPIO.output(led, True)

time.sleep(0.5)

GPIO.output(led, False)

time.sleep(0.2)

GPIO.output(led, True)


time.sleep(0.2)

GPIO.output(led, False)

time.sleep(0.5)

GPIO.output(led, True)







from flask import Flask

import json

import RPi.GPIO as GPIO

import threading

import time

import Adafruit_GPIO.SPI as SPI

import Adafruit_MCP3008

import sms

import configfile



GPIO.setmode(GPIO.BOARD)

relay1 = 7

relay2 = 11

relay3 = 13

alert = 15

start_led = 40

pin_status = {'switch1':0, 'switch2':0, 'switch3':0, 'gas_leakage':0}



GPIO.setup(relay1, GPIO.OUT)

GPIO.setup(relay2, GPIO.OUT)

GPIO.setup(relay3, GPIO.OUT)

GPIO.setup(alert, GPIO.OUT)

GPIO.setup(start_led, GPIO.OUT)

GPIO.setwarnings(False)

indicateStart(start_led)



# Hardware SPI configuration:

SPI_PORT = 0

SPI_DEVICE = 0

mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))



gasRead = threading.Thread(target=readGasReadings)


gasRead.start()



app = Flask(__name__)



@app.route('/')

def index():


# return "Showing Status"

response = app.response_class(response=json.dumps(pin_status),status=200,mimetype='application/json')

response.headers.add('Access-Control-Allow-Origin', '*')

return response



@app.route('/switch1/<int:state>')

def switch1(state):

pin_status['switch1'] = state

if state == 1:

GPIO.output(relay1, True)

# return "Switch 1 Turned on"

else:

GPIO.output(relay1, False)

# return "Switch 1 turned off"

response = app.response_class(response=json.dumps(pin_status),status=200,mimetype='application/json')

response.headers.add('Access-Control-Allow-Origin', '*')

return response



@app.route('/switch2/<int:state>')

def switch2(state):

pin_status['switch2'] = state

if state == 1:

GPIO.output(relay2, True)

# return "Switch 2 Turned on"

else:

GPIO.output(relay2, False)

# return "Switch 2 turned off"

response = app.response_class(response=json.dumps(pin_status),status=200,mimetype='application/json')

response.headers.add('Access-Control-Allow-Origin', '*')

return response



@app.route('/switch3/<int:state>')

def switch3(state):

pin_status['switch3'] = state

if state == 1:

GPIO.output(relay3, True)

# return "Switch 3 Turned on"

else:

GPIO.output(relay3, False)

# return "Switch 3 turned off"

response = app.response_class(response=json.dumps(pin_status),status=200,mimetype='application/json')

response.headers.add('Access-Control-Allow-Origin', '*')

return response



if __name__ == "__main__":

app.run(debug=True)

file name : nstart.sh

Python
This python file is use for generating random domain for making rashbery pie as an host,
ngrok this an open source library which is use for generation random domain and used as an host i.e to act rashbery as an serve this code generate https link
sudo chmod 777 ngrok
./ngrok authtoken 6Npy8RbBboDEWeJUojcnA_6vLfLSnHTfAjwZL3GePUJ
./ngrok http 5000

post_random_url.py

Python
This file contain the basic data like api key and basic configuration like country and its code
this file is use to interact with the sms service server so that authorized user can get access to the services
import time
import os
import json

import requests 

compare = "https:"

UI_URL = "http://mehendicreation.com/randomain.php"
postedurl = ""

while True:
    time.sleep(1)
    try:
        os.system("curl  http://localhost:4040/api/tunnels > tunnels.json")

        with open('tunnels.json') as data_file:    
            datajson = json.load(data_file)

        for i in datajson['tunnels']:
            url = i['public_url']
            if url[0: len(compare)] == compare:
                if url != postedurl:
                    #POST Request to server containing random url 'url' 
                    PARAMS = {'url':url} 
                    r = requests.post(url = UI_URL, data = PARAMS) 
                    data = r 
                    print(data)  
                    print(url)
                    postedurl = url

    except:
print("No")

sms.py

Python
This file contain the basic data like api key and basic configuration like country and its code
this file is use to interact with the sms service server so that authorized user can get access to the services
import requests 
import json

# apikey = 'key will get on google search'
# sender = 'TXTLCL'
# def send(message, numbers):
#     PARAMS = {'apikey': apikey, 'numbers': numbers, 'message' : message, 'sender': sender}
#     UI_URL = 'https://api.textlocal.in/send/'
#     res = requests.post(url = UI_URL, data = PARAMS) 
#     r = json.loads(res.text)
#     print(r.status)
#     return (r)

# send("Test",[7021513100])
# http://api.msg91.com/api/sendhttp.php


authkey = 'key will get on google search'
sender = 'SOCKET'
route = '4'
country = '91'
def send(message, mobiles):
    PARAMS = {'sender': sender, 'route': route, 'country' : country, 'message': message, 'mobiles': mobiles, 'authkey': authkey}
    UI_URL = 'http://api.msg91.com/api/sendhttp.php'
    # HEADERS = {'authkey': apikey, 'content-type': 'application/json'}
    res = requests.get(url = UI_URL, params = PARAMS) 
    # r = json.loads(res.text)
    print(res)
return (res)

Credits

Aniket Chavan

Aniket Chavan

0 projects • 0 followers

Comments

Add projectSign up / Login