Blog future

No posts for a long time, but I have completed and put live the whole project :)

I plan to write more posts – and indeed have 9 draft posts waiting to be completed, but I have other things on at the moment. I fully intend to flesh these posts out and get them live at some point though.

Also for now I will be de-activating user sign-ups and commenting. The number of fake sign-ups and spam posts has reached silly levels now – I checked my emails this morning to find 16 posts waiting to be moderated…

Playing media on the Pi

The Raspberry Pi comes with a command line program, called OMXPlayer. I had a few issues with the version that came with the Pi, so I downloaded the latest build and then didn’t have those issues anymore. It works pretty well, and can play local files, or stream things from the internet with the minimum of fuss.

I have 2 tips though. Firstly is to add “-o local” to the command so that it plays through the headphone socket in the Pi – otherwise it tries to play the audio out of the HDMI port. So, for example, to play the latest Confessiond podcast from Radio 2, you’d simply type…

omxplayer -o local

Secondly is that to play many videos you need to purchase an MPEG-2 codec from Raspberry Pi – this is cheap and works a treat. The only problem with the video is that it plays it full-screen by default, and only responds to keyboard commands, meaning that it is fairly useless as it stands for playing videos on a touchscreen. I’ll stick to music for now and cross that bridge when I come to it.

So, the problem from here for my KitchenBot program is how to use OMXPlayer from Python. Fortunately the bulk of the solution has already been written in the form of TBOPlayer – a simple Python front end. Brilliant. I say “simple” – it’s still over 1,000 lines long…

With a bit of tinkering I managed to integrate it into my program. I tweaked its appearance and wrote some extra methods so I can add some/many files to it all in one go. So now I can head on and write new modules for KitchenBot to find media, which will send information to my modified TBOPlayer and play what it finds. :) I’m very pleased I don’t have to “re-invent the wheel” when it comes to playing media…

Google Calender Module Part 1/3

Next up, a google calendar read-only interface. I wanted it set up like a family calendar, with one column for each person, similar to this. Instead of 1 day taking up the length of the page (a lot of empty space), or a whole month (boxes too small), I instead thought it best to split it up into a week-10 days, and split each day into 3 parts – am, pm and evening (6pm – midnight). I think this best captures real life – it’s unlikely people do something that doesn’t fit neatly into one of those three time slots.

As I looked into accessing google via Python, I hit a stumbling block of sorts. The API Google provided only works in Python 2.x, not Python 3, as I have been coding in beforehand. So I was left with little option but to change all my code to Python 2. This wasn’t too tricky, thankfully, a few bracket removed here, capitalisation removed there and I was done.

Getting your project going requires you to register it with google. They will then give you the necessary keys etc needed to access their data, and so they can keep track of its usage.

They also give you code samples for connecting to the data source, which are great. I used it without much in the way of alterations. I admit I don’t fully understand the mechanisms by which the code accesses and authenticates with the Google calendars. However I am not concerned about the security aspect of this, for the following reasons…

1) the method I am using to connect is expressly read only, and for the calendar only, hence is not data I am overly concerned about, given you have to be sitting in front of the machine in order to access it.
2) To rephrase part of point 1 – it’s just the same as the security risk of having a calendar hanging up in your house somewhere.
3) I had to log on to my google account to say that the application could access my account, but that was a one-off thing – no credentials are stored in the source code.

Google have good documentation about the various objects you can retrieve from the calendars. In my next post I will detail how I retrieved and stored this detail.

BBC weather module

I’ve added a new screen on my module which gets the weather forecast from the BBC website.

There are a couple of “official” ways of doing this – using the RSS feeds they provide, for either a) latest observations, or b) a 3 day forecast. As an aside – the mobile app does something similar – just gives you one weather symbol for each of the next 5 days.

This isn’t much use for in a kitchen environment, where the questions likely to be asked are “if I hang the washing out, will it get wet?” Or, “do I need my coat to go shopping”, things like that, where more detailed information is provided. The BBC’s website does give hour-by-hour forecasts so I went in search of that.

To get the information off the website requires the use of a tool called a screenscraper. I’ve gone with Beautiful Soup.

Next problem comes when looking at the source HTML for the BBC weather website…. it doesn’t have all the information stored in it. However after examining the code, I found that the data gets drawn from another web page using some web 2.0 wizardry. The web pages for the actual data can be found here…

Substitute BBCWEATHERLOCATION with the number the BBC uses to identify your location. TO find this, go to the BBC weather website, search for your desired location, press enter and you will see the number in the URL. To see the forecasts for future dates, just change the number at the end to go forward more days from today. Strangely the date in the URL appears to have on effect – it always counts forward from today.

Then I had the question of the images. I could link to them remotely, but this would increase load time and increase the load on the BBC website. So I found all the weather images and saved copies of them on my machine to link to. These files can be found at the following URL’s…

At the end of the address, type 0.png, 1.png and so no up to 32.png to get the 33 images. Tedious but worth it in my opinion. Also, type -99.png on the end of the first one for the “unknown” symbol (only discovered this after much testing).

(EDIT – the 0.5.257 in the above links change over time! You may have to look at the HTML source of the BBC weather page to find the current number)

Next problem was that the images didn’t show. I found out this is because Python does not have native support for PNG files (though it does for some other file formats) so you have to download and install an extra module. PIL if you’re using Python 2 or pillow if using Python 3. I had amazing problems getting this to work in Python 3 on the Pi, getting error messages that no-one could interpret, and this was part of the reasoning behind moving back to Python 2 (see next post).

So, all my code needed to do was draw from those webpages using Beautiful Soup, and put them into a table using the tkinter graphical engine and access the stored PNG image files. This wasn’t too difficult, with the use of many labels in a frame. The slightly unpolished outcome of all this coding is this…

Basic Python GUI

After a fair bit of fiddling about, googling and a question on StackOverflow, I’ve finally managed to come up with a basic GUI. Here is it in all its functional ugly glory…


The top two buttons execute scripts which do what they say on the tin. The “Main menu” button calls a procedure that removes all frames and dumps you back at the main menu. The “Does Jesus love me?” button is just a test, and brings up a box that says “yes”. The iPlayer button deletes everything except the top bar, and replaces it with a label that says “iPlayer menu”. In the future I can change that to do whatever I want.

It’s not much but it’s a foundation to build on.

Here’s the Python code if you’re interested….

Created on 24 Jul 2013

@author: Benjamin
#import tkinter
#import tkinter.frame
from tkinter import *
import tkinter.messagebox

# Firstly, the functions that actually do stuff other than GUI things
def runscript(scriptname):
    from subprocess import call
    if __name__ == "__main__":

def helloCallBack():
    tkinter.messagebox.showinfo( "Answer", "Yes")

def newmenu():
    tkinter.messagebox.showinfo( "New menu", "Doom and destruction!")

# Now define all the menus/screens

def displayiplayerhome():
    global iplayerhome
    #if iplayerhome in globals():
    iplayerhome = Frame(top)
    TitleBox2 = Label(iplayerhome, text="iPlayer menu")
    TitleBox2.grid(row=0, column=0)

def definebigwindow(top):
    top.minsize(1200, 300)

def displaymainmenu(top):
    #global mainmenu
    #mainmenu = Frame(top)
    TitleBox = Label(mainmenu, text="Welcome to KitchenBot!", font=("Comic Sans", 18))
    B = Button(mainmenu, text ="Does Jesus love me?", command = helloCallBack, font=("Helvetica", 30), width=25)
    #C = Button(mainmenu, text ="If I click this button, will it say Yes?", command = helloCallBack, font=("Helvetica", 30), width=30)
    #N = Button(mainmenu, text="Clear", command= newmenu, font=("Helvetica", 30), width=30)
    #sdb = Button(mainmenu, text="Shutdown", command= runscript(""), font=("Helvetica", 30), width=30)
    ipb = Button(mainmenu, text="iPlayer", command= displayiplayerhome, font=("Helvetica", 30), width=25)
    TitleBox.grid(row=0, column=0, columnspan=2)

def blah():
def displaytopbar(top):
    global topbar
    topbar = Frame(top)
    sdb = Button(topbar, text="Shutdown", command=lambda script="": runscript(script), font=("Helvetica", 20), width=18)
    rsb = Button(topbar, text="Reboot", command=lambda script="": runscript(script), font=("Helvetica", 20), width=18)
    mmb = Button(topbar, text="Main Menu", command= blah, font=("Helvetica", 20), width=18)
    sdb.grid(row=0, column=0)
    rsb.grid(row=0, column=1)
    mmb.grid(row=0, column=2)

# Main program starts here

top = Tk()

#iplayerhome = Frame(top)
#mainmenu = Frame(top)
#topbar = Frame(top)

mainmenu = Frame(top)


Starting off with Python

First off, I am not a beginner programmer, more an intermediate having experience in quite a few different languages. I want to develop a front end. It doesn’t have to be pretty – it will just be my family using it – it just needs to be functional and useful for a 10 inch touchscreen.

What language should I use? I decided on Python, which I have heard lots of good things about but have no experience of myself. Also, it’s free, comes bundled with the Pi and works on Windows as well. As I looked into it, two things in particular struck me about it. First it the amount of add-on modules out there. It seems like whatever you want to use Python for, there’s an add-on for you. Secondly is “enforced indentation”. What I mean by that is often in coding, there is a section of code you want to run in a certain circumstance, this code is usually but in brackets or with words at the start of the end – something to mark it as separate. Also, it is advised to indent this code chunk – ie have it all a set number of characters – in order to make it more readable. Python mixes these approaches by saying you don’t need anything to mark the start and end of this code – BUT you must indent it, that is how the code is marked as separate. So you end up with less code or confusing brackets, and code which is, by default, more readable. What a great idea.

Right, on to the coding. First up, downloading the actual Python files easy enough, though I was offered the decision between version 2 and 3. Fair play to the developers, there was a page explaining the differences to help you decide which one to use. In the end I decided on 3. It’s the future of the language, and future-ready code is good code.

Next up, what program shall I use to do my development? Eclipse is the IDE (Integrated Development Environment) of choice for many people. Figure I may as well learn to use it. An addon to Python called PyDev is needed to develop Python in Eclispe – its website has good instructions on installation.

Now, how shall I go about learning Python? This page gives some steadily-increasing-in-difficulty examples of code. Very nice to get a handle on the language if you are already have some programming experience. Also, Python tutorial looks like a decent tutorial. Finally, I want a graphical front end, and Tkinter looks like being the tool of choice. I’ve found a decent starter tutorial here.

My initial target is to have a window with some links that run scripts (like my reboot and shutdown ones – help on achieving this is found here), and a button that takes you to a new screen, which itself can have links. These will be the main building blocks for me menu system, so once this is achieved I will go back to techie Pi things.

Final goal for my Pi decided!

My wife has agreed to go ahead with the kitchen Pi Plan! :) This is dependant on proving that it will work before buying the extra stuff, which is fair enough.

The plan is to create a Python-based interface for the Pi, that will give options to do many of the things listed in my previous post. I will try and create this interface and many of the things from that page to prove that it will work, before going ahead and buying the extra things that need buying.

It will be a simple menu-based interface. The screen I’m intending to buy is not a super fancy one, so it will have difficulty/impossible to interpret more than the equivalent of a single left-click on a mouse, so I will keep that in mind at all times.

Things that will need to be purchased…

  • 10 inch touchscreen (probably this one, which does verifiably work with the Pi, and has instructions available and there are people on the RPi forums who have used them successfully)
  • Speakers – will probably live on top of our cupboard
  • Something to mount the screen on (flexible arm?)
  • Housing/protection for the touchscreen

That’s for the future. For now though, it’s on to learning Python. I plan to develop my work on my main machine (which is obviously a lot faster than the Pi) then port it over to the Pi later on.

Desktop shortcuts for reboot and shutdown

If you want to reboot or shutdown your Pi – and this is highly recommended to avoid anything bad happening to your SD card – then there are options to do that in “start menu”. Fair enough. But I wondered, is it possible to create a desktop shortcut to do that straight away? I had 2 motivations here – firstly as a learning exercise, and secondly if I ever create a GUI for a touchscreen then I would want it to be easy to reboot or shutdown the machine.

It turns out it is possible but it took me a bit of digging around and initiative to figure out. It takes two files to do it. One is a “desktop launcher” – the file that lives on the desktop – and the second is the script that it will call.

For the desktop launcher, right click the background on the desktop and choose new text file. Right click it then paste the following into it…

[Desktop Entry]
Comment=Shutdown the machine

Click file, save as and call it something like shutdown.desktop. The important thing it the .desktop part of the file name, this tells Linux to treat it as a “desktop launcher” – a kind of shortcut. For a good introduction to desktop launchers I recommend this link.

Create another file in exactly the same manner but replace every instance of the word shutdown with reboot. Save this and call it reboot.desktop.

There are three things I want to point out from the above code. Firstly is that the name shown on the desktop is NOT the name of the file as you might imagine, but it’s what it in the Name attribute in the file (the third line). Secondly is the icon image that will appear is whatever is in the Icon attribute. As the file I reference doesn’t exist, there will be no icon. I’m sure you can find or download one to use.

Thirdly, notice the line in the file that says Exec=/home/pi/bin/ – that refers to the script that we are going to execute. So let’s go and create that script, which consists of the following two lines…

sudo shutdown -h now

While you’re there, create another file called consisting of the following two lines.

sudo reboot

Now you need to make these two files executable. The rights different people have to certain files is an important part of the whole unix landscape. If you do not explicitly say these files are executable, then you won’t be able to execute them. There’s the GUI and command-line way to make them executable. The GUI way is to right click on the file in File Manager, click properties, permissions, then tick the “make this executable” box and click OK. The non-GUI way is to type the following line in on the command prompt when you are in the correct directory…

chmod +x

Now hopefully, those desktop shortcuts will work. Double click and watch your desktop disappear :) Success here is probably the happiest I have ever been to see a computer turn off :)

Future plans/ideas

I’ve done a few things now, where do I go from here? It in part depends on what the end goal is. I can think of two things at the moment…
1) A kitchen-based touchscreen PC that can do a few useful media/family related things, such as display our collated Google calendars (a good family organizer), display recipes, and hopefully have some media functionality too – be that from the relatively simple (such as the DLNA renderer I have already set up) through to slideshows of family photos, internet radio and most ambitiously to receive TV/radio signals from our aerial via our MediaPortal PC. I’ll have to create a GUI front end too for all this. It will involve a lot of coding and learning.

2) A replacement for my AM/FM bedside alarm clock (10-15 years old). This will require attaching a screen of some sort, to show the time. It will be able to play internet radio and maybe give information like the weather forecast.

Option 1 will be expensive, but fun and I will learn a ton of things. Option 2 could work, but ideally I want a DAB radio there, so it will work even if the internet is down for some reason, plus I find streamed radio to be patchy sometimes.

We have recently had some unexpected cash but not much in the way of things we want. So Option 1 could be a fun and useful way to spend some of it. However it’s still a fair bit of funds and I will need my wife’s approval. This is because we make all major financial decisions between us. Money is a big cause for arguments in marriage so we avoid a lot of them by just being open and taking through purchases. We do each have our own small private supply of money but this doesn’t really cover the costs of the project.

So that’s the big picture. On a smaller scale, I have a few more things I would like to try and get working, regardless of the end goal of the project.

  • Upgrade GMRenderer
  • Create an off script; put on desktop
  • Stream internet radio? Via/not via Android phone?
  • Stream TV via borrowing MediaPortal steams? (now this will be tricky without using XBMC which I don’t want to use due to system constraints)
  • Photo slideshow (screensaver?)
  • Auto on/off at given times
  • Linked to that, alarm functionality
  • Learn some python

Potential kitchen applications…

  • Weather
  • Calendar (Google?)
  • Retrieve recipes (without using much memory)

Running the Pi for the first time

First a quick word on my setup. My monitor has both an HDMI and a DVI port. My main machine uses the DVI port whereas the Pi uses the HDMI port, and there is a button on the front of my monitor to switch the source. This is very handy, eg for asking the internet questions when things go wrong on the Pi, or if I want a break form Pi work I can have a quick game of Spider Solitare.

Right, so after Rasbpian is installed via NOOBS (see my previous post) the system reboots and you are greeted with a setup menu, that looks something like this.

Most of these options are straightforward enough but I will draw attention to a few of them. Or you can go to this wiki page for a more complete rundown of the options.

Enable SSH – this is so you can connect to the machine remotely if desired.
Password – do set one up!
Boot to desktop – make sure this is enabled if you are newbie like me. It’s a lot friendlier than being faced with a command line prompt, and also this is necessary if you want to be able to remotely connect to the graphical interface (ie – you cannot start the graphical interface remotely).

If you ever want to get back to this setup menu again, then you need to type sudo raspi-config into the command prompt.

Now click OK, and before too long you will be greeted by the following desktop screen. Congratulations, you’ve now set up your Raspberry Pi! :)

As this is your home of all things Pi, let me take you around a few things to get you started.

“Start Menu” – click in the bottom right and a menu pops up, similar to the Start Menu found in Windows versions from Windows 95 through to Windows 7. It’s nice to have something familiar around.

Now to take you through a few of the desktop shortcuts.

Wifi setup – this is a fairly simple program to set up your wifi. I found it easy to use and straightforward. It did take a long time to register properly on my network, but once it did it worked flawlessly, and remembered the settings upon reboot. Well done to the makers for having such an important part of the setup for many people easy to find and easy to use.

Midori – this is a lightweight web browser. Always a useful thing to have available. It does run pretty slowly though (remember the machine costs £30), so the majority of Pi-related browsing I do on my main machine. If there’s a lot of text I need to copy & paste though I will load it up on Midori.

Scratch – this is a simple sudo-programming language – the sort of thing the Pi was invented for. My intentions go beyond this so it’s not for me.

LX Terminal – this bring up the command prompt, a familiar concept to many of us who can remember the days of MS-DOS or who have had reason to use the command prompt in Windows. As this is linux and not a Microsoft system the majority of the commands will be different though. Most of the techie things I will be doing will be done via this program. So let’s go through a few simple commands to get us started.

cd is how to change directory (just like the good old DOS command). So type cd etc to go into the etc subfolder.

ls means “list” and simply displays the contents of the current directory.

sudo as a prefix to a command and means that command should be executed as if you are the su – the “super user” also known as “root”. This use has the power to do almost anything. So if, for example, you are changing files that are run at startup, only the super user can do this. I’ll be using sudo an awful lot from here on.

nano is the command-line text editor. Typing nano filename will open up the file called filename in the editor. To exit the file and save the changes, you will need to press shift and X, followed by Y and then enter.

shutdown & reboot – two basic commands that everyone needs to know. Shutdown when you want to pull the power cable out, and reboot when things go wrong or you want to check that “on boot” options you have set up really work. Regular users don’t have the permissions to do this, so what you will actually need to type is sudo shutdown or sudo reboot.

I think learning how to turn your machine off is a good way to end this lesson. :)