Holy $hit RDS

So my life has been changed. Here’s why:

I was choosing platforms partially because of my database structure. I’d go with PHP if I had a sql database, or nodeJS if I could use Mongo.

No more. Just use Amazon RDS for your sql tables and it makes you platform agnosting. I can easily use it with nodejs instance running on heroku with almost no performance degredation!

I’ll write more later but for now do this:

  1. Get mysql native client here.
  2. Follow these instructions to get your RDS instance up and running
  3. Add some command line tools for rds, follow an example here.
  4. Set up your heroku app according to this.
  5. Export your sql database as a sql file, unzipped and place it directly in your mysql query window. Connect and run!

Boom, you have an RDS instance that ready to connect with heroku. Platform agnostic with the added benefit of being scalable on both ends.

I worked with it for 9 hours, and finally got it to work with the following command:

 

Raspberry Pi – The 5 Minute Setup

I’ll just dive in. Note: this is a mac only tutorial.

SD Card

You need to flash an sd card with the latest distro of raspbian. Don’t worry about it, just do it.

Download a copy of raspbian.

Unzip it on your desktop and do the following…

Open an terminal and type

df -h

Look at what you have. Then insert your sd card and do the same thing again.

df -h

Notice a difference? You should see something added like /dev/disk3s1 or something

Now unmount the sd card so you can write it.

sudo diskutil unmount /dev/disk3s1

Now look at the name, we’re gonna change it. disk3s1 becomes rdisk3 just drop the “s1” and add an “r”.

We need to copy it over so do this with your disk name and location of the download file:

sudo dd bs=1m if=~/Desktop/2013-02-09-wheezy-raspbian.img of=/dev/rdisk3

It’s not gonna tell you anything and it will take a long time. be patient. It will tell you when it’s done.

Setup

eject the sd card and pop it into your raspberry pi. Boot it up with a keyboard and hdmi cable to your monitor already plugged in. You should see s setup screen after it does some stuff.

Do the following setup:

  1. Expand root partition to fill SD card (just hit enter on this and it will expand)
  2. Set keyboard layout (I set it to logitech english US version generic)
  3. Enable ssh server
  4. Start desktop on boot? (I set this to no)
  5. Finish

Now your set up. It will ask you if you want to reboot. Say yes.

Install some stuff

log back in:

Username: pi

Password: raspberry

Now we need wifi.

sudo pico /etc/network/interfaces

Edit your interfaces file to look like this:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
        wpa-ssid "network name"
        wpa-psk "password"

Reboot your pi to it can log in and get wifi

Now we want to install some stuff we’ll use. But first:

sudo apt-get update
sudo apt-get upgrade

Your password is just “raspberry”

Next we want to install build-essential and git-core so we can check things out.

sudo apt-get install build-essential git-core nodejs npm

Next we want to make sure we can use node instead of typing nodejs everytime.

update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10

You can double check this is working with:

node -v

If you get a version number output then it worked. Then we want to make sure we can use the gpio pins. I use pi-gpio. To use this library without having to type “sudo”. To do this you need GPIO admin:

git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
make
sudo make install
sudo adduser $USER gpio

Next we install pi-gpio

sudo npm install -g pi-gpio

Now we want to manage our nodejs processes without worrying about them crashing. I user “forever” for this. NOTE: if you upgrade to node 0.10.5 there’s a known bug

sudo npm install -g forever

Let’s say you install your package and you have a node process like “web.js”. To start forever you might do:

forever start /PATH/TO/FILE/web.js

Then if you want to stop it:

forever list
forever stop 0

And boom. In about 5 minutes you can go from no pi to pi with a working install of NodeJS.

General stuff

You will also need to generate some ssh keys if you want to pull from github…

cd ~/.ssh
ssh-keygen -t rsa -C "my@email.com"
#enter
#enter
#enter

Then you will need to ssh into your pi. You can find it on your network by doing:

nmap 192.168.0.0/24

Then look for an interface that only has the ssh port open “22”

ssh pi@192.168.0.102

Enter your password “raspberry”.

If you really can’t find your pi’s ip by nmapping, then login into it with your keyboard and monitor and do:

ifconfig

This should tell you if your connected and what IP your on. Look for an IP that doesn’t have a .255 address.

Next we want to copy that ssh key we generated so we can pull down from github

less ~/.ssh/id_rsa.pub

copy the output and add it to your github repo ssh keys

Now you can pull from github.

If you want to reboot your pi:

sudo reboot

You should always shut your pi down, instead of just unplugging it. I just corrupted an sd card by unplugging it myself. Make sure you shutdown properly:

sudo shutdown -h now

Remember there really arent that many consequences. If you corrupt your card, just reflash it. and reinstall.

UPDATE:

If you would like to see how much memory is left on your pi:

df -h /dev/root

NodeJS on Raspberry Pi named NodeJS Instead of Node

So you followed the instructions and installed nodejs from the repositories on wheezy. The problem is, there’s already a package named node in the sources. So according to binary naming convention, nodeJS gets renamed to “nodejs”.

You can tell if you have this problem by simply typing

node -v

# then type

nodejs -v

If you get an error for the first and a version number for the second, your process is named “nodejs” instead of “node”. Which really isn’t a problem except that alot of scripts rely on “node” instead of the former. Packages like forever expect your process to be named node.

To fix this you simply need to link the nodejs process to node:

update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10

If you run this command under sudo, you should be able to enter

node -v

In the command line and get a version number back (as of writing this, the wheezy distro allows for 0.6.19).

Install the latest version of NodeJS

There are alot of theories out there on how to install the latest version of node. I prefer the following:

npm install n -g
n 0.10.5

Or whatever the latest version is…

Splitting Javascript Loops

There’s a programming trick, I was unaware of so I thought I’d share…

In Javascript for example, it’s single threaded. Which means you don’t want to do something like this:

for(var i =0; i < 1000000; i++) {
  // do lots of stuff
}

That’s because Javascript will try and complete the process all at once, meaning your page will hang while it’s trying to complete the process.

But you could do:

for(var i = 0; i < 1000; i++) {
  setTimeout(function(){
    for(var x = 0; x < 1000; i++) {
      // do spme stuff
    }
  }, 0)
}

This allows the browser to effectively “catch up” before it starts on the next batch. To make this into a function you could do

function part(array, callback) {
  setTimeout(function(){
    var i = array.shift();
    callback.call(i);

    if(array.length > 0) {  
      setTimeout(arguments.callee, 0);
    }
  }, 0);
}

This function from here, will set timeouts in order to process your array, but it’s the same concept. You pass an array and a function (your definition of what to do on each item in the array) as a variable to “part()” and it will process things according to when the browser has the next available tick

Pretty cool

NodeJS Dynamic Eventing

So here’s something…

I was trying to define things so I can key/value query later in nodeJS. I have an array of usernames. Then I try and create custom end points using nodeJS and express to customize a json return. If you pull down the code and run it you’ll see what I mean:


var FOLLOWS = new Array('GSPBetaGroup', 'FreedomRiders1');

var lastTweets = new Array();
lastTweets["FreedomRiders1"] = "this is a piece of content!";

for(var t = 0; t < FOLLOWS.length; t++) {
    
    app.get('/'+FOLLOWS[t]+'.json', function(request, response) {  
        
        console.log('loggin the function get last tweets')
        console.log(getLastTweets(x))
        console.log('nrt')

        console.log('loggin lastTweets')
        console.log(lastTweets)
        console.log('nrt')

        console.log('testing how to call out an associative arrays')
        console.log('lastTweets.FreedomRiders1 = ' + lastTweets.FreedomRiders1);
        console.log('lastTweets["FreedomRiders1"] = ' + lastTweets["FreedomRiders1"]);
        console.log('lastTweets['FreedomRiders1'] = ' + lastTweets['FreedomRiders1']);
        console.log('nrt')

        console.log('FOLLOWS = ' + FOLLOWS);
        console.log('x = ' + x);
        console.log('FOLLOWS.length = ' + FOLLOWS.length);
        console.log('FOLLOWS[t] = ' + FOLLOWS[x]);
        console.log('FOLLOWS["t"] = ' + FOLLOWS["t"]);
        console.log('FOLLOWS.t = ' + FOLLOWS.t);
        console.log('nrt')
        
        console.log('lastTweets[FOLLOWS[t]] = ' + lastTweets[FOLLOWS[t]]);
        console.log('nrt')
        
        for(var x = 0; x < FOLLOWS.length; x++) {
            console.log('loggin the function get last tweets')
            console.log(getLastTweets(x))
            console.log('nrt')

            console.log('loggin lastTweets')
            console.log(lastTweets)
            console.log('nrt')

            console.log('testing how to call out an associative arrays')
            console.log('lastTweets.FreedomRiders1 = ' + lastTweets.FreedomRiders1);
            console.log('lastTweets["FreedomRiders1"] = ' + lastTweets["FreedomRiders1"]);
            console.log('lastTweets['FreedomRiders1'] = ' + lastTweets['FreedomRiders1']);
            console.log('nrt')
            
            console.log('FOLLOWS = ' + FOLLOWS);
            console.log('x = ' + x);
            console.log('FOLLOWS.length = ' + FOLLOWS.length);
            console.log('FOLLOWS[x] = ' + FOLLOWS[x]);
            console.log('FOLLOWS["x"] = ' + FOLLOWS["x"]);
            console.log('FOLLOWS.x = ' + FOLLOWS.x);
            console.log('nrt')
            
            console.log('t = ' + t)
            console.log('x = ' + x)
            console.log('FOLLOWS = ' + FOLLOWS)
            console.log('FOLLOWS[t] = ' + FOLLOWS[t])
            console.log('FOLLOWS[x] = ' + FOLLOWS[x])
            console.log('nrt')
            
            console.log('lastTweets[FOLLOWS[x]] = ' + lastTweets[FOLLOWS[x]]);
            console.log('nrt')
        }
        
            
//        response.json({anObject: lastTweet});
//        lastTweets[name] = null;
    });
}

You can see that everything inside the second for loop is defined whereas everything outside is undefined. Why is this? I know it has something to do with t ending up being equal to the length of the array, but I don't understand why it would not loop through t = 0, t= 1 first?


			

Javascript Associative Arrays?

Ok, so first off, everything (I mean EVERYTHING) in Javascript is an object. So there are no such things as “Associative arrays”.

Let’s say you’re coming from PHP and you want to target something by it’s dynamically generated name. You might say:

$things = array();
$things["stuff"] = 123;
$things["otherthings"] = "more stuff";

print_r($things);

It’s fairly easy to callout the values of the variables, like:

echo $things['stuff'];
echo $things['otherthings'];

Let’s say I do the same thing in Javascript

var things = new Array();
things["stuff"] = 123;
things["otherthings"] = "more stuff";

console.dir(things);

I can still call out these things in the array like this

console.log(things['stuff']);
console.log(things["otherthings"]);

However I can also use dot syntax like

console.log(things.stuff)
console.log=(things.otherstuff)

But the object itself is not implicit so we can’t say something like

console.log(things[stuff])

That last one should error out

Ok that’s fine.



			

Free Up Some RAM Raspberry Pi

short example, let’s say your memory is low.

You can find out by typing

free -h

you can clear some of this up with the following

sync
echo 3 > /proc/sys/vm/drop_caches

That’s it, super simple

Out of Memory? Raspberry Pi

If you’re like me and ordered raspbian preinstalled on a 4gig card for model B pi, then you are also like me and have installed maybe two packages before you can’t run

sudo apt-get upgrade

You end up returning this error:

You don't have enough free space in /var/cache/apt/archives/.

So what do we do?

Well I just ordered another 16 gig card, I know, I could have bought a sd card, installed it for free and whatever… But I like to keep things easy.

In the mean time, if you need a couple extra megs, you can clean out your apt cache. It’s located here:

/var/cache/apt/archives

And if you try and get the disc usage of this folder you will see how much space your can clear out. Basically, apt just keeps cached copies of the packages in case you need to install them again, so most likely you’ll be ok if you remove them. To see how much storage you are using in this directory type:

cd /var/cache/apt/archives
du -sh .

I returned like 256 MB. So I cleared the cache:

sudo apt-get clean

And looked at how much memory I have available:

free -h

Before:

After:

Wifi on Your Raspberry Pi?

So you just bought your pi with wheezy and you’ve purched a usb dongle

First off, you should upgrade all your shit…always, unless you have crazy stuff that depends on other stuff being old and stuff…

sudo apt-get update
sudo apt-get upgrade

Next reboot your pi.

sudo reboot

Have a look around then alter:

sudo nano /etc/network/interfaces

If you’re finding it hard to type characters in the next section because your keyboard map is off, do this and reboot

sudo dpkg-reconfigure keyboard-configuration

To look like this (provided your have DHCP on your network, if you can’t figure it out look here.):

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

Next we’ll setup wifi and configuration (I’m getting this from this guy). Then we’ll install ssh so we don’t need that hefty HDMI cable anymore and you can remote into your pi. But first we need to get it working…

Insert the following into your supplicant conf file by typing

sudo pico /etc/wpa_supplicant/wpa_supplicant.conf

And insert the following

 

network={
ssid="SSID-GOES-HERE"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
psk="WIFI-PASSWORD-GOES-HERE"
}

If you need to know what’s on your network try this

sudo iwlist wlan0 scan

Make sure you’ve also removed anything pertaining to wlan0 in your 70-persistent-net.rules files

Once we’ve got our configuration correct shut down your system.

sudo shutdown -h now

Unplug your ethernet and turn your pi back on.

Log in to your system and type

iwconfig

You should see that you’re associated with your network. If you’re not and you have a realtek wifi dongle, look here. Basically you just need to install the firmware for your device.

You can also restart your network by issuing the following

sudo /etc/init.d/networking restart

 

UPDATE:

I had some problems getting the above to work properly. I followed this tutorial and it worked for me: http://kerneldriver.wordpress.com/2012/10/21/configuring-wpa2-using-wpa_supplicant-on-the-raspberry-pi/

Make sure if your wifi adapter isn’t coming up automatically, that you bring it up manually:

sudo ifup wlan0

I actually also changed everything to what “peppertarts” says here. So make sure you disregaurd the above if it doesn’t work for you basically all you need is:

sudo nano /etc/network/interfaces

and insert

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa.conf

at the end of the file, then

sudo nano /etc/wpa.conf

And finally

network={
ssid="NETWORK_SSID"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
psk="NETWORK_PASSWORD"
}

Then type

sudo ifup wlan0

and reboot after you make sure its running. Pretty simple after all that work. Either way, make sure your wifi works before you try to install ssh and run it

 

Installing SSH

So we don’t need to always have our pi plugged into an HDMI capable screen, we can ssh into our pi. This is useful especially when we want to remove peripherals like our mouse and keyboard to make room for other devices like wifi dongles etc. We’ll need to install ssh to allow us to log in remotely

sudo apt-get install ssh

Then start the daemon

sudo /etc/init.d/ssh start

Next we need it to run on startup

sudo update-rc.d ssh defaults

After rebooting you should see your PI’s IP address. This will tell you that its working.

Just to recap:

  1. Make sure your wifi adapter is working and automatically connecting to the network on startup
  2. Install ssh on your pi
  3. Find your pi on the network and have fun!