Raspberry Pi IR remote

Controlling the IR Remote with Android

To use the Raspberry Pi-based IR remote control, you need direct access to the computer, or you need to establish an SSH connection to it from a remote machine. Needless to say, this approach is hardly practical in real-world situations. However, if you happen to have an Android device, you can use it to connect to Raspberry Pi and control the IR trigger.

For this trick to work, your Android device must support USB tethering and have the VX ConnectBot app [3] installed on it. On Raspberry Pi, open /etc/network/interfaces in Nano, and add the following configuration:

iface usb0 inet static

This step effectively turns the first USB port into a network interface. Reboot Raspberry Pi, connect your Android device to it via USB, and enable USB tethering. Then, launch VX ConnectBot and use the pi@ address to establish an SSH connection to the Raspberry Pi (see the "Wireless Network Option" box for more information).

Wireless Network Option

If you plan to use the IR remote indoors, you can simply configure the Raspberry Pi to connect to the local wireless network (this requires a USB wireless adaptor). You can then use any machine or mobile device on the same network to connect to the Raspberry Pi and control the IR remote.

Writing a Simple IR Remote Server

Controlling the IR remote control via an SSH connection is not particularly user friendly. To make the Raspberry Pi-based IR remote easier to use, you might want to add a web interface to it. This approach also allows you to use a browser instead of an SSH utility or app to control the IR remote from any device. For this task, you can use the Bottle micro framework [4] for building web applications with Python. Installing Bottle on the Raspberry Pi requires the following first two commands:

sudo apt-get install python-pip
sudo pip install bottle
nano ir_remote_server.py

The third command then creates the ir_remote_server.py Python script and opens it for editing. Here, you enter the code in Listing 2, then save the script and make it executable with:

chmod +x ir_remote_server.py

Listing 2

Simple IR Remote Server

01 #!/usr/bin/python
02 from bottle import post, route, request, run
03 import os
04 @route('/')
05 @route('/', method='POST')
06 def release_control():
07     if request.method == 'POST':
08         os.system("irsend SEND_ONCE Nikon2 shutter")
09     return """
10     <meta name="viewport" content="width=device-width, initial-scale=1">
11     <form method="POST" action="/">
12     <input id="submit" name="submit" type="submit" value="Shutter Release">
13     </form>
14     """
15 run(host="",port=8080, debug=True)

The script generates a simple web app containing a single submit button. When the button is pressed, the script uses the os.system function to run the irsend SEND_ONCE Nikon2 shutter command. Run the sudo ./ir_remote_server.py command to launch the script, and you can access the web application by pointing the browser to http://<>:8080 (replace <> with the actual IP address of the Raspberry Pi).

The created script is rather bare-bones, but you can make it prettier and include additional functionality (Figure 3). Your options are only limited by your coding skills, but you can use the improved version of the script in Listing 3 as a starting point. This version adds styling and implements the feature that makes it possible to take a specified number of photos at the predefined time intervals.

Figure 3: Bare-bones web app for the Raspberry Pi-based IR remote.

Listing 3

Improved Version of the IR Remote Server

01 #!/usr/bin/python
02 from bottle import post, route, request, run
03 import os, time
04 @route('/')
05 @route('/', method='POST')
06 def release_control():
07     if (request.POST.get("shutter_release")):
08         os.system("irsend SEND_ONCE Nikon2 shutter")
09     if (request.POST.get("number")):
10         i = 1
11         number = int(request.forms.get('number'))
12         while (i <= number):
13             os.system("irsend SEND_ONCE Nikon2 shutter")
14             time.sleep(3)
15             i = i + 1
16     return """
17     <meta name="viewport" content="width=device-width, initial-scale=1">
18     <form method="POST" action="/">
19     <div id="content"><p><input id="submit" name="shutter_release" \
       type="submit" value="Shutter Release"></p>
20     <p>Number of photos: <input name="number" type="text" size="9"/></p>
21     <input id="submit" value="Start" type="submit" />
22     </form></div>
23     <style>
24         body {
25         font: 15px/25px 'Fira Sans', sans-serif;
26         }
27         #content {
28         margin: 0px auto;
29         text-align: center;
30         }
31         #submit {
32         width: 11em;  height: 2em;
33         background: rgb(66, 184, 221);
34         border-radius: 5px;
35         color: #fff;
36         font-family: 'Fira Sans', sans-serif;
37         font-size: 25px;
38         font-weight: 900;
39         text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
40         letter-spacing: 3px;
41         border:none;
42         }
43     </style>
44     """
45 run(host="",port=8080, debug=True)

When you run the script using the sudo ./ir_remote_server.py command, you have to keep the terminal window (or session if you connect to Raspberry Pi remotely) open. To stop the script, you can either use the Ctrl+C keyboard shortcut or simply close the terminal window. To make the script run as a background process, use the

nohup sudo ./ir_remote_server.py &

command. This way, you don't have to worry about accidentally stopping the script.

Instead of launching the script manually, you can create a cron job that automatically runs the script when the Raspberry Pi boots. To do this, run the

crontab -e

command and specify the following cron job:

@reboot nohup sudo /home/pi/ir_remote_server.py.py &

Save the changes, and the script will run automatically when the Raspberry Pi boots.

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