For a little challenge, I want to add a physical on/off switch to OpenElec. This switch should turn the Raspberry Pi on if the Pi is sleeping, and it should initiate a clean shutdown if the Pi is on. This should be a physical button, not just a software remote button.http://aton-mebel.ru
OpenElec makes this process a little difficult because it uses the squashfs filesystem. Therefore, you don’t have access to the same packages and directories as you typically do.
Please be advised, this projects rating is: More Difficult – Involves soldering, use of ssh, command prompt, and coding.
There are four major steps.
First, make and connect a simple “wake-up” button. To wake the Raspberry Pi up when it is asleep, all you need to do is short Pin 5 to ground. (Pin 5 is also known as GPIO03). Since Pin 6 is already at ground, you can do this by shorting Pins 5-6.
Find a contact switch
You can find these for a lot cheaper online, but sometimes you just want to complete a project right now! I suppose I’m just too impatient sometimes.
The important thing is that you are looking for a Normal Open (N.O.) Momentary Contact switch. This will short the two pins only when the button is pressed. If you choose a Normal Close (N.C.) button, you may have additional problems (such as the Pi perpetually resetting)
Below is a picture of the button mounted in the case:
Header Strip Plug The next thing that you need is a header strip to plug onto to the Pi. You could use a regular female header strip like this one:
If you have an old computer case laying around (which many of us do), you can just steal one of the cables connecting a case LED to the mother board. Motherboards have the same spacing as the Raspberry Pi header. For a previous project, I used a former speaker cable:
In this case, I used a jumper like the one shown here. I used two of them, one for each lead coming from your button.
The advantage is that you don’t have to do any soldering to the straight pins. Also, it allows your switch to be removable, but the contacts are pretty solid. As long as you use solid wire, place a solid wire lead on each terminal of the switch. Then you can plug the wire directly into the jumper. You want the switch to be able to unplug from the Pi so that you can mount it in a case and remove it if necessary (which always seems the case with my projects).
I recommend covering each lead as well as both contacts together with some head shrink tubing. This gives a little support to the connection and also prevents unintentional shorts. In this example, the leads are shown.
Connecting to the Pi
When you are all done, simply plug your wired switch across pins 5 and 6 on the Pi, as shown below:
The reset switch is connected using the green and black jumper cables. Since the switch only provides a short when pressed, it shouldn’t matter which lead is connected to which pin.
You’ll notice that this is shown with the original Raspberry Pi Model 2, but this should work with all versions of the Pi.
That’s it. You now have a button that can wake the Raspberry Pi from sleeping mode. This is a hardware switch, nothing needs to be done in software to wake the Pi from the sleeping state, it will automatically wake when Pin 5 is tied to ground.
OpenElec is a specialized install of Linux and has a few minor “perks” that can make things a little difficult.
This makes programming with the GPIO a little more difficult. Luckily, the work has already been done and there is an add-on available for OpenElec in the unofficial add-on repository. To install the addon you must do the following:
From the Kodi Menu, go to System
Press down until you get to Add-ons
Select Get Add-ons
Choose the Unofficial repository
Select RPi.GPIO and hit enter (or select)
This should bring up the screen below. Navigate down to “Enable” to enable the Add-on.
Once you have clicked on enable, your screen should look like the following, to show that it has been installed and enabled.
Notice that the only difference is that your option is now to “Disable” the Add-on, instead of enabling it.
Now, we can do some coding.
The following commands should be done through SSH. By default, SSH is enabled on OpenElec. You will need to find the IP address of your Pi. If you don’t know it already, you can find it by going to the Main Menu on Kodi, then System -> System Info. As long as you have your Pi plugged into the network, the IP address should be shown on this screen.
I recommend creating a directory to hold your scripts. In my case, I created the directory under the root /storage/ directory. It can be created by doing:
This will make a scripts directory under the
/storage folder. This is a writable and persistent area of the file system (all good things).
Next, we need to write the script. I personally use nano as my editor of choice, but you can use any editor you wish. Please Note: I highly recommend retyping the script. Do not copy and paste. It is possible that if you copy and paste, there will be slight changes in formatting (i.e. comment lines wrapping and no longer being comments) or incorrect characters (particularly quotes) that will keep your script from working!!!
We will call our script shutdown.py (it is written in python). Create and edit the script by doing:
The content of the script should be:
Once you have created your script, you can test it by typing the following at the command prompt:
It may give warnings regarding a pull-up resistor. This is OK. If error’s are encountered, resolve them first. If it is able to run, try pushing the button and see if the Pi shuts down.
Once you have the script working the way that you want it, let’s make it run at startup.
We still have problem with the fact that the core system of openelec is not easily writeable. So…you can’t put anything in init.d like usual to have it run at startup. Luckily, the makers of OpenElec created a mechanism for running programs at startup. It is similar to the old autoexec.bat found on DOS machines. Instead, you must place any programs in the
You can edit the autostart.sh file by doing the following:
The content of my autostart file looks like this:
That’s it! Now your script should run at startup. If you already have an autostart.sh file, simply add the line of code at the bottom. Once you restart your pi, you should be able to see shutdown.py running in the background. You can verify this either by pressing the button and seeing if OpenElec shuts down, or doing the following from the command line: