My first program

A while ago I write a blog post about my raspberry pi. About my setting it up and since then I’ve been using it as a streaming radio.

Well I got fed up of having to remote into the command line every time I wanted to play some music in the kitchen and started thinking about how I could automate it.

I thought about Bluetooth, but then I couldn’t work out how to trigger it when I wanted the music on, rather, I thought I could trigger it when the Bluetooth was present, but that meant buying a Bluetooth adapter and working out how to use Python to a) do the work and b) access the Bluetooth…

Back to the drawing board then, but I knew I wanted to automate it, in some way to save me having to dive into a command line every time.

So off my brain went and I spent a while talking to @philcooper about my thoughts and practicalities. He was very helpful, pointing out weaknesses in my plans and thoughts.. Forcing me to think things through like a programmer… Which I am most definitely not.

I am trying to learn Python, thanks to the guys at codecademy (link) for the online course, which I am (still) working through. It has thought me a lot about how the process works and how code should look as well as the actual words to use (apparently this is the big thing with coding..words and the things the words expect).

So in a moment of complete madness the other night I was checking out ifttt (link) to see if they had added any new channels for cool stuff I can link together and…

Yup, you guessed it. They do. They have added a cool ‘maker’ channel, allowing you to do cool stuff with a raspberry pi and the web.

“Oooh” thinks I…

Followed closely by…

“Phiiiiiiiil, how do I do a do this thingy on the pi?”

I’ll be honest, I got bored and lost what Phil was saying about the time he started talking about dynamic DNS and static IP addresses and sudo this, apt get that. Admittedly only *some* of it was complete gibberish.

This seemed massively complicated (for me) so it was back to the drawing board.

However, the germ of an idea had began to brew….

Maybe I could use ifttt to help… Maybe something with Dropbox… iftttt have a pretty interesting android app that I am also thinking about trying out called the ‘Do’ button. The ‘Do’ button can activate a single recipe (what ifttt calls it’s funky things) at the touch of a button.

Maybe that will do what I want…


I hatched a plan…

  1. Button on my phone gets ifttt to create a text file in a Dropbox folder,
  2. Pi checks for file.
  3. If the file exists then a specific activity happens.

Hmmm… A further discussion with Phil led to this growing in my mind….as something I could, maybe, eventually, cobble together.

After work I sat at the kitchen table and spent a couple of hours on the laptop in front of the command line.

Cue the hacker coding montage (link)

Eventually, after much swearing, I had a couple of bash scripts…like Windows batch files.. Okay… A clever way of running commands one at a time without typing them all in each time.

One that opens vlc and plays an Internet stream along with one that stops the stream… Now to set ifttt to create the required text files. Simple enough.

Then I hit a bit of a problem. There isn’t a Dropbox client for the raspberry pi. A slight problem that. But, it turns out that I’m not the only person who has come across this problem. Greater minds than mine have come across the raspberry pi/Dropbox conundrum and produced a clever little thing that can access Dropbox from the command line..

So, using that, it was time to hit Python.. (different hacker montage – Link – for Phil) With the help of Google I managed to cobble together the code (You can see a snippet below) – the pause for 2 seconds is because the script was checking for the file before the OS had actually updated to know the file was there…

And it worked. It ran the various scripts and downloaded the files and played the music.

Sort of.

Well it worked when I manually run the script. If the play file exists then the music plays, if the stop file exists then the music stops.

It’s a start. Now to fight with the the Linux scheduler. Okay, the scheduler works and the script is running as requested.

But, where is my music? Seriously, why is the music not playing?

Hmm.. More discussions with Phil might be required….but first, some googling and thinking… Can’t just go running to him every time I get stuck.

… …

12 hours later…

Turns out there is an issue with the scheduler and vlc. A bit more of the hacker montage and boom, music plays.

I can press the play button on my mobile and a couple of minutes later the music is playing. Hit the stop button and guess what? Yup, music stops.

Cue Matt feeling like a legendary hacker dude.boris

Seriously. My first program. And it works, it actually does what it was designed to do. Sure, when I show off tomorrow, Phil will no doubt have a much more simple/elaborate way of doing what I did, but the important thing is *I* did it.

From scratch.

I had an awesome time and I leaned a hell of a lot about how Linux works, how the pi works, and how I can code with Python.

Okay, so this post maybe isn’t a funny post, I know it’s not about games or tv… But I don’t care. I needed to share this experience with the world.

I’m already sitting here thing about what my next project can be…


Get in touch...