Like many of my fellow high school students, I face a dire problem: waking up in the morning. While I do already have an alarm clock, the snooze button is too easy to reach, and thus sleep is far too easy to reenter. This has been the case many times. However, I have found that if a room is lit I will inevitably wake up. To this end I set out on my latest little project: creating an automated lamp-based alarm for myself that I could control via the web. This is my report on the results.
The system required several layers. First, a way to turn my lamp on and off remotely was created by rerouting my lamp's power cord through a 5v electromechanical relay. The relay was then connected to the appropriate GPIO pins on the Raspberry Pi that runs this site. Next, control of the GPIO pins (and thus the relay and lamp) was accomplished using the Python RPi.GPIO library, with a script to turn on the light, turn off the light, and blink the light on and off for a minute. Now, having a way to control the light via the shell, I moved on to the web interface.
A simple control for turning the light on and off from the web was easily made using the good old PHP shell_exec command and the appropriate scripts. By the far the most difficult task was setting up the alarm activation system. The obvious way to do this was using cron, and that's how it was done, but not without some struggle. See, cron is very particular about its format, ownership, and permissions when you're trying to automatically create jobs (with files in /etc/cron.d). For example, if your file has any mode other than rw-r--r-- the cron daemon will simply refuse to load the file, even if the permissions are more than sufficient, such as rw-rw-r--. This particular issue, along with other annoyances such as hardware permission groups, specific per-command sudo privileges in order for the server to be able to reload cron and edit the needed files, and other minutia ate up the greater part of the time spent on this project.
But after all was said and done, all of my goals were accomplished. Here are some images of my results: