Before we get started, you’ll need a few things:
- Raspberry Pi - Preferably the 512 MB version
- Micro SD card - at least 8 GB
- Peripherals for your first boot into the Pi (e.g. keyboard, monitor)
- Lots of free time
A Few Notes First
My motivation for this project is home automation. My Pi will be making use of z-wave to control some devices around my house. Specifically, I’ll be using the RaZberry project as a basis for my home automation. What’s nice about Razberry is that it provides JSON API for accessing your z-wave devices and has a somewhat decent web application as well. The downside is that development of this project is fairly slow for my liking and there are a few features lacking, namely authentication. My end goal is to be able to control my house with my Android phone and I’m not about to open up my home automation controller to the world without at least some basic authentication wrapped around it. Additionally, the Razberry web app is more complicated than what I really need; what I really want is just a responsive web application with basic functionality and a REST API that I can later hook a future Android application up to.
Secondly, you really do want a 512 MB Pi. You can get by with a 256 MB version, but you will run into complications installing MongoDB. You also want at least an 8 GB SD Card. If you install the basic Raspbian image for your Pi (which we’re going to do), Raspbian (with X) and MongoDB by itself are almost 4 GB. You may be able to scrape by with a 4 GB card if you remove non-essential packages or use a slimmed down version of Raspbian, but SD cards are practically free these days, so just get one with adequate space.
Setting Up Your Raspberry Pi
For the sake of a tutorial, I’ll assume this is entirely new for those reading. If you have built a Pi before, you could probably disregard this section. To get started, you’ll need to drop the latest Raspbian image onto your SD card. You can download Raspbian here.
For Windows and OSX, there are plenty of image writing tools out there, which I won’t be covering. Since I’m using Linux, to write the image to the SD card:
After that completes, attach all your peripherals to your Pi, insert your SD card and boot into Raspbian. On your first boot, you’ll be presented with the raspi-config utility. It’s a tool that will help us configure a few basic things about our Pi. You’ll want to do the following:
- Expand the file system
- Change your locale and timezone (not really necessary, but might as well)
- Memory split - change from 64 to 16. We won’t be running any intense graphics or even X, so we don’t need much video memory.
- Enable SSH
- Change your hostname - if you want
- Change your password - this is highly recommended.
After saving and rebooting, you can always get back to this utility with:
Once your Pi is back up, let’s log in. The default user is “pi” (without quotes) and the password is either the default “raspberry” or whatever password you chose earlier. Let’s update our system to the latest:
This next bit is optional, but we’ll be giving the Pi a static IP address. First, backup your network interfaces config file:
Now let’s edit the file:
Here’s a copy of my network interfaces file:
One thing to note: I’m using wireless on my Pi, which you don’t have to. If you’re on wired ethernet, your eth0 config section will look similar to my wlan0 section. Now reboot your pi, or bring down and up your network interface. Check that you did indeed receive your static IP address:
Whichever you setup, eth0 or wlan0, you should now have a static IP. At this point, we’re going to ditch all the peripherals and just SSH into our Pi (again, I’m using Linux).
At this point your Raspberry Pi is configured. Now for the fun part.
Installing NodeJS on the Pi is pretty straight forward. You can opt to install NodeJS from the apt repository with:
But we’re going to install the latest version. At the time of this article, that’s 0.11.4 (the version in apt is a bit behind). Here we go:
Now we’re going to download the latest NodeJS:
We have to add NodeJS to our path variable:
Add the following lines to the file prior to the ‘export’ command:
Log out from the Pi so the settings take effect:
We’re going to create a startup script for NodeJS. It’ll assume the user “pi”, use a “web-server.js” file in “/home/pi/application”, but you can change these to your needs. Create a nodejs.sh file in your current directory and add the following:
Make the script executable with chmod and copy it to /etc/init.d:
Now let’s create the NodeJS web server and get it up and running:
Add the following:
To start and stop NodeJS, you can use the following commands respectively:
You should now have your NodeJS server up and running on port 8080. Special thanks to Matthias Rüedlinger who put this together. You can read more in depth about his process on his blog (
In the directory of our application, which we had as /home/pi/application, we’ll want to create a file called package.json:
In the directory of your application, execute the following:
So this part is pretty terrible. It’s not very difficult, but it will take somewhere in the 5-10 hour range to complete. Once you start compiling or installing, I’d highly recommend you go outside and live your life.
First, let’s install the essentials to compile Mongo:
We’ll clone Mongo, and start compiling:
The last two steps are what take a tremendously long time. Don’t say I didn’t warn you. After that completes, here’s the last bit of it:
Thanks to Andy Felong who helped out with this. You can read his blog post on the subject here.
You’re probably asking yourself, where the hell is the ‘A’? I asked for a MEAN Pi, not a MEN Pi (insert a joke of your choice here). My next entry will include the actual development stack, wiring up NodeJS with MongoDB, data binding with Angular, creating a NodeJS REST API, etc. Stay tuned!