这是我的光束传感器代码:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN) #Right level-1
GPIO.setup(16, GPIO.IN) #Right level-2
GPIO.setup(18, GPIO.IN) #Right level-3
while True:
if (GPIO.input(12) == 1 or GPIO.input(16) == 1 and GPIO.input(18) == 0):
print("lying down")
time.sleep(1)
elif (GPIO.input(12) == 1 or GPIO.input(16) == 1 and GPIO.input(18) == 1):
print("Sitting/Crawling")
time.sleep(1)
elif (GPIO.input(12) == 0 or GPIO.input(16) == 0 and GPIO.input(18) == 1):
print("Almost out")
time.sleep(1)
else:
print("Out of the crib")
time.sleep(1)
这是我的数据库代码:
#!/usr/bin/env python
import MySQLdb
db = MySQLdb.connect("localhost", "root", "raspberry", "cribdb")
curs=db.cursor()
curs.execute ("""INSERT INTO tbstatus values(NOT NULL, 'STATUSHERE')""")
db.commit()
number_of_rows= curs.execute("SELECT * FROM tbstatus")
if (number_of_rows <= 5):
print(number_of_rows)
else:
curs.execute("""DELETE FROM tbstatus order by id LIMIT 1""")
db.commit()
print("\n Record Deleted successfully ")
如何获取传感器打印的状态(躺下,坐姿/爬行,站立等)并将其放在此处
(curs.execute ("""INSERT INTO tbstatus values(NOT NULL, 'STATUSHERE')"""))
存储在我的数据库中。
答案 0 :(得分:0)
除非我对事情有误解,否则您可以简单地合并这两个脚本,例如
jrepl.bat /?
顺便说一句,我建议您在#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import MySQLdb
db = MySQLdb.connect("localhost", "root", "raspberry", "cribdb")
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN) # Right level-1
GPIO.setup(16, GPIO.IN) # Right level-2
GPIO.setup(18, GPIO.IN) # Right level-3
last_status = None
while True:
input_12 = GPIO.input(12)
input_16 = GPIO.input(16)
input_18 = GPIO.input(18)
if input_12 == 1 or input_16 == 1 and input_18 == 0:
status = "lying down"
elif input_12 == 1 or input_16 == 1 and input_18 == 1:
status = "Sitting/Crawling"
elif input_12 == 0 or input_16 == 0 and input_18 == 1:
status = "Almost out"
else:
status = "Out of the crib"
time.sleep(1)
if status != last_status:
print(status)
last_status = status
curs = db.cursor()
curs.execute(
"""INSERT INTO tbstatus values(NULL, %s)""", (status,)
)
db.commit()
表中添加某种类型的时间戳列,除非您已经有一个时间戳列。
另一个改进可能是仅记录状态更改时的状态。
编辑:我添加了跟踪最后状态的功能,并且仅按照评论的要求记录更改。