Controlling OSMC with hardware button

I’m trying to control OSMC like play/pause with hardware buttons. A touch sensor is connected to a GPIO port of my Raspberry Pi 2 B.
I use this code:

import RPi.GPIO as GPIO

import time, os, httplib, json, requests, pdb

from requests.auth import HTTPBasicAuth



buttons = [24                ]

methods = ['Player.PlayPause']

params  = [{ "playerid": 0 } ]

user    = ''

passwd  = ''

port    = '80'

def main():

   numbuttons = len(buttons)

   index = 0

   for index in range(numbuttons):

       GPIO.setup(buttons[index], GPIO.IN)

       GPIO.add_event_detect(buttons[index], GPIO.RISING)

   while True:

       index = 0

       for index in range(numbuttons):

           if GPIO.event_detected(buttons[index]):



def button_pressed(index):

   url = 'http://localhost:' + port + '/jsonrpc'

   postheaders = {'content-type': 'application/json'}

   command = {"jsonrpc":"2.0", "id": 1, "method": methods[index], "params": params[index]}


   resp =, auth=(user,passwd), data=json.dumps(command), headers=postheaders)

if __name__ == '__main__':


When playing music via SMB or streaming videos from the internet it works as intended. Music and videos play/pause when pushing the button.
Only when playing videos via SMB it doesn’t react at all. The video won’t play/pause.

Is there a mistake in the hardware script?

Instead of JSON-RPC it might be easier to use kodi-send, to directly call built in functions:

kodi-send is available at the command line, (which you could call with subprocess) or you could use the same function calls used in kodi-send directly in your python script, as kodi-send is actually a python script:

Thanks for that. I think I will try it with subprocess.This way I can integrate it in existing scripts using interrupt for the button.
I already tried typing
kodi-send --host= --port=9777 --action=“PlayerControl(Play)”
in command line via SSH.
The problem stays the same. I’m able to play/pause music and some videos but not all of the videos. It doesn’t matter which format the videos are or wether they are streamed from the internet or from samba share. Some videos play/pause others don’t.

The ones you have trouble with, do they fail to play at all or do they play but you can’t pause them ?

What if you try some of the other PlayerControl() actions on those files, such as SmallSkipForward or Rewind - do those work ?

Strange. After reboot I tried to use the action SmallSkipForward with a video that didn’t work before. SkipForward and any other action works now including Play.
I will further investigate what the problem was.

As a beginner I have some troubles with my script.
I mangaged to call a shell script with python:

# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time, signal
import os

# use the raspi board pin number

playPin = 26 # with GPIO.BOARD, pin#5 is gpio3

GPIO.setup(playPin, GPIO.IN)

def Interrupt_event(pin):
    if GPIO.input(playPin):

GPIO.add_event_detect(playPin, GPIO.RISING, callback=Interrupt_event, bouncetime=200)

    while True:

except KeyboardInterrupt:
    print "Ctrl-C - quit"


Is there an easier possibility? I’ve already tried with subprocess but having an error “system() takes no keyword arguments”.

I think you can use a bash script. No real need for python here?