yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

GPIO interrupts help.

Sun Nov 24, 2013 7:07 pm

hi all on the suggestion of several people I have changed my coding from Lua to python so that I can use interrupts to cut down on cpu time however I need some help I know how to do interrupts for when the pin is shorted but how can I do one for when it opens back up. Example when door magnet switch closes from door opening it triggers but how can I get it to trigger when the door closes and the switch reopens. Here is my code so far

Code: Select all

# GPIO 23 & 17 set up as inputs, pulled up to avoid false detection.
# Both ports are wired to connect to GND on button press.
# So we'll be setting up falling edge detection for both
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# GPIO 24 set up as an input, pulled down, connected to 3V3 on button press
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# now we'll define two threaded callback functions
# these will run in another thread when our events are detected
def my_callback(channel):
    print "falling edge detected on 17"
    sndSock = socket(AF_INET,SOCK_DGRAM)
    sndSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
    msg = "xpl-stat\n{\nhop=1\nsource=johnb-monitor.python\ntarget=*\n}\nDoorOpen\n{\ninterval=5\nport=" + str(port) + "\n}\n"
    sndSock.sendto(msg,("255.255.255.255",3865))


def my_callback2(channel):
    print "falling edge detected on 23"

# when a falling edge is detected on port 17, regardless of whatever 
# else is happening in the program, the function my_callback will be run
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)

# when a falling edge is detected on port 23, regardless of whatever 
# else is happening in the program, the function my_callback2 will be run
# 'bouncetime=300' includes the bounce control written into interrupts2a.py
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2, bouncetime=300)
# Define maximum xPL message size
buff = 1500

# Define xPL base port
port = 3865

# Sub routine for sending a heartbeat
def SendHeartbeat() :
  hbSock = socket(AF_INET,SOCK_DGRAM)
  hbSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
  msg = "xpl-stat\n{\nhop=1\nsource=johnb-monitor.python\ntarget=*\n}\nhbeat.app\n{\ninterval=5\nport=" + str(port) + "\n}\n"
  hbSock.sendto(msg,("255.255.255.255",3865))


# Initialise the socket
UDPSock = socket(AF_INET,SOCK_DGRAM)
addr = ("0.0.0.0",port)

# Try and bind to the base port
try :
  UDPSock.bind(addr)
except :
  # A hub is running, so bind to a high port
  port = 50000

  addr = ("127.0.0.1",port)
  try :
    UDPSock.bind(addr)
  except :
    port += 1

print "xPL Monitor for Python, bound to port " + str(port)

SendHeartbeat()

while 1==1 :
  readable, writeable, errored = select.select([UDPSock],[],[],60)

  if len(readable) == 1 :
    data,addr = UDPSock.recvfrom(buff)
    print data

try:
    print "Waiting for rising edge on port 24"
    GPIO.wait_for_edge(24, GPIO.RISING)
    print "Rising edge detected on port 24. Here endeth the third lesson."

except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO interrupts help.

Sun Nov 24, 2013 7:21 pm

Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: GPIO interrupts help.

Sun Nov 24, 2013 11:37 pm

Thanks for the link however that made very little sense to me are you saying that I need to install that library just use the GPIO pins the way I had intended?

I did try GPIO.BOTH but wasn't able to figure out a way to trigger based on the state of the pin.

Thanks,
Yonu

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: GPIO interrupts help.

Mon Nov 25, 2013 12:18 am

well I may have found a way to gewt what I needed

I changed the add event detect to GPIO.BOTH

and changed the callback to this it seems to work the way I want. just thought in case anyone later might want something similar I would post it

def my_callback(channel):
print "Pin 17 Door open"
print "input test"
print GPIO.input(17)
if GPIO.input(17):
print ("test 1")
else:
print ("test 0")
sndSock = socket(AF_INET,SOCK_DGRAM)
sndSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
msg = "xpl-stat\n{\nhop=1\nsource=johnb-monitor.python\ntarget=*\n}\nDoorOpen\n{\ninterval=5\nport=" + str(port) + "\n}\n"
sndSock.sendto(msg,("255.255.255.255",3865))

Thanks,
Yonu

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: GPIO interrupts help.

Mon Nov 25, 2013 1:42 am

hey guys I need to pick a brain here I thought I had it working however I am getting a bunch of double triggers say it goes to close when the pins are shorted when they are opened about every 5th time instead of triggering open they give another closed here is the code I have in the callback I am hoping I am missing something and another set of eyes will help.

Code: Select all

def my_callback(channel):
    door = GPIO.input(17)
    print "new door"
    print door
    if door:
	print ("test pin 17 open")
	sndSock = socket(AF_INET,SOCK_DGRAM)
	sndSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
	msg = "xpl-stat\n{\nhop=1\nsource=johnb-monitor.python\ntarget=*\n}\nDoorOpen\n{\ninterval=5\nport=" + str(port) + "\n}\n"
	sndSock.sendto(msg,("255.255.255.255",3865))
	door = None
    else:
	print ("test pin 17 close")
	sndSock = socket(AF_INET,SOCK_DGRAM)
	sndSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
	msg = "xpl-stat\n{\nhop=1\nsource=johnb-monitor.python\ntarget=*\n}\nDoorClose\n{\ninterval=5\nport=" + str(port) + "\n}\n"
	sndSock.sendto(msg,("255.255.255.255",3865))
	door = None

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: GPIO interrupts help.

Mon Nov 25, 2013 2:56 pm

Quick question all is there a speed issue with the GPIO pins I was doing some testing to try and figure out what my code was falling out of sync and I noticed it i pressed the button I had wired for testing slowly it stays in sync if I press it rather quickly it falls out of sync.

Any ideas?

Yonu

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: GPIO interrupts help.

Tue Nov 26, 2013 1:55 am

well not sure if it was my problem but I took out the bouncetime parameter and it seems to have stopped so when I build the final PCB I will just but a capacitor in to debounce it.

Yonu

Return to “Python”