BeagleBone Black - iPhone as keyboard/mouse

Note: I wrote this a while ago specifically for my BeagleBone Black so some of the instructions refer to "opkg" - the package manager used by Ångström Linux. It looks like they've moved on to use Debian instead of Angstrom nowadays, but realistically you can adapt this guide and use the software on any linux distro on any device.

I thought it would be an interesting exercise to use my iPhone as an input device instead of a physical mouse and keyboard. It turns out it's pretty simple, thanks to Tuomas Räsänen's cool python-uinput module together with pybonjour for simple device discovery via Bonjour.

The "server" running on the Beagle and the iOS "client" (bad metaphors, sorry) can be retrieved via git:

$ git clone https://github.com/smcl/iBeagle
$ cd iBeagle

Preparing your Beaglebone Black

First copy the file iBeagle.py file to your beagle.

$ cd iBeagleServer
$ scp iBeagle.py beaglebone.local: # or whatever the username/IP is

Next there's a little bit of setup on the BeagleBone Black is required, as a kernel module and a couple of libraries are required

$ ssh beaglebone.local

Load the "uinput" module. this requires libudev

$ sudo modprobe uinput

The python module for uinput we're using requires udev-systemd, so install via opkg

$ sudo opkg install udev-systemd
$ git clone https://github.com/tuomasjjrasanen/python-uinput.git
$ cd python-uinput && sudo python setup.py install

Ensure avahi-daemon process is up and running

$ ps ax | grep avahi | grep running

Finally setup pybonjour - bonjour library I used for this. If you're having trouble relating to libdns_sd.so see the section "Appendix - udev trouble" at the end.

$ pushd /usr/src
$ wget https://pybonjour.googlecode.com/files/pybonjour-1.1.1.tar.gz
$ pushd pybonjour-1.1.1
$ tar -xzf  pybonjour-1.1.1.tar.gz
$ cd pybonjour-1.1.1
$ python setup.py install

Finally return to your iBeagleServer directory and run the client

$ popd && popd
$ python iBeagle.py

Preparing the iPhone Client

Currently you need to load the application using Xcode, so you'll need an iOS dev account and an iOS device authorised to your account. Open up iBeagleClient/iBeagleClient.xcodeproj, and build + run on your hardware.

When the server is running you should see your BeagleBone Black listed on the table when you launch the app. If not then try restarting the server, or waiting a few seconds.

Appendix - libudev trouble

I actually had a bit of trouble with this step initially. libdns_sd.so was missing, so I retrieved the avahi sources and built my own copy. The libdns_sd.so library was missing on my version of Angstrom Linux, so I built and installed it separately (which is slightly hacky, there's probably a more sensible way to achieve this)

$ wget http://avahi.org/download/avahi-0.6.31.tar.gz
$ tar -xzf avahi-0.6.31.tar.gz && cd avahi-0.6.31

We need to grab some pre-requisites + set PTHREAD_CFLAGS

$ export PTHREAD_CFLAGS='-lpthread'
$ sudo opkg install libssp-dev
$ sudo opkg install libintltool

And run configure so we only build as little as possible to just get the library we want

$ ./configure --disable-static --disable-mono --disable-monodoc --disable-gtk3 --disable-gtk --disable-qt3 --disable-python --disable-qt4 --disable-core-docs --enable-compat-libdns_sd --disable-tests --with-distro=none
$ make

Then (without running make install) we copy libdns_sd.so and libdns_sd.h into /usr/lib and /usr/include respectively

$ cp ./avahi-compat-libdns_sd/.libs/libdns_sd.so /usr/lib
$ cp ./avahi-compat-libdns_sd/dns_sd.h /usr/include

In my case it was trying to use libdns_sd.so.1 - so I also created a symlink to libdns_sd.so.1.

BeagleBone Black - loading OpenBSD from an OS X machine

I thought I'd share the process of installing OpenBSD onto a BeagleBone Black from the perspective of someone who's maybe not experienced with either, and is doing so from OS X. Ted Unangst has a good article on this but there's a couple of bits that maybe need to be a little clearer. What I've got is a little guide to install OpenBSD, mount /var and /tmp in memory to spare our SD card, and build and setup the avahi port.

Equipment

Equipment needed:

  • BeagleBone Black
  • MicroSD card >4GB or so
  • A USB -> TTL cable - (try this one if you're in the EU, cheap and worked just fine)
  • Ethernet cable, and Router\Hub\Switch with internet a
  • Power source for the BBB (USB cable is fine)

Sadly the USB-TTL cable is necessary, since the OpenBSD installer doesn't seem to support the HDMI output on the BBB and this is probably the simplest (or only?) way you can interact with it during this process. Also note that the ethernet cable probably not necessary but it'll make your life a little simpler by letting you download and install everything first without having to worry about setting up some packages later.

Preparation

Firstly you'll want to download the OpenBSD image which is currently miniroot-am335x-58.fs (note: it is the am335x one and NOT the "beagle" one, which is actually for the slightly older BeagleBoard) and use it to create a bootable MicroSD card. You have probably done this dozens of times, but it can't hurt to repeat this info:

Then with the BBB powered off, remove the SD card from your Mac and plug it into the Beagle's SD card slot, plug the USB end of the USB->TTL cable into your Mac, and the other end into the BBB. It's a little hard to see but there's an arrow on the TTL plug and a little white dot beside one of the pins on the board - this indicates the orientation you should use when connecting it. It should look a little something like this:

Then open up a terminal on the Mac and connect to the BBB using screen:

Attach your BBB to your network and finally it to a power source. You should see the installer kick in and dump a load of text to the screen in your screen session in the terminal.

Installing OpenBSD

You can probably just hit <return> through the installer, but it is not very complicated so I'd suggest reading everything relatively carefully (disk layout can be skipped, the automatic layout was fine for me). By default OpenBSD will be installed onto your SD card instead of the BBB's internal eMMC chip, if that's fine by you then all good. If not, pay close attention and change it.

Note: this process will be VERY slow, and may appear to be hanging at some points (particularly building the disk partitions on your SD card). Just be really patient.

Once the install is finished you can restart the BBB. Keep the serial cable attached and let the board boot up, log in and use `ifconfig cpsw0` to find your ip address (for OpenBSD newbies: the network interfaces named after the device driver instead of like wlanN/ethN/enN) then log in via SSH. You can now kill your USB-TTL session in screen, disconnect the cable and put it somewhere safe.

Setting /tmp and /var to use mfs

To reduce wear on the SD card we're going to make some changes to /etc/fstab so that /tmp and /var are actually in-memory filesystems using mfs. First we might need to find the path to the device in the filesystem as OpenBSD may have set up /etc/fstab using DUID (take a look at this FAQ, and scroll to the "Disklabel Unique Identifiers" section):

Now that we know /var is /dev/sd0d and /tmp is /dev/sd0e we can open up fstab, comment out the existing lines where they were mounted as locations on the SD card and create new ones mounting them as mfs partitions:

The changes will take effect after a reboot.

Ports

Congrats, you now have a sort-of usable BeagleBone Black running OpenBSD 5.8. It's probably unlikely that everything you need is in the OpenBSD base install, so to install any extra packages we need to turn to the "ports" system - which is a prepared set of makefiles which can be used to build a whole host of software (on non-ARM architectures we'd use pkg_add to install pre-built binaries but these aren't available on ARM).

While connected to your BBB then download and extract the latest ports per the below: 

In the ports directory there are numerous folders containing makefiles which define how many different packages can be built and installed on OpenBSD. There’s a great deal of information on the OpenBSD website and a slightly gentler introduction here.

Installing Avahi

Avahi is a pretty useful piece of software which will allow us to communicate with the BBB on the local network using <hostname>.local - so if you've set your hostname to be pyongyang you can ssh to pyongyang.local. First we need to find the location of the avahi package in the ports directory, which we can do using `make search`

This tells us that the avahi port lives in the folder net/avahi, so we can just go there and install it as follows:

A bit of patience is again required here as this will take a while. 

Setting up Avahi

After the avahi package is installed there's a bit of setup required. Firstly enabling multicast_host in rc.conf.local by adding the following lines:

Then we need to open up /etc/rc.local and ensure that the require daemons are running. If you already have dbus-daemon in rc.local, you can just add the avahi-daemon and avahi-dnsconfd sections after it, otherwise copy-paste all of this at the end of the file:

Finally you'll need ensure that avahi advertises the correct hostname on the network, so open up /etc/avahi/avahi-daemon.conf, locate the [server] section and specify the hostname you'd like to use to connect to the BBB. I've used pyongyang.local:

There will be a lot of other settings in this file, but we can leave these alone for the time being. After you reboot the BBB you should be able to ping the board and connect via ssh using the hostname you selected:

At this point you should have a nice network-connected BeagleBone Black running OpenBSD which you can easily access through a *.local address on your local network and which shouldn't chew through an SD card in months. Now you just need to find something useful to do with your new setup. Have fun!