bloomer
Posts: 5
Joined: Fri Feb 23, 2018 3:11 pm

Interesting threaded callback issue

Mon Mar 12, 2018 5:49 pm

I'm using the rising and falling edges of a physical contact of a relay to initiate threaded callbacks like this:

def status_print(channel):
if GPIO.input(22) == 1:
with open('/home/pi/cp_v1log.txt', 'a') as f0in:
f0in.write('opened @ %s\n' % noww)
f0in.close()
elif GPIO.input(22) == 0:
with open('/home/pi/cp_v1log.txt', 'a') as f0in:
f0in.write('closed @ %s\n' % noww)
f0in.close()

GPIO.add_event_detect(22, GPIO.BOTH, callback=status_print, bouncetime=200)

This logs activity in 'cp_v1log.txt'. Everything works great but the logs show an odd behavior. This file is also written to by the command to switch the relay and appears like this:

commanded open @ 03-11-2018 08:56:12
opened @ 03-12-2018 08:56:12
commanded open @ 03-11-2018 12:32:10
opened @ 03-12-2018 12:32:10

... and so on. But sometimes I get this:

commanded open @ 03-12-2018 08:58:20
opened @ 03-12-2018 08:58:20
opened @ 03-12-2018 09:26:59
opened @ 03-12-2018 09:53:46
opened @ 03-12-2018 10:44:36

The rising and falling edges are reading the same set of contacts, so the contacts aren't opening because there are no 'closing' entries. What would cause the rising edge to repeatedly read?

User avatar
joan
Posts: 13540
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Interesting threaded callback issue

Mon Mar 12, 2018 5:54 pm

You are using an interrupt to trigger the call to your code.

Your mistake is to expect nothing has happened after or during the interrupt which triggered your code (possibly milliseconds after the interrupt).

Heater
Posts: 9707
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interesting threaded callback issue

Mon Mar 12, 2018 6:09 pm

Typically a mechanical switch or relay will bounce when it closes. Such contact bounce can cause many transitions on your input pin and hence trigger many calls to your interrupt handler. All in the space of a millisecond or so.

You have to debounce such switch closures in hardware or in your software.

bloomer
Posts: 5
Joined: Fri Feb 23, 2018 3:11 pm

Re: Interesting threaded callback issue

Mon Mar 12, 2018 6:15 pm

.... but look at the entries. I have a debounce set for 200ms, but a 20-30 minute bounce seems a little long to me.

Heater
Posts: 9707
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interesting threaded callback issue

Mon Mar 12, 2018 6:23 pm

Sorry, I did not bother reading your code as it is not wrapped in code tags and therefore illegible.

When this relay is open does that mean it is not electrically connected to anything? If so it can be floating up and down under the influence of whatever electrical disturbance is around. Which will cause spurious transitions.

You will need to take care of that with a pull up resistor.

Heater
Posts: 9707
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interesting threaded callback issue

Mon Mar 12, 2018 6:26 pm

It would be better to have two call backs, one for open and one for close. Which is what I think joan was hinting at.

emikosan
Posts: 2
Joined: Tue Mar 13, 2018 12:34 pm

Re: Interesting threaded callback issue

Wed Mar 14, 2018 2:16 am

But the entries Bloomer asked you to look at were from a text file. Would you normally wrap content from a text file in code tags?

Heater
Posts: 9707
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interesting threaded callback issue

Wed Mar 14, 2018 4:43 am

emikosan,
Would you normally wrap content from a text file in code tags?
Depends on what the content of the text file is.

If it's commands typed into a shell and their output then quite likely yes. As is common practice. For example, as seen in these instructions:
https://www.cockroachlabs.com/docs/stab ... uster.html

User avatar
joan
Posts: 13540
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Interesting threaded callback issue

Wed Mar 14, 2018 7:49 am

We wouldn't have to guess at what the Python was doing if it was in code quotes.

You have a bouncetime of 200. I think that's milliseconds. The bouncetime parameter is not a classical debounce, it is used to ignore subsequent events for 200 milliseconds, not to ignore an event of less than 200 milliseconds.

Return to “General discussion”

Who is online

Users browsing this forum: Baidu [Spider], travisfarmer and 28 guests