Unicorn Pi Tweet Bot

An exhibit for International Women In Engineering Day

Published on 21 June 2017

So, my partner and I - being STEM Ambassadors - were asked to prepare and present a talk about Raspberry Pis for a local university on International Women In Engineering Day. Unfortunately, this invitation came rather late as a previous presenter had dropped out and this left very little time to prepare.

While I had a couple of Pis around the house doing IoT related chores, I really wanted something a little more flashy to present so I quickly rushed out to the local Maplins and picked up a new Raspberry Pi 3 and a Pimoroni Unicorn HAT - the very definition of flashy.

Pimoroni Unicorn HAT

I then needed to think of something to do with it.

Wanting something interactive and, ideally, internet related (seeing a physical manifestation of an event in cyberspace really has 'wow' factor) I decided to write a Twitter bot that listens for a specific hashtag (#INWED17 in this instance) and scrolls the text of the tweet across the Unicorn HAT leds. As with most things of this nature, a short google session resulted in several articles / blog posts I could mash up to produce the desired effect. These were:

  1. Getting started with Twitter
  2. Settings up Unicorn HAT
  3. Unicorn HAT scroll

All in it took about an hour to get the various dependencies installed (including dealling with a rather annoying 'pip' error), accounts set up and code mashed together in order to get a functional app up and running. Not bad considering this was my first stab at writing Python on a Pi.

I pushed the code to a Github repository but have included it below simply because I am impressed with how concise the it is:

import sys
import random

from UHScroll import *
from twython import TwythonStreamer
from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

colours = ['red','white','pink','blue','green','cyan']

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

class UnicornPiBotStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            text = data['text'].translate(non_bmp_map)
            colour = random.choice(colours)
            print(text)
            unicorn_scroll(text, colour, 200, 0.05)

stream = UnicornPiBotStreamer(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

stream.statuses.filter(track='#INWED17')

I'm now beginning to see why Python has gained such traction in non-enterprise markets and will definitely consider using it for future projects. Skipping the edit-compile-run loop and simply interpretting the code via a command line call certainly makes for fast iterations but I don't think I'd appreciate it so much if I was trying to develop anything of any size/complexity.

Anyway, having an exhibit visitors can interact with (albeit indirectly) will make a good addition to the other Pi devices we'll be taking along and discussing.

Really quite looking forward to it now...