GPIO Hardware Pushbutton control for XBMC?
#1
Hey guys I've been doing a lot of research on the subject and honestly it has led me in way too many directions. The info I have seen is also kind of dated and I would like to know if there is a "new way to do things."

Here's what I want to do

I am going to hook a push button up to the RPI 2 Model B GPIO pin.

When I press that push button I want a script to be able to pick up that the button is pressed and then activate an action such as go "left."

I believe I have all the necessary libraries to do this but I am worried that I might be missing something vital.

I keep hearing a lot about how XBMC(KODI) does not have root access and cannot communicate with the GPIOs without proper configuring. If this is true how do I bypass it?

Should I write the script to activate the commands in the XBMC(KODI) module or through the command line module for OSMC? OR do I need to have scripts in both modules to successfully do this?

I am strictly focused on using hardware push buttons ONLY.
Reply
#2
Quote:I keep hearing a lot about how XBMC(KODI) does not have root access and cannot communicate with the GPIOs without proper configuring. If this is true how do I bypass it?

You should be able to use GPIO as an unprivileged user because /dev/mem is not strictly controlled under OSMC.

Sam
Reply
#3
I've just done this very thing (albeit using breadboard at the moment as I'm awaiting a pHat for my Pi Zero to build it properly).

Which distribution are you using? I've got it running beautifully on my OpenElec set-up and can happily share the script with you if you want. It basically uses 6 buttons, 5 are for navigation (up/down/left/right/select) and the 6th is a "shift" button giving alternative actions on the keys (back/info/stop/play-pause and context menu in my set-up).

Under OpenElec GPIO works fine as you are the root user anyway, but since 6.0.x you need to install the RPi.GPIO add-on (available from the OE unofficial repo).
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#4
(2016-02-22, 18:25)DarrenHill Wrote: I've just done this very thing (albeit using breadboard at the moment as I'm awaiting a pHat for my Pi Zero to build it properly).

Which distribution are you using? I've got it running beautifully on my OpenElec set-up and can happily share the script with you if you want. It basically uses 6 buttons, 5 are for navigation (up/down/left/right/select) and the 6th is a "shift" button giving alternative actions on the keys (back/info/stop/play-pause and context menu in my set-up).

Under OpenElec GPIO works fine as you are the root user anyway, but since 6.0.x you need to install the RPi.GPIO add-on (available from the OE unofficial repo).

I am using OSMC because I read that OpenElec Wouldn't give me the versatility I needed to control the GPIO aka (installing Rpi.GPIO is hard ??).

I wouldnt mind seeing those scripts regardless as I believe I could alter them to work with OSMC. The only problem i seem to be having is this whole communication between XBMC module versus Command Line Module. (I cannot just run a script that uses XBMC module outside of XBMC..??)
Reply
#5
It's just installing the unofficial repo (it's available from the official OE repo that comes built in with OE) and then installing the RPi.GPIO add-on like any other one. It was removed from OpenElec as it's a bit of a niche application, but it works just fine once you install the add-on. OSMC never removed it, so it's there and available for use out of the box.

Anyway the scripting should work fine in either distribution - I'll grab it for you tonight and post it here (it's set up as an add-on, so should be easy for you although I'm very much a beginner at Python scripting but it seems to work).
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#6
(2016-02-22, 18:24)Sam.Nazarko Wrote:
Quote:I keep hearing a lot about how XBMC(KODI) does not have root access and cannot communicate with the GPIOs without proper configuring. If this is true how do I bypass it?

You should be able to use GPIO as an unprivileged user because /dev/mem is not strictly controlled under OSMC.

Sam

So does that mean I can write my script that picks up the buttons, place it in the xbmc/script (w/e the thing is actually called), find a way to have it automatically start on boot, and my buttons should be able to communicate perfectly?

(2016-02-22, 18:40)DarrenHill Wrote: It's just installing the unofficial repo (it's available from the official OE repo that comes built in with OE) and then installing the RPi.GPIO add-on like any other one. It was removed from OpenElec as it's a bit of a niche application, but it works just fine once you install the add-on. OSMC never removed it, so it's there and available for use out of the box.

Anyway the scripting should work fine in either distribution - I'll grab it for you tonight and post it here (it's set up as an add-on, so should be easy for you although I'm very much a beginner at Python scripting but it seems to work).

I appreciate this very much.
Reply
#7
If you call it autoexec.py and put it in .kodi/userdata, it will run at startup. DarrenHill's suggestion of an addon is a better method. You do not need to the the root user on OSMC to do this.

RPI.GPIO isn't in OSMC by default (but you should be able to install it easily). See https://discourse.osmc.tv/t/gpio-control...am_nazarko for some help.
Reply
#8
OK, I've uploaded a zip file of the add-on to my box account - here.

You should be able to install it simply by downloading and then using "install from zip file" in Kodi and then pointing it at the downloaded file. It's very rough-and-ready as I only wrote it for my own usage, so haven't bothered to make it pretty or fancy.

Anyway it's set up as a services add-on, so should run at Kodi start-up by default. Hardware-wise, it's set up to take the GPIO inputs on GPIO 17, 27, 22, 6, 13 and 19 (board pins 11,13,15, 31, 33, 35 respectively) although you can change that in the script. The pins are set as inputs using the internal pull-up resistors to make them high, and then the buttons pull them low to give input. Basically the associated circuitry just has one switch per input, with one side connected to the relevant GPIO pin and the other to ground. It would probably be better to use external pull-up resistors, but on the board I'm going to be using I haven't got the space to wire them up (I'm going to be using a ProtoZero board, hence the naming). I'm also going to add an IR receiver diode to the board for a complete solution (I already use one on three jumper cable wires, this will be a more physically robust solution).

As I said before I'm very much a beginner at Python scripting so things may well not be optimised, but they do seem to work in my testing so far. If you (or anyone else) has suggestions for improvements or corrections then I'm happy to hear and learn Wink But hopefully it should get you started on things.

I'm not sure if you'll need the sys import and append at the start if you're using OSMC. The OpenElec add-on for RPi.GPIO does need them - maybe Sam can comment there?

Anyway the files are reproduced below. They're fairly simple, but if anything isn't clear let me know. Feel free to use them as you want, either as a basis of something more complex or as they are.

buttons.py
Code:
#!/usr/bin/python

import sys
sys.path.append('/storage/.kodi/addons/python.RPi.GPIO/lib')

import RPi.GPIO as GPIO
import xbmc

upPin = 17        # board pin 11
downPin = 27    # board pin 13
leftPin = 22    # board pin 15
rightPin = 6    # board pin 31
selectPin = 13    # board pin 33
shiftPin = 19    # board pin 35

def up_callback(channel):
    if(GPIO.input(shiftPin)):
        xbmc.executebuiltin("Action(Up)")
    else:
        xbmc.executebuiltin("Action(Back)")

def down_callback(channel):
    if(GPIO.input(shiftPin)):
        xbmc.executebuiltin("Action(Down)")
    else:
        xbmc.executebuiltin("Action(Info)")
      
def left_callback(channel):
    if(GPIO.input(shiftPin)):
        xbmc.executebuiltin("Action(Left)")
    else:
        xbmc.executebuiltin("Action(Stop)")

def right_callback(channel):
    if(GPIO.input(shiftPin)):
        xbmc.executebuiltin("Action(Right)")
    else:
        xbmc.executebuiltin("Action(PlayPause)")

def select_callback(channel):
    if(GPIO.input(shiftPin)):
        xbmc.executebuiltin("Action(Select)")
    else:
        xbmc.executebuiltin("Action(ContextMenu)")

class Main:
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(upPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(downPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(leftPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(rightPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(selectPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(shiftPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(upPin, GPIO.FALLING, callback=up_callback, bouncetime=300)
    GPIO.add_event_detect(downPin, GPIO.FALLING, callback=down_callback, bouncetime=300)
    GPIO.add_event_detect(leftPin, GPIO.FALLING, callback=left_callback, bouncetime=300)
    GPIO.add_event_detect(rightPin, GPIO.FALLING, callback=right_callback, bouncetime=300)
    GPIO.add_event_detect(selectPin, GPIO.FALLING, callback=select_callback, bouncetime=300)

    monitor=xbmc.Monitor()

    while not monitor.abortRequested():
        if monitor.waitForAbort(1):
            break

    GPIO.cleanup([upPin,downPin,leftPin,rightPin,selectPin,shiftPin])        
        
if (__name__ == "__main__"):
    Main()

addon.xml

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.protozero.buttons" name="Zero Buttons" version="1.0.0" provider-name="DarrenHill">
    <requires>
        <import addon="xbmc.python" version="2.20.0"/>
    </requires>
    <extension library="buttons.py" point="xbmc.service" start="login"/>
    <extension point="xbmc.addon.metadata">
        <summary lang="en">ProtoZero Buttons</summary>
        <description lang="en">Driver script for GPIO buttons</description>
        <disclaimer lang="en">This plugin requires hardware buttons connected by GPIO.</disclaimer>
        <platform>all</platform>
        </extension>
</addon>
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#9
(2016-02-22, 21:11)DarrenHill Wrote: ..

I won't be able to test it until tomorrow as I have left the device with a friend hoping he would have a better time with it. As of now I hold the hardware and he holds the RPI2.

I really appreciate this and I am hoping there won't be some permissions error.
Reply
#10
It should be OK I'd say.

To come back on one point - as you said above the xbmc module is only available for scripts run from within Kodi, at least on OpenElec. Hence just using an autoexec.py won't work, this was why I built my script up as a service add-on as that has it both running at start-up and also running within the Kodi environment to have the xbmc module available.

One other option if you do need to run the script standalone is to use:

Code:
kodi-send --action='RunScript("/path/to/script.py")'

That's designed more for testing iirc, but it should work from a normal shell script on the pi. But the add-on method works fine for me.

Whether the same restriction applies in OSMC or not I don't know, but I'm sure we can get this working as well for you as it does for me.
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#11
kodi-send --action='RunScript("/path/to/script.py")'

That will work on OSMC.
Reply
#12
(2016-02-23, 12:10)DarrenHill Wrote: It should be OK I'd say.

To come back on one point - as you said above the xbmc module is only available for scripts run from within Kodi, at least on OpenElec. Hence just using an autoexec.py won't work, this was why I built my script up as a service add-on as that has it both running at start-up and also running within the Kodi environment to have the xbmc module available.

One other option if you do need to run the script standalone is to use:

Code:
kodi-send --action='RunScript("/path/to/script.py")'

That's designed more for testing iirc, but it should work from a normal shell script on the pi. But the add-on method works fine for me.

Whether the same restriction applies in OSMC or not I don't know, but I'm sure we can get this working as well for you as it does for me.

Hey so, I installed them through the zip and KODI gave me an error. Tells me to check the logs but I don't know how to access them to check them.

We're trying to use Kodi Log Uploader but that fails to install even though I am connected to the internet.
Reply
#13
(2016-02-22, 18:24)Sam.Nazarko Wrote:
Quote:I keep hearing a lot about how XBMC(KODI) does not have root access and cannot communicate with the GPIOs without proper configuring. If this is true how do I bypass it?

You should be able to use GPIO as an unprivileged user because /dev/mem is not strictly controlled under OSMC.

Sam

This appears not to be true. I have found a way to read my logs and the scripts provided by Hill give me an error that I have no access to dev/mem. It asks me to try running as root and I don't really know how to do that.

OSMC doesn't really give me a big screen telling me why I don't have root access.
Reply
#14
(2016-02-23, 19:59)DroppedPJK Wrote: Hey so, I installed them through the zip and KODI gave me an error. Tells me to check the logs but I don't know how to access them to check them.

We're trying to use Kodi Log Uploader but that fails to install even though I am connected to the internet.

Hmm odd, it installs fine on my OpenElec Pi's (zero and 2B). But if the log uploader also fails to install, then I'd wonder if your set-up has a problem somewhere. Is it a clean set-up, or do you have add-ons or other stuff installed that could be messing with your workings?

If you want to test just the script, unzip the file, move buttons.py somewhere suitable (e.g. the downloads folder) and then run it from a terminal (ssh to your Pi from a PC or Mac on your network, username and password both osmc) using the command I quoted before:

Code:
sudo kodi-send --action='RunScript("/storage/downloads/buttons.py")'

That's what it would be under OpenElec - I'll let Sam hopefully confirm if it's the same under OSMC (and also to answer your query about GPIO access - there I have no idea). I've added the sudo command to the code above, as that should allow the command to be run with root priv's from a terminal screen.
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#15
I was not clear.

From our kernel config:

+# CONFIG_STRICT_DEVMEM is not set

This means that /dev/mem access is not filtered. But you still need to be root to access that node. The correct node is /dev/gpiomem which will allow you to use GPIO without any changes to permissions and you will not need to be root.

Info about the root user is here: https://osmc.tv/wiki/general/usernames-and-passwords/, but OSMC will always run Kodi as the osmc user.

I do believe the latest version of RPI.GPIO uses /dev/gpiomem first and falls back to /dev/gpio if the device node is not present or accessible: https://sourceforge.net/p/raspberry-gpio...ckets/115/.

Sam
Reply

Logout Mark Read Team Forum Stats Members Help
GPIO Hardware Pushbutton control for XBMC?0