17th November 2018

Building a Patreon Supporter Display

It’s humbling to be able to say that I have the backing of supporters through Patreon.com.

With this video I wanted show how to make a display that scrolls the names of those who are kind enough to support the work that I do here.

If you want to build one yourself, you can get the parts by following these (non-affiliate) links.

Raspberry PI ZeroW – https://shop.pimoroni.com/products/raspberry-pi-zero-w
Scroll pHat HD – https://shop.pimoroni.com/products/scroll-phat-hd?variant=38472781450
Solderless Connector kit – https://shop.pimoroni.com/products/gpio-hammer-header?variant=35643318026

Now for the code that I said I mentioned while building the display.

 

There are two scroller scripts in I mentioned in the video, both are written in the python language. Before you can start using them, you will need to have set-up python and download the scrollPhatHD add-on module to know how to connect to the hardware.

To do this, enter the following command at the console on the pi.

curl https://get.pimoroni.com/scrollphathd | bash

Once this has finished, you’re ready to start creating the scroller scripts.

IP Address scroller

The IP Address scroller looks at the wireless network on the raspberry pi. This is done using the netifaces module. So you’ll need to make sure you have that installed first. This command will help you to do just that.

pip install netifaces

The python code I used is shown here.

#!/usr/bin/python

import scrollphathd as sphd
import time

from netifaces import ifaddresses
from subprocess import check_output

ipaddr = ifaddresses('wlan0')[2][0]['addr'];
scanoutput = check_output(["/sbin/iwlist", "wlan0", "scan"])

for line in scanoutput.split():
if line.startswith("ESSID"):
ssid = line.split('"')[1]
break

msg = ' ' + ssid + ':' + ipaddr
print msg

sphd.write_string(msg, brightness=0.2);

cnt = 0
while cnt<(len(msg) * 10):
sphd.show()
sphd.scroll(1)
time.sleep(0.1)
cnt = cnt + 1

sphd.clear()

If you want to make sure you’re always using the latest version of this code,  it’s available directly from my github repositories here

git clone https://github.com/tinkerneering/patreonScroller.git

This code is ready to use straightaway.

python ipScroller.py

Patreon Scroller

The Patreon scroller works in a similar way, in that it builds a message to be shown on the display.

However, it will take a little setting up first as we need to get special codes from Patreon to act as passwords. These will then allow us to connect to Patreon, prove who we are and get access the information we need.

So before you go any further, head over to the Patreon Client & API Keys page and select Create Client. Once you’ve created a client, you will need to make a note of the Creator’s Access Token.

You’ll also need the Patreon Python module, which you is used to talk Patreon.

pip install patreon

Then take that creator access token and add it into the following script.

#!/usr/bin/python
import scrollphathd as sphd
import patreon
import json
import time

xcludeuser = 'tinkerneering'
access_token = '#### INSERT YOU CREATOR ACCESS TOKEN HERE ####'
api_client = patreon.API(access_token)

# Get the campaign ID
campaign_response = api_client.fetch_campaign()
campaign_id = campaign_response.data()[0].id()

list = ""
user_count=0

# Fetch all pledges
pledges = []
pledgesIncl = []
cursor = None
while True:
pledges_response = api_client.fetch_page_of_pledges(campaign_id, 25, cursor=cursor)
pledges += pledges_response.data()
pledgesIncl += pledges_response._all_resource_json_data()

sep = ""
for i in pledgesIncl:
if "relationships" in i:
r = i["relationships"]
if r:
a = i["attributes"]
if a:
t = i["type"]
if t == "user":
n = a["vanity"]
if n != xcludeuser:
list += sep + n
user_count += 1
sep = ", "

cursor = api_client.extract_cursor(pledges_response)
if not cursor:
break

msg = " Thank you Patreons. " + str(user_count) + " supporters : " + list
print msg

sphd.write_string(msg, brightness=0.3);

while True:
sphd.show()
sphd.scroll(1)
time.sleep(0.02)

sphd.clear()

You can also get this code directly from my github repositories

git clone https://github.com/tinkerneering/patreonScroller.git

Again, this can be run from directly from the console.

python patreonScroller.py

There are a number of ways to get these scripts to execute automatically when the PI is turned on. The simplest way is to add them to a scheduler called CRON. You can bring up the cron table of jobs (crontab) with this command.

crontab -e

Then add these lines at the very bottom.

 @reboot sleep 20 && /usr/bin/python ~/ipScroller.py > ~/isScroller.out 2>&1
@reboot sleep 60 && /usr/bin/python ~/patreonScroller.py 

@reboot indicates that the job is to be run when the device is rebooted. Technically every boot from here on is a reboot. Just disconnect the power for ten mississippis and then turn it back on.


If you have any questions, let me know in the comments below.