Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有RPi(python3)运行时错误的AlarmSystem:已为此GPIO通道启用冲突边缘检测_Python_Python 3.x - Fatal编程技术网

带有RPi(python3)运行时错误的AlarmSystem:已为此GPIO通道启用冲突边缘检测

带有RPi(python3)运行时错误的AlarmSystem:已为此GPIO通道启用冲突边缘检测,python,python-3.x,Python,Python 3.x,我想用Python3中的RPi做一个报警系统。但是在第二次尝试之后,出现了两个错误。 这是完整的代码(用ThonnyIDE编写): 以下是出现的错误: 请不要关上这个。我需要确切的帮助。 这不起作用: 问候语很难真正说出您的报警系统应该做什么,但这里有一个简化版本(省略了代码输入位) 这里的想法是,每个程序只设置一次输入GPIO引脚,sensor\u回调只设置loop()中的主循环等待的线程事件;设置事件后,播放音乐并闪烁 根据注释,blinkenlights现在通过背景线程闪烁 导入线程

我想用Python3中的RPi做一个报警系统。但是在第二次尝试之后,出现了两个错误。 这是完整的代码(用ThonnyIDE编写):

以下是出现的错误:

  • 请不要关上这个。我需要确切的帮助。 这不起作用:


    问候语

    很难真正说出您的报警系统应该做什么,但这里有一个简化版本(省略了代码输入位)

    这里的想法是,每个程序只设置一次输入GPIO引脚,
    sensor\u回调
    只设置
    loop()
    中的主循环等待的线程事件;设置事件后,播放音乐并闪烁

    根据注释,blinkenlights现在通过背景线程闪烁

    导入线程
    导入时间
    将RPi.GPIO导入为GPIO
    导入pygame
    传感器针脚=23
    红色针=4
    绿色引脚=17
    蓝色针脚=27
    trip_event=threading.event()
    quit_event=threading.event()
    闪烁灯光=错误
    def重置_灯():
    全球闪烁灯
    GPIO.output(蓝色_引脚,GPIO.LOW)
    GPIO.output(红色_引脚,GPIO.LOW)
    GPIO.output(绿色_引脚,GPIO.LOW)
    闪烁灯光=错误
    def blinkenlights_thread_callback():
    #循环直到被告知退出。
    而不是退出事件。等待(0.25):
    对于范围(5)内的x:
    如果没有闪烁指示灯:#允许中断闪烁周期。
    打破
    GPIO.output(绿色_引脚,GPIO.HIGH)
    睡眠时间(0.50)
    GPIO.output(绿色_引脚,GPIO.LOW)
    睡眠时间(0.50)
    def传感器_回调(通道):
    trip_事件集()
    def prompt_code_直到_纠正(提示,纠正_code):
    尽管如此:
    如果输入(提示)=正确的\u代码:
    打破
    打印(“错误代码”)
    def loop():
    全球闪烁灯
    #(1)激活码提示
    提示\u代码\u直到\u正确(“输入激活代码:”,“123”)
    trip_event.clear()#如果在提示期间传感器跳闸。
    #(2)等待警报触发
    尽管如此:
    跳闸事件。等待(超时=0.5)
    如果跳闸事件已设置():
    跳闸事件清除()
    重置灯光()
    闪烁灯=真
    pygame.mixer.music.play()
    提示\u代码\u直到\u正确(“输入解除武装代码:”,“123”)
    pygame.mixer.music.stop()
    重置灯光()
    休息,别等了。
    def main():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(红色引脚,GPIO.OUT)
    GPIO.setup(绿色引脚,GPIO.OUT)
    GPIO.setup(蓝色引脚,GPIO.OUT)
    GPIO.setup(传感器引脚,GPIO.IN)
    GPIO.add\u event\u detect(传感器引脚,GPIO.RISING,回调=传感器回调)
    blinkenlights\u线程=线程。线程(目标=blinkenlights\u线程回调)
    blinkenlights_thread.start()#线程将被“退出”事件停止`
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load(“alarm.mp3”)
    尝试:
    尽管如此:
    循环()
    最后:
    退出_event.set()
    重置灯光()
    GPIO.cleanup()
    如果名称=“\uuuuu main\uuuuuuuu”:
    main()
    
    该消息暗示您应该在代码中只调用一次
    add\u event\u detect
    。现在它在
    aktiv_ierung()
    中每次都被调用。谢谢你的回答,但这对我来说太复杂了。我是python3的初学者。有没有更简单的方法?这并不比你原来的方法复杂多少。你搞混了什么?公平-我添加了一些位来要求输入代码。希望有帮助。最后一个问题。您知道如何在警报声响起时使LED闪烁吗@阿克西认为这个问题可能会出现。您需要另一个后台线程来管理它,因为
    input()
    是同步的。我可以补充答案。。。
    import RPi.GPIO as GPIO
    import time
    import pygame
    import itertools
    import threading
    import sys
    import os
    
    
    GPIO.setwarnings(False)
    
    SENSOR_PIN = 23
    rot = 4
    gruen = 17
    blau = 27
    
    #GPIO
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(rot, GPIO.OUT)
    GPIO.setup(gruen, GPIO.OUT)
    GPIO.setup(blau, GPIO.OUT)
    GPIO.setup(SENSOR_PIN, GPIO.IN) 
    
    
    done = False
    def animate():
        for c in itertools.cycle(['|', '/', '-', '\\']):
            if done:
                break
            sys.stdout.write('\rLaden... ' + c)
            sys.stdout.flush()
            time.sleep(0.1)
            sys.stdout.write('\rFertig! ')
    
    t = threading.Thread(target=animate)
    t.start()
    
    time.sleep(5)
    done = True
    time.sleep(1)
    
    sys.stdout.write('\rWillkommen\n ')
    
    def aktiv_ierung(): 
     while True:
      GPIO.output(blau, GPIO.LOW)
      GPIO.output(rot, GPIO.LOW)
      GPIO.output(gruen, GPIO.HIGH) 
      an = input('CODE EINGEBEN UM ALARM ZU AKTIVIEREN: ')   
      if an == '123':
       pygame.mixer.init()
       pygame.mixer.music.load("aktiv.mp3")
       pygame.mixer.music.play()
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.HIGH)
       time.sleep(0.50)
       GPIO.output(gruen, GPIO.LOW)
       time.sleep(1.00)
       GPIO.output(rot, GPIO.HIGH)
       print('ALARMANLAGE SCHARF')
       try:
         GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING , callback=mein_callback)
         while True:
            time.sleep(1)
    
       except KeyboardInterrupt:
            GPIO.cleanup()
            print('ALARM DEAKTIVIERT')
    
    def mein_callback(channel):
     GPIO.output(rot, GPIO.LOW)
     GPIO.output(blau, GPIO.HIGH)
     print('Bewegung erkannt! 5 sek.')
     time.sleep(5)
     pygame.init()
     pygame.mixer.music.load("alarm.mp3")
     pygame.mixer.music.play()
     alarmaus = input('CODE EINGEBEN UM ALARM ZU DEAKTIVIEREN:')
     if alarmaus == '123':
         pygame.mixer.music.load("mute.mp3")
         GPIO.output(blau, GPIO.LOW)
         GPIO.output(gruen, GPIO.HIGH)
         print('Alarmanlage deaktiviert')
         time.sleep(2)
         aktiv_ierung()  
    
    aktiv_ierung()
    
    File "/home/pi/Desktop/Alarmanlage/AlarmEXPERIMENT.py", line 104, in mein_callback
    aktiv_ierung() ```
    
    GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING , callback=mein_callback)
    RuntimeError: Conflicting edge detection already enabled for this GPIO channel```