Michael Svanström

developer, photographer

Build a realtime Bitcoin price display

Use a Raspberry Pi to follow Bitcoin trades in realtime on a cheap, tiny OLED display

tl;dr: Hook up a small OLED display to a Raspberry Pi, install luma.oled , clone luma.examples . Then run luma.examples/bitstamp_realtime.py

Bitcoin display!

Bitstamp feed

Parts

  • Raspberry Pi
  • 0.96”/1.3” OLED display (buy for $5 on ebay/Aliexpress)

OLED display

The tiny OLED display connects via either an I2C or SPI bus. The SPI is supposedly faster but also requires more cables. For my setup I purchased a display that connects via I2C and happened to work with the sh1106 driver (even though the seller suggested a ssd1106 driver that doesn’t exist).

luma.oled

Follow the docs on connecting the display as well as installing luma.oled at https://luma-oled.readthedocs.io/en/latest/

You need to figure out which driver to use with the display. In my case it’s sh1106. For 0.96” displays, ssd1306 seems to be common. Use of an improper driver causes all kinds of crazy pixels, if any, on the display.

luma.examples

Clone luma.examples and run any example for testing the display.

$ git clone https://github.com/rm-hull/luma.examples
$ python luma.examples/examples/demo.py --help
$ python luma.examples/examples/clock.py -d [DRIVER]

Bitcoin feed

Our friends at Bitstamp are kind enough to publish all of their trades in a Web socket API

According to the docs they use Pusher. There’s no official Python client library, but PythonPusherClient seems to be working very well.

Here’s my (relevant) code to subscribe to Bitstamp’s feed and write it to the OLED display.

...
rows = []

def trade_callback(data):
    json_data = json.loads(data)

    str_row = "${}  {}".format(json_data['price_str'], json_data['amount'])
    rows.insert(0, str_row)
    if len(rows) > 5:
        rows.pop()

    with canvas(device) as draw:
        for i, line in enumerate(rows):
            draw.text((0, 2 + (i * 12)), text=line, font=font, fill="white")

def connect_handler(data):
    channel = pusher.subscribe('live_trades')
    channel.bind('trade', trade_callback)

pusher = pusherclient.Pusher(BITSTAMP_PUSHER_KEY)
pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()
...

Feeding the trades to the display

Since I contributed this code to luma.examples, all you need to do now is run

$ python luma.examples/examples/bitstamp_realtime.py -d [DRIVER]

Run on boot

$ sudo nano /etc/rc.local

Insert this line before “exit 0”

/usr/bin/python /home/pi/luma.examples/examples/bitstamp_realtime.py -d [DRIVER]