title: Magnetic door sensor connected to Raspberry Pi running Python script is reporting false alarms
body: <p>This is such a strange issue. I'm not sure even how to troubleshoot it...</p>
  
  <p>I wrote a very simple piece of code for a Raspberry Pi at the entrance of my house. It just poles the GPIO every 2 seconds to see if the circuit has been completed. It's connected via GPIO to a magnetic door sensor, like one of these:
  <a href="https://s3.amazonaws.com/assets.controlanything.com/photos/CKN6004-900.jpg" rel="nofollow">https://s3.amazonaws.com/assets.controlanything.com/photos/CKN6004-900.jpg</a></p>
  
  <p>Here is the python code:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Anyways, for some reason, like maybe once a week, the alarm will get triggered falsely. Sometimes when I'm sleeping, at work, whatever. I'm not sure how the code could possibly be reading a value from the GPIO as long as the magnets are close to one another, it shouldn't complete the circuit. I've played with opening the door and they have to be about 1.5-2 inches away from one another before the the sensor gets triggered, so I have no idea how it could possibly trigger while they're basically touching (less than 1 mm apart).</p>
  
  <p>So... does anyone have any ideas or explanations?</p>
  
  <p>Thanks!</p>