Rick Hurst Web Developer in Bristol, UK

Menu

Category: camputer

The Camputer Part 3 – API hacking

In part 2 I got RasbBMC installed, and worked out how to get vnc server running. The next goal was to get to know how the music player works, how to create playlists and most importantly hack around with the API so that I can work out how to control it from a python script.

raspbmc displaying on small lcd monitor

My original intention was to run the pi entirely headless, but then I remembered I had already bought a small LCD monitor for a reversing camera, that I never got round to fitting. Hooked up to a 12 volt power supply and to the PI using the RCA socket, I now have a tiny monitor, which is handy for keeping an eye on what is going on with the player.

Most of the text is illegible at that size/ resolution, so I upped all the font sizes for the default skin:-


cd .xbmc-current/xbmc-bin/share/xbmc/addons/skin.confluence/720p
sudo nano Font.xml


Now all the line-spacing is wrong, so is more legible in some places, but less so in others. I don’t want to get side-tracked right now into learning how to skin XBMC. Maybe at a later date, it might be a nice project to create (or find) something specifically for very small screens.

IInitially I couldn’t hear any Audio, so I had to change the audio settings in XBMC to use analog audio out rather than HDMI.

I spent ages reading up on the api, the documentation is pretty confusing and lacking in examples, but eventually I found some simple examples on a forum showing how to interact with the JSON-RPC API.

I was expecting to find some examples of using the python API “directly”, but couldn’t really find anything (or at least anything complete enough to understand real-world use). I eventually found this python xbmc json client.

With that library downloaded to the pi, I was able to speak to xbmc from the python prompt:-


>>> from xbmcjson import XBMC
>>> xbmc = XBMC("http://localhost:8080/jsonrpc")
>>> print xbmc.JSONRPC.Ping()
{u'jsonrpc': u'2.0', u'id': 0, u'result': u'pong'}


To open a saved playlist:-


xbmc.Player.Open([{'file':'/home/pi/.xbmc/userdata/playlists/music/test.m3u'}])


I was then able to play/pause XBMC and move to the next track from the python prompt with:-


xbmc.Player.PlayPause([0])
xbmc.Player.GoTo([0,'next'])


Before I worked out how to open a saved playlist via the API, I noticed that the above play/pause command doesn’t work if the player hasn’t been opened yet i.e. when the Pi has been rebooted. I found a way of opening a playlist and pausing at XBMC startup, by creating a file called autoexec.py in /home/pi/.xbmc/userdata with the following in it:-


import xbmc
xbmc.executebuiltin("PlayMedia(/home/pi/.xbmc/userdata/playlists/music/all.m3u)")
xbmc.executebuiltin( "XBMC.Action(Pause)" )


This is run by XBMC at startup, and doing so ensures that the player has a playlist loaded and ready to play at startup. Currently not sure where this script imports xbmc from or how I could have this available to my own application scripts and managed by XBMC, but there must be a way. Either way I seem to be able to do most of what I need with the JSON-RPC API currently, with the exception of shuffle. The following command toggles shuffle on and off:-


xbmc.Player.SetShuffle({"playerid":0, "shuffle":"toggle"})


The JSON response sais “OK”, but annoyingly it has no effect on the XBMC player. When operating XBMC manually you can open a saved playlist, and at any point click the shuffle button, to toggle shuffle on and off, and the next track will either be in order, or “random” from a shuffled list. I was hoping that this toggle on the API would have the same effect, but apparently not. I think I may have to consult the XBMC forum, once i’ve put togther a precise list of commands that i’ve tried, and checked exactly which version of XBMC and API I am using (you know how forums can be if you go asking questions without providing enough background info!).

If it turns out that this is a bug and there is no solution for shuffling the current playlist with the API, I can envisage a workaround involving dynamically creating a shuffled version of each of the saved playlists. As I intend to create some kind of playlist management app for this (outside of XBMC), it would be a simple step on from that.

So, besides working out a solution for shuffle, the next step is to connect the Piface digital I/O expansion board, and work out how to trigger API stuff in response to button presses.

The Camputer Part 2 – raspBMC installation

(See Part 1 for details on what the project is about and the kit bought for it so far).

I decided to go with RaspBMC as the operating system, on the basis that I wanted something Raspian/debian based, and this distro is exactly that, but with the benefit of being preconfigured as an XBMC media player.

I ordered a 16gb SD card (I intend to keep my media on an external USB drive) and used the instructions to create a bootable raspBMC installation, using my macbook air, which has an SD card slot.



Then I put the SD card in the Raspberry Pi board that I got from UK Farnell, hooked up to a keyboard, mouse, ethernet cable (connected to our home router), power supply, audio cable to my hifi amp and our TV via HDMI cable.

The bootable SD then takes you through the rest of the installation – it took about 25 minutes.



The Pi then booted into XBMC, which i’ve never actually seen before, and it looks great. I enabled AirPlay in the settings and found that I could stream audio and (non-apple DRMed) movies from itunes and from my wife’s iPhone. I’ll probably set one up specifically as a home media system in the future, and probably others just for audio streaming to amps/speakers around the house.

I then went of at a tangent, trying and failing to get a vnc server installed and working, so that I can control it from another device, as it will likely be run headless in the camper van. After a bit of digging though, I realised it already has a VNC server installed which can be enabled in the system settings – the raspBMC settings rather than XBMC settings. You can also start it from the command line with:-


service vnc start


I couldn’t get it to connect using OSX screen sharing, so I installed Real VNC viewer. It seems very sluggish, but works.



So the next stage is to hook it up to a USB drive with some music on it, create some playlists and investigate how I can interact with the player programatically via the API.

Follow-up posts:

Part 3: The Camputer Part 3 – API hacking

Raspberry Pi project – the Camputer (Part 1)

raspberry Pi kit for my camper van in-vehicle computer

I’m building an in-vehicle computer for my camper van. I wasn’t sure whether to blog about this here or on my Camper van blog, but as the project will initially be about software, i’ll start here, then move over to the campervan blog when I get to the point of installing it.

Why does a campervan need an on-board computer?

Well obviously it doesn’t need a computer, but here are a few things i’d like to do with it:-

Media system

I don’t have many CD’s these days, so I currently use my phone or an ipod to provide music to the stereo system, but it is a bit of a faff, fiddling around with cables and chargers and stuff. I’m also pretty disorganised with my music collection and have several small collections spread out over different iTunes libraries etc. It’s also a pain (not to mention dangerous) to operate a touchscreen while driving. The main requirements for my ideal music system therefore are:-

  1. All my music in one library
  2. Physical buttons to play/stop and skip tracks (a bit like an iPod shuffle)

I have in mind a set of switches and buttons on the dash that I can operate by feel. Aswell as the basic functions listed above, there might be toggle switches for different playlists or genres.

The media system could have a small monitor or LCD display, but should be able to be operated using only the buttons. Other operations (such as creating playlists etc.) I would envisage a web interface, VNC or maybe plugging in a monitor and keyboard.

I haven’t decided what media player software i’ll use, but hopefully there is something with a decent api that I can hook my custom controls to.

I’m thinking mainly music, but with a ceiling mounted pull-down monitor it could also drive a (motor)home cinema system in the future,

Engine diagnostics

I have a bluetooth OBII adapter that I use with the Torque Android app. From a quick google around, people are using them with a Pi, so i’d like to set up some logging.

Security/ Tracking

It would be great to have the Pi send location updates to my server, to tie in with my travel blogging. It could also potentially be useful if the vehicle gets stolen, or I lose it in an unfamiliar town!

Remote switching of the heater

My camper has an on-board propex-heater, so the Pi could potentially be used as a thermostat or to switch the heater on from a web/ phone app.

Kit

So far I have:-

So the first phase will be to decide on an OS and media player and work out how to control it using physical switches – watch this space!

Follow-up posts:

Part 2: RaspBMC installation

Part 3: The Camputer Part 3 – API hacking