Bitcoin Wallet Display

Total Bitcoin Wallet Value Variant

If you'd like to have your PyPortal Bitcoin Display do some math for you and add up the total value of your Bitcoin wallet, based on the current Bitcoin Price Index value, no problem!

This variation on the code does just that!

"""
This example will access the coindesk API, grab a number like bitcoin value in
USD, multiply it by your # of bitcoins and display it on a screen
If you can find something that spits out JSON data, we can display it!
"""
import time
import board
from adafruit_pyportal import PyPortal

# You can display in 'GBP', 'EUR' or 'USD'
CURRENCY = 'USD'
NUM_BITCOINS = 3.14    # how many bitcoins to display
# Set up where we'll be fetching data from
DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json"
DATA_LOCATION = ['bpi', CURRENCY, 'rate_float']

# We will convert the value from coindesk into a string that calculates our
# total bitcoin value!
def text_transform(val):
    format_str = "{:,.2f} Bitcoins\n = {:,d}"
    if CURRENCY == 'USD':
        format_str = "{:,.2f} Bitcoins\n = ${:,d}"
    if CURRENCY == 'EUR':
        format_str = "{:,.2f} Bitcoins\n = €{:,d}"
    if CURRENCY == 'GBP':
        format_str = "{:,.2f} Bitcoins\n = £{:,d}"
    return format_str.format(NUM_BITCOINS, int(val*NUM_BITCOINS))

# the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION,
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/bitcoin_background.bmp",
                    text_font=cwd+"/fonts/Arial-BoldItalic-12-Complete.bdf",
                    text_position=(192, 130),
                    text_color=0x0,
                    text_transform=text_transform)
pyportal.preload_font()  # preload alphanums
pyportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol

while True:
    try:
        value = pyportal.fetch()
        print("Response is", value)
    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)

    time.sleep(3*60)  # wait 3 minutes

If you've already set things up based on the instructions on the previous page for the simple bitcoin.py example, you can now copy this new code and paste it into Mu, then save it as code.py on your PyPortal, overwriting the existing version.

This one works nearly identically, with a few small changes.

First, there is now a NUM_BITCOINS variable. Change this to represent the number of Bitcoins in your wallet. 

NUM_BITCOINS = 3.14

Second, the text_transform() function now does more than just format your text to include the currency symbol. It also multiplies the Bitcoin value in your chosen currency by the NUM_BITCOINS value to give you your grand total!

Download: file
def text_transform(val):
    format_str = "{:,.2f} Bitcoins\n = {:,d}"
    if CURRENCY == 'USD':
        format_str = "{:,.2f} Bitcoins\n = ${:,d}"
    if CURRENCY == 'EUR':
        format_str = "{:,.2f} Bitcoins\n = €{:,d}"
    if CURRENCY == 'GBP':
        format_str = "{:,.2f} Bitcoins\n = £{:,d}"
    return format_str.format(NUM_BITCOINS, int(val*NUM_BITCOINS))

This also does a more sophisticated text string formatting job so that the result looks something like this:

         3.14 Bitcoins

            = $12,343

And, we're also specifying a smaller font so that it will fit the screen properly.

This guide was first published on Mar 26, 2019. It was last updated on Mar 26, 2019. This page (Bitcoin Wallet Display) was last updated on Jul 15, 2019.