Observing that LED will give us knowledge if it is being used or not. I am using a Light Dependent Resistor (LDR) as eyes for the Raspberry Pi. The LDRs change their resistance as per the intensity of light falling upon them. Our goal is to register the LED being lit by driving a GPIO pin on the Raspberry Pi LOW. Below is the schematic of my setup:
It is a voltage divider, with a weak pull up provided by a 150K resistor. The LDR has huge resistance when dark, so the GPIO pin will remain in HIGH state when the LED is dark. Upon lighting it up, the LDR’s resistance falls and the voltage divider tend to go LOW. We watch for that even in a python script on the Raspberry Pi and take action.
For the above schematic to work, we need to ensure that the LDR is in really shielded from ambient light. I cut a piece of pen and placed my LDR inside it, then painted the tube black to prevent outside light from falling on the LDR. The LDR was hot-glued in position.
Now to the software side, we need a python script that will watch the state of GPIO pin 17 and take some action when the coffee machine brings it to ‘LOW’
and it should look like this:
import RPi.GPIO as GPIO import time import os #adjust for where your switch is connected buttonPin = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(buttonPin,GPIO.IN) while True: if (not GPIO.input(buttonPin)): #this is the script that will be called (as root) os.system("python /home/pi/sendnotify.py") #debounce time.sleep(720)
To have this run upon booting, we need to edit /etc/rc.local (as root since this is the owner).
sudo nano /etc/rc.local
At the bottom, just above
exit 0 we’ll add a call to our script.
The notification script that will be run is actually sending us an email:
For more detail: Tweeting coffee machine using Raspbery Pi