Jul 21, 2014

Make a copy of your SD card: Raspberry Pi drive recorder with GPS logger

With previous steps, we installed required hardware/software and finished basic configuration so we can now take photo, shoot video, output data to external touchscreen and fetch location data. Before writing some Python code to let those modules work together, I think we should take some time to make a copy of the disk we worked on. This way, even if you make a huge mistake in the near future and everything is messed up, you can install this saved disk image and start from where we are now. It's just like playing your favorite RPG. You don't wanna start from scratch, right?

Making a copy

Just like when we installed Raspbian OS to empty SD card, we use dd command to make a disk image. You insert your SD card to your iMac and hit `diskutil list` to see which disk we are going to use. Then execute `sudo dd if=/dev/disk1 of=Odyssey-Mk1_20140721.dmg`. You should change 'if(input file)' and 'of(output file)' value to suit your environment and preference. My 8GB SD card took 20-30 minutes to complete. It will be longer if you have SD card with larger space.


When you want to install the saved disk image, the process is almost the same as when we installed Raspbian to a fresh SD card. First you insert an SD card that you want to install this disk image to. Then unmount this just like the initial installation. Then `sudo dd if=Odyssey-Mk1_20140721.dmg of=/dev/disk1`. Again, you must set 'if ' and 'of' value to suit your environment.
It takes longer than making an OS image. In my case it took me more than 2 hours. You must be really patient.

My trick

If your SD card has larger space and you have chosen to 'Expand Filesystem' on the initial configuration, both steps take longer time to complete. So I make my 8GB SD card to be a master and make a copy of it. This way it doesn't have to copy each and every byte of 64GB. Then I install this to Micro SD card with larger volume. Right after this installation, this Micro SD card only utilizes 8GB of the entire space because I made a copy of 8GB SD card.
So I `sudo raspi-config` again and choose 'Expand Filesystem' to utilize all spaces left.

Jul 20, 2014

Setting up GPS module: Raspberry Pi drive recorder with GPS logger

Previously we attached PiTFT and finished touchscreen configuration. Now we are going to add GPS module to fetch location data.

For my project, I chose Adafruit's Ultimate GPS Breakout. I searched some GPS modules on the web and Adafruit's one have detailed official guide and many reference article by users. Spec. seemed good, too:

Freeing UART

2 ways of connection are introduced on the official guide:

The former seems easier and the guide recommends this way, but I chose the later one because I didn't want to use one of two USB ports. To do it some extra steps are required.

First you need to edit /boot/cmdline.txt

You need to remove those parts that include console=ttyAMA0 because we are going to use ttyAMA0 from now. I added rpitestmode=1 because other users' blog entries seem to include it.

Second you edit /etc/inittab

Then reboot and activate your change. This step is done.

Installing required packages

Before connecting GPS module, install required packages. It includes some interface that you can later use to interact with GPS data from python code.

Connecting GPS module

This time we use the cobbler cable I introduced in my previous article about PiTFT. It looks like the capture below.

As shown in the second capture, you should connect GPS module's VIN to Pi's 5v, GND to GND, RX to TXD and TX to RXD. Note that TX and RX are cross wired since module's input is Pi's output and vise versa.

Check and debug

You execute `sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock` and now gpsd is running. Check if you can properly read the receiving data by `cgps -s`. If it's working, the output is something like below.

In my case it didn't work at the first place and I had to go through debug process.
To add debug option to gpsd you add -D followed by a number that indicate debug level. e.g. `sudo gpsd /dev/ttyAMA0 -N -D3 -F /var/run/gpsd.sock`. I saw messages like below. It kept saying 'Satellite data no good (1 of 1).'

I couldn't really tell if the wiring connection has a problem or the GPS module is not receiving data. So I checked what's in /dev/ttyAMA0.

The GPS module was set by a window, soldering seemed O.K. and it looked like my GPS module was receiving some partial data. I couldn't really tell what to do so I posted to Adafruit's forum. As shown on the forum topic, they were kind and helped me well. Now my GPS module works perfect and I can receive consistent data.


I didn't know GPS module is so sensitive until I faced the problem. Now I have my external antenna attached. You can set this antenna on the roof of your car to receive better data. To do this you'll need:
Basically we installed all required packages and attached all modules we need. We are going to make a copy of OS before making any mistake and being forced to start all over again.

Jul 19, 2014

Setting up touchscreen: Raspberry Pi drive recorder with GPS logger

Since PiTFT itself is already setup in the last step, now I continue to setup touchscreen. This step is also documented on the official document so you should read though this first.

Basic setup

To start with, we make udev rule for the touchscreen.
Then remove and re-install touchscreen package. Then check if current setup went O.K. Now touchscreen should refer to eventN where N is number.


Before calibration process I installed some handy tools that were introduced on the document.


I installed mplayer for later preview use.
I downloaded video from and played it with the code below, which is exactly the same as documented.
Then I saw some scary message. I'll have to check this out later. I'm just going to add GPS module and see if it works.

Setting up PiTFT: Raspberry Pi drive recorder with GPS logger

Since we are done installing RaspiCam, now we are going to attach PiTFT and install required software. Before setting up PiTFT you should read through the complete document because it covers pretty much everything. What we need to do is to solder hardware and install some software packages.


When you open the PiTFT package you'll find extra 2x13 IDC socket for additional cobbler cable. PiFTF's female header covers Pi's all 26 GPIO header pins, but it actually uses 3 of them so it seems a good idea to connect cobbler cable to utilize the rest of 23 pins.
Cobbler breakout and cable package includes parts as shown below. You can stick the pins right to your breadboard so its really easy to add and test other modules like GPS and microphone amplifier module.

If you solder IDC socket, don't solder it on top of it. Do it on the reverse side.

And you make sure the white code, pin 1 indicator, goes right next to the female header.

When you solder female header and optional IDC socket, hardware part is done. Set the female header to your Pi's GPIO header pins.


Software part needs a bit more work than RaspiCam. You'll need to get 5 packages as described on the document:
  • http://adafruit-download.s3.amazonaws.com/libraspberrypi-bin-adafruit.deb
  • http://adafruit-download.s3.amazonaws.com/libraspberrypi-dev-adafruit.deb
  • http://adafruit-download.s3.amazonaws.com/libraspberrypi-doc-adafruit.deb
  • http://adafruit-download.s3.amazonaws.com/libraspberrypi0-adafruit.deb
  • http://adafruit-download.s3.amazonaws.com/raspberrypi-bootloader-adafruit-112613.deb
And then use dpkg to install them. The install log is shown below. You need to reboot your Pi to activate this change.


After rebooting your Pi, you need to let your Pi install screen driver. Commands are below. This modprobe command lets you load kernel modules. In this case we load spi-bcm2708 and fbtft_device.
If it goes O.K., the desktop shows up.

From next boot, we want Pi to load those kernel modules automatically so we add some modifications to /etc/modules. The modules listed in /etc/modules are loaded on boot so we don't have to repeat the previous step to load them.
And add new file: /etc/modprobe.d/adafruit.conf. rotate value indicate the rotation angle while frequency tells how fast it should drive the display. If the display acts weird document suggests you to drop this frequency value to 16000000.
Reboot and add some calibration setting.
Add some setting to .profile for later convinience.
Then run `startx` and desktop will show up.

Display STDOUT on PiTFT

If you wish to output standard output to PITFT on boot, make some change to /boot/cmdline.txt like below.

Then hit some commands and PiTFT will display standard output.
When you want to turn it off you simply write 0 to /sys/class/gpio/gpio252/value.

We are going to setup and calibrate touchscreen, now.

Shooting video with RaspiCam: Raspberry Pi drive recorder with GPS logger

In last article we connected RaspiCam and took photo. Now we are going to shoot video with it.

Shooting video is as easy as taking picture. Just execute `raspivid -o video.h264 -t 10000`. -o option decides the output file and if '-' is given, output goes to STDOUT. -t is time in millisecond(s). So the command above will give you a 10-second-video with a name of video.h264. To easily convert this to .mp4 you can install gpac and use MP4Box command.

Document for raspivid command can also be found at RaspiCam Documentation. Next we are going to attach PiTFT and finish basic configuration.

Setting up camera module: Raspberry Pi drive recorder with GPS logger

Currently we installed VNC server and client to emulate Raspberry Pi's screen on your PC. We are now going to take picture with RaspiCam and display the picture on the emulated screen.

Setting up Raspberry PI Camera Board is pretty easy. In the previous Raspbian update/upgrade process, all required software are installed so you only have to connect camera board properly, which takes less than one minute.

Connecting camera board

Camera board is as small as 25mm x 20mm. Just stick the end of the cable to your Raspberry Pi and all connection is done.

Taking a picture via CLI

Since we installed VNC server to Raspberry Pi and VNC client to iMac, we can emulate Pi's screen on iMac. So what I do here is to move to Pi's desktop with `cd ~/Desktop` and take a picture with `raspistill -o image.jpg`. Taken photo is saved as image.jpg on desktop and it appears on your VNC screen. Double click on image.jpg. You'll see your photo displayed.

Hitting `raspistill` without any option will show manual of raspistill command. Or you can find detailed document at at 'RaspiCam Documentation' and official document at raspberrypi.org.

Now we are going to shoot video.

Installing VNC server: Raspberry Pi drive recorder with GPS logger

With previous steps you installed Raspian, finished up initial configuration and update/upgraded Raspbian. So the minimum setup is all done now. Before connecting Raspicam to take picture and/or shoot video, I think we should now install VNC server and client. With this, you won't have to connect your Raspberry Pi to external display to see the screen output, but you can emulate the screen on your PC.

To display and interact with Raspberry Pi's screen on my iMac, I installed VNC server to my Raspberry Pi and VNC client to my iMac. This VNC client will emulate the screen on client PC so it's easier to debug camera module taking video and pictures.

Server on Raspberry Pi

All steps are covered in 'Take Control of Your Raspberry Pi Using Your Mac, PC, iPad or Phone' and are really straight forward so you should read this once. Basically what you need to do is just do `sudo apt-get install tightvncserver` and the server is ready. You only have to execute `tightvncserver` and type in password to run VNC server.

Client on iMac

Visit download page and install Chicken of the VNC. Then launch it. Default port is 5901, host IP is the one that you use for ssh connection and the password is the one you just set. You type them in and it's done. Remember not to check 'view only' or you won't be able to click on anything on the desktop on emulated display.

In next article we are going to connect RaspiCam and take a picture with it.

Update/Upgrade Raspbian: Raspberry Pi drive recorder with GPS logger

Since system configuration is done and SSH AcceptEnv problem is solved, I think it's a good time to update/upgrade Raspbian. It doesn't matter if you installed the latest version of Raspbian. Even if you do, you should still update/upgrade before going any further. Actually if you skip this step, camera module may not work. It's pretty easy as doing `sudo apt-get update` and `sudo apt-get upgrade`. Why not do it?

Now that basic configuration and update/upgrade is done, we can install VNC server/client to emulate Raspberry Pi's screen on your PC.

Fixing SSH locale problem: Raspberry Pi drive recorder with GPS logger

In my previous article I finished initial configuration with SSH login. This article helps to fix issues with language setting that occur during SSH login.

While I went through raspi-config in the previous step, I saw a lot of warning messages like below.
To fix this simply edit /etc/ssh/sshd_config and comment out "AcceptEnv LANG LC_*". My iMac is sending some language settings, but this modification will let my Raspberry Pi ignore it.

It's done.
Now we can update/upgrade Raspbian before going any further.

Initial SSH login and configuration: Raspberry Pi drive recorder with GPS logger

Installing Raspbian to SD card is done. In this article I cover how to boot Raspbian and finish initial configuration with SSH login.

After successfully installing OS to your SD card, now it's time to stick this to your Raspberry Pi and do the initial configuration. Connect LAN cable to Raspberry Pi, insert the SD card to it and connect power cable. Raspberry Pi starts up and several seconds later it can be accessed with ssh. IP address should be found with iNet or some other equivalent app/method. Initial username and password are set to pi and raspberry.


Do `sudo raspi-config` and start configuration. When you hit `sudo raspi-config` you will see a console like below.

1. Expand Filesystem

Usually you wanna expand filesystem. The OS distribution images are normally around the size of 2GB and when your SD card has unused portion, it will expand to fill the rest of the SD card so you can use more space. If you can't find any particular reason not to do this, you should do this. You must reboot before enabling this modification.

2. Change User Password

It changes password for pi user. As I wrote above, the initial password is set to raspberry. You should change this.

3. Enable Boot to Desktop/Scratch

I left it with default value.

4. Internationallization Options


en_GB.UTF-8 is sellected as default so I unchecked this and checked en_US.UTF-8. You hit space key to switch. Then hit Tab and choose OK.


Since I visit a lot of places including Japan, the U.S., Australia and other countries for road trip, it's kind of pointless to set a particular timezone. And my GPS module gives me UTC time so I chose 'None of the above > UTC', anyway.

Change keyboard layout

I used U.S. keyboard so I didn't have to do anything about it.

5. Enable Camera

Default is 'Disabled', but this project is going to use camera for sure so I enabled it.

6. Add to Rastrack

It has nothing to do with my project, but it's fun. You might wanna do it when you have internet connect now.

7. Overclock


8. Advanced Options

Some advanced settings. For this project, make sure SSH and SPI are enabled.

It's done. When you choose to finish, it asks you if it should reboot now. To activate changes you should let it reboot. After reboot, it's all activated. During this configuration process, you might have seen some warning messages like below.
If so, you can change some setting with the next article. If you didn't have trouble you can ignore the next one and go on to update/upgrade Raspbian.

Installing OS: Raspberry Pi drive recorder with GPS logger

On my previous article I described why I'm working this project and what this project goal is. This article covers how to install OS and get your Raspberry Pi ready to boot.

To install OS, you need to get an SD card or Micro SD card with adaptor. You are going to install your choice of OS image to it and stick this SD card to Raspberry Pi. Some starter kits offer Raspberry Pi with OS installed SD card. It's easy to get started with one of those, but as you work on your project you'll want to make a copy of your OS image before making a huge modification. You don't wanna mess up your entire work and start all over again, right? So sooner or later you'll have to know how to install OS on your SD card and make copies of it. I think it is nice to learn it at first place.

I did it with the items below:
I also recommend:

1. Download OS Image

Visit official download page and download the latest version of Raspbian. There are some other distributions listed on that page, but I think choosing Raspbian is the easiest for beginners since we can find many reference articles on the web. I downloaded the .zip archived file. We are going to unzip and install this OS image to the SD card.

2. Unmount SD Card

Insert your SD card to iMac and launch terminal. Type `df -h` and see what appears. When I did it SD card wasn't recognized so I launched disk utility and formatted this card. Then hit `df -h` and the file systems show up like below. Check which to unmount and do `sudo diskutil unmount /dev/********`. '********' should be replaced depending on your environment. You should remember what this device name was since you are going to use it later.

3. Flash disk image

After unmounting your SD card on the previous step, it won't appear on `df -h` again. You unzip your downloaded OS image and then flash this to the unmounted SD card with dd command. On dd command you must be careful what you give to 'of' because it is a bit different from what you remembered on the previous step. You need to omit the last 's1' and replace "disk" with "rdisk". e.g. '/dev/disk1s1' becomes '/dev/rdisk1'. This process may take a while and this doesn't give us visual feedback to indicate progress so you must be patient.

Installing OS is done now so you can eject SD card and stick it to Raspberry Pi. If you are going to bring your Raspberry Pi with you like I do, I really recommend you to buy a low-profile Micro SD adaptor for Raspberry Pi.

Now we are going to boot Raspberry pi, connect with SSH and finish initial configuration.

Introduction: Raspberry Pi drive recorder with GPS logger

Every year my wife and I go on a road trip with no detailed schedule or motel reservation; Just book our flight and rental car and the rest is up to us. Last time we started from L.A. airport, stayed one night at Las Vegas and won $15 at casino, stopped by some national parks, visited our college in Oklahoma, saw rodeo in Fort Worth and arrived at Dallas/Fort Worth airport.

We usually drive 2000 to 3000 miles in a week, which means we must spend most of the time in a mid-class rental car. We don't have time to pull over every time when my wife finds some fancy view. So, before the last trip in June, I came up with the idea of having a drive recorder record everything. If it works, we can concentrate on what we see and don't have to do something nonsense like looking into a tiny camera finder right in front of a great wilderness passing by.

It, however, didn't work well. I bought a cheap drive recorder with GPS module to start with, but it broke in 2 days. It was a shock. A tool that must have assisted my annual trip just became a piece of junk in a couple of days. This night at Anasazi Inn, my second idea came up: making a drive recorder myself.

After this trip I searched on the web and found out that Raspberry Pi and some handy peripherals can make cheap drive recorder with GPS logger. Raspberry Pi itself and peripherals are less expensive and easily obtainable so even when something breaks I can fix it myself, which I think is a great advantage after experiencing that terrible breakage.

I named my project Odyssey. I thought it would make perfect sense thinking about our unplanned annual trip. My favorite part is that Odyssey is attributed to Homer while my name is Oklahomer and people call me Homer. Coincidence? I don't think so.

This project includes features below:
  • Photo taking
  • Video/audio recording
  • GPS logging
  • Live preview
Later articles will cover my project. They should help beginners get used to Raspberry Pi since I am one, too. In next article we install Raspbian OS to SD card with Mac OSX.