Monday, December 24, 2012

AllTheStars: A magical arduino-based christmas computer

In preparation for the holidays, I put together a cheesy karaoke machine for the Arduino.


The following is a quick description of how I went about things.

Parts list
- OSEPP UNO R3 Plus - 30$ - here
- 4x 20mA LED Red - 3$ - here
- 4x 20mA LED Green - 3$ - here
- 8x 330 Ohm resistors - 5$ - here 
- Assorted wires - 10$ - here
- Breadboard - 8$ - here
- Parllax 2x16 Serial LCD (Backlit) with Speaker - 35$ - here

I actually purchased this Arduino starter kit and supplemented it with the LCD. If you're starting from scratch, a kit like this with various resistors and wires is useful.

Layout
The layout (click to enlarge)

The 8 LEDs and 330 Ohm resistors were connected in sereis to the following pins on the device: 2,3,4,5,8,9,10,11 

The screen was connected to the serial transmission pin (1) on the device.

I'm using two bread boards in this pic for debugging purposes. I could have easily fit everything on to one.

Code
The code that controls the light show can be found on github here:
https://github.com/phyous/all-the-stars

There is a Note class that represents a single note being by the parallax speaker. There is also a light controller class that allows you to set arbitrary lights on or off. If you look at AllTheStars.ino, you'll see I wrote a wrapper function called  'play' that brings it all together by playing a note, setting arbitrary text and setting a light configuration. The song that is played in the video can be found in 
> void playOdeToJoy()

The hardest part was figuring out how to sync the audio with the lights and text (since there is no in built mechanism for doing this on the device). I solved this with an inelegant 25ms sleep between notes. I also don't have enough power to support all the lights and the LCD; this is why the screen brightness changes depending on how many LEDs are active. In theory, I should have powered the screen separately.

Thats it! Happy holidays everyone :)

Wednesday, December 19, 2012

Bootstrap your Scala skills


I recently decided to take a swing at learning Scala. The following summarizes why I found the language interesting enough to look at and some excellent learning resources.

Why scala?
  • Type inference. When the type for an element is obvious, the Scala compiler is able to automatically make the inference. No need for any additional type annotations. Less typing is great (pun intended).
  • Strong Static typing. As fun as dynamic typing can be, I'm sick of running into type mismatch related bugs in rails. Coupled with inference, you can catch the bugs early with the compiler and write less code.
  • Flexible approach to immutability. Mutable & immutable variants of all collections conveniently provided. Bootstrap your way to functional programming.
  • Higher order functions. Allows the definition of functions that take other functions as input. Makes for very concise and expressive code.
  • Multiple inheritance. Sort of. With traits.
  • Pattern matching. Case statements on steroids. See here for some neat example applications.
  • A JVM language. Can interoperate with Java libraries easily.
  • Increasing enterprise adoption. Twitter, LinkedIn and Foursquare(to name a few) make active use of Scala. It's been claimed to be an active ingredient in the secret sauce that helps them scale.
Learning Resources

Scalatron



I find the premise here brilliant. Scalatron teaches you Scala while you program the AI to compete in a virtual arena game. Play against the computer or other people's bots as you level up your Scala skills.

Coursera



If you prefer the classroom based approach to learning and you have the time to dedicate, coursera is always a great option. Learn about programming the functional way in Scala.

Scala School



Scala school started as a series of lectures at Twitter to prepare experienced engineers to be productive Scala programmers. Geared towards seasoned programmers, it does a great job of exposing the various aspects of the language through concrete examples. It's comprised of 13 lessons, start with the basics and you'll be building a distributed search engine by the end.

Learn in small bites



I ran across this blog and was impressed by the clear and concise nature of the examples provided. Each well-commented "bite" demonstrates a different facet of Scala.

References 


The Scala API


http://www.scala-lang.org/api/current/index.html#package
When in doubt, go to the source. Useful for understanding and discovering methods of common classes.

Effective Scala



The unofficial "best practices" guide for Scala. Definitely worth a look once you've gone over the basics.


Wish me luck as I learn! What has your experience with Scala been? What resources do you rely on for reference and learning?

Saturday, December 15, 2012

Raspberry Pi purchase & setup guide for developers

Purchase tips, assembly instructions and configuration for remote development



The following are the steps I took to get my raspberry pi set up for development. I like to keep wires to a minimum, so by the end of this you should be able to plug your rPi into an outlet somewhere and connect to it remotely via ssh.


1- Ordering parts

Lets start from the beginning. The following is a minimal list of parts you'll need.

A Raspberry Pi Model B - 35$ - here

Shipping times can vary wildly due to high demand. Mine took 4 months(!) to ship (although I think its down to a couple of weeks now). You can pay more to get it sooner from re-sellers on Amazon/eBay but this  goes against the principles of the raspberry pi foundation.

A micro-usb cable - 4$ - here

Used to power the device

A compatible flash card >2GB  - 5$ - here 

Stores the operating system

Edimax EW-7811Un wireless adapter - 10$ - here

Internet & local wan connection. 

A low amperage keyboard - 12$ - here 

Only needed for setup

HDMI cable - 5$ - here 

Only needed for setup

You probably have some of these parts lying around the house. Its important to check that parts are compatible beforehand; if they draw too much power, they will simply not work with the rPi. For a list of verified peripherals, see this wiki from elinux.org.


2- OS installation

So you received all the parts, now its time to put things together. This section outlines the process for geting an rPi image installed onto an sd card. I'm assuming you're running on a mac (OSX). If you're running linux or unix, steps should be pretty much similar. If you're a windows user, check out this guide.

  1. Stick the flash card into your computer
  2. Download the Raspbian “wheezy” image here and Unzip the image. This will produce a .img file: 
    • (From terminal)> unzip ~/Downloads/2012-10-28-wheezy-raspbian.zip
  3. Get the name of your sdcard by running dh command (Ex: /dev/disk3):
    • > df -h
  4. Unmount the sd card so that you will be allowed to overwrite the disk:
    • > sudo diskutil unmount /dev/disk3
  5. Install dcfldd
    • > brew install dcfldd
    • dcfldd is a fancy version of the older dd. You can use either tool(I tested both), but the flashing process takes ~30 minutes and dd does not print any status information. If you have trouble installing dcfldd, dd will suffice for the next step.
  6. Flash the image using dcfldd. This will take ~30 minutes.
    • > sudo dcfldd if=~/Downloads/2012-10-28-wheezy-raspbian.img of=/dev/disk3 bs=1m
    • if = Path to .img file
    • of = Path to sdcard disk
    • Setting 'of' ot the wrong path will do verry bad things to your computer. Be careful.
If you run into any problems, follow this more detailed guide which I abridged here. 

3- Device configuration

Once the flashing process is complete, its time to boot the rPi for the first time and configure things. In this section, we'll enlarge the sdcard partition to make use of all available space and modify boot behavior.

1- Take the sdcard out of your computer and plug it into the rPi. In the two USB slots, place the wireless adapter and the keyboard. Hook the hdmi cable up to a monitor/tv.When you plug the micro-usb cable into the device (from your computer), it will start to boot for the first time. You should see lights start to flash red and green.
2- Once the initial boot sequence is complete, you'll be able to enter raspi-config. This is a configuration utility that allows you to change the settings on the rPi. If prompted for a password, use user:pi, pass:raspberry (defaults for wheezy image).
See here for a full details about each of these options

3- Select "expand-rootfs" and follow the instructions here to enlarge your main partition to take up the entire sdcard. If you don't do this, you'll only have ~200MB of free space to play arround with (regardless of how big your sdcard is). This change will take effect on the next device boot.
4- Select "ssh" and enable the ssh server on the device. We'll need this active so we can later connect to the device remotely.
5- (optional) Select "boot_behaviour" and then select "command line". This will ensure that we always boot into the command line. You can skip this if you want to use the desktop.
6- Once back in the main menu, press the right arrow twice (to select "Finish") and hit eneter. When you get back to the command line, restart your device using "sudo halt" or unplug/re-plug your device.

4- Wi-fi setup
Ok, we're almost done. Its time to get the wifi working on the EW-7811Un wireless adapter. The OS image we installed already comes with the drivers for this device, so this saves us some headache. Follow these steps (borrowed from savagehomeautomation.com) to complete setup.

1- Check that the wireless adapter is hooked up properly and recognized by the operating system. Execute the following command:
>lsusb
You should see the following:

2- Next check that the driver is properly installed & loaded. Execute the following command
>lsmod
You should see the following:

3- Edit the network interface configuration file for the operating system. You can edit the file with the following command:
sudo nano /etc/network/interfaces
Modify the file to look like this:

4- Next, lets add the login information for your wireless network. Run the following:
> sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Edit the file to look like this (replace '__SSID__' with your wireless network ssid and '__PASSWORD__' with your password):

5- Restart your network interface:
> sudo ifup wlan0
6- Restart your device. Once you log back in, you should be able to see the wireless adapter connected to your network:
ifconfig wlan0
7- You can test things out by running:
> curl "www.google.com"
You should see a bunch of html and javascript pop up on your screen if you're connected to the internet.

5- Connect & start developing!
Now that you have a network connection, you shouldn't need the keyboard or hdmi. You can connect to the rPi through ssh and work remotely. Here I am on my laptop connecting to the rPi:


You can also send files to/from you rPi using scp:

Thats it! Hopefully this saves you some time & headaches.

Push notification + Terminal = Win!

Developing with Pushover

I was compiling ruby on my new Raspberry Pi (a process that takes about 2 hours) and I thought to myself, wouldn't it be great if I had some way of knowing when it was done? Wouldn't it be even more awesome if I could get info about any task in my terminal completing wherever I was?

I decided to give Pushover (https://pushover.net/) a spin during a late night hacking session. Pushover is essentially a (paid) app that allows you to receive push notifications on your phone through their simple API.

I was quickly able to hack together a bash/ruby function that I think will be pretty useful for me given how much time I spend in the terminal.

usage example: 
>sleep 2 ; notify "I slept for 2 seconds and woke up!"

The result (after ~2 seconds):

One could easily imagine kicking off a sereis of lengthy operations in the terminal, interspersing a couple of calls to notify, then going for a coffee until your phone says its all over. 
Ex: ./build_code.sh ; notify "Build complete" ; ./deploy_to_server ; notify "deploy complete, come back!" 

If this sounds interesting, follow the steps below to get setup:

1- First, you'll need to get the Pushover app for iOS or Android. It's not free, so you'l need to deal with that. Once you've download the app, create an account on your phone.

2- Go to their site(https://pushover.net/) and login. Once logged in, you'll see your user key in the dashboard. Note that down. 
3- You should have the option of creating a new application. Go through their application creation flow
4- Once you create the application, you'll get an application API token:
5- The last step is to add the following code to the end of your ~/.bashrc (or ~/.zshrc if you like to live on the edge). NOTE: replace application token with data from #4 and user key with data from #2.

Thats it! Now go kick off a build and wait for a pleasant ring to bring you back.