Steps
sudo apt-get install php5 libapache2-mod-php5 -y#Import All the Libraries
import threading, re, time, os, sys
import sqlite3 as lite
from time import strftime
from BeautifulSoup import BeautifulSoup
import RPi.GPIO as GPIO
import subprocess
import os
from time import sleep
#Setup GPIO Pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#serverRoom
#GPIO.setup(5,GPIO.IN)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor2BoysRestRoom
#GPIO.setup(13,GPIO.IN)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor2MeetHall1
#GPIO.setup(20,GPIO.IN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor2MeetHall2
#GPIO.setup(12,GPIO.IN)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor3BoysRestRoom
#GPIO.setup(16,GPIO.IN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor1MeetHall1
#GPIO.setup(17,GPIO.IN)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Floor1BoysRestRoom
#GPIO.setup(23,GPIO.IN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Light pins
#serverRoom
GPIO.setup(6,GPIO.OUT)
#Floor2BoysRestRoom
GPIO.setup(19,GPIO.OUT)
#Floor2MeetHall1
GPIO.setup(21,GPIO.OUT)
#Floor2MeetHall2
GPIO.setup(22,GPIO.OUT)
#Floor3BoysRestRoom
GPIO.setup(26,GPIO.OUT)
#Floor1MeetHall1
GPIO.setup(27,GPIO.OUT)
#Floor1BoysRestRoom
GPIO.setup(24,GPIO.OUT)
#Define some variables
count1 = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
pollTime = 1
statusPath = "/home/pi/index.html"
#This makes sure that an HTML file exists..not completely necessary but helped while testing
os.remove("/home/pi/index.html")
with open("/home/pi/index.html", "w+") as f:
f.write("<html>\n<head><meta http-equiv='refresh' content='5'></head>\n<body>\n<div class='temp'>temperature: never</div>\n<hr />\n<div class='Floor1MeetHall1' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='Floor1BoysRestRoom' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='Floor2MeetHall1' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='Floor2MeetHall2' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='Floor2BoysRestRoom' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='Floor3BoysRestRoom' style='background-color: #008000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='ServerRoom' style='background-color: #FF0000; font-size:xx-large;'>\nCalibrating, please wait...\n</div>\n<hr />\n<div class='update'>Last updated: never</div>\n</body></html>")
f.close
#def addToDatabase(timeOpened, count, bathNumber):
#Connect to a SQLite Database I created
# con = lite.connect('bathroom.db')
# with con:
# cur = con.cursor()
# inputValues = (
# (timeOpened, count, bathNumber)
# )
# cur.executemany("INSERT INTO bath VALUES(NULL, ?, ?, ?)", (inputValues,))
def getCPUtemperature():
try:
s = subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"])
return(s)
except:
return 0
#######
def checkStatus():
# call f() again in pollTime seconds
threading.Timer(pollTime, checkStatus).start()
global count1
global count2
global count3
global count4
global count5
global count6
global count7
#################################
if (GPIO.input(23)):
#Floor1BoysRestRoom is closed
updateHTML("Floor1BoysRestRoom", 1)
GPIO.output(24, GPIO.LOW)
count5 += pollTime
else:
#Floor1BoysRestRoom is open
if (count5>0):
updateHTML("Floor1BoysRestRoom", 0)
count5 = 0
GPIO.output(24, GPIO.HIGH)
if (count5 >= 120):
updateHTML("Floor1BoysRestRoom", 3)
###################################
if (GPIO.input(17)):
#Floor1MeetHall1 is closed
updateHTML("Floor1MeetHall1", 1)
GPIO.output(27, GPIO.LOW)
count6 += pollTime
else:
#Floor1MeetHall1 is open
if (count3>0):
updateHTML("Floor1MeetHall1", 0)
count6 = 0
GPIO.output(27, GPIO.HIGH)
if (count6 >= 3600):
updateHTML("Floor1MeetHall1", 3)
#################################
##############################
if (GPIO.input(5)):
#ServerRoom is closed
updateHTML("ServerRoom", 1)
GPIO.output(6, GPIO.LOW)
count1 += pollTime
else:
#ServerRoom is open
if (count1>0):
updateHTML("ServerRoom", 0)
count1 = 0
GPIO.output(6, GPIO.HIGH)
if (count1 >= 600):
updateHTML("ServerRoom", 3)
#################################
if (GPIO.input(13)):
#Floor2BoysRestRoom is closed
updateHTML("Floor2BoysRestRoom", 1)
GPIO.output(19, GPIO.LOW)
count2 += pollTime
else:
#Floor2BoysRestRoom is open
if (count2>0):
updateHTML("Floor2BoysRestRoom", 0)
count2 = 0
GPIO.output(19, GPIO.HIGH)
if (count2 >= 120):
updateHTML("Floor2BoysRestRoom", 3)
###################################
if (GPIO.input(20)):
#Floor2MeetHall1 is closed
updateHTML("Floor2MeetHall1", 1)
GPIO.output(21, GPIO.LOW)
count3 += pollTime
else:
#Floor2MeetHall1 is open
if (count3>0):
updateHTML("Floor2MeetHall1", 0)
count3 = 0
GPIO.output(21, GPIO.HIGH)
if (count3 >= 3600):
updateHTML("Floor2MeetHall1", 3)
#################################
if (GPIO.input(12)):
#Floor2MeetHall2 is closed
updateHTML("Floor2MeetHall2", 1)
GPIO.output(22, GPIO.LOW)
count7 += pollTime
else:
#Floor2MeetHall2 is open
if (count3>0):
updateHTML("Floor2MeetHall2", 0)
count7 = 0
GPIO.output(22, GPIO.HIGH)
if (count7 >= 3600):
updateHTML("Floor2MeetHall2", 3)
#################################
if (GPIO.input(16)):
#Floor3BoysRestRoom is closed
updateHTML("Floor3BoysRestRoom", 1)
GPIO.output(26, GPIO.LOW)
count4 += pollTime
else:
#Floor3BoysRestRoom is open
if (count4>0):
updateHTML("Floor3BoysRestRoom", 0)
count4 = 0
GPIO.output(26, GPIO.HIGH)
if (count4 >= 120):
updateHTML("Floor3BoysRestRoom", 3)
###################################
def upload():
# Uploading every 15 seconds
threading.Timer(10, upload).start()
os.system('cp /home/pi/index.html /var/www/html/index.html')
def updateHTML(bathNumber, status):
with open(statusPath, "r+") as f:
data = f.read()
if (status == 1):
#Set status to closed with beautifulsoup
if (bathNumber == "ServerRoom"):
replaceString = bathNumber + " is Closed "
else:
replaceString = bathNumber + " is Occupied "
soup = BeautifulSoup(data)
div = soup.find('div', {'class': bathNumber})
if (bathNumber == "ServerRoom"):
div['style'] = 'background-color: #9932CC; font-size:xx-large;'
else:
div['style'] = 'background-color: #FF0000; font-size:xx-large;'
div.string=replaceString
#update last updated
lastUpdated = soup.find('div', {'class': 'update'})
lastUpdated.string="Updated Time:- " + strftime("%H:%M:%S")
lastUpdated = soup.find('div', {'class': 'temp'})
lastUpdated.string="Raspberry " + getCPUtemperature()
f.close
html = soup.prettify("utf-8")
with open(statusPath, "wb") as file:
file.write(html)
if (status == 0):
#Set status to open with beautifulsoup
if (bathNumber == "ServerRoom"):
replaceString = bathNumber + " is open "
else:
replaceString = bathNumber + " is Vacant !"
soup = BeautifulSoup(data)
div = soup.find('div', {'class': bathNumber})
if (bathNumber == "ServerRoom"):
div['style'] = 'background-color: #FA1D2F; font-size:xx-large;'
else:
div['style'] = 'background-color: #00FF00; font-size:xx-large;'
div.string=replaceString
#update last updated
lastUpdated = soup.find('div', {'class': 'update'})
lastUpdated.string="Updated Time:- " + strftime("%H:%M:%S")
lastUpdated = soup.find('div', {'class': 'temp'})
lastUpdated.string="Raspberry " + getCPUtemperature()
f.close
html = soup.prettify("utf-8")
with open(statusPath, "wb") as file:
file.write(html)
if (status == 3):
#If door has been closed too long
replaceString = bathNumber + " door has been closed for a long time. "
soup = BeautifulSoup(data)
div = soup.find('div', {'class': bathNumber})
if (bathNumber == "ServerRoom"):
div['style'] = 'background-color: #458B00; font-size:xx-large;'
else:
div['style'] = 'background-color: #2F4F4F; font-size:xx-large;'
div.string=replaceString
#update last updated
lastUpdated = soup.find('div', {'class': 'update'})
lastUpdated.string="Updated Time:- " + strftime("%H:%M:%S")
lastUpdated = soup.find('div', {'class': 'temp'})
lastUpdated.string="Raspberry " + getCPUtemperature()
f.close
html = soup.prettify("utf-8")
with open(statusPath, "wb") as file:
file.write(html)
#Set off inital functions so timers can kick in
checkStatus()
upload()
#########
#try:
# while True:
# if GPIO.input(5) == GPIO.HIGH:
# GPIO.output(6, GPIO.HIGH)
# else:
# GPIO.output(6, GPIO.LOW)
# sleep(0.01)
#except KeyboardInterrupt:
# pass
#GPIO.cleanup()

vedio coming soon in youtube with complete circuit.