im writing a sensor program with python 3 that has a GUI and if the accelerometer detects motions it goes into an interrupt routine and saves and plots the data for a specific timeframe.

Now onto the problem, sometimes when the interrupt routine starts i get an error, sometimes it does not. The error doesnt always stop at the same location, it can stop when it goes into the readACCx/y/z() methods or when it initialises some registers outside of the interrupt routine. Can anyone make sense of the error or what i should do? Thanks already for the help

Here is the error:

interrupt 1
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
  File "/usr/lib/python3/dist-packages/pigpio.py", line 1213, in run
    cb.func(cb.gpio, newLevel, tick)
  File "/home/pi/Masterarbeit/tkinter/mix/mix.py", line 330, in my_callback1
    self.pi.i2c_read_byte_data(self.acc_sensor, FF_MT_SRC)
  File "/usr/lib/python3/dist-packages/pigpio.py", line 2874, in i2c_read_byte_data
    return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CRB, handle, reg))
  File "/usr/lib/python3/dist-packages/pigpio.py", line 1011, in _u2i
    raise error(error_text(v))
pigpio.error: 'I2C read failed'

Here is my interrupt routine. I know its pretty full but tbh i dont know how to shorten it.

Interrupt routine:

def my_callback1(self, gpio, level, tick): #first interrupt function
        print("interrupt 1")
        self.pi.i2c_read_byte_data(self.acc_sensor, FF_MT_SRC)
        print("interrupt cleared")
        print(gpio, level, tick)
        self.x_graph = []
        self.y1_graph = []
        self.y2_graph = []
        self.y3_graph = []
        a = datetime.datetime.now()
        start_time = time.time()*1000
        while(time.time()*1000-start_time) < self.timeframe:
                b = datetime.datetime.now()
                delta = b-a
            except pigpio.error as e:
                print("error:", e)
  • In the old days I would have just set a flag in the interrupt routine and then do the work in the main event loop.
    – NomadMaker
    Jul 29, 2022 at 11:41
  • Hello, thanks for the suggestion. My new code does that but since my code is mostly a gui with tkinter which has its own main loop I tried threading. So I have a thread which constantly checks for a flag and if flag is set the old callback gets executed. Sometimes the error still happens. I will see if I can update the code since I'm on vacation
    – Xenoshell
    Jul 30, 2022 at 13:03
  • @nomadmaker forums.raspberrypi.com/viewtopic.php?p=2024204#p2024204 I posted my new code there, I can't really effectively update my code on my smartphone. Would really appreciate it if you could check it out
    – Xenoshell
    Jul 30, 2022 at 13:06
  • @NomadMaker have you looked at the code? Did you maybe find something that would be interesting or looks wrong?
    – Xenoshell
    Aug 8, 2022 at 10:48
  • I'm sorry, but I don't know enough about python to say.
    – NomadMaker
    Aug 8, 2022 at 10:53


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.