Raspberry Pi Code

#wait for swipe
#check user input against database
#if user exists, increment count and log
#if user doesn't exist, create new entry and log
#trigger arduino

import sqlite3 as lite
import sys
import getpass
import serial
import time
from time import strftime

masterID = ''; #master ID here
USBPORT = '/dev/ttyACM0'

def getAdmin():
    input = getpass.getpass("Get admin to swipe in order to setup account") #can also be hashed
    if input[8:16] == masterID:
        return True;
    else:
        return False;

def sendIt():
        #connect and give Arduino a sec to reboot
ser = serial.Serial(USBPORT, 9600,timeout=3)
time.sleep(2)
        #send anything to trigger
ser.write('4')
time.sleep(7)
ser.close()

con = lite.connect('master.db')
with con:
            
    cur = con.cursor()

    while True:

        input = getpass.getpass("Swipe!") #can also be hashed
        idNum = input[8:16]

        t = (idNum,)
        cur.execute("SELECT * FROM main WHERE sid= ?", t)
        entry = cur.fetchone()
            
        if entry != None:
            print "Welcome back " + entry[0]
            soda = entry[2]+1;
            cur.execute("UPDATE main SET total=? WHERE sid = ?", (soda, entry[1]))
            #log it
            timestamp = strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
            cur.execute("INSERT INTO log VALUES(?,?,?)",(entry[0], entry[1], timestamp))
            con.commit()
            sendIt() #send command to arduino
        else:
            print "welcome"
            if (getAdmin()): #wait for admin approval
                allegedName = raw_input("Name: ")
                cur.execute("INSERT INTO main VALUES(?,?,1,0,1,'temp.edu')",(allegedName, idNum))
                #log it
                timestamp = strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
                cur.execute("INSERT INTO log VALUES(?,?,?)",(allegedName, idNum, timestamp))
                con.commit()
                sendIt() #send arduino command
            else:
                print "You need admin approval."


            

Comments