Apes and Embedded Systems (part two)

In part one I just hooked up a little monkey keychain called Chuggzilla to my Arduino. A day or so prior to my 90 Kč spending spree on Chuggzilla I'd just received a 128x64 Adafruit OLED display and realised this was a nice opportunity to play a bit with it.

The idea was that Chuggzilla would still scream his weird morse code madness, but the screen would display the actual text behind the message, with the current letter highlighted. I was just wanting to quickly get something working, so instead of integrating with the OLED library using C I just ported my morse code stuff to the Arduino IDE so I could easily re-use the existing API. The final result is pretty fun - though I suspect the current drain from the screen is too high because the previously manic ape noises were almost whisper quiet.

I am not sure exactly who would want to reproduce this, but if you're interested hook the OLED screen up as per the Adafruit wiring instructions then connect up whatever animal or thing you want to control per my original schematic (connecting digital 2 instead of digital13 to the transistor's base, since we need that for the screen), create a sketch with the code below and add the Adafruit libraries.

Code is on this gist on github. 

Apes and Embedded Systems (part one)

I woke up today and decided to do some neurosurgery. I set off into the town and sought out a willing test subject - ladies and gentlemen, meet Chuggzilla (credit goes to Miriam for the name):

Chuggzilla is a keychain monkey I bought in Tiger for around 90 Kč. Pressing a little button on the back of his head causes his LED eyes to light up and a little speaker inside to play a monkey noise:

This is what he looks like on the inside

I soldered a couple of wires to either side of the button element, so that I could control him electronically:

Testing him out with a button on a breadboard

Finally being controlled (via a transistor) by an output pin on the Arduino using the morse code program I previously wrote - the message is "hello world" or ".... . .-.. .-.. --- / .-- --- .-. .-.. -.."

The circuit driving this is below. I used a little LED for debugging when I was checking Chuggzilla was connected fine, and just kept it in the circuit - you can omit it if you're certain your animal is wired up fine :)

The code behind this is at https://github.com/smcl/arduino_morse - it's straight C, so if you want to load it to your Arduino you'll need to have the AVR GCC tools installed, and change the AVRDUDE_PROGRAMMING_DEVICE variable in the Makefile, then run make flash.

Arduino primer/refresher - OS X perspective

I've been wanting to get back into some interesting embedded systems and electronics work - so I figured I'd dust off my old Arduino. In the process I ended up having to re-learn how to program it using C without using the Arduino IDE (it's not my favourite thing), which is what I'm going to go into here.

I downloaded CrossPack which contains GCC cross compiler for the AVR architecture, as well as a handful of other useful tools (like avrdude, which we'll use to flash the Arduino). Once that's setup we can start off with a pretty simple program which rapidly blinks the arduino's onboard LED, which is sort of the Hello World of the Arduino community:


To build this using GCC we just need to call avr-gcc, making sure to specify the processor (using -mmcu), define the processor frequency (the F_CPU macro)


This creates an ELF binary. One odd thing is that pretty much everyone building C code for Arduino seems to include the "-R .eeprom" switch which removes the ".eeprom" section from the output binary. However I've not ever seen this section does not exist in my ELF binary - see below:


Anyway, perhaps there's something else at play that I've not understood so I've kept this switch in (I'm making a mental note to explore this later). Again many other places seem to insist on using the avr-objcopy utility to produce intel hex file before uploading to the Arduino - so often you'll see the below:


However again this is not a necessary step it seems, since the utility we use to upload the binary to the Arduino - avrdude - supports ELF so you can skip it. When we want to upload the binary to the Arduino we'll use avrdude, but first we need to know the programming device. For me this is just the USB type A to type B cable that comes with the arduino, which in the example below is /dev/tty.usbmodem1421:


I've found it useful to create a little Makefile to tie these steps all together:


Finally to tie this together in a slightly more existing example than blink.c, I've created a little program that will flash the LED attached to pin 13 with a message encoded in morse - "hello world" or ".... . .-.. .-.. --- / .-- --- .-. .-.. -..":


The code is on github at https://github.com/smcl/arduino_morse and only requires a bare Arduino and USB cable.

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!

New Year's Resolutions - 2015

1. Read a book every 2 weeks

I had one resolution from 2014 which I chose when I was in Busan, South Korea - to average one book every two weeks for 2014 (so 26 for the entire year). Due to some hefty work commitments that hit me around September I fell pretty far short of this (I managed 16). This was a nice resolution to have because too easy to forget that I enjoy reading and need just a little bit of motivation to make time for it, so I'm trying again in 2015.

Measurable goal: 26 books read by 31st December 2015

2. Assemble my own Single-board Computer

I've been messing around with my Arduino and BeagleBone for a while now, but I'd to learn more and have a more structured goal. For Electronic Engineers it's not too tough to put single-board computers together, but my experience with Embedded Systems is limited to the software\tools side of things so it's actually quite a challenge for me. 

I've found a number of blow-by-blow guides[1] on various microcontrollers so it's nice to see that it's not too uncommon to do this, but realistically I need to start from the beginning. Roughly speaking I think the first steps I want to do are the following:

  1. Undertake a primer in electronics (the last time I touched this was on my Physics Higher when I was 16)
  2. Put a simple but useful circuit together (Arduino ISP programmer for my BeagleBone, for example) then fully understand and document it
  3. Start looking at the microcontrollers I might want to use, pick one and get familiar with the architecture and ecosystem.

This will likely not turn into anything special or groundbreaking, but it'll help me scratch an itch I've had for quite a while.

[1]: The first few links I picked through were for Zilog Z80, Atmega\Arduino, and 6502 - there's a lot of nice support in the community it seems so this could be a nice achievable resolution :)

Measurable goal: Completed SBC on a breadboard with simple OS\output by 31st Dec 2015

3. Contribute (more) to Open Source

This is another long-standing one, I've been meaning to find a nice open source project where my contributions would be valued. I've already started contributing to OpenCorporates and I've enjoyed this so far, but I'd like to do more if possible. However this is a pretty vague resolution, and one which many people make and fail. I may need to define it a little better before I have any chance of considering it "done".

Measurable goal: ???

4. Learn to Pick a Lock

I've had a cheap set of lockpick tools for a month or so and have made no real attempt to learn other than skimming the MIT lockpicking guide a couple of times when I was in uni. I'd love to be able to pick locks.

Measurable goal: Able to pick the lock on my flat's door by 31st Dec 2015

xkcd 1287

Made a quick bookmarklet to do this - create a new bookmark and copy-paste the below into the URL field

So the below...

becomes...

PDFCat

I wanted an easy way to read the Handbook of Applied Cryptography ebook @ http://cacr.uwaterloo.ca/hac which is split into a number of separate PDFs, so I created PDFCat (PDF + "cat" from the *nix command). It uses CherryPy and PyPDF2 as well as Bootstrap for layout (because I wanted an excuse to try Bootstrap) and Sortable for the nice draggable, sortable list to change the order of the PDFs in the final document. 

GPS Speedometer with Python and SL4A

On a very hungover 8 hour train journey back from a spontaneous weekend trip to Berlin I was mucking around SL4A with my Nexus 7 and somehow we ended up wanting to know how fast the train was going and figured we could use the GPS positions to do this. A challenge was issued - I had to write this before we reached Brno. We were just passed Usti nad Labem at this point giving me plenty of time in theory, but my dwindling battery life added a bit of drama to the proceedings. Anyway I managed:

Here's what the output looks like when I run it on my morning tram to work:

The journey this tram follows looks a little like this:

Reshma's flight - EK139 (but only between the Black Sea and Austria)

You can get the FlightRadar24 data as a JSON object for playing with at http://db8.flightradar24.com/zones/full_all.js

I altered my Earth program from a previous post to periodically poll this URL to plot the progress of my Girlfriend's Emirates flight from Dubai to Prague, and created a nice little gif of this. Sadly her flight wasn't on the feed at all except for this very short period between the Black Sea and Austria so the GIF is rather shirt and boring!