我们正在尝试使用Python中的raspberry Pi设计一个智能停车系统。
我们创建了一个包含4个字段的数据库,即Slot1 slot 2 slot3 Slot4
这是我们正在测试的停车位,如果它是空的或占用了红外传感器。
我们使用python完全设计和使用代码,并且能够实现传感器,并且工作正常。
但我们面临的唯一问题是它没有更新数据库中的值0或1(1被占用)。
我们有8个红外传感器(每个插槽2个)
唯一的问题是,如果插槽被占用,则数据库不会更新为1。
这是我们的代码。
#!/usr/bin/python
import RPi.GPIO as GPIO # import RPi.GPIO module
from time import sleep #for 2 second delay
#import MySQLdb
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(host='localhost', user='root', password='raspberry', database='m2')
cursor = mariadb_connection.cursor(buffered=True)
GPIO.setmode(GPIO.BOARD) # choose BCM or BOARD
GPIO.setup(7, GPIO.OUT) # set a port/pin as an output
GPIO.setup(11, GPIO.OUT) # set a port/pin as an output
GPIO.setup(13, GPIO.OUT) # set a port/pin as an output
GPIO.setup(15, GPIO.OUT) # set a port/pin as an output
GPIO.setup(29, GPIO.OUT) # set a port/pin as an output
GPIO.setup(31, GPIO.OUT) # set a port/pin as an output
GPIO.setup(33, GPIO.OUT) # set a port/pin as an output
GPIO.setup(35, GPIO.OUT) # set a port/pin as an output
GPIO.setup(12, GPIO.IN) # set a port/pin as an output
GPIO.setup(16, GPIO.IN) # set a port/pin as an output
GPIO.setup(18, GPIO.IN) # set a port/pin as an output
GPIO.setup(22, GPIO.IN) # set a port/pin as an output
GPIO.setup(32, GPIO.IN) # set a port/pin as an output
GPIO.setup(36, GPIO.IN) # set a port/pin as an output
GPIO.setup(38, GPIO.IN) # set a port/pin as an output
GPIO.setup(40, GPIO.IN) # set a port/pin as an output
while(True):
a=GPIO.input(12)
b=GPIO.input(16)
c=GPIO.input(18)
d=GPIO.input(22)
e=GPIO.input(32)
f=GPIO.input(36)
g=GPIO.input(38)
h=GPIO.input(40)
print h
left=0
right=0
if(a == e and a == 0):
sleep(2)
if(a == e and a == 0):
GPIO.output(7,0)
left=left+1
aa=1
cursor.execute("UPDATE slots SET slot1='1' WHERE id='1'")
mariadb_connection.commit()
# print a
# print e
elif((a == e and a == 1) or (a != e)):
GPIO.output(7,1)
left=left-1
aa=0
cursor.execute("UPDATE slots SET slot1='0' WHERE id='1'")
mariadb_connection.commit()
#print a
# print e
elif((a == e and a == 1) or (a != e)):
GPIO.output(7,1)
left=left-1
aa=0
cursor.execute("UPDATE slots SET slot1='0' WHERE id='1'")
mariadb_connection.commit()
#print a
#print e
if(b == f and b == 0):
sleep(2)
if(b == f and b == 0):
GPIO.output(11,0)
left=left+1
bb=1
cursor.execute("UPDATE slots SET slot2='1' WHERE id='1'")
mariadb_connection.commit()
# print b
# print f
elif((b == f and b == 1) or (b != f)):
GPIO.output(11,1)
left=left-1
bb=0
cursor.execute("UPDATE slots SET slot2='0' WHERE id='1'")
mariadb_connection.commit()
#print b
#print f
if(c == g and c == 0):
sleep(2)
if(c == g and c == 0):
GPIO.output(13,0)
right=right+1
cc=1
cursor.execute("UPDATE `slots` SET `slot3`=1 WHERE `id`=1")
mariadb_connection.commit()
# print c
# print g
elif((c == g and c == 1) or (c != g)):
GPIO.output(13,1)
right=right-1
cc=0
cursor.execute("UPDATE slots SET slot3='0' WHERE id='1'")
mariadb_connection.commit()
#print c
#print g
if(d == h and d == 0):
sleep(2)
if(d == h and d == 0):
sleep(2)
if(d == h and d == 0):
GPIO.output(15,0)
right=right+1
dd=1
cursor.execute("UPDATE slots SET slot4='1' WHERE id='1'")
mariadb_connection.commit()
# print d
# print h
elif((d == h and d == 0) or (d != h)):
GPIO.output(15,1)
right=right-1
dd=0
cursor.execute("UPDATE slots SET slot4='0' WHERE id='1'")
mariadb_connection.commit()
#print d
#print h
"""if((a == e and a == 0) or (a != e)):
GPIO.output(7,1)
left=left-1
print a
print e"""
""" if((b == f and b == 0) or (b != f)):
GPIO.output(11,1)
left=left-1
print b
print f"""
"""if((c == g and c == 0) or (c != g)):
GPIO.output(13,1)
right=right-1
print c
print g"""
"""if((d == h and d == 0) or (d != h)):
GPIO.output(15,1)
right=right-1
print d
print h"""
if((left < right and left < 2) or (left < 0)):
GPIO.output(29,1)
GPIO.output(31,1)
GPIO.output(33,0)
GPIO.output(35,0) elif(left > right and right < 2):
GPIO.output(29,1)
GPIO.output(33,1)
GPIO.output(31,0)
GPIO.output(35,0)
elif(left == right and left == 2):
GPIO.output(35,1)
GPIO.output(29,0)
GPIO.output(33,0)
GPIO.output(31,0)
else:
GPIO.output(29,1)
GPIO.output(31,1)
GPIO.output(33,0)
GPIO.output(35,0)
if(aa == 1):
aa=0
if(bb == 1):
bb=0
if(cc == 1):
cc=0
if(dd == 1):
dd=0
print left
print right
cursor.execute("SELECT slot1,slot2,slot3,slot4 FROM slots WHERE id='1'")
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
非常感谢任何帮助!