Automating the home with the Raspberry Pi

Remote Control

A definite advantage of using a web application (Figure  4) is that it is independent of any platform. A web app operates equally well on a desktop computer and a smartphone.

Figure 4: Setting the wake up time and start times via the web GUI is easy in a browser.

The application consists of three files. The index.php file contains the user interface based on HTML, JavaScript, and PHP. Here, you need to set the time your motivation assistant is to start. Additionally, alarm.php and switch.php contain the JavaScript and PHP code that communicate the desired commands to the Raspberry  Pi.

Basically, the little Rasp Pi based motivation assistant will already function in this constellation. You only have to tell it what it should do. If you used the same codes as I, then everything should work fine; otherwise, you can change the values in the alarm.php file (Listing  2).

Listing 2


$hour = $_GET["hour"];
$mins = $_GET["mins"];
//Commands for the Raspberry Pi cron job
exec('echo "'.$mins.' '.$hour.' * * * sudo /home/pi/raspberry-remote/./send 11010 1 1; \
  sudo /home/pi/raspberry-remote/./send 11010 2 1; sudo /home/pi/raspberry-remote/./send \
  11010 3 1; sudo omxplayer -o hdmi /home/pi/Motivation/video.mp4" | crontab -');

The script first reads the variables for the time of day you want your motivation assistant to start working. Then it creates a cron job that executes commands for the operation of the wireless communication outlets.

As you can see, the file contains commands for three wireless communication outlets and the video that starts on the TV. In general, the exec command needs to have each command separated by a semicolon:

exec('echo "'.$mins.' '.$hour.' * * * <command1>; <command2>;\
  <command3>;<command4>" | crontab -');

You also need to modify the switch.php file (Listing  3) when using other codes. The file shuts all wireless communication outlets off again when you press the Turn off all button in the web GUI.

Listing 3


exec("sudo /home/pi/raspberry-remote/./send 11010 1 0");
exec("sudo /home/pi/raspberry-remote/./send 11010 2 0");
exec("sudo /home/pi/raspberry-remote/./send 11010 3 0");

Installing the Application

To make the application accessible, you should copy the contents of the entire homeautomation folder from the Raspberry Pi Geek FTP server [4] onto the web server in the var/www/ directory. Additionally, you should make sure that the www-data user has the rights necessary to execute the commands via PHP.

As long as the Raspberry  Pi is only accessible in the home network and you have no significant security concerns, you can set the rights for this as shown in Listing  4. Afterward, you should start the web application on the client in the browser under <Pi-IP>/homeautomation/index.php.

Listing 4

Setting Rights

$ sudo chmod g+s /var/www
$ sudo chmod 775 /var/www
$ sudo chown -R www-data:www-data /var/www

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

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