blank sd card to sending email with ruby on the raspberry pi
Fri, Feb 1, 2013
In this tutorial I'll show you how to start with a completely blank SD card, a card reader and an internet connection and end up sending email from your Raspberry Pi (RPi) using Ruby and GMail. The instructions for burning the RPi image to disk are for OS X but there are a ton of tutorials for doing it on Windows. See the References section at the end of the tutorial for instructions on how to burn on Windows.
To prepare for this you'll need a way of getting your RPi onto the internet. You need to do this to install Ruby. I used this wonderful tiny wee wifi adapter from The Pi Hut and followed their tutorial for setting it up once I'd got the RPi powered up with the Wheezy operating system (OS). You need the latest Wheezy OS release as it contains the drivers for the wifi adapter. Of couse you can also just plug the RPi into your router with an ethernet cable but I like to have the freedom of wifi to wander around while cogitating on things RPi! The wifi adapter is the toty wee white thing on the right of the RPi in the photo:
But let's start at the very beginning and the first thing is to download the latest Raspbian Wheezy release from here. Unzip it and use dd to burn it to the SD card. This is quite faffy and has the potential to go seriously wrong if you're not careful. What you have to do is unmount the partition on the blank SD card and burn the image to the disk. To do this, before you plug in the card reader first see what disks you have. So enter this command from a terminal:
and you'll see something similar to this:
Now plug in the card reader with the card in it, do another df -h to see what the disk partition is:
From the above, the SD card is /dev/disk1s1. This is the partition so you need to get to the actual disk by first unmounting the partition:
sudo diskutil unmount /dev/disk1s1
and working out the full disk name, which involves removing the s1 and prefixing with r to get:
You can then burn the Wheezy image to the SD card:
sudo dd bs=1m if=2012-12-16-wheezy-raspbian.img of=/dev/rdisk1
It'll take a while and when it's done unmount the card reader:
sudo diskutil eject /dev/rdisk1
and put the card in the RPi and power it up.
If you ever need to backup an RPii OS just bung the SD card in the reader, plug it in and reverse the process:
sudo dd bs=1m if=/dev/rdisk1 of=~/dev/raspberrypi/images/backup/wheezy.img
bearing in mind /dev/rdisk1 will change depending on what you have mounted at the time.
Now you have a working RPi OS but it lacks storage. The SD card is 4Gb, let's say but the image you downloaded is only 2Gb so that means there is 2Gb of space on the card which you can't get to. So you need to resize the partitions. Gulp. Yes, sounds complicated but it's actually rather easy if you follow the steps. Put the card in the RPi and boot it up. Login as the pi user and get into root:
Now bring fdisk up:
and list the partition tables:
What you'll see is dependent on various things. I didn't see a Linux Swap partition, only W95 FAT32 and Linux but the process is the same. This is what I saw:
You first need to write down the Start of the Linux partition. In my case it's 122880. This is very important. Write it down now!
You then want to delete everything apart from the W95 FAT32 partition. So go into delete mode:
and choose 2, then delete mode again and choose 3, if you have a third partition. You should now have only one partition, called W95 FAT32.
Now go into new partition mode:
create a primary partition:
choose 2 for the partition number:
You'll then be asked for the start of the new partition. Enter the Start position you wrote down earlier:
Accept the default for the last sector and save the changes:
Once it's done it, reboot:
and when you log back in to the RPi, resize the new partition:
sudo resize2fs /dev/mmcblk0p2
and when it's finished reboot again. When you log back in to the RPi, check you have access to the missing 2Gb:
It looks scary but it was quite easy. There is another way which I didn't try, using GUI tools from the RPi desktop which you can read about here.
So that's the RPi setup with a full 4Gb or whatever size your SD card is. Now you can go ahead and install Ruby. I used rvm as it's much easier but before you do that, you need some packages:
That last one is important as GMail uses SSL. Once all that lot is installed you can go ahead and install rvm and Ruby at the same time:
curl -L https://get.rvm.io | bash -s stable --ruby
It'll take ages, an hour or so at least. When it's finished, logout of the RPi and log back in. This is to ensure your shell profile now has access to rvm and Ruby. There are other ways to do this but the easiest is to logout and log back in again.
Once you're back in the RPi there's a little bit of housekeeping to do. Before installing any gems I like to prevent the rdoc and ri gubbins being installed as there isn't much room to spare on the RPi. So in the file called:
gem: --no-ri --no-rdoc
Now you're ready to write some code! First create a new gemset:
rvm gemset create mailer
and select it:
rvm use 1.9.3@mailer
Now install the GMail gem:
gem install gmail
and put this code in a file called gmail.rb:
Change USER, PASSWORD and email@example.com to values that suit your GMail setup. If you don't have a GMail account just create one specifically for your RPi to use.
Now run the program:
and in the best Unix tradition nothing should appear! Unix programs tend not to output anything if everything went right. It's only if they become noisy that you start to worry. So if nothing appears it means you should check your email for a message from your RPi!
You may well see this error though:
uninitialized constant Net::IMAP::VERIFY_NONE
which means rvm hasn't installed Ruby with SSL support. If that happens you'lll need to reinstall Ruby but this time showing rvm where the OpenSSL libraries are located on the RPi:
rvm reinstall 1.9.3 --with-openssl-dir=/usr
Running ruby gmail.rb again should work this time.
So you should now have a Raspberry Pi connected to the internet and sending emails from its very own GMail account. Some people like to get their RPi to email them every time it boots. Perhaps you want to hook a webcam to your RPi and email an image once a minute. The possibilities are endless.