Mapping Buttons on my Kodi Remote to shell scripts that invoke remote Web server calls

I was asked how do I invoke the web services I've set up for home automation. For Kodi, I have buttons on my remote linked to shell scripts on the Kodi instance running on a Rasberry Pi 2.

For LibreElec there is a folder for Librelec on RP it is here /storage/.kodi/userdata check the Kodi Wiki for your platform

/.kodi/userdata/keymaps 

In this folder create a file e.g. gen.xml

In this file, I map/remap the keys on my remote.

I've added a

  1. context menu, 
  2. a button to toggle between Bluetooth and the HDMI output for the AV Amp 
  3. remote web server calls to an ESP8266 that has a 433Mhz transmitter to power on the TV, Sub, Speakers x7, DI Box, AV Amp, etc This allows me to power up just the TV and AMP for headphone listening or all the units including the speakers 
This is my file called gen.xml

<keymap>
<global><keyboard>
<key id="61654">contextmenu</key>
<key id="61674">runaddon(script.audio.profiles,0)</key>
<key id="61636">system.exec("/storage/avspeakerson.sh")</key>
<key id="61675">system.exec("/storage/avspeakersoff.sh")</key>
<key id="61637">system.exec("/storage/avonlyon.sh")</key>
<key id="61662">system.exec("/storage/toggleavpower.sh")</key>
<key id="61572">system.exec("/storage/volup.sh")</key>
<key id="61573">system.exec("/storage/voldown.sh")</key>
<key id="61586">system.exec("/storage/sat.sh")</key>
<key id="61585">system.exec("/storage/kodi.sh")</key>
<key id="61623">system.exec("/storage/mute.sh")</key>
</keyboard></global>
</keymap>

When starting this process from scratch I normally use a program add-on called keyboard mapper to kick off the keyboard mapping the program is menu driven and walks you through mapping functions to buttons. I start by deciding which buttons I want to map, then use the keyboard mapper to map each of the buttons I want to map to some simple navigation e.g. the first button to up, the second button to down, etc Then I save the generated XML file using the keyboard mapper. While I'm mapping the keys I make a note of which keys are mapped to which function e.g.

BLUE - UP
YELLOW - DOWN
RED - LEFT
GREEN - RIGHT

This approach maps out the key ID on the remote to known functions. I don't really want to use these navigation functions they are just placeholders to get the keyboard mapper to write a sample XML file that I can modify.

When I SSH to the Kodi box and open then gen.xml file created by the keyboard Mapper Program Add-on it looks something like this

<keymap><global><keyboard>
<key id="389169">down</key>
<key id="389184">right</key>
<key id="389266">left</key>
<key id="389188">up</key>
</keyboard></global></keymap>

Now I create shell scripts in the storage directory and it is called avspeakerson.sh it has a single line wget command to my ESP8266 based web server with a 433mHz transmitter that will send a command to several remote RF sockets

wget http://192.168.0.237/AVSPEAKERSON -O /dev/null

This shell script is linked to the button I want to use on the remote one line 4 below.

You can set up buttons that are specific to each Kodi instance. For example, in the Gym, I have these scripts to run on the TV, AMP and optionally the running machine. There is one to turn everything off as well. Note how I've set the execute bit (chmod +x file). It makes sense to test each script from the command line in your SSH session.

-rwxr-xr-x    1 root     root            47 Sep 30 22:31 gymoff.sh
-rwxr-xr-x    1 root     root            46 Sep 30 22:33 gymon.sh
-rwxr-xr-x    1 root     root            46 Sep 30 22:34 runon.sh

So we have an XML file with the correct buttons linked to navigation keys replace the navigation command with the script or Kodi add-on you want associated with each button. In this example for my Kodi box in the gym, there is a toggle for the audio profile to switch from Bluetooth to AMP / HDMI and buttons to power on the Gym TV, Amp and running machine plus an ALL OFF button



 
<keymap><global><keyboard>
<key id="389169">runaddon(script.audio.profiles,0)</key>
<key id="389184">system.exec("/storage/gymon.sh")</key>
<key id="389266">system.exec("/storage/runon.sh")</key>
<key id="389188">system.exec("/storage/gymoff.sh")</key>
</keyboard></global></keymap>

You may need to restart Kodi to reread the new keymappings.

It's worth backing up this file as some of the automated tools for recording key bindings on Kodi can overwrite the mappings files!