The Camputer Part 3 - API hacking

This post was written 3 months ago.
Fri, 16 May 2014
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.
Tags: camputer / raspberry pi / xbmc /

The Camputer Part 2 - raspBMC installation

This post was written 3 months ago.
Wed, 07 May 2014
(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
Tags: raspberry pi / camputer / XBMC /

Raspberry Pi project - the Camputer (Part 1)

This post was written 3 months ago.
Sat, 26 Apr 2014
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

Search and replace on a large file on OSX using Sed

This post was written 5 months ago.
Thu, 06 Mar 2014
My code editor of choice, Sublime text 2, crashes when you try to do a search and replace on a large file. This makes it difficult to do a search and replace on a large sql file, as I sometimes need to do when moving a wordpress site from one domain to another

The command-line tool sed can be used instead like this:-

sed -i '.backup' 's/olddomain/newdomain/g' wordpress-backup.sql
This makes a backup of the original file, then updates the original
Tags: wordpress / osx /

Rasberry Pi as low power office server

This post was written 6 months ago.
Mon, 20 Jan 2014
raspberry pi running as low power office server
Part of a system that I look after for a small distribution company includes a linux server based in their offices. The server doesn't do much - it runs a few cron jobs and a mySQL database, acting as a monitoring system on the internal network and as a middle-man receiving data extracted from Sage Line 50 database on a windows server and posting off data updates to a web-based server. It is however a vital part of the system, and needs to be running 24/7. For years this has been handled by an assortment of dusty old desktop PC's, the last of which was starting to show signs of needing to be retired. The next old PC in line turned out to have a faulty network port, so I though it might be time to buy some kit specifically for this job. It occured to me that due to the low power requirements, a Rasberry Pi might be ideal.

Being fairly new to the Rasberry Pi, I ordered a starter kit including a Model B Pi, a 4GB SD card with "noobs" installed on it, a case and a power supply. Connected up to the TV at home via an HDMI cable, it booted into noobs, from where I installed raspbian linux. Once raspbian was installed, I found that I could install everything I needed (mysql, PHP5, apache) using apt much as I would on a regular debian-based linux box. One thing that I found I couldn't do (from googling rather than actually trying) was install dropbox, however this wasn't essential.

After installing it in the office and monitoring it for a few days, it seems to be coping extremely well. There is one particular php/mysql task that sends the cpu temporarily very high, but I think this can be tamed with a bit of code optimisation. I also wish I had started with a bigger SD card, as there is only just enough "disk" space when everything is installed.

One advantage of this low-power system is that I have it running from a USB cable plugged into another server rather then requiring another port on the office UPS. I've considered having it run from a rechargeable USB power pack, kind of like a mini dedicated UPS. I'll be interested to see how it stands up to this kind of use. Due to the low cost of this system, the plan is to have a spare Rasberry Pi or two and some spare cloned SD cards on standby.

One other thing to note is that to plug the Pi into a monitor you may need an HDMI to DVI cable, unless you have a monitor with an HDMI port, or some HDMI or RCA to VGA solution (that will be another blog post). I only needed to plug it into a monitor to congure the static IP address - after that I ssh'ed into it from another machine.

So why didn't I just use a virtual machine? A couple of reasons: firstly, one of the tasks for this machine is to monitor the windows server from which the sage line 50 data is sent. Secondly, the aforementioned windows server is the only machine on the network running 24/7 and already struggles a fair amount, and I didn't want to hog any more RAM or cpu with a virtual machine.

Update 2014-01-22: The Pi has been up and running for nearly a week with no issues, but i've been reading about SD card wear that can occur if the disc is being written to frequently, so I plan to move the MySql and log file storage to an external drive, otherwise I suspect the card might "wear out" fairly quickly.
Tags: raspberry-pi / linux /

Managing Google App Engine SDK versions on OSX with virtualenv

This post was written 9 months ago.
Wed, 13 Nov 2013
When I first started developing Django apps on Google App Engine (GAE), I downloaded the google app engine launcher. Installing this will install the latest version of the SDK, and will (if you let it) update your path to point to it. This is fine if you are always using the latest version and don't need to use an older version for some reason. If that is the case you will need to uninstall GAE launcher, find an old installer for the version you want and reinstall.

A more manageable way is to not use the GAE launcher, but to download the SDK manually, then set up a virtualenv to use a particular SDK. I also use virtualenvwrapper to help me switch easily between environments.

This is the pertinent bit - in the folder where your virtual envs are stored, add the path to the app engine sdk in bin/postactivate. Postactivate gets run when you activate the virualenv through virtualenvwrapper. E.g for something using version 1.8.8 you might have something like:-

#!/bin/bash
# This hook is run after this virtualenv is activated.
export PATH=/Users/rickhurst/gae-sdk/1.8.8/google_appengine:$PATH
Tags: osx / appengine / virtualenv /

Everyday Designer Article: Freelance to permanent and back again

This post was written 9 months ago.
Mon, 21 Oct 2013
screen grab of everyday designer article by Rick Hurst
Earlier this month I wrote an article for Everyday Designer talking about my experiences about my experience of working as both freelance and permanent web developer.
Tags: freelance / articles /

Django Talk interview

This post was written 10 months ago.
Fri, 18 Oct 2013
screengrab of Rick Hurst django talk interview
I was interviewed for django talk earlier this month, and here it is.
Tags: django / freelance /

Catching up - Summer 2013

This post was written 10 months ago.
Mon, 07 Oct 2013
Rocky the T25 being towed
Ok after a productive start to the blog this year, i've failed to post since the start of the summer. I'm going to blame it mostly on my camper van, both literally spending time working on it and camping in it with my family, and also generally all my blogging effort has gone into my "camper van things" blog. I've had a fantastic summer, worked less than I probably should have, but now getting back to work (and saving up for some more summer adventures next year!).

So to catch up - i'm now freelancing more or less full-time back at Potato, working on some interesting stuff, notably cutting my teeth with AngularJs framework, which i've enjoyed so far. My initial observations are that it is significantly different to Backbone/Marionette that there is no confusion over what technology i'm using, it allows you to be productive very quickly and that it litters your markup with non-standard attributes. The latter may upset purists, but otherwise it gets the thumbs up from me. If you're interested in learning AngularJs, a good place to start would be the tutorial.

Braindump June 2013

This post was written 1 year ago.
Mon, 01 Jul 2013
Where exactly did June 2013 go?