A Year Passes Quickly

So yeah, a year passed by. That happens sometimes.

Not much was achieved blog-wise, but behind the scenes I’ve been busy. We had a child, a wonderful boy who will turn two in September. Six months paternity leave just wooshed by. It was glorious. I barely touched a computer for half a year. That’s an experience I can heartily recommend.

Also, this experiment is coming to a close. In August we’ll be moving back to Uppsala. Turns out homesickness is a powerful thing. Good-bye Piteå, we had a good run.


I sit down to write, I tell myself. I enjoy the creative flow. There is a certain gratification to committing your words to paper. And anyway, I tell myself, today is going to be different.

I sit for a while. Nothing is different.


I built a simple node.js client for fetching Pokémon data from pokeapi.co. You can run it on the server as well as in a browser, although in the latter case you’d need to direct the requests through a proxy on your origin server.

PokeApi = require 'pokeapi'
api = PokeApi.v1()

api.get('pokemon', 1).then (bulbasaur) ->
    api.get(bulbasaur.moves).then (moves) ->
        console.log 'here the move list for', bulbasaur.name
        console.log move.name for move in moves
.fail (err) ->
    console.log err

Get the npm here, or check out the code here.

JavaScript Race Conditions

Yesterday I was rudely reminded that just because Node.js is single threaded doesn’t mean you don’t have to worry about race conditions.

Our architecture is heavily message-oriented and the code that handles ACKing messages looks something lite this. The idea is that application code can call either acknowledge(), which acknowledges that the task has been handled, or retry(), which means that the we should retry (a copy of) the task at a later time and then acknowledge that the original task has been dealt with. It should only be possible to retry or acknowledge a task once, which is why we keep track of the state in a variable @resolved.

class Ack
    constructor: (@task, @queue) ->
        @resolved = false

    acknowledge: =>
        unless @resolved
            @resolved = true

    retry: =>
        unless @resolved
            .then =>

Can you spot the error? It took me a while to realize that even though retry() makes sure that @resolved hasn’t been set and we call acknowledge() to ACK the task and set @resolved immediately afterwards, that actually happens asynchronously. We are using the Q promise library here, which means that if retry() is called repeatedly within the same process tick, then scheduleRetry() will almost certainly be called multiple times.

I think promises are wonderful, but the fact that they make asynchronous code look synchronous can really trick you. I’m pretty sure I wouldn’t have made this mistake if I’d written this piece in the regular old callback-oriented style.

Busy Playing Hearthstone

So yeah, one reason I haven’t blogged or done any OSS work recently is I’ve been playing Hearthstone obsessively. I installed it with some trepidation and immediately got hooked so hard it is kind of scary. Now I can’t get enough. I read the blogs, watch the streams, follow the drama.

I’m strictly a casual player, and I’ve promised myself I won’t spend any actual money on this game. So in order to beef out my collection I’ve been grinding the Arena for the better part of two months. The Arena is pretty fun, as you get to play around with a lot of cards you wouldn’t usually get your hands on. It’s also punishing, as you really need to rack up those seven wins in order to recoup your gold investment. Last week I decided enough was enough, and switched to playing ranked games.

This season I was toiling away at level 20 for quite a while playing different decks, but then made a conscious commitment to learn the Shaman Way. Shaman is good for casuals like me since you can put together a solid deck out of just commons and rares, and it will both be fun to play and have a decent chance on the ladder. Playing Shaman correctly isn’t easy though; you have to pace yourself and learn when to strike with Lightning Storm, when to unleash your Feral Spirits, and what to hit with your Earthshock. And most importantly, when to hold back and just play a totem.

You also need to think hard about the mana curve. Despite being an all-round awesome card, I realized Chillwind Yeti had no place in my deck; on turn three I usually play an Overload card, which ruins turn four. After dropping the yetis, and a few other tweaks I was shocked how good the deck was doing. I went on win streak after win streak and hit rank 9 today. My one to two hours of game time a day isn’t enough to take me to Legend, but it was pretty sweet to at least break single digit.

My First Board

I’m immensely proud of the fact that I soldered together my first prototype board last night. It holds two DS18B20s; one naked sensor soldered straight onto the board and one on a waterproof cable that I’ve hung out the window. This lets me measure current indoor and outdoor temperature.

The board is connected to my Raspberry Pi, where a node.js program using sensorjs is regularly sending the sensor data to a TempoDB database.

Hardware Is Hard

Where I live there aren’t really any options for electronics shopping, so I was resigned to do it mostly through mail order. Fortunately I discovered that Kjell & Co., which is probably the closest thing to Radio Shack we have in Sweden, not only carries a lot of Arduino stuff, but also has an shop in the next town over (Luleå, about 50km away).

So I got myself a soldering station, some lead-free solder1 some sensors and grab bag of diodes, resistors and cables to play with. It must be something like 25 years since I last had a soldering iron in my hands, but putting the T-cobbler together I all just came back to me. Even the smell was exactly as I remembered it.

I had some problems getting the DHT11 sensor up and running. All the examples I could find had a four-legged sensor, but mine came pre-mounted on a board with only three. Also, the configuration of the legs had been switched around. That one took some figuring out.

The DS18B20 however, just plain refused to show up as a device in /sys/bus/w1/devices. I tried everything: reflashed the SD card, checked the kernel messages, double-checked my connections (twice), moved things around on the bread board in case there was a break (which incidently there is; the vertical strips break in the middle, but this didn’t affect my wiring) and tried every combination of power and GND I could find on the breakout board in case I had done a bad soldering job (which I hadn’t). I did countless reboots. Still nothing.

I had just about written that sensor off when I discovered that the pullup resistor2 I’d used wasn’t yellow-purple-red as specified, but yellow-purple-yellow. 470K instead of 4.7K. Oops.

Hardware is hard.

  1. Which everyone seems to think is terrible, but I haven’t had any problems with. Yet.

  2. I confess I have no idea what a pullup resistor actually does.

Hello, World

Since I pretty much do node.js full-time nowadays, I was curious if I’d be able to run it on my Pi as well. Turns out that’s no big deal; there are binaries available for download so you don’t have to go through the hassle of compiling (which I hear can take quite some time).

Interfacing with the PiFace is another matter. There is a module called piface-node, but I couldn’t get it to do anything, and looking at the source code I could see that it was a bit too low-level for what I wanted to do. However, I did find a C library called libpifacecad, and started hacking to see if I could write a node wrapper around it.

After a few evenings worth of work I am at a point where I can read the sensors pretty reliably and writing to the screen somewhat less so (it works maybe one time out of three). That’s not the fault of the underlying library though, it seems pretty solid. Rather, I suspect there’s either a race condition somewhere (possibly), or I’m making assumptions about C++ memory management that’s not entirely true (probably), or I need to rethink how to do I/O in the node.js event loop (almost certainly).