GPIO.add_event_detect behavior - Raspberry Pi Forums


i'm having difficulty using gpio.add_event_detect in home security text alert system have been trying use old wired security system in home.

goal able turn alerts on , off via sms messages such "a on" , "a off". when "a on" sent should receive sms texts each time switch(reed, motion detector, glass break etc) in zone opens. sending "a off" should disable alerts , clean things via gpio.cleanup().

works, in that, if send "a on" 1 text when switch opens" if send "a off" text alerts stop. next time send "a on" two(duplicate) texts each time switch in zone opens. if turn system on , off 5 times 5 duplicate texts per switch opening. have stop program @ command line via ctrl-c , restart 1 text per switch opening.

guess i'm going wrong , event_detects still running in background somehow , gpio.cleanup() not stop each thread when call function destroy().

suppose sending ctrl-c interrupt via text work have not been able find method doing this.

general question gpio.add_event_detect - stable way monitor security system or should use loop poll status of each zone?

code below seems function bugs i'm trying hash out.

code: select all

#!/usr/bin/env python #working copy not ready primetime # new text interface operational   flask import flask, request twilio import twiml twilio.rest import twiliorestclient  import rpi.gpio gpio import time datetime import datetime dt     def setup():     gpio.setmode(gpio.board)       # numbers gpios physical location     gpio.setup(zone1, gpio.in, pull_up_down = gpio.pud_up)    # input mode zones pulled     gpio.setup(zone2, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone3, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone4, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone5, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone6, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone7, gpio.in, pull_up_down = gpio.pud_up)     gpio.setup(zone8, gpio.in, pull_up_down = gpio.pud_up)      def zonereport(): # simple zone status report sms     = 0     zone in zones:         += 1         if gpio.input(zone) == 1:             print('zone %d off' % (i))         else:             print('zone %d on' % (i))              def zonetextreport(): # zone change report - fires when zone loop switch opens     = 0     message_extra = ''     zone in zones:         += 1         if gpio.input(zone) == 1:             message_extra += ('zone %d off\n' % (i))         else:             message_extra += ('zone %d on\n' % (i))     return(message_extra)  #callbacks 8 alarm zones start here ------  def alertchange1(zone1):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone1 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5)      #garage doors      def alertchange2(zone2):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone2 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="+196", to="(907) 50", body=body)     time.sleep(5)  #montion detectors      def alertchange3(zone3):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone3 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)  #main level doors      def alertchange4(zone4):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone4 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5)      #glass break , audio shocks      def alertchange5(zone5):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone5 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5)      #main level windows      def alertchange6(zone6):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone6 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5)  #upstairs window , slider      def alertchange7(zone7):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone7 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5)      #fire alarm       def alertchange8(zone8):     timenow = dt.strftime(dt.now(), '%y-%m-%d %h:%m:%s')     body =('alert - zone8 changed ' + timenow)     print body     client = twiliorestclient(account='number', token='number')     client.messages.create(from_="number", to="number", body=body)     time.sleep(5))  # event detect --       def eventdetect():          # add falling edge detection , callbacks zones     gpio.add_event_detect(11, gpio.falling, callback=alertchange1, bouncetime=12000)       gpio.add_event_detect(12, gpio.falling, callback=alertchange2, bouncetime=12000)      #gpio.add_event_detect(13, gpio.falling, callback=alertchange3, bouncetime=12000) turned off debugging purposes     gpio.add_event_detect(15, gpio.falling, callback=alertchange4, bouncetime=12000)       gpio.add_event_detect(16, gpio.falling, callback=alertchange5, bouncetime=12000)     gpio.add_event_detect(18, gpio.falling, callback=alertchange6, bouncetime=12000)        gpio.add_event_detect(31, gpio.falling, callback=alertchange7, bouncetime=12000)       gpio.add_event_detect(32, gpio.falling, callback=alertchange8, bouncetime=12000)    #activates edge detection zones via sms          def activatealarm():     try:         eventdetect()     except:         pass     try:         zonereport()     except keyboardinterrupt:  # when 'ctrl+c' pressed, child program destroy()  executed.         destroy()  def destroy():     gpio.cleanup()             # release resource message_extra = ''  #text stuff via twillio -- starts here   sapp = flask(__name__)  @sapp.route('/sms', methods=['post']) def sms():     message_extra = ''      number = request.form['from']     message_body = request.form['body'].lower()          #text stuff -- end          global zone1     global zone2     global zone3     global zone4     global zone5     global zone6     global zone7     global zone8      zone1 = 11    # pin11     zone2 = 12    # pin12     zone3 = 13    # pin13     zone4 = 15    # pin15     zone5 = 16    # pin16     zone6 = 18    # pin18     zone7 = 31    # pin31     zone8 = 32    # pin32      global zones      zones = (zone1, zone2, zone3, zone4, zone5, zone6, zone7, zone8)     setup()                if message_body == "a on":  #turn text alerts on         activatealarm()         message_extra = 'alarm system monitoring active'              if message_body == 'a off': #turn text alerts off         message_extra = 'alarm system monitoring off'         destroy()              if message_body == 'a status': #get status report         message_extra = zonetextreport()      else:         pass          print message_extra #for debugging  #send twiml reponse via sms phone      resp = twiml.response()     resp.message(message_extra)     return str(resp)  if __name__ == '__main__':        sapp.run(debug=true, use_reloader=false)



raspberrypi



Comments