Da es schon etwas älter ist, hat es auch wohl länger gedauert, bis es bir mir erstmal ohne Fehlermeldung lief.
Hier der code, den ich von dem Dropbox link habe.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import os
import signal
import MySQLdb
import RPi.GPIO as GPIO
import MFRC522
import termios
from threading import Timer
from termcolor import colored, cprint
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(15, GPIO.LOW)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
# Connect to the Database
db = MySQLdb.connect(host="localhost",
user="root",
passwd="root",
db="pibar")
cursor = db.cursor()
TIMEOUT = 10 # number of seconds the user has to give an Input
def interrupted(signum, frame):
# if user has not made an Input a New user can connect
GPIO.output(12, GPIO.LOW) #Gelbe LED
GPIO.output(16, GPIO.LOW) #Grüne LED
GPIO.output(18, GPIO.HIGH) #Rote LED
cprint(' Sorry, aber da war keine Eingabe ey! ', 'white', 'on_red', attrs=['bold'])
time.sleep(4)
GPIO.output(18, GPIO.LOW)
signal.alarm()
hello()
signal.signal(signal.SIGALRM, interrupted)
def input():
try:
GPIO.output(12, GPIO.HIGH)
foo = raw_input()
return foo
except:
GPIO.output(12, GPIO.LOW)
hello()
def end_read(signal,frame):
global continue_reading
print "Ctrl+C gedrückt, Programm beendet."
continue_reading = False
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, end_read)
MIFAREReader = MFRC522.MFRC522()
def hello():
GPIO.output(12, GPIO.LOW)
GPIO.output(16, GPIO.LOW)
GPIO.output(18, GPIO.LOW)
os.system('cls' if os.name=='nt' else 'clear')
cprint (41*'*','green', 'on_grey')
print colored('* Willkommen zur PiBar v3.0 *', 'green', 'on_grey')
print colored('* Präsentiert von Tom Erber *', 'green', 'on_grey')
print colored('* Unterstützt durch Alkoholkonsum *', 'green', 'on_grey')
cprint (41*'*','green', 'on_grey')
sql = ("SELECT COUNT(customerId) AS OrdersFromCustomerID FROM orders where isPaid=0")
cursor.execute(sql)
result = cursor.fetchall()
cprint (41*'-','green', 'on_grey')
cprint(" Bitte scanne deine Benutzer Karte! ", 'white', 'on_green', attrs=['bold'])
cprint (41*'-','green', 'on_grey')
for data in result:
txt = (" " +str(data[0]))
print colored('Anzahl aller getrunken Getränke: ' ,'white', 'on_grey' ), colored(txt, 'yellow', 'on_grey')
sql =("SELECT CONCAT(c.firstName,' ', c.lastName ) AS 'Name', SUM( p.price ) AS 'Money' FROM customers c LEFT JOIN orders o ON o.customerId = c.id LEFT JOIN products p ON p.id = o.productId WHERE o.isPaid =0")
cursor.execute(sql)
result = cursor.fetchall()
for data in result:
print colored('Wert aller getrunken Getränke:','white', 'on_grey' ),colored(str(data[1])+' €', 'yellow', 'on_grey')
scan()
def scan():
read = True
while read:
GPIO.output(16, GPIO.HIGH)
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
GPIO.output(16, GPIO.LOW)
time.sleep(0.60)
(status,uid) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
cardid = str(uid[0]) +str(uid[1]) +str(uid[2]) +str(uid[3]) #+str(uid[4])
read = False
if db.open:
who_am_i(cardid)
else:
db.close
cursor = db.cursor()
GPIO.output(12, GPIO.LOW)
def admin():
cprint (' ADMIN MODUS!!!1! ', 'red', 'on_white')
read = True
cardid = "NOPE"
cprint (' Scanne einen Benutzer zum Stornieren ', 'white', 'on_red')
while read:
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
GPIO.output(18, GPIO.HIGH)
GPIO.output(12, GPIO.HIGH)
GPIO.output(16, GPIO.HIGH)
time.sleep(0.5)
(status,uid) = MIFAREReader.MFRC522_Anticoll()
GPIO.output(18, GPIO.LOW)
GPIO.output(12, GPIO.LOW)
GPIO.output(16, GPIO.LOW)
if status == MIFAREReader.MI_OK:
cardid = str(uid[0]) +str(uid[1]) +str(uid[2]) +str(uid[3]) #+str(uid[4])
read = False
cursor.execute("SELECT * FROM customers where tagid = " +cardid)
if not cursor.rowcount:
sql =("INSERT INTO customers (tagid, firstName, lastName, admin, userCard) VALUES(%s, %s, 'Blanko', 0, %s)")
timer = time.strftime("%H:%M:%S")
cursor.execute(sql, (cardid, timer, timer))
db.commit()
GPIO.output(16, GPIO.HIGH)
cprint('Neuen vorübergehenden Benutzer angelegt!', 'white', 'on_red', attrs=['bold'])
time.sleep(2)
GPIO.output(16, GPIO.LOW)
db.close
hello()
else:
result = cursor.fetchall()
for data in result:
userid = data[0]
cursor.execute("DELETE FROM orders WHERE customerId="+str(userid)+" AND isPaid=0 ORDER BY orderDate DESC LIMIT 1")
cprint(' Letztes Produkt gelöscht! ', 'white', 'on_red', attrs=['bold'])
db.commit()
GPIO.output(16, GPIO.HIGH)
time.sleep(2)
GPIO.output(16, GPIO.LOW)
db.close
hello()
def who_am_i(cardid):
GPIO.output(15, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(15, GPIO.LOW)
cursor.execute("SELECT * FROM customers where tagid = " +cardid)
if not cursor.rowcount:
GPIO.output(18, GPIO.HIGH)
cprint(' Sorry aber den Benutzer gibt es nicht! ', 'white', 'on_red', attrs=['bold'])
print time.strftime("%H:%M:%S")
time.sleep(2)
GPIO.output(18, GPIO.LOW)
db.close
hello()
else:
os.system('cls' if os.name=='nt' else 'clear')
result = cursor.fetchall()
for data in result:
userid = data[0]
name = (str(data[2]) +" "+str(data[3]))
if str(data[4]) == "1":
admin()
else:
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
sql =("SELECT CONCAT_WS( '', c.firstName, c.lastName ) AS 'Name', SUM( p.price ) AS 'Offener Betrag' FROM customers c LEFT JOIN orders o ON o.customerId = c.id LEFT JOIN products p ON p.id = o.productId WHERE c.id = %s AND o.isPaid =0 GROUP BY c.id")
cursor.execute(sql, (userid))
result = cursor.fetchall()
txt = "0.0 €"
for data in result:
txt = (str(data[1]) +" €")
#colored('Your account: ' ,'blue', 'on_white' ), colored(txt, 'white', 'on_red')
text= ("Hallo, " +name)
while len(text)< 41:
text = text + " "
cprint (text, 'blue', 'on_white')
while len(txt)< 19:
txt = txt + " "
print colored('Auf deiner Karte sind: ' ,'blue', 'on_white' ), colored(txt ,'white', 'on_red', attrs=['bold'])
cprint (41*'-','green', 'on_grey')
cprint(' Bitte scanne ein Produkt! ', 'white', 'on_green', attrs=['bold'])
cprint (41*'-','green', 'on_grey')
signal.alarm(TIMEOUT)
ean = input()
signal.alarm(0)
os.system('cls' if os.name=='nt' else 'clear')
cursor.execute("SELECT * FROM products where ean = '" +ean+"'")
if not cursor.rowcount:
GPIO.output(18, GPIO.HIGH)
cprint('Sorry aber das Produkt kenne ich nicht!', 'white', 'on_red', attrs=['bold'])
time.sleep(3)
db.close
hello()
else:
result = cursor.fetchall()
for data in result:
drinkid = data[0]
txt = (str(data[2]) +" ")
platzhalter = ""
txt2 = ("-> " +str(data[3])+" €")
while len(txt)+len(txt2)+len(platzhalter)< 42:
platzhalter = platzhalter + " "
print colored (txt+platzhalter, 'blue', 'on_white'), colored(txt2, 'red', 'on_white')
sql =("INSERT INTO orders (customerId, productId) VALUES(%s, %s)")
cursor.execute(sql, (userid,drinkid))
db.commit()
sql =("SELECT CONCAT_WS( '', c.firstName, c.lastName ) AS 'Name', SUM( p.price ) AS 'Offener Betrag' FROM customers c LEFT JOIN orders o ON o.customerId = c.id LEFT JOIN products p ON p.id = o.productId WHERE c.id = %s AND o.isPaid =0 GROUP BY c.id")
cursor.execute(sql, (userid))
result = cursor.fetchall()
for data in result:
txt = (str(data[1]) +" €")
platzhalter =""
while len(txt)+len(platzhalter)< 21:
platzhalter = platzhalter + " "
print colored('Jetzt auf der Karte: '+ platzhalter ,'blue', 'on_white' ), colored(txt, 'white', 'on_red')
sql = ("SELECT COUNT(customerId) AS OrdersFromCustomerID FROM orders WHERE customerId=%s")
cursor.execute(sql, (userid))
result = cursor.fetchall()
for data in result:
txt = (" " +str(data[0]))
platzhalter = ""
while len(txt)+len(platzhalter)< 18:
platzhalter = platzhalter + " "
print colored('Anzahl von Getränken: '+platzhalter ,'blue', 'on_white' ), colored(txt, 'yellow', 'on_white')
time.sleep(6.5)
db.close
hello()
hello()
Display More