Skip to content

Instantly share code, notes, and snippets.

@mattvukas
Last active July 1, 2016 17:12
Show Gist options
  • Save mattvukas/9264034 to your computer and use it in GitHub Desktop.
Save mattvukas/9264034 to your computer and use it in GitHub Desktop.
Python script for the Raspberry Pi, which fetches the last Bitcoin price via the Bitstamp API, then outputs the quote to an attached Adafruit LCD screen (http://www.adafruit.com/products/1110).
"""
bitcoinAdafruitLcdTicker.py
Tested with Python 2.7.3
Libraries required:
- Requests
http://docs.python-requests.org/en/latest/
- Adafruit LCD library
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
Copyright (C) 2014 Matt Vukas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import requests, json
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
"""
Uses the Bitstamp API to grab the current price of Bitcoin, and returns it
as a formatted string (dollar sign beforehand, ending in 2 decimal places)
"""
def getBitcoinPriceStr():
URL = 'https://www.bitstamp.net/api/ticker/'
try:
r = requests.get(URL)
priceStr = "$" + "{0:.2f}".format(float(json.loads(r.text)['last'])) + "/BTC"
return priceStr
except requests.ConnectionError:
print "Error fetching Bitcoin price from Bitstamp API"
# initialize the LCD screen and turn the backlight green
lcd = Adafruit_CharLCDPlate()
lcd.backlight(lcd.GREEN)
lcd.clear()
while True:
outString = "Bitstamp quote:\n " + str(getBitcoinPriceStr())
# catch all exception handling for any hardware issues
try:
lcd.clear()
print outString
lcd.message(outString)
except:
print "Error pushing BTC price to LCD display"
sleep(10) # updates screen every 10 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment