Any help would be much appreciated!
I'm working on a project where I'm listening to 8 pins (7,11,12,13,15,16,18,22) for pulses (falling edges on the pins which have been pulled up).
Currently I'm using threaded callbacks (see code) to run the following SQL statement which just inserts a row into a table each time there is a pulse.
Code: Select all
"""INSERT INTO t_pulses
(pulse_id, pin_number, pulse_timestamp)
VALUES
({}, {}, NOW());""".format(count, channel)My Problem:
My code seems to escape with the following error 70% of the time, whereas it works the other 30% of the time.
It runs the callback several times (more times the longer I hold the pin shorted) and then escapes with an error.
The error message that is printed is as follows:
My table:<class '_mysql_exceptions.OperationalError'>
pulse_id int(11) Not null default null
pin_number varchar(250) Not null default null
pulse_timestamp datetime Not null default null
My Code:
Code: Select all
#testing threaded callbacks
import RPi.GPIO as GPIO
import MySQLdb
import sys
from time import sleep
global count
global db
global cursor
def test(channel):
global count
global db
global cursor
count = count + 1
SQL = """ INSERT INTO t_pulses
(pulse_id, pin_number, pulse_timestamp)
VALUES
({}, {}, NOW());""".format(count, channel)
print "executing {}\n".format(SQL)
try:
cursor.execute(SQL)
db.commit()
except:
err = sys.exc_info()[0]
print err
db.rollback()
def setupPin(pin):
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.FALLING, callback=test, bouncetime=300)
#--------------------=============== MAIN ==============---------------
count = 0
#db stuff
try:
db = MySQLdb.connect("localhost", "guest", "guest", "TestDB")
cursor = db.cursor()
except:
err = sys.exc_info()[0]
print err
#
GPIO.setmode(GPIO.BOARD)
setupPin(7)
setupPin(11)
setupPin(12)
setupPin(13)
setupPin(15)
setupPin(16)
setupPin(18)
setupPin(22)
raw_input("press any button when u want to terminate")
GPIO.cleanup()
db.close()