Introduction: Make Your Phone/tablet/desktop a Remote Control With a Raspberry Pi

Picture of Make Your Phone/tablet/desktop a Remote Control With a Raspberry Pi

I have my networking and audio/video equipment in a wiring closet. Some equipment like the satellite receivers have RF remote control. But most equipment has Infrared remote control. This Instructable will show how to program a Raspberry Pi to give a web interface to control the IR devices. One can access the web interface from a smart phone, tablet or any networked computer.

NOTE: For the almost no step Instructable, the Pi image is attached. Follow the instructions at the following link for burning an image. Note: I made this image on a 16 G SD card.

https://www.raspberrypi.org/documentation/installation/installing-images/README.md

We will use LIRC (Linux Infrared Remote Control) to generate the IR signals to the devices. With LIRC one can send or receive signals for most devices. LIRC requires some hardware to generate (and receive) IR signals.

http://www.lirc.org/

While this Instructable is written for a specific setup, it can be useful for a wide range of setups. I am using the IguanaWorks USB IR adapter to generate the IR. The LIRC front page above gives a list of other compatible IR adapters. All of them will require very similar steps, many of them will need less. The IguanaWorks adapter is not completely integrated into LIRC and needs extra steps.

The other possible major difference is the equipment being controlled. I am controlling a Monoprice HDMI switch.

http://www.monoprice.com/Product?c_id=101&cp_id=10110&cs_id=1011002&p_id=6415&seq=1&format=2

LIRC has a list of remote controls that have been characterized (link below). The Monoprice HDMI switch is not in that list, it will need extra steps to characterize its remote.

http://lirc-remotes.sourceforge.net/remotes-table.html

The IquanaWorks USB IR adapter will transmit and receive IR. In normal operation the receiver is not needed. But it is handy for reading codes on an uncharacterized remote to generate a configuration file for the remote. I can use LIRC and the receiver in the USB IR adapter to find the codes for each function without having the remote control specifications for the device. If the device you are controlling is not in the list above, make sure you get an adapter that transmits and receives. The good news is that most adapters do both.

Don't be alarmed by the number of steps in this Instructable. Yes, this is a massive Instructable, but it is not as big as the number of steps would indicate. There are two reasons for so many steps. One, I try to cover as many possible gotchas that I can identify. Hopefully, many of those will not apply in most cases. Second, Instructables shrinks the graphics beyond the first two for any given step. I expand individual steps when I have more than two graphics for that step. I believe my setup is a worse case setup. Almost any other setup should take fewer steps.

Step 1: Install Webmin

Picture of Install Webmin

I like to use Webmin for my Linux tasks. It makes a lot of things easier, in particular uploading files, which we will do for the web pages amongst other things. I have written an Instructable on installing Webmin on the Pi.

https://www.instructables.com/id/Adding-Webmin-to-m...

For those of you who are fond of the command line, I will list the command line commands along the way.

I personally use the command line a lot, but I don't like editing in the terminal. So I won't be showing the commands for editing files. For that we will use the File Manager in Webmin. A note about File Manager, it caches the directories. If for example a file gets placed in a directory during install and it needs to be edited. The "Refresh" button must be pressed to make it visible.

Now with Webmin installed and running in a browser, we can use it to install LIRC. Of course this can also be done from the command line using apt-get. First thing we will do is find the proper name of the LIRC package.

In the Webmin menu open the System branch and select Software Packages.

In the screen that opens, click the button that says Search APT ...

The command line for the above operation is

apt-cache search lirc

Step 2: Installing LIRC Using Webmin (finding Proper Package Name)

Picture of Installing LIRC Using Webmin (finding Proper Package Name)

In the window that opens from the Software Packages screen, type lirc in the box and click Find packages matching.

When the results show up, we will find a package called lirc. It turns out that this package does not have any additional parts in the name. Later we will see how the actual package name could be different and how this procedure makes sure we get the right package.

Click on lirc in the list. It takes us back to the Software Packages screen with lirc in the line Package from APT

On the command line, one can search the repositories with the command

apt-cache search lirc

Step 3: Installing LIRC Using Webmin (doing Actual Install)

Picture of Installing LIRC Using Webmin (doing Actual Install)

Make sure the radio button next to Package from APT is selected, then click the Install button.

Webmin will install LIRC and its dependencies. The screens above show the results.

The command line for the above operation is

sudo apt-get install lirc

Step 4: Trouble? the Unknown Step (Is There a /dev/lirc0 Device?)

Picture of Trouble? the Unknown Step (Is There a /dev/lirc0 Device?)

When LIRC finishes installing and tries to start, it complains about an invalid conf file and says "Remote control support has been disabled." My original plan was to create a spoof configuration file to get us to the point where we could characterize the remote and create a valid file. But I discovered trouble in our future.

One of the reasons for Instructables like this is to help everyone avoid the gotchas. This is one of them. I am not sure the situation that is happening here will happen to everyone else, but I will explain it and hopefully it will help everyone understand and handle any weirdness that happens to them.

There are a couple of things that I discovered about the IguanaWorks USB adapter. One, it is supported by the kernel. That is, it has a driver built into the kernel that shows up as /dev/lirc0. I believe that device would work if we were using the IR receiver to control a TV tuner for example. I don't believe it will serve our transmission purpose, we need LIRC to handle the transmit functions and to read the configuration file that characterizes our remote.

The second thing I discover is that LIRC does not have IguanaWorks support. lircd will need to be rebuilt using instructions from the IguanaWorks web site (I will cover that later).

I have struggled with the best way to handle this. I have started from scratch several times with fresh Pi builds and doing the steps in different order. I am trying to find a sequence of steps that will work for the largest number of setups.

It turns out that the kernel support for the receiver allows us to characterize the remote right now if the USB adapter we are using is an IguanaWorks adapter. That is probably true for a bunch of adapters. I doubt that will apply in every case.

I believe the following sequence of steps will help the greatest number of people to find success. You may find that they work for you in a different order. Hopefully, I have given you enough information to recognize when you need to do that. (Please make comments if you find this to be the case!)

Right now there should be no USB adapter plugged in. Run the following command in the terminal.

ls /dev

This sets a baseline, no lirc0 device, as shown above.

Step 5: Plug in the USB Adapter (find the Lirc0 Device?)

Picture of Plug in the USB Adapter (find the Lirc0 Device?)

Now plug in the USB adapter. Run the following command again.

ls /dev

Is there a lirc0 device now? If there is, one could go to the "Creating LIRC configuration file" steps and characterize the remote now. If not, you will need to go to the manufacturer's web site and try to get drivers for your adapter. I think that if support for your adapter is not built into the kernel, the drivers you will find will be LIRC drivers.

NOTE: The kernel driver does interfere with the LIRC driver. It captures the USB adapter and blocks the LIRC driver. We will disable the kernel driver later, after we characterize our remote.

Step 6: Verifying That the USB Adapter Is Detected

Picture of Verifying That the USB Adapter Is Detected

In the SSH terminal window, enter

lsusb

This will list the USB devices plugged in. The IguanaWorks USB (or your adapter) should be in the list. This should be true regardless of whether the lirc0 device shows up or not. This is a lower level detection that happens whether there is a driver or not. If your adapter does not show up, you have hardware issues.

In any case, it appears that installing the LIRC drivers has no effect on the receive functions of the IguanaWorks adapter. Installing the LIRC drivers should be the path for those who don't have kernel support and it doesn't seem to matter for those who do. That will be our next step.

Step 7: Installing IguanaWorks Driver (Getting Info From Web Site)

Picture of Installing IguanaWorks Driver (Getting Info From Web Site)

Every USB device needs a driver to be installed when the device is plugged in. As stated before, I am using the IguanaWorks USB device. The following link has various choices for installing the driver.

http://www.iguanaworks.net/files/?OS=DEB

I like the repository solution. It makes getting updates easy. It is worth the trouble to set it up now. On the web page I have highlighted the line.

deb http://www.iguanaworks.net/downloads/debian binary-rasp/

This line needs to be added to the sources.list file in the /etc/apt directory. In the File Manager, navigate to this directory, select sources.list and click on the Edit icon in the toolbar at the top.

Note: One can also make a separate file for IguanaWorks in the /etc/apt/sources.list.d sub-directory. The line above would go in that file.

Step 8: Installing IguanaWorks Driver (Adding IguanaWorks to the Repository)

Picture of Installing IguanaWorks Driver (Adding IguanaWorks to the Repository)

In the box that pops up, copy and paste the line in the previous step or from the web site and paste it as shown. Then click on Save & Close.

Step 9: Installing IguanaWorks Driver (Refreshing Apt)

Picture of Installing IguanaWorks Driver (Refreshing Apt)

In the Webmin menu, go to System, Software Packages.

At the bottom of the screen is the upgrade section.

Make sure the "Resynchronize package list (update)" is set to Yes.

The "Upgrade mode" is set to Don't upgrade.

And the "Only show which packages would be upgraded" is set to No.

Then click on the Upgrade Now button.

For the command liners, the same thing can be done with

sudo apt-get update

Step 10: Installing IguanaWorks Driver (Finding Package Name and Installing It)

Picture of Installing IguanaWorks Driver (Finding Package Name and Installing It)

Now click on the Search APT... button.

In the window that opens, type iguana in the box and click on the Find packages matching: button.

After the search, the list will have an entry called iguanair. Click on that.

Back on the Software Packages screen, make sure Package from APT is selected, then click the Install button.

Webmin will install "iguanair" and its dependencies.

BTW, here is an example where the search of APT helps us find the right package name. Notice there are a couple of other packages that have iguana in their name.

The command line commands are (the cache search is not really needed since we know the package name)

apt-cache search iguana

sudo apt-get install iguanair

If using the command line, say y to the following query

Install these packages without verification [y/N]? y

Step 11: More Trouble! Does LIRC Support Your Adapter?

Picture of More Trouble!  Does LIRC Support Your Adapter?

In an eariler step, I talked about how lircd needed to be rebuilt with the IguanaWorks driver linked in. The time has come to do that. Does your adapter also need lircd to be rebuilt? Run the following command.

lircd --driver=?

We find no IguanaWorks driver. If you don't find yours, then you must also rebuild LIRC.

Back to the IguanaWorks web site and we find how to build lircd with the IguanaWorks driver support. We run the following command:

Below is a link with instructions to do that.

http://www.iguanaworks.net/wiki/doku.php?id=usbir:...

Step 12: Compiling LIRC With IguanaWorks Support (enable Source Repository)

Picture of Compiling LIRC With IguanaWorks Support (enable Source Repository)

Not everyone will need to rebuild LIRC. Anyone who has a transceiver driver listed can skip ahead to the "Testing Our Adapter" step.

First we need to have a source repository. In the File Manager, navigate to /etc/apt, select sources.list and click on the Edit icon.

Remove the # from the deb-src line.

Add the line below from the IguanaWorks site in an earlier step.

deb-src http://www.iguanaworks.net/downloads/debian source/

Click Save & Close.

Then as we did in an earlier step, in the Webmin menu, go to the System branch and click on Software Packages.

Make sure the "Resynchronize package list (update)" is set to Yes.

The "Upgrade mode" is set to Don't upgrade.

And the "Only show which packages would be upgraded" is set to No.

Then click on the Upgrade Now button.

One can do this on the command line with

sudo apt-get update

Step 13: Compiling LIRC With IguanaWorks Support (get Source and Compile)

Picture of Compiling LIRC With IguanaWorks Support (get Source and Compile)

Now that we have a source repository we can get the source. In the terminal window enter
sudo apt-get build-dep lirc

Then compile LIRC by entering

sudo apt-get -b source lirc

NOTE: These will take a while and will request verification at some steps

Step 14: Install LIRC With IguanaWorks Support

Picture of Install LIRC With IguanaWorks Support

Now we can install the new lircd.

Enter sudo dpkg -i lirc_0.9.0~pre1-1_armhf.deb

After the install it restarts the daemon.

Step 15: Edit the Hardware.conf File

Picture of Edit the Hardware.conf File

In the File Manager, navigate to /etc/lirc.

Select hardware.conf and click on the Edit icon.

In the file, uncomment the following lines

START_LIRCMD=false

START_IREXEC=false

Comment out the following lines.

#LOAD_MODULES=true

#DRIVER="UNCONFIGURED"

#DEVICE=""

#MODULES=""

#LIRCD_CONF=""

#LIRCMD_CONF=""

And add the following lines. Then click Save & Close

#Chosen Remote Control
REMOTE="Iguanaworks USB IR Transceiver"

REMOTE_MODULES=""

DRIVER="iguanaIR"

REMOTE_LIRCD_CONF=""

REMOTE_LIRCD_ARGS="-r"

LIRCD_CONF="/etc/lirc/lircd.conf"

#Chosen IR Transmitter

TRANSMITTER="None"

TRANSMITTER_MODULES=""

TRANSMITTER_DRIVER=""

TRANSMITTER_DEVICE=""

TRANSMITTER_LIRCD_CONF=""

TRANSMITTER_LIRCD_ARGS=""

Step 16: Creating a LIRC Configuration File (Identifying LIRC Device)

Picture of Creating a LIRC Configuration File (Identifying LIRC Device)

LIRC needs a configuration file that defines the codes for the remote control(s) being used. There are a lot of existing files for various remotes at the following site.

http://lirc-remotes.sourceforge.net/remotes-table.html

My remote is not in the list, so I need to create one. LIRC has a program called "irrecord" to allow one to do this. This is where the IR receiver in the IguanaWorks USB will be handy.

BTW, the receiver can also be used to read the signals from the IR remote to control a program or some device that is networked or otherwise connected to the computer. TV tuner cards are one common example. In our use case we will be transmitting IR signals to control IR devices. In normal operation, we won't need the IR receiver.

"irrecord" needs to know the device to read the IR data from. It has a -H command for many predefined devices. In the terminal window type

irrecord -H help

This command lists the devices that are supported. IguanaWorks is in the list. But we will use the kernel driver and the mounted device.The -d option will allow us to specify a mounted device in the device list. As discussed before, lirc0 is our device.

I have the command above to possibly help those who don't have a kernel driver. Hopefully, at this point the -H option will work for those people.

Step 17: Creating a LIRC Configuration File (verifying That Remote Is Working)

Picture of Creating a LIRC Configuration File (verifying That Remote Is Working)

Now we know the device to use in the "irrecord" command. We can use it to record our remote's functions. But first let us verify that the remote is working. One can do this with the equipment we are controlling, but I would like to demonstrate a handy trick. Most digital cameras can "see" IR. I took the above picture with my phone camera. The light showing at the end was not visible to the eye. And one need not take a picture, the light is visible when looking at the preview screen.

Step 18: Creating a LIRC Configuration File (reading the Remote)

Picture of Creating a LIRC Configuration File (reading the Remote)

Now a little more discussion before we use "irrecord". The LIRC community is trying to create a standard for naming keys for possible future standard Linux support of IR devices. As one can see on the previous page, my remote has two 1s, two 2s, etc. I couldn't find reasonable names in the namespace for my remote, so I am turning off namespace checking with the -n option. So in the terminal window, we will enter the command

sudo irrecord -n -d /dev/lirc0 lircd.conf

When one runs the command it shows a little primer screen. We can just press RETURN as they ask us to do. The next section asks us to press buttons repeatedly until there are two lines of dots. It asks that the buttons be pressed for one second. That is fine for the first line of dots, one gets multiple dots per press and that line goes quickly. The second line is different. Between lines, a message comes up asking us to keep pressing buttons. But now there is only one dot per press no matter how long we hold it. It will take longer with one second presses. My advice is to press long enough to make a dot and then release and press again. I worked my way across the remote pressing each button several times.

Step 19: Creating a LIRC Configuration File (naming the Buttons)

Picture of Creating a LIRC Configuration File (naming the Buttons)

Once we have two lines of dots, the program asks us to name the buttons. If one has not used the -n option, then each name must come from the list generated with the "irrecord -l" command. We have used the -n option so we can make up our own names. I am going to create names that are similar to those in the namespace. Since I have an A section and a B section, I am going to use the names KEY_A_x and KEY_B_x where x is 1,2,3,4 and POWER. The reset button will be KEY_RESET.

Step 20: Creating a LIRC Configuration File (finishing the Recording Process)

Picture of Creating a LIRC Configuration File (finishing the Recording Process)

When we have named all the buttons, we press RETURN without a name. The program is not done with us. It wants to look for a toggle bit mask Whatever that is, actually one can find out about it at several sites on the web. It is part of the RC-5 protocol one of the first IR protocols. You can read about it on Wikipedia.

https://en.wikipedia.org/wiki/RC-5

When the program has seen enough random button presses, it will announce "Successfully written config file."

Step 21: Looking at and Editing Our New Configuration File

Picture of Looking at and Editing Our New Configuration File

In File Manager, navigate to /home/pi and select lircd.conf.

Click on the Edit icon.

Change the name of the remote to something sensible. I named mine HDX-420X.

Click Save & Close.

Step 22: Creating a LIRC Configuration File (copy the File to Correct Location)

Picture of Creating a LIRC Configuration File (copy the File to Correct Location)

Now we can copy our newly created lircd.conf file to its proper location /etc/lirc/lircd.conf.

Select the lircd.conf file again.

Click on the Copy icon.

Then navigate to /etc/lirc.

Select the existing lirc.conf.

Click on the Delete icon.

Click on the Delete button in the pop up dialog.

Then click on the Paste icon.

Step 23: Disable Kernel Dirver

Picture of Disable Kernel Dirver

Now is the time to disable the kernel driver. It must be blacklisted to use LIRC. Note the irrecord session before was actually using the device driver, so it must be enabled up to that point.

Here is the link to the page with that info.

http://www.iguanaworks.net/wiki/doku.php?id=usbir:...

There is a lot on that page, we are interested in the following:

Create the file /etc/modprobe.d/iguana-blacklist.conf with the content of blacklist iguanair

One can navigate in File Manager to /etc/modprobe and click on the New icon.

A box pops up where we can enter iguana-blacklist.conf.

Then the file pops up in an editor window where we can add the above line.

Then Save & Close. A reboot will make this change take effect.

One can do it from the command line with

sudo shutdown -r now

It can also be done in the Webmin menu at System, Bootup and Shutdown.

Click on the Reboot System button at the bottom of the screen.

Click again on the one that shows up to verify.

A little tip about Putty. One can reconnect a window that lost its connection by right clicking in the upper left hand corner and choosing Restart Session

Step 24: Testing Our Adapter

Picture of Testing Our Adapter

According to the IguanaWorks web site, we should give our transceiver an id. The command below names it fred

igclient --set-id fred

Enter the following commands

igclient --get-version

irsend set_transmitters 1 2 3 4

irsend list "" ""

We see the name I gave the remote "HDX-420X". Now for an actual test.

irsend send_once HDX-420X KEY_A_POWER

IT WORKS! The HDMI section A turns on and off as I send that command.

Step 25: Setting Up the Web Server (Installing Mini-httpd)

Picture of Setting Up the Web Server (Installing Mini-httpd)

We are going to use a web page for the controls. In my opinion, web pages are better than apps. They can be accessed from any number of devices with no need to write different code for every device. The functions we need are very simple, we don't need a full blown server like Apache. The server I like to use for simple tasks like this is mini-httpd. It is in the repository.

Go to "System, Software Packages

Click on "Search APT...

Search for mini-httpd

Click on it, when found.

Select Package from APT, click on Install.

Skipping the search, one can install on the command line with:

sudo apt-get install mini-httpd

Step 26: Configure the Server

Picture of Configure the Server

Navigate to /etc in File Manager.

Click on the Refresh button.

Select mini-http.conf and click on the Edit button.

Comment out the line

#host=localhost

Edit the lines

user=nobody

data_dir=/usr/share/mini-httpd/html

to say

user=pi

data_dir=/home/pi/web_ir

Click Save & Close

We want the daemon to run at boot.

Navigate to /etc/default

Select mini-httpd and click on Edit

Change the line

START=0

to say

START=1

Click Save and Close

Step 27: Upload the Web Page

Picture of Upload the Web Page

I have created a web page which has everything one needs.

Here is one of Webmin and File Manager's very useful functions. Uploading files.

Download the zip file.

Then in File manager navigate to the /home/pi directory.

Click on the Upload button.

Then in the pop up window browse to the zip file that was just downloaded.

Click on Upload.

Step 28: Expand the Web Page

Picture of Expand the Web Page

Another handy feature of File Manager is the Extract button.

Select the web_ir.zip file and click on the Extract button.

Click on yes in the pop up.

Now we want all these files to be accessible to the pi user. In the /home/pi directory (which is the default on login), run the following command

sudo chown -R pi:pi *

A problem with zip files is that they don't carry Linux attributes. The cgi file needs to be set to executable.

In File Manager navigate to /home/pi/web_ir/cgi-bin.

Select the rc file.

Click on the Info button.

Select the user exec check box and click on Save.

Now let's reboot. Use the command line or the Webmin method.

sudo shutdown -r now

Step 29: Check Out Our Networked Remote Control

Picture of Check Out Our Networked Remote Control

Now everything should be configured and running. We can go to our web page at the address for our Raspberry Pi we found earlier.

In our example we type http://192.168.5.73 in the address bar of a browser. The above screen will pop up.

A click on an input device will move the TV under that device and surround it with a red box, indicating that they are connected. A click on the TV will turn that input section (A or B) off or on. A click on the Prev or Next button will shift to the previous or next TV. In our case, it goes between "Bedroom" and "Family Room".

Step 30: Web Page Design

Picture of Web Page Design

It is not in the spirit of Instructables to say here is a web page, it works, use it. I have placed a lot of comments in the code. Consider them additional steps to the Instructable. Also in the spirit of things, here is a discussion of the web page design.

The basic idea of the web page is to use large icons to make it easy to use on touch screen devices; in particular, smart phones. Note I have used a little CSS scaling trick. The font-size property for the body selector controls the scale of the page (everything is defined in units of em). I have sized the page to fit comfortably on my tablet. One can change font-size to scale for their device.

The layout is a table, when one clicks on a input device, javascript sends the key code to select that device. The javascript also moves the TV element to the proper column and draws the box around the two icons.

I use an http GET because it uses a query string embedded in the url. This allows one to type a url in the address bar of the browser to send key codes. The cgi file is a bash script which extracts the key code from the query string and uses the "irsend" command with that key code. The HDMI switch can control inputs to two TVs. I can switch between these TVs with large buttons. The name changes to indicate for which TV the inputs are being controlled. The commands are stored in javascript doubly indexed arrays. The Prev and Next buttons change the first index to match the TV being controlled. The 2nd index accesses the proper key code to perform the given function.

I have created a video showing the system in operation.

http://youtu.be/HToL1c9Grcw

Comments

steve.m.graves (author)2015-06-30

I have tried to set every link to open in a new tab. Those settings go away when I publish the Instructable. I have tried to make that change several times as well as being part of the original. Taking away the Instructable when you click on a link is not part of my plan. Sorry for the inconvenience.

About This Instructable

2,748views

42favorites

License:

More by steve.m.graves:Deodorant FrugalityFreescale FRDM-K64F: USB communicationRaspberry Pi Configured for a Maker Fair
Add instructable to: