Kind Acts of Randomness

Arduino Controlled Raspberry PI Pandora Radio

We have a nicely running little Raspberry PI that we use as a Pandora radio at the Gold Coast TechSpace. It’s a great setup and people have enjoyed upping and downing tracks on the playlists to get a good mix of tunes that we all like. We run the pianobar application which gives a really simple command line interface that works beautifully with us.

I wanted to make it even better by having some big buttons that people could hit to like/dislike a track or just move on to the next (don’t want to ban it - just tired of it).

How could I do this simply? Well - I realised that by replacing the DFU bootloader you can turn any Arduino into a HID device to control keyboard and mouse (imagine the prank opportunities here people!)

Anyway - here is my MVP (Minimum Viable Product) that is now being used at the space and getting me early feedback.

I don’t have the big buttons yet, I’ve just hooked up some simple little push buttons to a breadboard and an Arduino (Freetronics Eleven). Once I get it more finished I might move it to a Leostick.

Here is my initial arduino sketch, I realise now there are better ways to handle multiple presses in hardware using interupt lines or capacitors, but for my initial release this seemed to work:

uint8_t buf[8] = {
  0 };     /* Keyboard report buffer */

#define PIN_LOVE 5
#define PIN_HATE 6
#define PIN_NEXT 7
#define LED 13

int love = 1;
int hate = 1;
int next = 1;
int state = 1;

void setup()
  pinMode(PIN_LOVE, INPUT);
  pinMode(PIN_HATE, INPUT);
  pinMode(PIN_NEXT, INPUT);
  // enable internal pull-ups
  digitalWrite(PIN_LOVE, 1);
  digitalWrite(PIN_HATE, 1);
  digitalWrite(PIN_NEXT, 1);


void loop()
  state = digitalRead(PIN_LOVE);
  if (state != 1 && love == 1) {
    buf[2] = 87;     // + key
    Serial.write(buf, 8);    // Send keypress
  love = state;

  state = digitalRead(PIN_HATE);
  if (state != 1 && hate == 1) {
    buf[2] = 86;     // - key
    Serial.write(buf, 8);    // Send keypress
  hate = state;

  state = digitalRead(PIN_NEXT);
  if (state != 1 && next == 1) {
    buf[2] = 17;     // n key
    Serial.write(buf, 8);    // Send keypress
  next = state;


void releaseKey()
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8);  // Release key  

Note: The numbers you see for the keypresses are not ASCII codes - you need to refer to the HID spec to get the correct codes for your keyboard.

Upload the sketch to the Arduino, the board will be doing nothing until you load the keyboard firmware. To do this you have to use the dfu_programmer utility that is in the standard ubuntu repos.

However for it to work, you need to reset the DFU so it can take commands, to find out all about this have a look at the DFU programming guide - the key section is “Reset the 8u2”.

This is what I did:

  • jumper the reset pin with the ground (near the USB connector)
  • Whilst the pins are jumpered, press and hold the reset pushbutton
  • remove the jumper, then release the reset pushbutton. Usually D13, will blink a few times quickly after releasing the jumper.

Now you should be able to run the dfu-programmer utility:

sudo dfu-programmer at90usb82 erase
sudo dfu-programmer at90usb82 flash Arduino-keyboard-0.3.hex
sudo dfu-programmer at90usb82 reset

When you need to put the old bootloader firmware on (ie. to load a new sketch) do this:

sudo dfu-programmer at90usb82 erase
sudo dfu-programmer at90usb82 flash Arduino-usbserial.hex
sudo dfu-programmer at90usb82 reset

Remember you will have to do the reset trick every time. There are quicker ways using dedicated programmers - but this worked for me.

Once the keyboard DFU bootloader is in place, when I hit the keys on my arduino it will now send the keys +, - and “n” to the Raspberry PI. Simple but effective.

My next step for version 0.2 is to put some proper coloured buttons on it, put it in a better case (probably an extension of my lego case) and tidy up the code a bit.

Lego Mindstorms on Ubuntu 12.04 Part 1

We’ve been doing some Lego workshops at Gold Coast TechSpace lately and with the older kids we’ve been spending a lot of time on Lego Mindstorms.

I quite like the Lego NXT software that comes with the kits for a simple introduction, it’s easy to teach and there are plenty of resources available to help.

However 2 things really irk me a bit about it:

  • It’s a bit limiting at times, I start the workshop talking about programming and what I and others do for a job - but when students ask me about how to do more advanced things I’m sometimes stumped on how to do it in the GUI - even sometimes quite simple things that would be ridiculously easy in REAL code.
  • It only runs on Windows (and Mac). I purged my machines of Windows a long time ago to take a free software path and it really pains me to have to dual boot perfectly good Linux machines to run the Lego software. I could run it in wine but that makes bluetooth connectivity a little difficult.

So I am embarking on a bit of discovery mission on how to work with Lego on a platform of my choice - GNU Linux, and for the sake of keeping things simple and accessible - Ubuntu 12.04 LTS (in my case Lubuntu - but it doesn’t matter for the purposes of this). I’ll blog my discoveries here as I go along.

There’s many alternative programming paradigms on Lego LXT, and as a Java programmer for many years you would think I might jump for something like LeJOS which runs a JVM on the NXT brick, but I did want to keep the NXT bricks with clean firmware so that they could be used with the stock software if necessary. I was a C programmer before Java and a lot of the kids seem to like Python so I feel myself searching for something different.

Your first NXC program

I’ll start by installing the NBC NXC compiler from Bricx Command Center. The project is a little old and hasn’t had an update in a while, but appears stable and well used. NBC is a simple byte code like language similar to assembly language, NXC stands for Not Exactly C and is a C like like language that we will use on our way to Python.

If you want to go to the exact version I downloaded - try here, just extract it to a directory somewhere and put the NXT/nbc executable in the path. You can also have a look at the source code. It’s a shame the code isn’t in a source repository - if someone reading this knows of the source of truth, it would be good to know.

Now that you have the compiler you can write little C programs that you can run on the NXT. Here is the classic Hello World example:

task main() {
    TextOut(0, 0, "HelloWorld!");

You can then compile this up using

nbc -O=HelloWorld HelloWorld.nxc

Note: If you are curious to see what the intermediate NBC code looks like you can also use the -nbc flag to write it to a file.

Now you need to copy your program to the NXT brick, but to do this you’ll first need to do a couple more steps.

Connecting your NXT Brick

To test your NXT connectivity I use the rather excellent Next Tools. Even if you don’t want to program the NXT like this Next Tools is still a lot of fun, you can see all sorts of information about your NXT brick and even test all the sensors and motors in a more flexible way than the “Try” built in feature on NXT. I usually go straight to the music section to test I can connect to my brick.

Running this tool would normally be a no-brainer, just plug it into the USB port and use it, and it is if you run as root via sudo, but to do it properly you should add udev rules to allow your user to access the USB port properly.

If you want a script that will do all of this, feel free to check out this thread, but I like to keep it simple and do the bare minimum of changes, so this is what I did:

First run “lsusb” to see what the id of the NXT is, you should see a line of text something like this:

Bus 002 Device 006: ID 0694:0002 Lego Group Mindstorms NXT

Now create a file in the /etc/udev/rules.d directory with rules like this:

# Lego NXT brick in normal mode
SUBSYSTEM=="usb", DRIVER=="usb", ATTRS{idVendor}=="0694", ATTRS{idProduct}=="0002", MODE="0660", OWNER="dalts"
# Lego NXT brick in firmware update mode (Atmel SAM-BA mode)
SUBSYSTEM=="usb", DRIVER=="usb", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="6124", MODE="0660", OWNER="dalts"

You don’t need the 2nd line - but it’s useful for firmware updates, replace “dalts” with your own user id. Some of the examples on the web create groups and add your user to the group but I like to keep things simple and just add my regular user.

Now restart udev

service udev restart

You should now be able to run Next Tools from your own user id!

Uploading your Program

Now you are ready to upload your program that you created at start. You have a few choices here. One is to use the file browser in Next Tools - there is an upload feature. Another is to use the nxt_push command that is found in the python-nxt package. To run it just install using

sudo apt-get install python-nxt

then upload your HelloWorld program

nxt_push HelloWorld

You can then run the program using the buttons on the NXT

Another final option is to use the the built in download feature of nbc, you can do something like this when you compile:

nbc -d -S=usb HelloWorld.nxc

This will automatically download a successful build to the NXT brick, there is also a -r option to download and run.

In the next section, we’ll learn how to write NXT programs in Python - see you then!

How to Build a Startup - Steve Blank

Our first lecture in the Steve Blank Lean Startups Course is this Wednesday at 8am. Please be on time and be prepared for an awesome course. Please RSVP

I’ve watched ahead on the first lecture to get a feel for it - and it looks excellent.

Would be great if all those attending watch Lecture 0 in their own time. You can get to it via the Classroom page, the video links are on the right-hand-side. It’s very short and just gives you an intro to what the course is about.

I will print off some Business Model templates in A3 in preparation for our first exercise. I also have plenty of post-its.

Following the first lecture I’ll probably set up a separate google group so we can all discuss the course without disturbing the others on this mailing list. I have it all setup on the big TV and we’ll probably just sit informally on the couches to watch.

If anyone has any ideas for startups that they would like to form a team on, please bring along to the meeting to put forward. It’s ok for people to not do startups and just watch the lectures, and also to work on your own solo - but Steve advises starting a team to get the most of the course, so if there are enough people who want this, it would be good to have some ideas to kick around.

Lego Workshops Are Back

I am running the Lego workshops again at Gold Coast TechSpace during the school holidays. They were a huge success last time and I am following through on my promise to Parents to continue with them.

There’s a more details post on the Gold Coast TechSpace Blog

So, if you’re a Gold Coast parent with a Lego obsessed child like mine - send them my way!

I’ve now introduced an additional “Machines” class for younger kids that doesn’t involve the programming or mindstorms, but does teach them a thing or 2 about Physics and Mechanics in a fun way. More advanced kids can even hook their creations up to the electric motors if they want.

You can RSVP for the workshops at the links in the blog post above or go straight to our meetup and scroll down to the Lego events.

Building Pianobar on (L)Ubuntu

I’ve been using Linux since slackware 0.9 and I’m quite used to compiling software and kernels from source, but in recent years it’s very rare that I have to do this anymore. I run Lubuntu on my desktop as I just like things things to be clean, simple and just work - I often sacrifice the latest and greatest for stability.

One of the tools I just can’t do without is pianobar which is a command line tool for listening to music on Pandora, which incidentally is now available in Australia without the use of proxies! However, Pandora don’t particularly like people using standalone clients as it cuts out their advertising so they change the protocol often which makes the version pianobar in the Ubuntu repositories quickly get out of date.

So I had to compile it from source. There’s a few dependencies and people who aren’t familiar with the various development packages might get stuck, so I thought I’d document what I did here for posterity.

From somewhere in your home directory, do the following:

sudo apt-get install git
git clone https://github.com/PromyLOPh/pianobar.git
cd pianobar
sudo apt-get install libao-dev libgcrypt11-dev libgnutls-dev libfaad-dev libmad0-dev libjson0-dev make pkg-config
sudo make install

To keep pianobar up-to-date, just run “git pull” in the pianobar directory, then make and sudo make install.

You can then run pianobar but just typing “pianobar”. You’ll be prompted for your pandora username and password and you can then just follow the simple menus.

If you want to avoid typing in user/password every time, create a file ~/.config/pianobar/config with something like the following in it:

user = [email protected]
password = cleartextpasswordthatidontuseanywhereelse

The password is stored in cleartext, so obviously don’t use a password that is used elsewhere

If you like Pandora and want to help them continue to run (without having to look at adverts) I’d encourage you to upgrade to Pandora One. It’s $36/year and gives you higher quality audio and fewer interuptions.

Now this blog post took me 10 times longer to write than install pianobar!, hopefully it’s useful to someone.

Gold Coast TechSpace Storage Funded!

I am very happy to announce that Gold Coast TechSpace managed to raise $1709 for our shelving and storage project. Our target was $1500 and we will use the extra to go towards storage boxes & lockers.

We purchased the first of the racking on Monday and have already installed a couple to get some stored items out of the way. Additional money will be used towards storage boxes and lockers and once everything is in place I will make sure I post plenty of pictures for you.

A big thankyou goes to all our pledgers, a boast board (see draft below) will be attached to the wall next to the shelves and all those that pledged over $50 are clearly mentioned. I have also included smaller pledges at the bottom in smaller text right at the bottom as every dollar is greatly appreciated. Those that pledged $100 or more will be getting their personalised trophy once our 3D printers are all back on line in a couple of weeks, so please bear with us there.

A special thankyou to those from other hackerspaces around Australia that helped us, I am really proud of the way that the hackerspace community has come together recently and it is a great community. Please come and visit us at Gold Coast Techspace - we hope to have a Hackerspace passport stamp soon too.

Despite a few issues with the UI and in particular the payments system I would also like to thank Andy and Bryan at iPledg for giving us the platform to raise these funds. The project was a big learning opportunity for all of us. We were able to find quite a few bugs in the system and I learnt a thing or to about promotions - Andy was a great help here, calling me almost every week with words of encouragement and advice.

Now we are looking a bit tidier we are going to concentrate on getting our space up to full speed - so many things are happening now, so please drop in and see us. We are usually there Wednesday nights and Sunday afternoons for casual drop-in, but check out meetup site for full details of all our events.

Gold Coast TechSpace Storage

One of my big projects over the last year outside of my day job has been helping to setup the Gold Coast TechSpace. It’s been a really rocky ride; things like this are really difficult to setup in a place like the Gold Coast and it’s been a huge learning experience for myself and the founding committee.

However, it does feel like we are over the hump now with people starting to get what we are trying to achieve and a handful of loyal members paying a membership fee of $40/month ($20 for students) to help cover our rent. Some interested projects are on the go including Reprap, Raspberry PI, Open Source Lab and a whole range of Arduino based projects and we regularly run the Open Source Gold Coast meetups and Barcamps at the Space.

We meet regularly mostly on evenings and weekends, with a regular open evening on Wednesdays, educational sessions on Saturdays (including some for kids) and Sunday afternoon hangout - see our meetup group to see what’s happening and don’t forget to join the group to be kept informed of what is going on.

One thing we are really lacking is storage space for all of our gear, we need shelving to the ceiling (our unit is tall and a lot of vertical space is being wasted) but also storage boxes and lockers for members. As an aside I have also been hunting for a snack vending machine to sell snacks from and also Arduino and other electronics kits - if you know of any for sale I’m all ears!.

We couldn’t afford any of these things from our regular budget and this is why I started the GCTechSpace Storage project on the iPledg crowdfunding site. We are going really well and I thank those that have pledged from the bottom of my heart. We managed to raise over $800 of the $1500 target so far in quite a short time - those that have pledged over $50 will get their names on the boast board and over $100 will also receive a little 3D printed trophy.

However, the way that iPledg and these crowd funding sites works is this - if you don’t get your targetted amount by the expiry date you get zilch!, nothing!, $0. We have less than 2 weeks to go, so I’m basically writing to see if anyone out there reading this blog might be able to help us. I don’t mind how much, you can pledg as little as $1, it all adds to the target - I’d just really appreciate the help if you can spare a little for a great project. There is a little video of the space below if you want to see for yourself.

You can pledge right here, it’s a pretty simple and painless procedure, you just need a paypal account.


Noteboard OLPC Project at Griffith Uni

Last night I had the pleasure of attending the presentation of 4 Griffith University Gold Coast Students Yukito Tsunoda, Harold Haigh, Ben Cowley & Maryam Nemati. They have been working on an OLPC Activity “NoteBoard” as part of their Industry project.

The students developed the activity based on specs supplied by Sridhar at OLPC Australia. This approach differs from some of the other OLPC Activities in that it was  based on a real customer need rather than the meandering evolution that we sometimes see with some of these projects.

They did a great job and I was impressed to see how quickly they picked up Python and the Sugar environment and came up with a really nice little application that I believe kids will love.

There are some bugs, but I am sure these will be ironed out and some of the team have expressed an interest in continuing on with the application after it is given back to the OLPC community.

My only feedback for the team was that they had no automated testing, this is a definite weakness amongst a lot of the OLPC activities and I am hoping future projects will take this on board to improve quality but also create a legacy for the next set of developers that take on activities as they evolve.

I’d be interested to hear from other Sugar developer on any good techniques for automated testing - I presume unit tests can be just standard PyUnit, BDD with something like freshen, but not sure how you go about automated Functional testing of the UI part of a Sugar activity… is this just standard GTK automation?