Cocoa Programming

At a time everyone is learning Objective-C and Cocoa in order to program for the iPhone, I've spent most of my time programming for OSX. After doing some contributions to Sidestep during my stay at CMU, I decided to endeavor deeper and develop some apps of my own, just to scratch my back.

So during the holiday season I came up with two very simple applications:

The first app (MeuComando.app) a simple MS MediaRoom remote control app, that enables me to control my STB without reaching for the command. It follows a trivial protocol which can be easily found online (examples in java, python, etc). Thing is, I really didn't want a crappy Java app, so I reinvented the wheel and made myself a new app.

My last app (StatusPing.app) was a request by my father. His internet connection through a router supplied by his ISP keeps dropping and unless he periodically "debugs" by opening/trying to open a webpage or firing up Terminal.app and running ping, he never knows for sure if the connection is up or not. Therefore I developed an app that stays in the menu bar of OSX right there in the top, which provides the user with a visual alert if something is wrong.

You can find my apps at github.

Two months at CMU

In a time where Portugal is facing a severe recession, with several country men moving abroad to greener pastures link, I decided to endeavor myself on the pros and cons of living abroad even thought for a very brief period of time. Under the scope of the CMU-Portugal program, I spent July and August in Pittsburgh, Pennsylvania, USA. During this time I was a visiting professor at the ECE department at Carnegie Melon University and got the chance to be part of the research activities taking place at the Parallel Data Lab.

Although short, it was a very enriching experience. Living abroad you miss important things such as family but you also get to do realize your self professionally in a away you cannot in your home country (at least not at this time).

The economic crisis is driving all the skilled people abroad, family, friends, everyone is leaving for greener pastures in the hope of a future. I didn't want to make this post too negative, but the truth is that Portugal at this time and date, cannot offer much of the things you want for yourself and your family.

I deeply hope to be part of a solution for the problems we face, by doing my best at teaching at undergraduate and graduate level, shaping good engineers able to do anything in their life's. But several times I wonder, if I shouldn't be the one buying the ticket out...

Creating a KVM virtual machine using debootstrap

How to create a Debian virtual machine outside a virtualization environment.

The challenge here proposed is to create a virtual machine inside a remote server and boot it up using KVM. Sure I could have used VNC and go through all the setup steps of the Debian install disk running inside the virtual machine, but that would have taken ages and would be virtually impossible to automate.

Therefore the next steps detail how to install Debian using debootstrap on a qcow2 image disk and how to successfully boot the debian install using kvm (I'm assuming here your kvm environment is already properly setup).

First thing is to install debootstrap

apt-get install debootstrap

Next step, create an image file in which we will install Debian (example uses a 2Gb virtual disk) using qemu-tools (or kvm-tools depending on your distribution naming scheme)

kvm-img create -f qcow2 debian.qcow2 2G

Next lets load the nbd kernel module (you read it right, the Network Block Device), and associate the previous image to one of the nbd devices.

modprobe nbd max_part=16
kvm-nbd -c /dev/nbd0 debian.qcow2

You should now be able to partition your disk at will. In this example I'll use an initial swap partition of 512MB plus the root partition. To help all you copy&paste "guru's" here's how to do it using sfdisk:

sfdisk /dev/nbd0 -D -uM << EOF
,512,82
;
EOF

Next we need to format the newly created partitions and mount them.

mkswap /dev/nbd0p1 
mkfs.ext3 /dev/nbd0p2
mount /dev/nbd0p2 /mnt/

Next stop, install the Debian system using debootstrap, feel free to include extra packages and to use a different mirror.

debootstrap --include=less,locales-all,vim,sudo,openssh-server stable /mnt  http://ftp.us.debian.org/debian

Now go take a coffee or something… By the time you get back your Debian system should be almost installed.

Next step, configure your system to boot. We will need to chroot into the new system, but we will also require all the special filesystems in the chroot environment, therefore lets mount everything:

mount --bind /dev/ /mnt/dev
LANG=C chroot /mnt/ /bin/bash
mount -t proc none /proc
mount -t sysfs none /sys

Next lets install a kernel and grub (apt will try to guess how to install grub, unfortunately not successfully, just ignore it and don't install grub automatically)

apt-get install linux-image-amd64 grub
grub-install /dev/nbd0
update-grub

Last but not least, setup a root password

passwd root

Now lets unload the image, fix grub once again and be done with.

umount /proc/ /sys/ /dev/
exit
grub-install /dev/nbd0 --root-directory=/mnt --modules="biosdisk part_msdos"

Now edit /mnt/boot/grub/grub.cfg and find&replace nbd0p2 by sda2.

umount /mnt
kvm-nbd -d /dev/nbd0

The End.

You should now be able to boot the machine.

HINT: You should probably like to configure your network interfaces (/etc/network/interfaces) and /etc/fstab before booting. Just copy from your existing Debian system ;)

An Arduino shield to control your house

This post is long due since I've built this shield almost half a year ago and works great :)

Arduino IR & RF shield

So here is the story of how it all came about.

Having too many remote controls around (TV, STB, Home Cinema, PS3, PowerSwitches) I decided to get ride of them all and use only my laptop/smartphone. Using an Arduino I developed a shield that works as an universal IR remote control and as an RF remote control (315Mhz). So here are the instructions:

The schematic pretty much explains how everything should be connected:

circuit schematic

Having everything setup, we now need to program the Arduino. There were here two options: to control directly the Arduino using a network shield, or connect the Arduino to my already existing linux server. Well I opted for the second option since it saved me from buying a network shield and the server was already placed in a good position to target all the devices (this is important, all devices need to be in range of the Arduino shield).

The code is on Github

This code enables me to send through the serial port a string with the desired command encoded in the following way:

L00000000

L - Letter indicating the device protocol
            (S = Sony IR, Z = RC5 IR, R = Chacon RF)
00000000 - Hex encoded code

Example: codes for my SONY TV (attention there are several SONY protocols)

power: S00000a90
1: S00000010
2: S00000810
3: S00000410
4: S00000c10
5: S00000210
6: S00000a10
7: S00000610
8: S00000e10
9: S00000110
0: S00000010
source: S000002f0
AV: S00000a50
vol+: S00000490
P+: S00000090
vol-: S00000c90
P-: S00000890

The question now is: how to get these codes?

You will need to either built an IR receiver or get them from existing libraries such as LIRC, and surely read through Ken Shirriff's blog and Github project

As for the RF, my recommendation is that you buy an RF receiver and use the example code in fuzzillogic/433MHzForArduino - ShowReceivedCode

At this stage you should already be able to control all your devices directly through your serial port. Next step is now to network your Arduino.

I decided to connect my Arduino to my linux server (an Iomega iconnect ) since it had enough usb ports to connect an extra device and most importantly it was already connected to my home network. Next I installed ser2net through Debian apt-get util and configured /etc/ser2net.conf.

2000:telnet:600:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT banner

now it is just a matter of sending the proper command string through a TCP socket, which can be done even from the command line using netcat:

echo -e "S00000a90" | nc 192.168.1.1 2000

In the end, I'm able to connect any device using the RF transmitter and some very inexpensive switches from AKI in addition to a wonderful universal IR remote control.

Hope you enjoy your new remote :)