I all this BTC craze I wanted a nice way to get all the time the latest Bitcoin value and the value of my BTC stash 🙂
Because I had laying around an old Raspberry Pi 3 and a Pimoroni Unicorn Hat HD this was easy to do and the result is spectacular.
STEP 1: Get the BTC quote
This is easy to do using the free information from following URL. Note that a JSON with several currencies is returned.
https://blockchain.info/ticker
STEP 2: Write the code
I started from the example that comes with the Unicorn Hat HD that displays your IP.
The only method I wrote is in fact get_latest_bitcoin_price(). This method simply calls the free API call from blockchain.info and extracts the latest BTC/EUR quotation from the JSON.
Note that under BITCOIN_STASH variable you can put the value of your stash in BTC so you will know how “rich” you are every 10 minutes.
See the full code python attached.
import socket
import time
import unicornhathd
import requests
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError:
exit('This script requires the pillow module\nInstall with: sudo pip install pillow')
BITCOIN_API_URL = 'https://blockchain.info/ticker'
BITCOIN_STASH = 0.5
def get_latest_bitcoin_price():
response = requests.get(BITCOIN_API_URL)
response_json = response.json()
# Convert the price to a floating point number
response_string = "{0:.2f}".format(float(response_json[u'EUR'][u'last'])*BITCOIN_STASH)
return "BTC: "+str(response_json[u'EUR'][u'last'])+" EUR" + "......... BTC STASH: "+response_string+" EUR"
def create_image_from_text(in_text):
colours = (255, 255, 250)
font_file = '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'
font_size = 10
font = ImageFont.truetype(font_file, font_size)
w, h = font.getsize(in_text)
text_x, text_y = width, 0
text_width, text_height = width, 0
text_width += w + width # add some padding so the ticker scrolls off the unicorn hat
text_height = max(text_height, h, 16) # no more than the size of the unicorn hat
image = Image.new('RGB', (text_width, text_height), (0, 0, 0))
draw = ImageDraw.Draw(image)
draw.text((text_x, text_y), in_text, colours, font=font)
return (image, text_width)
# DISPLAY
def scroll_txt(image, text_width):
unicornhathd.rotation(90)
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.04)
unicornhathd.off()
# one stop call for scrolling text
def scroll_text(in_txt):
image, text_width = create_image_from_text(in_txt)
scroll_txt(image, text_width)
if __name__ == '__main__':
width, height = unicornhathd.get_shape() # 16, 16 by default
ticker = get_latest_bitcoin_price()
scroll_text(ticker)
scroll_text(ticker)
scroll_text(ticker)
scroll_text(ticker)
scroll_text(ticker)
STEP 3: Create a Linux service that will retrieve the quotation every 10 minutes
Create a file show_btc.service
[Unit]
Description=display IP on the unicorn HD hat
[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/Pimoroni/unicornhathd/George/show_btc/show_btc.py
Restart=always
RestartSec=600
[Install]
WantedBy=multi-user.target
Then copy it to the systemd services directory and register it as a service
sudo cp show_btc.service /etc/systemd/system/
sudo systemctl enable show_btc.service
sudo systemctl start show_btc.service
STEP 4: Enjoy your new BTC banner !!!
STEP 5: Download full setup
See the link to download the whole setup.