IoT for Smart Buildings
Smart Garage Door
December 22, 2015
Project Leader: Mike Bucceroni
Team Members: Jonathan Carofilis and Nick Parisi
Introduction
Smart Garage is a technological advancement that can be applied to any garage door; both
commercially and residentially. The most precious resource that no one ever seems to have
enough of is time. Commercially, the Smart Garage makes time more efficiently spent by
workers by allowing remote entrance for guests, package deliveries, or automate the shipping
deck without even having to leave the desk. Using the app or webpage, the user can open and
close or know if the automatic feature is enabled or disabled. Besides the applications in smart
Buildings, the Smart Garage can be used for residential purposes. Those late, sleepless nights
were you panic over the constant internal debate if you closed the garage door or left the
automatic open feature enabled become worries of the past. With the Smart Garage, one can
merely just check the status of the garage from their iPhone.
Requirements
To make the product successful, the design had to achieve the two goals: just opening and
closing the garage door and performing the previous operation securely. Opening and closing the
garage door were lumped into the same goal because to perform each task required the same
design and command to the Raspberry Pi. Each goal was broken down into sub-goals desired to
achieve. For simply opening and closing the garage, the Smart Garage was required open/close
the door from the created Web Page, app, and automatically open/close upon approach by
detection of the beacon. Additionally, for product development and sales, the garage door must
follow Consumer Product Safety Commission (CPSC) regulations, Specifically, the garage door
must meet the entrapment protection requirements set by the Underwriters Laboratories (UL
325). These regulations were met through the use of magnetic door sensors and a PIR sensor.
These sensors work simultaneously to detect if there is something impeding the path of closure
of the garage door. If an item is detected, they prevent the garage door from closing. All garage
doors manufactured and sold after the date of January 1st, 1993 abide by these regulations.
Solution Architecture
Alternative Design 1
Preliminary design reviews showed that the microcontroller of choice (Raspberry Pi) was a bit
overpowered and the price of the overall design of the smart garage door could be decreased
through use of a cheaper microcontroller. An alternative microcontroller that offered enough
capabilities was the ESP8266. This WiFi enabled microcontroller, when coupled with an
adafruit bluetooth module and an arduino, provided a barebones internet capable module that
could be used to host a web server. The concept was to design a web server to open and close
the garage door, hosted on the ESP8266, that could communicate with with a wireless bluetooth
module. This bluetooth module would act as a beacon and when it is within a designated range
of the garage door, it could open the garage door automatically. Unfortunately, due to Rowan
Secure’s authentication process, the ESP8266 was not a realizable design.
Final Design
Resorting back to original design plans, the Raspberry Pi provided an easy to use setup with
plenty of room for future design improvements and capabilities to integrate with other
technologies. Leveraging the Pi’s internet capabilities, a modified Linux distribution known as
WebIOPi was installed. This platform provided IoT capabilities to the Pi through a quick and
easy to use interface. Through use of HTTP protocols, a web server with authentication is easily
configured using WebIOPi’s mainframe. Fig. 1 shows the initial webpage configuration that is
available out of the box.
Fig. 1: WebIOPi Webpage Layout
The webserver is configured in a manner that allows the user to control the GPIO pins on the Pi.
Each pin can be controlled individually through use of the a button on the web server. Designed
intuitively, the web server allows the user to control each individual pin through HTML button
controls and can output a maximum of 3.3V. The out of the box functionality is impressive, but
unnecessary for the smart garage door. The design was remastered to fit the application of a
garage door in which 5V is required to trigger a relay to open or close the door. A transistor was
used to amplify the digital logic voltage of the pin and was fed to the input of the relay. The
webpage was remastered to include a single button for opening and closing the garage door that
corresponds to a single GPIO pin (Pin 23). The second button is used as a form of security to
read the state of the garage door. Fig. 2 shows the final layout design of the webpage.
Fig. 2: Smart Garage Door Webpage Layout
The webpage was generated through use of HTML code, which can be found in Appendix C. In
order to tie it to the functionality of the GPIO pins of the Pi, an additional python script was
generated, which can be found in Appendix B.
Abiding by the entrapment regulation UL 235, an ultrasonic distance sensor is used to detect an
obstruction in the path of the garage door. Additionally, most garage doors come equipped with
IR beam sensors that perform similar functionality. The code of the Pi is structured in a manner
that allows for the integration of these sensors as interrupts. Currently, the code simply detects
an obstruction and attempts to close until the obstruction is moved.
Application Hardware
Fig. 3: Ultrasonic Sensor Pinout
Fig.4: Ultrasonic Sensor and Relay Breakout Board
Application Software
The code written for the Smart Garage was written in a Python script, HTML code, and iPHONE
code, which can be found in Appendices B, C, and D, respectively.
Testing
Web Page
The first test done to verify that the web page was working was done by loading both the Python
script and the HTML code onto the Raspberry Pi. This was done through Linux using the
command $ sudo nano /etc/init.d/webiopi. This will run a singular instance of WebIOPi until
shutdown. The Pi’s boot sequence can also be modified so that the Pi runs WebIOPi upon
bootup through use of the command $ sudo update-rc.d webiopi defaults. If the load was
successful, the webpage would ask you to enter a username and password. The username is
webiopi, and the password is raspberry. The successful username and password combination
would bring the user to the website. To test the Smart Garage, the user would click the OPEN
button. If the leads for the actual garage door were not connected to the board, the relay would
make a click sound when successful because the relay was used to power the circuit once the
code sent the open command. If the leads were connected, the motor would start and that familiar
garage door sound could be heard.
Beacon
The beacon was originally purchased and designed for use with alternative design of the
ESP8266. It taps into the geolocation capabilities of any smartphone and provides a simple
method of address pinging to detect how near or far a given device is located. The smart garage
uses this technology to find the location of a smartphone. Upon approach with the beacon, a
device is detected and allows the door to automatically open within a given proximity. The
distance of the proximity is specified by the beacon, approximately 65 feet. The beacon was an
additional feature that allowed the user to open the garage door without the need for an app or
pressing any button. It automates the process a step further and is ideal within the confines of a
home or business.
Future Work
Unfortunately, much to Apple’s disappointment, the world does not solely operate on iPhones. In
order for the Smart Garage to reach more customers and be more applicable for residentials users
without iPhones, an Android platform for the app will be developed. This will make the Smart
Garage be applicable now to anyone with a Smart Phone. Another addition to the app and web
page would be to have a third status bar that updates when the garage is opening or closing and
sends a notification to the mobile platform. This is just another possible security feature the
product could bring to Smart Buildings or at home. If there is ever anyone breaking into your
office building or home through the garage, you will be updated that the garage is updating. If
security would like to be taken even further, the Raspberry Pi could be used to film the garage
door to see who is entering/would like to enter and play a live security feed on the web page.
References
http://www.cpsc.gov/en/Newsroom/News-Releases/1993/Safety-Commission-Publishes-Final-
Rules-For-Automatic-Garage-Door-Openers/
https://learn.adafruit.com/downloads/pdf/raspberry-pi-garage-door-opener.pdf
Appendices
Appendix A: Bill of Materials
Table1: Bill of Materials
Material
Name
Quantity Cost
Raspberry Pi - B 1 $25.49
SPDT 1A 5 Volt Relay 1 $4.99
1 K Ohm Resistor 2 $0.00
2 K Ohm Resistor 2 $0.00
3 K Ohm Resistor 1 $0.00
2N3904 BJT 1 $0.00
1N5817 Diode 1 $0.00
Garage Door 1 $0.00
Ultrasonic Distance
Sensor
2 $0.00
Adafruit Bluefruit LE
UART Bluetooth
Beacon
1 $17.99
Micro USB Charger 1 $0.00
Appendix B: Python Script
# Imports
import webiopi
import time
# Enable debug output
webiopi.setDebug()
# Retrieve GPIO lib
GPIO = webiopi.GPIO
insideTrigger = 25
insideEcho = 24
outsideTrigger = 20
outsideEcho = 16
reedSwitch = 12
lockState = True
lockText = 'LOCKED'
doorState = 0 #0=closed 1=opening 2=open 3=closing 4=stopped
doorText = 'CLOSED'
oldInRange = 0
oldOutRange = 0
autoOpen = False
timePass = time.time()
# Called by WebIOPi at script loading
def setup():
global oldInRange
global oldOutRange
global timePass
timepass = time.time()
webiopi.debug("Script with macros - Setup")
# Setup GPIOs
GPIO.setFunction(23,GPIO.OUT)
GPIO.setFunction(insideTrigger,GPIO.OUT)
GPIO.setFunction(insideEcho,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setFunction(outsideTrigger,GPIO.OUT)
GPIO.setFunction(outsideEcho,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setFunction(reedSwitch,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
oldInRange = pingSensor(insideTrigger,insideEcho)
#oldOutRange = pingSensor(outsideTrigger,outsideEcho)
# Looped by WebIOPi
def loop():
global insideTrigger
global insideEcho
global outsideTrigger
global outsideEcho
global lockState
global lockText
global doorState
global doorText
global timePass
global autoOpen
if (GPIO.digitalRead(reedSwitch)==1 and (time.time()-timePass)>6):
doorState = 0
if doorState == 0:
if (not lockState):
if insideRange():
buttonPress(1)
elif doorState == 1:
if ((time.time()-timePass)>12):
doorState = 2
timePass = time.time()
elif doorState == 2:
if (((time.time()-timePass)>12)and autoOpen):
buttonPress(1)
elif doorState == 3:
if GPIO.digitalRead(reedSwitch)==1:
doorState = 0
elif ((time.time()-timePass)>24):
doorState = 2
buttonPress(1)
timePass = time.time()
webiopi.debug(time.time()-timePass)
webiopi.sleep(0.5)
# Called by WebIOPi at server shutdown
def destroy():
webiopi.debug("Script with macros - Destroy")
# Reset GPIO functions
# Ping the inside sensor
def insideRange():
if (pingSensor(insideTrigger,insideEcho)<(0.75*oldInRange)):
return True
else:
return False
# Ping the outside sensor
def outsideRange():
if (pingSensor(outsideTrigger,outsideEcho)<(0.75*oldOutRange)):
return True
else:
return False
def pingSensor(triggerPin,echoPin):
GPIO.digitalWrite(triggerPin,GPIO.LOW)
webiopi.sleep(2)
GPIO.digitalWrite(triggerPin,GPIO.HIGH)
webiopi.sleep(0.00001)
GPIO.digitalWrite(triggerPin,GPIO.LOW)
while GPIO.digitalRead(echoPin)==0:
pulseStart = time.time()
while GPIO.digitalRead(echoPin)==1:
pulseEnd = time.time()
pulseDuration = pulseEnd - pulseStart
distance = pulseDuration * 17150
distance = round(distance, 2)
return distance
# A macro to toggle the relay
@webiopi.macro
def buttonPress(auto):
global timePass
global doorText
global doorState
global RELAY
global autoOpen
if auto == 1:
autoOpen = True
else:
autoOpen = False
if doorState == 0:
doorState = 1
doorText = 'OPENING'
elif doorState == 1:
if ((time.time()-timePass) < 12.5):
doorState = 4
doorText = 'STOPPED'
elif doorState == 2:
doorState = 3
doorText = 'CLOSING'
elif doorState == 3:
if ((time.time()-timePass) < 12.5):
doorState = 1
doorText = 'OPENING'
elif doorState == 4:
doorState = 3
doorText = 'CLOSING'
timePass = time.time()
GPIO.digitalWrite(23,GPIO.HIGH)
webiopi.sleep(0.1)
GPIO.digitalWrite(23,GPIO.LOW)
webiopi.debug(doorText)
return doorText
# A macro to lock or unlock the auto-open functionality
@webiopi.macro
def setLock(onoff):
global lockText
global lockState
if onoff == '0':
lockState = False
elif onoff == '1':
lockState = True
elif onoff == '3':
lockState = not lockState
if lockState:
lockText = 'LOCKED'
else:
lockText = 'UNLOCKED'
webiopi.debug(lockText)
return lockText
# A macro which returns the lock state
@webiopi.macro
def getUpdate():
global doorState
global lockState
global doorText
global lockText
if lockState:
lockText = 'LOCKED'
else:
lockText = 'UNLOCKED'
if doorState == 0:
doorText = 'CLOSED'
elif doorState == 1:
doorText = 'OPENING'
elif doorState == 2:
doorText = 'OPEN'
elif doorState == 3:
doorText = 'CLOSING'
elif doorState == 4:
doorText = 'STOPPED'
webiopi.debug(doorText)
webiopi.debug(lockText)
return "%s;%s" % (doorText,lockText)
Appendix C: HTML Code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "height = device-height, width = 375, user-scalable =
no" />
<title>Smart Garage Door</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
document.addEventListener("touchmove",function(e){
e.preventDefault();
return false;
},false);
var openButtonText = "";
var lockedButtonText = "";
webiopi().ready(function() {
var content, button;
content = $("#content");
// this button will update the lock button
button = webiopi().createButton("openMacro", "OPEN", callOpenMacro);
content.append(button); // append button to content div
// this button will toggle the auto-open functionality
button = webiopi().createButton("lockMacro", "LOCKED", callLockMacro);
content.append(button); // append button to content div
webiopi().refreshGPIO(true);
setTimeout(updateButton,10);
});
function callOpenMacro() {
var args = 0;
webiopi().callMacro("buttonPress", args, openMacroCallback);
}
function openMacroCallback(macro, args, data) {
webiopi().setLabel("openMacro",data);
}
function callLockMacro() {
var args = 3;
webiopi().callMacro("setLock", args, lockMacroCallback);
}
function lockMacroCallback(macro, args, data) {
webiopi().setLabel("lockMacro",data);
}
function updateButton(){
webiopi().callMacro("getUpdate", [], getUpdateCallback);
setTimeout(updateButton,1000);
}
function getUpdateCallback(macro, args, data) {
var response = data.split(";");
webiopi().setLabel("openMacro",response[0]);
webiopi().setLabel("lockMacro",response[1]);
}
</script>
<style type="text/css">
button {
display: block;
margin: 0px 10px 10px 0px;
width: 365px;
height: 100px;
font-size: 40pt;
font-weight: bold;
color: black;
}
input[type="range"] {
display: block;
width: 160px;
height: 45px;
}
.LOW {
background-color: White;
}
.HIGH {
background-color: Red;
}
.outer{
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle{
display: table-cell;
vertical-align: middle;
}
.inner{
margin-left:auto;
margin-right:auto;
width:300;
</style>
</head>
<body>
<div class="outer">
<div class="middle">
<div class="inner" id="content" align="center">
</div>
</div>
</div>
</body>
</html>
Appendix D: iPHONE App Code
IntrotoSmartBuildingsFinalProject

IntrotoSmartBuildingsFinalProject

  • 1.
    IoT for SmartBuildings Smart Garage Door December 22, 2015 Project Leader: Mike Bucceroni Team Members: Jonathan Carofilis and Nick Parisi Introduction Smart Garage is a technological advancement that can be applied to any garage door; both commercially and residentially. The most precious resource that no one ever seems to have enough of is time. Commercially, the Smart Garage makes time more efficiently spent by workers by allowing remote entrance for guests, package deliveries, or automate the shipping deck without even having to leave the desk. Using the app or webpage, the user can open and close or know if the automatic feature is enabled or disabled. Besides the applications in smart Buildings, the Smart Garage can be used for residential purposes. Those late, sleepless nights were you panic over the constant internal debate if you closed the garage door or left the automatic open feature enabled become worries of the past. With the Smart Garage, one can merely just check the status of the garage from their iPhone. Requirements To make the product successful, the design had to achieve the two goals: just opening and closing the garage door and performing the previous operation securely. Opening and closing the garage door were lumped into the same goal because to perform each task required the same design and command to the Raspberry Pi. Each goal was broken down into sub-goals desired to achieve. For simply opening and closing the garage, the Smart Garage was required open/close the door from the created Web Page, app, and automatically open/close upon approach by detection of the beacon. Additionally, for product development and sales, the garage door must follow Consumer Product Safety Commission (CPSC) regulations, Specifically, the garage door must meet the entrapment protection requirements set by the Underwriters Laboratories (UL 325). These regulations were met through the use of magnetic door sensors and a PIR sensor. These sensors work simultaneously to detect if there is something impeding the path of closure of the garage door. If an item is detected, they prevent the garage door from closing. All garage doors manufactured and sold after the date of January 1st, 1993 abide by these regulations. Solution Architecture Alternative Design 1 Preliminary design reviews showed that the microcontroller of choice (Raspberry Pi) was a bit overpowered and the price of the overall design of the smart garage door could be decreased through use of a cheaper microcontroller. An alternative microcontroller that offered enough capabilities was the ESP8266. This WiFi enabled microcontroller, when coupled with an adafruit bluetooth module and an arduino, provided a barebones internet capable module that could be used to host a web server. The concept was to design a web server to open and close
  • 2.
    the garage door,hosted on the ESP8266, that could communicate with with a wireless bluetooth module. This bluetooth module would act as a beacon and when it is within a designated range of the garage door, it could open the garage door automatically. Unfortunately, due to Rowan Secure’s authentication process, the ESP8266 was not a realizable design. Final Design Resorting back to original design plans, the Raspberry Pi provided an easy to use setup with plenty of room for future design improvements and capabilities to integrate with other technologies. Leveraging the Pi’s internet capabilities, a modified Linux distribution known as WebIOPi was installed. This platform provided IoT capabilities to the Pi through a quick and easy to use interface. Through use of HTTP protocols, a web server with authentication is easily configured using WebIOPi’s mainframe. Fig. 1 shows the initial webpage configuration that is available out of the box. Fig. 1: WebIOPi Webpage Layout The webserver is configured in a manner that allows the user to control the GPIO pins on the Pi. Each pin can be controlled individually through use of the a button on the web server. Designed intuitively, the web server allows the user to control each individual pin through HTML button controls and can output a maximum of 3.3V. The out of the box functionality is impressive, but unnecessary for the smart garage door. The design was remastered to fit the application of a garage door in which 5V is required to trigger a relay to open or close the door. A transistor was used to amplify the digital logic voltage of the pin and was fed to the input of the relay. The webpage was remastered to include a single button for opening and closing the garage door that
  • 3.
    corresponds to asingle GPIO pin (Pin 23). The second button is used as a form of security to read the state of the garage door. Fig. 2 shows the final layout design of the webpage. Fig. 2: Smart Garage Door Webpage Layout The webpage was generated through use of HTML code, which can be found in Appendix C. In order to tie it to the functionality of the GPIO pins of the Pi, an additional python script was generated, which can be found in Appendix B. Abiding by the entrapment regulation UL 235, an ultrasonic distance sensor is used to detect an obstruction in the path of the garage door. Additionally, most garage doors come equipped with IR beam sensors that perform similar functionality. The code of the Pi is structured in a manner that allows for the integration of these sensors as interrupts. Currently, the code simply detects an obstruction and attempts to close until the obstruction is moved.
  • 4.
    Application Hardware Fig. 3:Ultrasonic Sensor Pinout
  • 5.
    Fig.4: Ultrasonic Sensorand Relay Breakout Board Application Software The code written for the Smart Garage was written in a Python script, HTML code, and iPHONE code, which can be found in Appendices B, C, and D, respectively. Testing Web Page The first test done to verify that the web page was working was done by loading both the Python script and the HTML code onto the Raspberry Pi. This was done through Linux using the command $ sudo nano /etc/init.d/webiopi. This will run a singular instance of WebIOPi until shutdown. The Pi’s boot sequence can also be modified so that the Pi runs WebIOPi upon bootup through use of the command $ sudo update-rc.d webiopi defaults. If the load was successful, the webpage would ask you to enter a username and password. The username is webiopi, and the password is raspberry. The successful username and password combination would bring the user to the website. To test the Smart Garage, the user would click the OPEN button. If the leads for the actual garage door were not connected to the board, the relay would make a click sound when successful because the relay was used to power the circuit once the code sent the open command. If the leads were connected, the motor would start and that familiar garage door sound could be heard. Beacon
  • 6.
    The beacon wasoriginally purchased and designed for use with alternative design of the ESP8266. It taps into the geolocation capabilities of any smartphone and provides a simple method of address pinging to detect how near or far a given device is located. The smart garage uses this technology to find the location of a smartphone. Upon approach with the beacon, a device is detected and allows the door to automatically open within a given proximity. The distance of the proximity is specified by the beacon, approximately 65 feet. The beacon was an additional feature that allowed the user to open the garage door without the need for an app or pressing any button. It automates the process a step further and is ideal within the confines of a home or business. Future Work Unfortunately, much to Apple’s disappointment, the world does not solely operate on iPhones. In order for the Smart Garage to reach more customers and be more applicable for residentials users without iPhones, an Android platform for the app will be developed. This will make the Smart Garage be applicable now to anyone with a Smart Phone. Another addition to the app and web page would be to have a third status bar that updates when the garage is opening or closing and sends a notification to the mobile platform. This is just another possible security feature the product could bring to Smart Buildings or at home. If there is ever anyone breaking into your office building or home through the garage, you will be updated that the garage is updating. If security would like to be taken even further, the Raspberry Pi could be used to film the garage door to see who is entering/would like to enter and play a live security feed on the web page. References http://www.cpsc.gov/en/Newsroom/News-Releases/1993/Safety-Commission-Publishes-Final- Rules-For-Automatic-Garage-Door-Openers/ https://learn.adafruit.com/downloads/pdf/raspberry-pi-garage-door-opener.pdf Appendices Appendix A: Bill of Materials Table1: Bill of Materials Material Name Quantity Cost Raspberry Pi - B 1 $25.49 SPDT 1A 5 Volt Relay 1 $4.99
  • 7.
    1 K OhmResistor 2 $0.00 2 K Ohm Resistor 2 $0.00 3 K Ohm Resistor 1 $0.00 2N3904 BJT 1 $0.00 1N5817 Diode 1 $0.00 Garage Door 1 $0.00 Ultrasonic Distance Sensor 2 $0.00 Adafruit Bluefruit LE UART Bluetooth Beacon 1 $17.99 Micro USB Charger 1 $0.00 Appendix B: Python Script # Imports import webiopi import time # Enable debug output webiopi.setDebug() # Retrieve GPIO lib GPIO = webiopi.GPIO insideTrigger = 25 insideEcho = 24
  • 8.
    outsideTrigger = 20 outsideEcho= 16 reedSwitch = 12 lockState = True lockText = 'LOCKED' doorState = 0 #0=closed 1=opening 2=open 3=closing 4=stopped doorText = 'CLOSED' oldInRange = 0 oldOutRange = 0 autoOpen = False timePass = time.time() # Called by WebIOPi at script loading def setup(): global oldInRange global oldOutRange global timePass timepass = time.time() webiopi.debug("Script with macros - Setup") # Setup GPIOs GPIO.setFunction(23,GPIO.OUT) GPIO.setFunction(insideTrigger,GPIO.OUT) GPIO.setFunction(insideEcho,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) GPIO.setFunction(outsideTrigger,GPIO.OUT) GPIO.setFunction(outsideEcho,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) GPIO.setFunction(reedSwitch,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) oldInRange = pingSensor(insideTrigger,insideEcho) #oldOutRange = pingSensor(outsideTrigger,outsideEcho) # Looped by WebIOPi def loop(): global insideTrigger global insideEcho global outsideTrigger global outsideEcho global lockState global lockText global doorState global doorText global timePass global autoOpen
  • 9.
    if (GPIO.digitalRead(reedSwitch)==1 and(time.time()-timePass)>6): doorState = 0 if doorState == 0: if (not lockState): if insideRange(): buttonPress(1) elif doorState == 1: if ((time.time()-timePass)>12): doorState = 2 timePass = time.time() elif doorState == 2: if (((time.time()-timePass)>12)and autoOpen): buttonPress(1) elif doorState == 3: if GPIO.digitalRead(reedSwitch)==1: doorState = 0 elif ((time.time()-timePass)>24): doorState = 2 buttonPress(1) timePass = time.time() webiopi.debug(time.time()-timePass) webiopi.sleep(0.5) # Called by WebIOPi at server shutdown def destroy(): webiopi.debug("Script with macros - Destroy") # Reset GPIO functions # Ping the inside sensor def insideRange(): if (pingSensor(insideTrigger,insideEcho)<(0.75*oldInRange)): return True else: return False # Ping the outside sensor def outsideRange(): if (pingSensor(outsideTrigger,outsideEcho)<(0.75*oldOutRange)): return True else: return False
  • 10.
    def pingSensor(triggerPin,echoPin): GPIO.digitalWrite(triggerPin,GPIO.LOW) webiopi.sleep(2) GPIO.digitalWrite(triggerPin,GPIO.HIGH) webiopi.sleep(0.00001) GPIO.digitalWrite(triggerPin,GPIO.LOW) while GPIO.digitalRead(echoPin)==0: pulseStart= time.time() while GPIO.digitalRead(echoPin)==1: pulseEnd = time.time() pulseDuration = pulseEnd - pulseStart distance = pulseDuration * 17150 distance = round(distance, 2) return distance # A macro to toggle the relay @webiopi.macro def buttonPress(auto): global timePass global doorText global doorState global RELAY global autoOpen if auto == 1: autoOpen = True else: autoOpen = False if doorState == 0: doorState = 1 doorText = 'OPENING' elif doorState == 1: if ((time.time()-timePass) < 12.5): doorState = 4 doorText = 'STOPPED' elif doorState == 2: doorState = 3 doorText = 'CLOSING' elif doorState == 3: if ((time.time()-timePass) < 12.5): doorState = 1
  • 11.
    doorText = 'OPENING' elifdoorState == 4: doorState = 3 doorText = 'CLOSING' timePass = time.time() GPIO.digitalWrite(23,GPIO.HIGH) webiopi.sleep(0.1) GPIO.digitalWrite(23,GPIO.LOW) webiopi.debug(doorText) return doorText # A macro to lock or unlock the auto-open functionality @webiopi.macro def setLock(onoff): global lockText global lockState if onoff == '0': lockState = False elif onoff == '1': lockState = True elif onoff == '3': lockState = not lockState if lockState: lockText = 'LOCKED' else: lockText = 'UNLOCKED' webiopi.debug(lockText) return lockText # A macro which returns the lock state @webiopi.macro def getUpdate(): global doorState global lockState global doorText global lockText if lockState: lockText = 'LOCKED' else: lockText = 'UNLOCKED' if doorState == 0:
  • 12.
    doorText = 'CLOSED' elifdoorState == 1: doorText = 'OPENING' elif doorState == 2: doorText = 'OPEN' elif doorState == 3: doorText = 'CLOSING' elif doorState == 4: doorText = 'STOPPED' webiopi.debug(doorText) webiopi.debug(lockText) return "%s;%s" % (doorText,lockText) Appendix C: HTML Code <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content = "height = device-height, width = 375, user-scalable = no" /> <title>Smart Garage Door</title> <script type="text/javascript" src="/webiopi.js"></script> <script type="text/javascript"> document.addEventListener("touchmove",function(e){ e.preventDefault(); return false; },false); var openButtonText = ""; var lockedButtonText = ""; webiopi().ready(function() { var content, button; content = $("#content"); // this button will update the lock button button = webiopi().createButton("openMacro", "OPEN", callOpenMacro); content.append(button); // append button to content div // this button will toggle the auto-open functionality button = webiopi().createButton("lockMacro", "LOCKED", callLockMacro); content.append(button); // append button to content div
  • 13.
    webiopi().refreshGPIO(true); setTimeout(updateButton,10); }); function callOpenMacro() { varargs = 0; webiopi().callMacro("buttonPress", args, openMacroCallback); } function openMacroCallback(macro, args, data) { webiopi().setLabel("openMacro",data); } function callLockMacro() { var args = 3; webiopi().callMacro("setLock", args, lockMacroCallback); } function lockMacroCallback(macro, args, data) { webiopi().setLabel("lockMacro",data); } function updateButton(){ webiopi().callMacro("getUpdate", [], getUpdateCallback); setTimeout(updateButton,1000); } function getUpdateCallback(macro, args, data) { var response = data.split(";"); webiopi().setLabel("openMacro",response[0]); webiopi().setLabel("lockMacro",response[1]); } </script> <style type="text/css"> button { display: block; margin: 0px 10px 10px 0px; width: 365px; height: 100px;
  • 14.
    font-size: 40pt; font-weight: bold; color:black; } input[type="range"] { display: block; width: 160px; height: 45px; } .LOW { background-color: White; } .HIGH { background-color: Red; } .outer{ display: table; position: absolute; height: 100%; width: 100%; } .middle{ display: table-cell; vertical-align: middle; } .inner{ margin-left:auto; margin-right:auto; width:300; </style> </head> <body> <div class="outer"> <div class="middle"> <div class="inner" id="content" align="center"> </div> </div> </div>
  • 15.