连接到 Raspberry Pi 运行 Python 脚本的磁性门传感器正在报告误报

Magnetic door sensor connected to Raspberry Pi running Python script is reporting false alarms

这真是个奇怪的问题。我什至不确定如何解决它...

我为我家门口的Raspberry Pi写了一段非常简单的代码。它只是每 2 秒对 GPIO 进行一次极化,以查看电路是否已完成。它通过 GPIO 连接到磁性门传感器,例如以下之一: https://s3.amazonaws.com/assets.controlanything.com/photos/CKN6004-900.jpg

这里是 python 代码:

import os
import time
import socket
import RPi.GPIO as io
io.setmode(io.BCM)

ADDRESS = "192.168.1.118"
PORT = 1234

def doorOpened():
    "report door has been opened"
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ADDRESS, PORT))
        s.send(b'DOOR_OPEN')
    except Exception as e:
        print("Exception: " + e)
    s.close()
    return

DOOR_PIN = 23

io.setup(DOOR_PIN, io.IN)
print("Watching over the door")
while True:
    time.sleep(2)
    if io.input(DOOR_PIN):
        doorOpened()
        time.sleep(60)

无论如何,由于某种原因,比如可能每周一次,警报会被误触发。有时当我睡觉时,在工作时,无论如何。我不确定只要磁铁彼此靠近,代码怎么可能从 GPIO 读取值,它不应该完成电路。我玩过开门,在传感器被触发之前,它们必须彼此相距大约 1.5-2 英寸,所以我不知道当它们基本接触时(小于 1 毫米)它怎么可能触发分开)。

所以...有人有任何想法或解释吗?

谢谢!

只出现的问题 'once a week' 可能是我在嵌入式系统中遇到过的最烦人的问题,它们使调试变得特别困难。

以下是您可以尝试的一些方法:

首先,通过其他方式进行调查。我的意思是用某种记录设备(例如录像机)监视门,最好是可以适度交叉引用警报响起时间的设备。

其次,如果是电气间歇性问题(比如我们以前看到的老式廉价键盘的按键弹跳),您可以稍微修改代码以减少误报。

换句话说,不要对第一个检测到的问题发出警报。相反,进入更紧密的循环并(例如)以十分之一秒的延迟对输入进行五次采样。

然后根据优势证据决定它是否是实际警报。

And, just as an aside, I wonder at the wisdom of closing a socket that may never have been opened. It's not likely to be your issue here but it would be worth cleaning up the logic in doorOpened at some point.