Hardware changes:
Instead of using an RC circuit, I swapped the capacitor for a 1kΩ resistor, as I am no longer interested in the analogue light level, just whether a light input is on or off. (
I also added a mini voltmeter I've had kicking around for ages from a previous project, so that I could see what was happening to the voltage level on pin 23 for debugging.
Software
This was very helpful: http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
I had an 'unusual' set up for testing, in that I was having a lie-in on a Saturday morning. I could control the blinds in my room from the bed, and that was enough to set Pin 23 high or low.
here's the code and output. It still needs a few tweaks but it works well enough for testing. Next job is to rebuild the hardware and attach it to the electricity meter.
#!/usr/bin/env python
# Example for edge sensing on pin 23 using a LDR and Resistor in series
#thanks to Alex Eames @ http://raspi.tv for the useful hints
#Outline:
#1)set pin low
#2)wait for falling edge
#3)start timer
#4)Wait for next falling edge
#5)stop timer and log or print time since last event
#6)reset and restart timer
#7) repeat
import RPi.GPIO as GPIO, time, os
#procedure to wait for transit light-->dark:
def wait_for_dark():
try:
GPIO.wait_for_edge(23,GPIO.FALLING)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
#initialise pins:
DEBUG = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
wait_for_dark()
#timing
for i in range (0,5):
start_time =time.time()
wait_for_dark()
elapsed_time = time.time()-start_time
print (GPIO.input(23),elapsed_time)
time.sleep(2)
GPIO.cleanup() # clean up GPIO on normal exit
print("Ending PRogram")
No comments:
Post a Comment