Using Apache Thrift for Python & C++

Apache Thrift is a software framework for cross-language: providing what is essentially a remote-procedure call interface to enable a client application to access services from a service — which can be written in the-same, or another language. Thrift supports all of the major languages that you’d expect to use: including Python, C++, Java, JavaScript / Node.js, and Ruby.

Unfortunately, whilst there are quite a few tutorials on how to use Thrift: some of them concentrate on explaining how Thrift is working behind the scenes (which is important, of course): rather than on how to use it. There also aren’t that many that concentrate on using C++. So having spent some time working through some of the tutorials (and the book Learning Apache Thrift), I though I’d have a go at writing something of a practical guide. I’m quite deliberately not going to focus on how Thrift works: if you want that, let me suggest the Apache Thrift – Quick Tutorial. I’m also going to assume that you have Thrift installed on your computer already (again, there are lots of sets of instruction on how to do this — Google will be your friend), and that you’re using a Linux or MacOS computer. You should be able to follow-along with most of this if you’re using Windows: but there will be a few differences, especially when it comes to compiling C++ files.

Continue reading “Using Apache Thrift for Python & C++”

RegEx Golf

RegEx Golf
Inspired by the famous XKCD cartoon that started it and by Google’s Peter Norvig over on O’ I decided to play some RegEx Golf myself this Easter…

If you’re not familiar with the game – the idea is to write the shortest regular expression possible that will select every element of a list, without selecting any elements from a second list. But what are regular expressions, you ask? Well, they’re a special sequence of characters that specify a particular textual patter – so you can use the appropriate regular expression when programming to automatically extract specific text from a list, file, or other text. There are some great tutorials on the web if you want to learn regular expressions (and you really should – because they’re great) – for example

I didn’t want to use lists of Presidents of the United States (as that had been done) – so I decided to have a go at the age-old challenge: animal, mineral, or vegetable

Continue reading “RegEx Golf”

Arduino I2C with the MCP9808 Temperature Sensor

The Microchip MCP9808 is a high precision temperature sensor with an I2C interface – making it easy to interface with microcontroller or embedded Linux electronics. The chip has a very small form-factor: available as either an 8-pin DFN package – or an 8-pin MSOP package. For hobbiest purposes DFN is almost impossible to use due to the complexity of having to reflow solder under the contacts; and MSOP is quite challenging (though it can be done with care). If you want something easy to use with a breadboard however, the good folks at Adafruit can come to the rescue – as they sell the device pre-soldered to a SIP breakout board. (Also available from Makersify in the UK).

Adafruit MCP9808 Breakout board
Adafruit MCP9808 Breakout board

If all you want to do is get something up and running quickly – then you can’t do better than to use the Adafruit Aurduino library (or if you prefer a Python library for BeagleBone or RPi).

Adafruit have done a great job with these libraries and most users will be delighted that “it just works”; but I want to explore how it all works: how to actually communicate with chip directly – using I2C. This guide written from the point of view of an Arduino; but you should be able to follow along from any other device if you’re reasonably familiar with I2C and low-level coding.

Continue reading “Arduino I2C with the MCP9808 Temperature Sensor”

Arduino & XBee

XBee Module Xbee modules provide a very simple way to add a wireless capability to communications on Arduino. Essentially they let you work in exactly the same way as a direct serial connection between two boards – albeit wirelessly.

The easiest way to connect an Xbee to your Arduino, is to use the Arduino Wireless Shield.

XBee Sheild

It’s possible to connect Xbee’s without a shield – but given the fact that they don’t have the (standard) breadboard 0.1″ pin-pitch, this requires a special breakout board, or some custom hardware. If you’re using an Arduino, I think the shields are definitely the best way to go (and they’re pretty inexpensive).

There are a number of different types of XBee – the most notable distinction is between the original, “series 1” devices (directly using the 802.15.4 wireless protocol) and the “series 2”, ZNet 2.5 (or ZB) modules: which add an additional communications layer over the base-standard. Series 1 and series 2 devices are incompatible with each other (and cannot communicate with each other) – but they are pin compatible with each other: so for point-to-point applications, it’s possible to swap out S1 devices with S2 devices (providing you exchange both ends of the communication).

Continue reading “Arduino & XBee”

GPIO with sysfs on Raspberry Pi (Part 2)

In my last post on using sysfs for GPIO on Raspberry Pi, I showed you how you can use the sysfs file system to manipulate the state of the GPIO pins.

This time, we’re going to explore how to do the same thing programmatically. I’m going to walk you through developing a C library to control the GPIO – but exactly the same principles could be used in any other language.

Why C? Well, mostly because I like C. But proficiency in C is useful if you’re going to be working with embedded systems, and microcontrollers; so it’s a good language to use. I’m also picking C because there are a lot of tutorials & libraries using Python – and few (if any) working with C. None of which is to say that Python isn’t a great language (and if you don’t know it – then I highly recommend learning it too).

Continue reading “GPIO with sysfs on Raspberry Pi (Part 2)”

GPIO with sysfs on a Raspberry Pi


The Raspberry Pi (in case you’ve been living under a rock for the last six-months) is a cheap ($25) ARM Linux computer, primarily designed to be used to help teach kids to learn programming & computer science.

It’s also makes a pretty nice alternative to something like a BeagleBone, if you’re looking to play with some embedded computing technology. Whilst it’s less powerful than BeagleBone (and has less GPIO connectivity), Raspberry Pi does have a few advantages. It’s cheap, and it has an HDMI output – meaning that you can interact with it directly, without the need to ssh in from another computer.

The whole ethos of the Raspberry Pi foundation is to make the device as easy to use as possible: to encourage children (and adults!) to play with it, and learn to program. As with Arduino though, this user-friendly layer doesn’t mean that you can’t get your hands a little dirty and see what’s going on underneath.

Continue reading “GPIO with sysfs on a Raspberry Pi”

Arduino Function Generator (Part 3)

In my previous posts in this series I looked at a couple of ways to use an Arduino to generate analogue waveforms.

In this third part I look at a much simpler, IC-based digital to analog (DAC) circuit to provide the waveforms, and look at ways of changing the frequency of the output.

Continue reading “Arduino Function Generator (Part 3)”