In the end, I got everything working in a bench setup--including prevention of unintended relay activation during RPi reboot. Then... I accidentally killed bootup of uSD card by trying to test a power outage scenario--thankfully I had already saved my working Python script to main computer.
In short, here was my setup:
Components & Circuit:
- RPi2 w/ Raspbian "WHEEZY"
- SunFounder 5V mechanical relay board
- Breadboard
- (2) push buttons
- Wiring
- male-male jumper
- female-female jumper
- alligator clip jumpers
- spare pieces of stranded/solid wire
- 3V battery source
- LED
- 100-Ohm resisto
Software:
A single Python script named garageCtrl.py as follows:
#!/usr/bin/env python2.7
# Purpose: Testing garage door state toggle via relay triggered by button on RPi. For use in garage w/o keyboard/monitor.
# Button1 - (P23) Kill program
# Button2 - (P24) Trigger relay
# Relay - (P18) Toggle garage door opener next state
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def my_callback(channel):
print "Rising edge detected on port 24, in the main thread."
GPIO.output(18, False)
time.sleep(0.1)
GPIO.output(18, True)
print "Press Button #1 (P23) to kill program."
print "Press Button #2 (P24) to trigger relay."
#raw_input("Press Enter when ready\n")
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback, bouncetime=300)
try:
print "Waiting for falling edge on port 23"
GPIO.wait_for_edge(23, GPIO.FALLING)
print "Falling edge detected. Here endeth the second lesson."
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
Raspbian Configuration:
In order enter a "ready" state automatically upon applying power to the RPi, I tweak the OS configuration as follows:
- At bottom of .bashrc file, added line: sudo ./garageCtrl.py
- Modified /etc/inittab line 1:2345:respawn:/sbin/getty --noclear 38400 tty1 with line 1:2345:respawn:/sbin/getty --autologin pi --noclear 38400 tty1
- Changed boot-up to console rather than X-Window via raspi-config tool.
These changes cause the RPi to bootup headless (w/o GUI resource overhead), automatically login, and finally execute python program to listen for button presses that trigger the relay/garage door opener.
Garage Door Test:
It works on the bench lighting up an LED, but just for validation-sake also confirmed it worked in the garage by replacing the Snap Circuit node w/ leads to the garage door opener's contacts 1 & 2.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.