Using the Raspberry Pi as a smart alarm clock

The Web Page

The simple /var/www/alarm.php application is the heart of the software. This is what you will use to specify all of the settings for the wake-up alarm in the MySQL database (Figure 4). The preset versions offer only rudimentary functions for setting a wake-up alarm, leaving lots of room for further development (Listing 16).

Figure 4: The web application to set wake-up times. The sound file you choose to play when the alarm triggers goes in the Sound folder.

Listing 16

More Functions

01 <HTML>
02   <form action="setalarm.php" method="post">
03    <p>Alarm time<br>YYYY-MM-DD HH:mm:ss<br><input type="text" name="ts"/></p>
04    <p>Sound<br><input type="text" name="sound"/></p>
05    <p>Light<br><input type="text" name="light"/></p>
06    <p><input type="submit" name="save" value="store" /></p>
07   </form>
08   <?php
09    $con=new mysqli("localhost","alarm","alarm","alarm");
10    if(isset($_POST['save'])) {
11      $ts=$_POST['ts'];
12      $sound=$_POST['sound'];
13      $light=$_POST['light'];
14      if(!$con){ die ("Database Connect failed"); }
15      $sql="insert into alarm (ts,sound,light) values('".$ts."','".$sound."','".$light."')";
16      if($con->query($sql)===false){echo $con->error;}
17    }
18     $sql="select * from alarm order by ts asc";
19     $result = $con->query($sql);
20     echo "<table border=1>";
21     while($row = $result->fetch_assoc()) {
22       echo "<tr><td>".$row["ts"]."</td><td>".$row["sound"]."</td><td>".$row["light"]."</td></tr>";
23     }
24     echo "</table>";
25     $con->close();
26   ?>
27 </HTML>

Potential Problems

If the Raspberry Pi is operating in a time zone different from the rest of the network, it will wake you up at the wrong time. This problem is easy to overlook. Also, you should make sure that as few programs as possible are running when the Rasp Pi activates the LED strip because timing is very important.

Because alarm.php works with nohup to terminate software components, programs that do not stop cleanly can get hung up in the process table. Therefore, if the Rasp Pi keeps getting slower, you should check whether that is indeed the problem with ps -ef.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content