commit 119ac3fcb225c49dc58156fae41bf268c8cfec70 Author: Carlos Solís Date: Mon Aug 14 12:08:26 2017 -0600 Reimporting GIT repository diff --git a/Presentación.odp b/Presentación.odp new file mode 100644 index 0000000..e538746 Binary files /dev/null and b/Presentación.odp differ diff --git a/ProyectoPráctico.odt b/ProyectoPráctico.odt new file mode 100644 index 0000000..a029ee4 Binary files /dev/null and b/ProyectoPráctico.odt differ diff --git a/ProyectoPrácticoV2.odt b/ProyectoPrácticoV2.odt new file mode 100644 index 0000000..e4d4dc7 Binary files /dev/null and b/ProyectoPrácticoV2.odt differ diff --git a/ProyectoPrácticoV3.odt b/ProyectoPrácticoV3.odt new file mode 100644 index 0000000..17dc15b Binary files /dev/null and b/ProyectoPrácticoV3.odt differ diff --git a/ProyectoPrácticoV4.odt b/ProyectoPrácticoV4.odt new file mode 100644 index 0000000..00f1dbc Binary files /dev/null and b/ProyectoPrácticoV4.odt differ diff --git a/ProyectoPrácticoV5.odt b/ProyectoPrácticoV5.odt new file mode 100644 index 0000000..d7b1b09 Binary files /dev/null and b/ProyectoPrácticoV5.odt differ diff --git a/db.csv b/db.csv new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..789bcb5 --- /dev/null +++ b/main.py @@ -0,0 +1,105 @@ +import RPi.GPIO as GPIO #This program must be executed as root. +from subprocess import check_output +from re import search +import csv +import datetime +import threading +from time import sleep + +#Define file name here! +fileName = '/home/pi/Desktop/db.csv' +#Define timeout here! +#timeoutSecs = 5 * 60 #5 minutes +timeoutSecs = 5 +#Define number of alarm seconds here! +cycles = 5 + +def readIWList(): + storage = [] + #now = datetime.datetime.strptime(str(datetime.datetime.utcnow()), "%Y-%m-%d %H:%M:%S.%f") + now = datetime.datetime.utcnow() + output = check_output("iwlist wlan0 scan |egrep ' - Address: '", shell=True) #Dependency: IWList + for i in output.splitlines(True): + arrayElement = search('([0-9A-F]{2}([:-]|$)){6}', i).group() #RegExp to obtain the MAC Address + storage.append([arrayElement, now, 0]) #Dirty bit defaults to clean + return storage + +def writeFile(storage, file): + with open (file, 'wb') as db: + dbfile = csv.writer(db, delimiter='\t', quotechar='"') + for j in storage: + dbfile.writerow(j); + +def readFile(file): + storage = [] + with open(file, 'rb') as db: + dbfile = csv.reader(db, delimiter='\t', quotechar='"') + for dbrow in dbfile: + then = datetime.datetime.strptime(dbrow[1], "%Y-%m-%d %H:%M:%S.%f") #Date reconversion + storage.append([dbrow[0], then, dbrow[2]]) + return storage + +def compareTime(time): + now = datetime.datetime.utcnow() + #Timeout is defined, in seconds, at the beginning of the file. + timeout = datetime.timedelta(seconds = timeoutSecs) + val = 0 + if ( abs(now - time) > timeout ): + #print "Timeout: " + str(abs(now - time)) + return 1 + return val + +def buttonPressed(): + #print "Button pressed." + storage = readIWList() + file = readFile(fileName) + for x in file: + for y in storage: + if x[0] == y[0]: + file.remove(x) + #print "Removed " + x[0] + " from the list." + for y in storage: + file.append(y) + writeFile(file, fileName) + +def cyclicProcess(): + file = readFile(fileName) + for x in file: + if int(x[2]) == 0: + if compareTime(x[1]) == 1: + x[2] = 1 #Mark dirty bit, timeout achieved + #print "Timeout achieved for " + str(x[0]) + isAlarmTriggered = False + storage = readIWList() + for x in file: + for y in storage: + if x[0] == y[0]: + if x[2] == 1: + isAlarmTriggered = True + file.remove(x) + #print "Removed " + x[0] + " from the list." + if isAlarmTriggered: + buzzAlarm() + #print "Alarm activated!" + writeFile(file, fileName) + threading.Timer(timeoutSecs, cyclicProcess,()).start() + +def buzzAlarm(): + for n in xrange(0, cycles): + GPIO.output(18, 1) + sleep(0.5) + GPIO.output(18, 0) + sleep(0.5) + +#def main(): +GPIO.setmode(GPIO.BCM) +GPIO.setup(24, GPIO.IN) +GPIO.setup(18, GPIO.OUT) +threading.Timer(timeoutSecs, cyclicProcess,()).start() +try: + while True: + sleep(0.1) + if GPIO.input(24): + buttonPressed() +except: + GPIO.cleanup() #Resets the GPIO ports.