Monday, July 15, 2013

Door Sensor Working

I finally got the Magnetic Switch / Door Open sensor working. My wiring isn't "exactly" like anything I saw on the Web.

There are 2 undifferentiated wires out of the device. In the try that worked I connected:

wire 1 to 10K resistor to GND

wire 2 to a GPIO pin (e.g., 25 for code below)

Here's a messy pic:

Here's my crude (but working) Python code:

import time
import RPi.GPIO as GPIO

DOOR_pin = 25 # 1 wire->10kRes->GND, other->GPIO25
LED_pin = 17
GPIO.setup(DOOR_pin, GPIO.IN)
GPIO.setup(LED_pin, GPIO.OUT)

tm = 0
changes = 0
last = 0
h = 0

for x in range(20): # tries
        m = GPIO.input(DOOR_pin)
        if m:  # Mag. sw open detected
                if last != m:
                        GPIO.output(LED_pin,GPIO.HIGH) # LED on
                        tm = time.strftime("%y, %m/%d:%H:%M:%S")
                        print tm #temp -- should log
                if last:
                        GPIO.output(LED_pin,GPIO.LOW) # LED off

        if m==1 and last==0 
                changes += 1
        h += m
        last = m

print "m's:", h
print 'changes:', changes

The 2 halves of the sensor have to be about .5" apart to register "ON" (HIGH).

Now for my photo sensor.

No comments:

Post a Comment