It seems like I have a lot of posts devoted to lamps… anyway, my latest one involves programming a Raspberry Pi to switch my living room lamp on approximately a half-hour before sunset. My previous design for switching my lamp involved using an old Pentium 3 that I have since replaced with a newer computer. The new computer doesn’t have a parallel port, and isn’t near the lamp any more, so I decided that the RPi’s compact size suited itself to this project. Also, the old program turned the lamp on and off at the same time every day. This is problematic for two reasons: first, I could buy a simple timer to do this, and second, it required me to change the program’s source code any time the lamp started turning on too late or early as the seasons changed.
Another goal for this project was to familiarize myself with Python. I am fairly well-versed in C (don’t ask me about pointers though), but Python seems to be a tool that more and more people are using. The script I wrote uses a python module called “pyephem” to calculate the sunset time at my location (south Florida) every 30 seconds. Then it subtracts 30 minutes from this time, and if the current time matches the calculated sunset time, it turns the lamp on.
Another thing that I implemented in my Python script was random turn-off times. Importing a module called “random” allows me to call a function “random.randint”. Giving it a range of values allows it to generate random numbers within this range when it is called. So “random.randint(0,59)” generates a random turn-off minute for my program, which turns the lamp off every night between 12:00 and 12:59. Hopefully this confuses anyone spying on my home!
It started out on the floor just to get things rolling. I spliced into an old extension cable.
This wasn’t a permanent solution, obviously. I opened the lamp up to get at the internal wiring to tap off of the 120. From there I used an old cell phone charger to get power for the Pi and also spliced the 120 into the relay. The relay is tied to the control electronics which get their signal to turn the relay on or off from the RPi.
Since everything’s under the lamp’s table, it’s not noticeable at all! The one change I need to make is getting a wireless card for the RPi so it doesn’t need an ethernet connection. Apparently the OS version that’s on the Pi doesn’t have very good wireless support, but I hear they fixed it in a more recent release.
# program calculates sunset each day and turns the lamp on 30 minutes before
# then the program calculates a random minute to turn the lamp off within an hour after midnight.
# REMEMBER TO USE 24-HOUR TIME FORMAT
# AND THAT PYEPHEM USES UTC/GMT AND NOT EST
It also needs a script in /etc/init.d to tell the Pi to start this program at boot.
UPDATES!
I added a Staples Easy Button to the lamp since there was no way to turn it off or on except by SSHing to the Pi, and then running the “off” and “on” python programs manually. I took the Easy Button apart and soldered some wires to the button. The button is powered by two AA batteries, which is about 3 volts. I thought this would be enough for the Raspberry Pi’s 3.3V logic, so I hooked the button’s output up to a PNP transistor’s base to watch for button presses. When a button is pressed the wire connected to the transistor’s base goes low (3.0 V to 0 V) and the transistor turns on. The collector pin on the transistor is connected to one of the Pi’s input pins, and when it sees the button was pressed it toggles the lamp.
import RPi.GPIO as GPIO
import time
Notice that I needed to physically bond pin 11 to pin 10. There’s no way that I know of to monitor the state of an output pin like you can do by polling a microcontroller’s registers. But there are plenty of input/output pins for me to waste one like this. The reason I needed to do this is because more than one program can change the state of the output pin, so the Easy Button program has to check to see if the lamp is on or off before deciding whether to turn it off or on.
BlueHost is definitely the best website hosting company with plans for all of your hosting needs.