My Adventures with Raspberry Pi - Setting up the Environment

It's been long overdue but I finally took the plunge and purchased a few Model A Raspberry Pis. While I'm excited to get into it my past experience with Linux has been limited and painful. True, I did manage to get it running and connected to my network all those years ago after several hours of frustrating research. However, my victory was short lived when my kernel suddenly became corrupted and I ran back to Windows with my tail between my legs. I've made a couple of attempts since then to get back into it with similar outcomes. This time around I feel a bit more confident. I'm older, wiser and it only takes about 30 minutes to image an 8 gigabyte SD card before I try something new. That being said, I did have some difficulties getting my wireless internet connected, more on that later.

As a developer, my primary interest in the Linux platform is the plethora of languages and frameworks that are available for use. Sure, many of these can be configured to run on Windows but I find the temptation to give up and get back to writing .Net is just too much to resist. Yes I'm aware that Mono can be run on Linux but it's like moving to a new city. It's easier to avoid going back to all your typical hangouts. I did a bit of deliberating on which language I'd like to get into. I've dabbled in PHP some but it keeps conjuring up all these painful memories of ASP Classic. The new kid on the block Node.JS seems to be gaining traction but I already write enough javascript as it is. I want something completely different.

Python

It's got a solid community, a good selection of libraries and it's old enough that it's short comings are fairly well understood. Significant whitespace is something I'll have to get used to but given that I started out in QBasic and VB it shouldn't take that long for me to get used to it again. Plus, Reddit runs on it so it's gotta be good right? Right.

Getting the Pi Up and Running

But before I could get into any of that I needed to get the thing running. Here's what I purchased:

I've had no trouble using the Edimax Wifi dongle without USB power. However I did use a powered USB hub while I was getting everything set up so I could hook up a keyboard. I've heard there can be compatibility issues with USB hubs so for reference this is the one I used.

I felt like I was familiar enough with Linux to avoid the NOOBS install so I decided to start with the Raspbian image available for download here. As I stated earlier I'm a Windows guy so I also downloaded the utility recommended for writing the image to the SD card called Win32DiskImager. In case you haven't realized, you will need a SD card reader/writer. Though I haven't tested it, you may be able to use a camera if you're desperate but I'd recommend just buying a USB SD card reader as they only run for about ~$15.

Using the utility was fairly intuitive, select the file, select the SD card drive and go. This part all went pretty smoothly for me and I was sitting at the login screen in almost no time. At the time of writing the default credentials are pi/raspberry On first boot a utility will load that will allow you to resize your partition to fill the rest of the SD card. If you accidentally forget this step you can always run the command sudo raspi-config from the command line to load it up again.

Connecting the Wifi

Prior to switching to the Edimax wifi dongle I initially tried a TP-Link TL-WN725N. The driver wasn't already available but I did manage to find it after some extensive Googling. However once I got the driver installed it kept hanging when I would try to connect to the network. After a bit of trying this and that I finally decided it must be an issue with the driver and I should get the Edimax dongle since that's what everyone was recommending. I was pretty frustrated when after waiting for the dongle to arrive and plugging it in I experienced the exact same issue. Eventually I ran across an article that recommended I not use wpa-supplicant and after following it's advice everything worked beautifully. Here's the configuration settings I used.

/etc/network/interfaces


auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "MySSID"
wp-psk "MyPassword"

To edit this file all you need to do is run the command sudo nano /etc/network/interfaces. From there I had to reboot the pi using sudo shutdown -r now and after a couple DCHP requests it grabbed an IP address. Once you've pinged a couple websites to verify everything is working you should probably run some updates using the following commands.

sudo apt-get update
sudo apt-get upgrade

One caveat I've noticed is that the Pi doesn't seem to hang on to it's DCHP IP address even if it isn't shut off. While I'm aware DCHP IP addresses aren't guaranteed to be constant this strikes me as unusual since all my other devices such as XBox, Wii, Android and Windows haven't varied their dynamic IPs in years. Point being, if you need a constant IP change the settings to use a static IP configuration.

So now we're ready to start installing python related packages but I'd recommend you shut down the pi using sudo shutdown -h now and create an image using your SD card reader so you won't have to go through this whole process again if something goes wrong.

Installing the Applications

Like I've said, I'm not real familiar with what's hot and what's not in the world of Linux so my decision to use Werkzeug and Storm is mostly just based on some light research and recommendations by Stack Overflow. I may switch to something else later on but for now I just wanted to get something going. I'm not going to pretend I understand everything I'm doing here but I'm just showing the steps I took to get the results I was looking for. I expect down the road to realize there are better methods but I've always learned best by trial and error.

PIP and VirtualEnv

I've discovered that most packages relating to python should be installed using the pip application so let's get that installed first. We can do that by running the command apt-get install python-pip. Once that's complete we'll install an application called virtualenv which is useful for development environments in that it allows you to isolate projects from one another so you can have different sets of tools and their versions. This application is installed by using pip install virtualenv.

Now that we have the virualenv application installed we should select a directory. Once you're in the directory you've created go ahead and use the command virtualenv env to create your virtual environment in the env folder. It takes about a minute and once complete we can activate it using . env/bin/activate. If your directory location is now prepended with (env) that means we're ready to proceed.

Werkzeug

Werkzeug is what's known as a Web Server Gateway Interface or WSGI for short. Unlike ASP.Net MVC where there is a clear distinction between the framework and the webserver Werkzeug handles both sides for you. The examples I read made it look pretty easy and after a little research about python syntax I felt comfortable altering the tutorial to suit my needs. It's pretty easy to install in that it didn't have any lingering dependencies that I had to go looking for. A command of pip install werkzeug should work without issue.

Storm

While the project I have in mind isn't incredibly complex after all these years of using nHibernate I really can't touch dynamic SQL without wincing. After a little research I came across Storm. Storm is a Object Relational Mapper (ORM) for Python. Like Werkzeug I was mostly sold on how simple it looked. Installation had one minor hiccup in that it requires the python-dev package which can be installed using apt-get install python-dev. Once that's done you can install Storm using pip install storm. Remember to do this while virtualenv is activated.

MySQL

I chose MySQL primarily because I've had some experience with it in the past. I considered trying out PostgreSQL but I felt I'd be biting off more than I could chew. I'm already in strange territory so taking on a new SQL engine seemed a bit unwise at this point. However, Storm supports both engines so there's nothing preventing me from giving it a try down the road. MySQL can be installed using apt-get install mysql-server. The final two packages we need to grab are libmysqlclient-dev and mysql-python. They can be installed using apt-get install libmysqlclient-dev and pip install mysql-python respectively.

Conclusion

That wasn't so bad was it? Remember to create another image so you won't have to go through this process again if something goes wrong. Also it's probably a good idea to install a version control system at this point. Personally I'll be using Subversion since it's what I'm familiar with. Poke around the tutorials for these applications and frameworks if you're unfamiliar with them or just wait until the next post where we'll be laying the foundation for our application. We're going to build a passport web service!