Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 6:37 am

Hi,

Currently working on a script for XBMC (raspbmc)
I have several pushbuttons connected to the GPIO (model B)

I have a working script that detects the button presses, and also a working script that will command XBMC to play a playlist. (shown below)

where I run into problems is that XBMC seems to have it's own python interpreter with it's own set of modules.

So say if I try to "import xbmc" using python over SSH - it fails, stating no module found.

however launching this from /home/pi/.xbmc/usedata/autoexec.py on boot,
finds the module and works just fine.

likewise "import RPi.GPIO" fails from autoexec.py, and does not on python over SSH.

As I understand it, I must run "xbmc.executebuiltin" commands from within xbmc, and not externally (such as over SSH)

so.... how do I load the GPIO module in such a way that I can import it from within xbmc's python interpreter?

Code examples below:
My working GPIO detection script (working over SSH but NOT from XBMC)

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
#GPIO.setup(18, GPIO.OUT)#indicator light on relay card
GPIO.setup(15, GPIO.OUT)#screen power button
GPIO.setup(8, GPIO.IN,pull_up_down=GPIO.PUD_UP)#front panel button
GPIO.setup(9, GPIO.IN,pull_up_down=GPIO.PUD_UP)#front panel button
GPIO.setup(10, GPIO.IN,pull_up_down=GPIO.PUD_UP)#front panel button
GPIO.setup(11, GPIO.IN,pull_up_down=GPIO.PUD_UP)#front panel button
#GPIO.setup(17, GPIO.IN,pull_up_down=GPIO.PUD_UP)#power button
while True:
#       print "Waiting!"
        time.sleep(0.5)
        if (GPIO.input (8) == False):
                print "Detected GPIO 8!"
                GPIO.output(15, True)
                time.sleep(0.1)
                GPIO.output(15, False)
        if (GPIO.input (9) == False):
                print "Detected GPIO 9!"
        if (GPIO.input (10) == False):
                print "Detected GPIO 10!"
        if (GPIO.input (11) == False):
                print "Detected GPIO 11!"

My working autoplay script (NOT working over SSH but working in XBMC)
Note the disabled lines. the script "fails" if I enable these lines.

Code: Select all

import xbmc
#import RPi.GPIO as GPIO
#import time
#import os
xbmc.executebuiltin( "PlayMedia(/home/pi/.xbmc/userdata/playlists/video/all.m3u)" )
xbmc.executebuiltin( "PlayerControl(repeat)" )

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 8:28 am

You may have already read it but there's some info here https://code.google.com/p/xbmc-gpodder- ... loads/list specifically mentioning set up on linux with remote access (plus lots of other useful info). I don't know how the xbmc python sorts out a path to use for imports, maybe you could import sys then print out sys.path and append to it wherever RPi.GPIO has been installed (/usr/lib/python2.7/dist-packages/ or whatever) before trying to import it. Does your XBMC script fail with just either 'import time' or 'import os' enabled?

PS presumably you've read all the standard google results such as this http://forum.kodi.tv/showthread.php?tid=153612 and this
http://kodi.wiki/view/Addons_for_XBMC
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 9:07 am

This is quite a common question.

Yes, you are right, XBMC uses a built-in interpreter. As such, the XBMC modules (xbmc, xbmcgui etc etc) are only accessible from scripts executed from within XBMC. Nothing wrong with that so far...

The problem is when you want to use GPIO pins from within XBMC. You need to run your script as root to access the pins, but xbmc scripts run as the default user.

There are a couple of options available to you:
1) Write a script that runs outside of XBMC and send commands to XBMC using the JSON RPC interface (very easy once you're familiar with the structure)
2) I believe the pigpio module can access gpio pins without being root. However, I've never tried this so can't provide any guidance.

I can certainly help if you go down route #1.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 9:27 am

Yes I have just been playing with wiringpi2 and have discovered that openelec runs xbmc as a user and not as root.

This appears to be my issue - I cant import certain modules at user level.
I have been able to replicate the failures over ssh, by simply omitting the sudo command.

I have also had a look at JSON commands, but having a little issue there too.
The project is a proof-of-concept interactive museum display.

I already have a code running from cron @reboot, to switch on the display after a video autoplays.

But as we know - cron runs as root.
I'm currently attempting to tackle the problem at the root cause - so to speak, and trying to get xbmc to run as root.

but I'll have a look at JSON, and see if I can get this happening externally.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 9:32 am

As for the cited links - Thankyou muchly, but they are all ones I have read in depth, and are responsible for my current predicament...

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 10:17 am

You can do a huge amount of control via JSON.

What commands do you need your script to trigger?

Assuming there's nothing very unusual then I think you may have more success this way than getting xbmc to run as root.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 10:42 am

It seems a long and convoluted way to do something that should be a one-liner type code.

Just need the basics - play, pause, previous, next.

I'll post a few images shortly.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 11:22 am

Just for interest sake - This is what I'm making:

A proof-of-concept Interactive display system for the
Gippsland Armed Forces Museum

https://www.facebook.com/22878271718804 ... to_comment

If you get curious about the museum:
https://www.facebook.com/GippslandArmedForcesMuseum

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 12:39 pm

I love the plywood-with-burned-edges styling. Have you looked at using pipresents and github source I'm not sure what you are wanting to do but pipresents is quite mature and aimed at museum displays etc.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Importing modules (RaspBMC) in python - confused...

Wed Nov 05, 2014 3:22 pm

Dranoweb wrote:It seems a long and convoluted way to do something that should be a one-liner type code.

Just need the basics - play, pause, previous, next.
I'd disagree with this - it's actually very, very simple to do this (but I will concede it could be more than one line).

If you use this module you can do something like (NB I didn't write the module, but I did fix an authorisation issue in the code):

Code: Select all

from xbmcjson import XBMC
x = XBMC("http://[IP_ADDRESS_OF_YOUR_XBMC:PORT/jsonrpc")

def get_active_players():
  result = x.Player.GetActivePlayers()
  return result.get("result", [])

def play_file(filepath):
  x.Player.Open({"item": {"file": filepath}})

def play_pause():
  players = get_active_players()
  for player in players:
    x.Player.PlayPause({"playerid": player["playerid"]})

def stop():
  players = get_active_players()
  for player in players:
    x.Player.Stop({"playerid": player["playerid"]})
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Thu Nov 06, 2014 12:05 am

Thanks for the reply - I'll try out this code later today when I get a few minutes spare.
the convoluted part I refer to is the fact that we have to run an external python script, and then send a network command back to the localhost - each extra layer of complexity is more things that can go wrong and more lag.

However - if it works, don't fix it I guess.

My coding technique is to try and keep code to a minimum and with as few "layers" as possible.
it's kind of how one can still achieve a similar level of functionality with DSL or puppy linux, to windows that uses vastly more wasteful coding.

In either case, I'm grateful for the help, and I'm learning many more tricks to squeeze into my already full head.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Thu Nov 06, 2014 12:07 am

paddyg wrote:I love the plywood-with-burned-edges styling. Have you looked at using pipresents and github source I'm not sure what you are wanting to do but pipresents is quite mature and aimed at museum displays etc.
The edging is burnt, as I cut most of the components on my larger laser cutter.
I have two cutters. I also have a 3d printer on the way for more projects like this.

3mm ply is nice to work with as well as acrylic, but it's hard to find a supplier that doesn't sell you warped stuff.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Importing modules (RaspBMC) in python - confused...

Tue Nov 11, 2014 11:24 am

OK so some progress

I had real difficulty in Json to work, and what little success I had was really really laggy.

So in the end I hacked open a $6 silicone keyboard and hardwired the buttons to the shift register connections.

I messed up on one button, so what was meant to be comma, is now "n", but keymapping fixed that.
hopefully this post will help someone else on their way.

Progress pictures:

https://www.facebook.com/22878271718804 ... 75/?type=1

https://www.facebook.com/22878271718804 ... permPage=1

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Importing modules (RaspBMC) in python - confused...

Tue Nov 11, 2014 12:23 pm

Looks really good. Well done.

EDIT: I'm surprised that JSON was laggy for you but it looks like you found a solution anyway.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”