raspberry pi addEvent. Runtime error:Failed to add edge detection











up vote
0
down vote

favorite












I am writing python code on raspberry pi 3. I am registering an event on input channel 21, to check moisture detection. I am getting this error Runtime error:Failed to add edge detection.
My code is:



import RPi.GPIO as GPIO
import sys,os
import time
import datetime


channel = 21
led_output = 18
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(channel, GPIO.IN)
GPIO.setup(led_output, GPIO.OUT)


def callback(channel):
filehandle = open("output.txt", "w") or die ("Could not write out")
if GPIO.input(channel) == 1:
print ("Water Detected!")
filehandle.write("1")
GPIO.output(led_output, GPIO.LOW)
else:
print ("Water Not Detected!")
filehandle.write("0")
GPIO.output(led_output, GPIO.HIGH)
filehandle.close()




GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(channel, callback)

print(GPIO.input(channel))

while True:
time.sleep(5)









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am writing python code on raspberry pi 3. I am registering an event on input channel 21, to check moisture detection. I am getting this error Runtime error:Failed to add edge detection.
    My code is:



    import RPi.GPIO as GPIO
    import sys,os
    import time
    import datetime


    channel = 21
    led_output = 18
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(channel, GPIO.IN)
    GPIO.setup(led_output, GPIO.OUT)


    def callback(channel):
    filehandle = open("output.txt", "w") or die ("Could not write out")
    if GPIO.input(channel) == 1:
    print ("Water Detected!")
    filehandle.write("1")
    GPIO.output(led_output, GPIO.LOW)
    else:
    print ("Water Not Detected!")
    filehandle.write("0")
    GPIO.output(led_output, GPIO.HIGH)
    filehandle.close()




    GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
    GPIO.add_event_callback(channel, callback)

    print(GPIO.input(channel))

    while True:
    time.sleep(5)









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am writing python code on raspberry pi 3. I am registering an event on input channel 21, to check moisture detection. I am getting this error Runtime error:Failed to add edge detection.
      My code is:



      import RPi.GPIO as GPIO
      import sys,os
      import time
      import datetime


      channel = 21
      led_output = 18
      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(channel, GPIO.IN)
      GPIO.setup(led_output, GPIO.OUT)


      def callback(channel):
      filehandle = open("output.txt", "w") or die ("Could not write out")
      if GPIO.input(channel) == 1:
      print ("Water Detected!")
      filehandle.write("1")
      GPIO.output(led_output, GPIO.LOW)
      else:
      print ("Water Not Detected!")
      filehandle.write("0")
      GPIO.output(led_output, GPIO.HIGH)
      filehandle.close()




      GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
      GPIO.add_event_callback(channel, callback)

      print(GPIO.input(channel))

      while True:
      time.sleep(5)









      share|improve this question













      I am writing python code on raspberry pi 3. I am registering an event on input channel 21, to check moisture detection. I am getting this error Runtime error:Failed to add edge detection.
      My code is:



      import RPi.GPIO as GPIO
      import sys,os
      import time
      import datetime


      channel = 21
      led_output = 18
      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(channel, GPIO.IN)
      GPIO.setup(led_output, GPIO.OUT)


      def callback(channel):
      filehandle = open("output.txt", "w") or die ("Could not write out")
      if GPIO.input(channel) == 1:
      print ("Water Detected!")
      filehandle.write("1")
      GPIO.output(led_output, GPIO.LOW)
      else:
      print ("Water Not Detected!")
      filehandle.write("0")
      GPIO.output(led_output, GPIO.HIGH)
      filehandle.close()




      GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
      GPIO.add_event_callback(channel, callback)

      print(GPIO.input(channel))

      while True:
      time.sleep(5)






      python raspberry-pi3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 1:26









      Muhammad Raghib

      1




      1
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          When I reboot the Raspberry and run your code it works perfect.
          Only after killing the process or CTRL-C keyboard interrupting and running it again the problem/error occurs. I think this has to do with the fact that you exit the program without cleaning up properly...
          I got it working in case you exit the running the program with CTRL-C with the code below in which I included a GPIO.cleanup()
          However...this unfortunately it does not cover the situation in which you simply kill the running programm...In that case you still need to reboot.
          So there is room for improvement.
          Please re-insert your own file management commands again.



          import RPi.GPIO as GPIO  
          import sys,os
          import time
          import datetime

          channel = 21
          led_output = 18
          GPIO.setmode(GPIO.BCM)
          GPIO.setwarnings(False)

          GPIO.setup(channel, GPIO.IN)
          GPIO.setup(led_output, GPIO.OUT)

          def callback(channel):
          if GPIO.input(channel) == 1:
          print ("Water Detected!")
          GPIO.output(led_output, GPIO.LOW)
          else:
          print ("Water Not Detected!")
          GPIO.output(led_output, GPIO.HIGH)

          GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
          GPIO.add_event_callback(channel, callback)
          print(GPIO.input(channel))


          try:
          while True:
          #main loop here with some (dummy) code
          eg_set_a_dummy_variable = 0

          except KeyboardInterrupt:
          # here you put any code you want to run before the program
          # exits when you press CTRL+C
          print ("Program interrupted by CTRL-C")

          except:
          # this catches ALL other exceptions including errors.
          # You won't get any error messages for debugging
          # so only use it once your code is working
          print ("Other error or exception occurred!")

          finally:
          # this ensures a clean exit and prevents your
          # error "Runtime error:Failed to add edge detection"
          # in case you run your code for the second time after a break
          GPIO.cleanup()

          # credits to:
          # https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi





          share|improve this answer






























            up vote
            0
            down vote













            It is not very clean solution, but you can call GPIO.cleanup() at the start of your script too for case when your process was killed before and GPIO.cleanup() was not called.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245060%2fraspberry-pi-addevent-runtime-errorfailed-to-add-edge-detection%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              When I reboot the Raspberry and run your code it works perfect.
              Only after killing the process or CTRL-C keyboard interrupting and running it again the problem/error occurs. I think this has to do with the fact that you exit the program without cleaning up properly...
              I got it working in case you exit the running the program with CTRL-C with the code below in which I included a GPIO.cleanup()
              However...this unfortunately it does not cover the situation in which you simply kill the running programm...In that case you still need to reboot.
              So there is room for improvement.
              Please re-insert your own file management commands again.



              import RPi.GPIO as GPIO  
              import sys,os
              import time
              import datetime

              channel = 21
              led_output = 18
              GPIO.setmode(GPIO.BCM)
              GPIO.setwarnings(False)

              GPIO.setup(channel, GPIO.IN)
              GPIO.setup(led_output, GPIO.OUT)

              def callback(channel):
              if GPIO.input(channel) == 1:
              print ("Water Detected!")
              GPIO.output(led_output, GPIO.LOW)
              else:
              print ("Water Not Detected!")
              GPIO.output(led_output, GPIO.HIGH)

              GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
              GPIO.add_event_callback(channel, callback)
              print(GPIO.input(channel))


              try:
              while True:
              #main loop here with some (dummy) code
              eg_set_a_dummy_variable = 0

              except KeyboardInterrupt:
              # here you put any code you want to run before the program
              # exits when you press CTRL+C
              print ("Program interrupted by CTRL-C")

              except:
              # this catches ALL other exceptions including errors.
              # You won't get any error messages for debugging
              # so only use it once your code is working
              print ("Other error or exception occurred!")

              finally:
              # this ensures a clean exit and prevents your
              # error "Runtime error:Failed to add edge detection"
              # in case you run your code for the second time after a break
              GPIO.cleanup()

              # credits to:
              # https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi





              share|improve this answer



























                up vote
                0
                down vote













                When I reboot the Raspberry and run your code it works perfect.
                Only after killing the process or CTRL-C keyboard interrupting and running it again the problem/error occurs. I think this has to do with the fact that you exit the program without cleaning up properly...
                I got it working in case you exit the running the program with CTRL-C with the code below in which I included a GPIO.cleanup()
                However...this unfortunately it does not cover the situation in which you simply kill the running programm...In that case you still need to reboot.
                So there is room for improvement.
                Please re-insert your own file management commands again.



                import RPi.GPIO as GPIO  
                import sys,os
                import time
                import datetime

                channel = 21
                led_output = 18
                GPIO.setmode(GPIO.BCM)
                GPIO.setwarnings(False)

                GPIO.setup(channel, GPIO.IN)
                GPIO.setup(led_output, GPIO.OUT)

                def callback(channel):
                if GPIO.input(channel) == 1:
                print ("Water Detected!")
                GPIO.output(led_output, GPIO.LOW)
                else:
                print ("Water Not Detected!")
                GPIO.output(led_output, GPIO.HIGH)

                GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
                GPIO.add_event_callback(channel, callback)
                print(GPIO.input(channel))


                try:
                while True:
                #main loop here with some (dummy) code
                eg_set_a_dummy_variable = 0

                except KeyboardInterrupt:
                # here you put any code you want to run before the program
                # exits when you press CTRL+C
                print ("Program interrupted by CTRL-C")

                except:
                # this catches ALL other exceptions including errors.
                # You won't get any error messages for debugging
                # so only use it once your code is working
                print ("Other error or exception occurred!")

                finally:
                # this ensures a clean exit and prevents your
                # error "Runtime error:Failed to add edge detection"
                # in case you run your code for the second time after a break
                GPIO.cleanup()

                # credits to:
                # https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  When I reboot the Raspberry and run your code it works perfect.
                  Only after killing the process or CTRL-C keyboard interrupting and running it again the problem/error occurs. I think this has to do with the fact that you exit the program without cleaning up properly...
                  I got it working in case you exit the running the program with CTRL-C with the code below in which I included a GPIO.cleanup()
                  However...this unfortunately it does not cover the situation in which you simply kill the running programm...In that case you still need to reboot.
                  So there is room for improvement.
                  Please re-insert your own file management commands again.



                  import RPi.GPIO as GPIO  
                  import sys,os
                  import time
                  import datetime

                  channel = 21
                  led_output = 18
                  GPIO.setmode(GPIO.BCM)
                  GPIO.setwarnings(False)

                  GPIO.setup(channel, GPIO.IN)
                  GPIO.setup(led_output, GPIO.OUT)

                  def callback(channel):
                  if GPIO.input(channel) == 1:
                  print ("Water Detected!")
                  GPIO.output(led_output, GPIO.LOW)
                  else:
                  print ("Water Not Detected!")
                  GPIO.output(led_output, GPIO.HIGH)

                  GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
                  GPIO.add_event_callback(channel, callback)
                  print(GPIO.input(channel))


                  try:
                  while True:
                  #main loop here with some (dummy) code
                  eg_set_a_dummy_variable = 0

                  except KeyboardInterrupt:
                  # here you put any code you want to run before the program
                  # exits when you press CTRL+C
                  print ("Program interrupted by CTRL-C")

                  except:
                  # this catches ALL other exceptions including errors.
                  # You won't get any error messages for debugging
                  # so only use it once your code is working
                  print ("Other error or exception occurred!")

                  finally:
                  # this ensures a clean exit and prevents your
                  # error "Runtime error:Failed to add edge detection"
                  # in case you run your code for the second time after a break
                  GPIO.cleanup()

                  # credits to:
                  # https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi





                  share|improve this answer














                  When I reboot the Raspberry and run your code it works perfect.
                  Only after killing the process or CTRL-C keyboard interrupting and running it again the problem/error occurs. I think this has to do with the fact that you exit the program without cleaning up properly...
                  I got it working in case you exit the running the program with CTRL-C with the code below in which I included a GPIO.cleanup()
                  However...this unfortunately it does not cover the situation in which you simply kill the running programm...In that case you still need to reboot.
                  So there is room for improvement.
                  Please re-insert your own file management commands again.



                  import RPi.GPIO as GPIO  
                  import sys,os
                  import time
                  import datetime

                  channel = 21
                  led_output = 18
                  GPIO.setmode(GPIO.BCM)
                  GPIO.setwarnings(False)

                  GPIO.setup(channel, GPIO.IN)
                  GPIO.setup(led_output, GPIO.OUT)

                  def callback(channel):
                  if GPIO.input(channel) == 1:
                  print ("Water Detected!")
                  GPIO.output(led_output, GPIO.LOW)
                  else:
                  print ("Water Not Detected!")
                  GPIO.output(led_output, GPIO.HIGH)

                  GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
                  GPIO.add_event_callback(channel, callback)
                  print(GPIO.input(channel))


                  try:
                  while True:
                  #main loop here with some (dummy) code
                  eg_set_a_dummy_variable = 0

                  except KeyboardInterrupt:
                  # here you put any code you want to run before the program
                  # exits when you press CTRL+C
                  print ("Program interrupted by CTRL-C")

                  except:
                  # this catches ALL other exceptions including errors.
                  # You won't get any error messages for debugging
                  # so only use it once your code is working
                  print ("Other error or exception occurred!")

                  finally:
                  # this ensures a clean exit and prevents your
                  # error "Runtime error:Failed to add edge detection"
                  # in case you run your code for the second time after a break
                  GPIO.cleanup()

                  # credits to:
                  # https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 20:43

























                  answered Nov 11 at 14:13









                  rrrRbert360

                  367




                  367
























                      up vote
                      0
                      down vote













                      It is not very clean solution, but you can call GPIO.cleanup() at the start of your script too for case when your process was killed before and GPIO.cleanup() was not called.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        It is not very clean solution, but you can call GPIO.cleanup() at the start of your script too for case when your process was killed before and GPIO.cleanup() was not called.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          It is not very clean solution, but you can call GPIO.cleanup() at the start of your script too for case when your process was killed before and GPIO.cleanup() was not called.






                          share|improve this answer












                          It is not very clean solution, but you can call GPIO.cleanup() at the start of your script too for case when your process was killed before and GPIO.cleanup() was not called.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 14 at 7:19









                          Koxo

                          694




                          694






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245060%2fraspberry-pi-addevent-runtime-errorfailed-to-add-edge-detection%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Lugert, Oklahoma