Wednesday, 6 November 2013

Preparing the Raspberry PI, pairing Pebble to Raspberry

Concerning OS, I didn't give much thought on the distribution I was going to use, Raspbian has been my OS of choice on Raspberry, had no reason to look elsewhere.
I installed the latest release of Raspbian, updated and upgrated (apt-get update, apt-get upgrade).
As media, I used a 8GB Sandisk Ultra SD and, in contrary to what I usually do, I didn't expand the root partition. Instead I created a new primary partition (3rd) and mounted it to /mnt/data using /etc/fstab.
The idea is to use this partition for writing the logs. I know that this, because of the wear-leveling mechanism integrated in the SD , doesn't increase the life of the SD but nevertheless it makes the system more manageable and easier to backup.
After that, I set the IP address of the ethernet interface to a static one, valid for my internal network.
I also set the system hostname to "akka-pc", Akka being the name of my boat.
The next thing I had to do was to make sure the SD has as a small wear as possible.
The SDs fail after a finite number of write cycles, and in order to ensure longevity, these write cycles have to be kept as few as possible (actually, everything will eventually fail, the thing with SDs is that if you don't pay attention, the card might stop working after only a few months).

The obvious first step was to disable SWAP. 
This was accomplished using the following commands:
sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove


Then, I relocated the /var/log & /var/run to ram-based tmpfs filesystems and set the SD-based filesystems not to log access times, further eliminating unneeded writes and the same time increasing speed of operation.
All this were done using the following /etc/fstab file:
root@akka-pc:~# cat /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults,noatime  0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
/dev/mmcblk0p4  /mnt/data       ext4    defaults,noatime  0       1
none        /var/run        tmpfs   size=1M,noatime    0    0
none        /var/log        tmpfs   size=1M,noatime    0    0

Since I intend to write most of the Raspbery software in Python, I went and installed the Adafruit WebIde.
Something to take a note, the Adafruit WebIDE won't run after reboot on this setup! After a bit of checking, the reason for this was because /var/log is volatile storage and there was attempt to access a file a couple of levels further down the tree, and this folder was missing after review.
All was needed to be done was to change the location a the log file, as specified in  file:
/etc/redis/redis.conf

Next, I added a bluetooth and a WiFi USB dongle and set the Raspberry to act as a Access Point. Info on how to do that can be found in many places for example at one of my favorite sites, here!
I also set a few iptables rules and enabled IP forwarding so that the device that would get connected to the Raspberry via WiFi, will also have internet access, assuming of course that the Raspberry itself is connected to the net, something that in lab environment is done through the ethernet and on an production environment might get done using 3G connectivity.

After completing this step, I had the tablet able to connect to the Raspberry. That way, the Version 1 Primary UI software that will eventually be written, which will be running on a tablet, will have IP connectivity with the Raspberry.

Next was the Bluetooth connectivity. I had to "pair" the Pebble smartwatch to the Raspberry.
That proved to be little complicated.
Eventually, I managed to complete this task using the Linux libraries Lightblue and libpebble. These are not specifically written for Raspberry, nevertheless it wasn;t much trouble making them work with Raspberry. 
A note of attention, if anyones tries to do the same, look closely at the dependencies, in order to set the system to work properly I had to "apt-get install" a number of packages related to bluetooth.
Unfortunately, I was unable to pair the Pebble to the Raspberry sing command line. Instead I had to use the GUI utility blueman-manager (anyone knowing otherwise, please let me know!)

Just to remind anyone that might have forgotten, when you connect to a X capable *nix host from another host that supports X using ssh , if you connect using the -X flag, X-Window apps can be run on the remote terminal.

I took advantage of this in order to keep having me Raspberry setup headless (i.e. without locally connected monitor & keyboard) and still able to run the GUI program. Connecting to the Raspberry from my host using the ssh -X, I could run the GUI utility "blueman-manager" and have its window appear in my desktop:
(Yes, I am working on a Mac!)
As you can see. the Pebble is connected to the Raspberry and the Raspberry thinks Pebble is a pager!

After pairing, on the bluetooth menu the Pebble shows this info:
It show that has been paired to the Raspberry which is named akka-pc. The funny thing is Pebble thinks akka-pc is a phone but this doesn't have any negative effects to the communication link.

Using the libpebble library, we can "ping" the pebble from the Raspberry, as shown in this screenshot:
After issuing this command, the pebble displays:
Which means that we have communication with the Pebble watch!.


So, the UI front end devices, the tablet through use of WiFi and the Pebble through use of BluTooth have been connected to the Ship Computer. All is left is to connect the sensors and write the software (sounds easier than what it is...)

More on the next post!

Thanks for reading,

G.