Using the Raspberry Pi as a smart alarm clock

wiringPi

The system needs the wiringPi library [2] in order to query the button that will turn off the alarm. This library makes it possible to gain access to the GPIO interface of the Raspberry Pi. It contains simple commands and a powerful C API for writing and reading the I/O ports. The library also includes drivers for the I2C Bus. The installation is simple for the practiced Rasp Pi user. Listing 12 is a helpful aid for the less experienced user.

Listing 12

Install wiringPi

$ cd /tmp
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build
$ cd ..

The build process takes quite a while to complete – especially on the first-generation Raspberry Pi. The build script moves the data generated after a root query into the system. When the build is complete, you should test whether it has been successful by calling gpio readall (Listing 13).

Listing 13

Test the Build

$ gpio readall
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 1 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+

Controlling Time

To activate your alarm clock at the times entered into the database, you still need a small script that looks in the database for alarm settings and also executes them at the correct times. The script starts every 15 minutes as a cron job and evaluates the data in table column ts for an alarm entry. If the script finds that the time for a particular task has come, it plays the file that has been defined in sound and simultaneously starts the script that turns on the lights. Afterward, it deletes the task from the database to avoid re-execution. After a period of five minutes, the script turns off the lights again.

Because the library for controlling the LED strip needs root rights, you should create a suitable entry, as shown in Listing 14, in the root crontab. See also the "Cron" box.

Listing 14

Set Root Rights

$ sudo crontab -e */15 * * * *   php /var/www/alarm.php

Cron

The Unix service cron makes it possible to execute arbitrary tasks at specific times. Go to the crontab command line to configure the service. Using crontab -l, you will see the current settings for the registered user. Choose the -e option to switch crontab into editing mode.

The format of an entry is as follows: Minute Hour Day Month Weekday Task. The last parameter indicates which program or script cron should execute. Make sure the system can execute the task completely before the next start; otherwise, memory will overflow after a while. The first five parameters control the time of execution. You can use numbers or stars. Numbers indicate to cron when it should execute the task, and a star serves as a wildcard (Table 2).

You can enter settings for all kinds of times and intervals. The extensive man page from cron describes how this works. Because the system executes programs and scripts with the rights of the user, but doesn't set an environment, you need to specify absolute paths instead of using aliases for a cron job.

Table 2

Cron Examples

Parameter

Meaning

* * * * *

Daily every minute

*/10 * * * *

Daily every 10 minutes

0 * * * *

Daily at the beginning of each hour

0 0 * * *

Daily at midnight

30 18 * * *

Daily at 6:30 p.m.

* * 1 * *

On the first day of each month, every minute all day long

The program /var/www/alarm.php controls all processes (Listing 15). It first turns off all of the LEDs to be sure the LED strip is not on longer than necessary (line 3). Then, it asks the database whether it is time to wake up (lines 6-8). If yes, it plays the alarm sound (line 10) and starts the light show specified in the light script (line 11). The data entries for that alarm are then deleted from the database (line 13). A while loop (starting with line 16) then asks whether the button was pushed. For good measure, the loop stops after 600 seconds (line 17). If the button was pushed, the script will stop the music player (line 22).

Listing 15

/var/www/alarm.php

01 <?php
02   $home="/home/pi/alarm/";
03   system($home."light/all_off" ); system($home."light/all_off");
04   $con=new mysqli("localhost","alarm","alarm","alarm");
05   if(!$con){ die ("Database Connect failed"); }
06   $sql="select * from alarm where ts < now() order by ts asc";
07   $result = $con -> query($sql);
08   $row = $result->fetch_assoc();
09   if ($row["id"]) {
10     system('/usr/bin/nohup /usr/bin/ogg123 '.$home."sound/".$row["sound"].'>/dev/null  &' );
11     system('/usr/bin/nohup '.$home."light/".$row["light"].'>/dev/null  &' );
12     exec('/usr/local/bin/gpio mode 0 in');
13     $sql="delete from alarm where id=".$row["id"];
14     $con -> query($sql);
15     $sts=time();
16     while(!system('/usr/local/bin/gpio read 0',$o)) {
17       if(time()> ($sts+600)) {
18         break;
19       }
20     }
21   }
22 system('/usr/bin/killall ogg123 ');
23 $con->close();
24 ?>

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content