Home

last update: 2020-03-30 at 17:00:05 CEST

Projects

Bitcoin Price Ticker On A LCD Display

I have a 16x2 character display on my Silverstone LC16B case. You can use LCDd + lcdproc to display various system information on it. I decided to display the current bitcoin price as a fun project. Here is how

This is my LCDd.conf

$egrep -v "^#" /etc/LCDd.conf

[server]
Bind=127.0.0.1
Driver=imon
DriverPath=/usr/lib/x86_64-linux-gnu/lcdproc/
NextScreenKey=Right
Port=13666
PrevScreenKey=Left
ReportToSyslog=yes
ToggleRotateKey=Enter
User=nobody
WaitTime=5

[imon]
CharMap=hd44780_euro
Device=/dev/lcd-imon
Size=16x2

[menu]
DownKey=Down
EnterKey=Enter
MenuKey=Escape
UpKey=Up

Getting the current price on the command line:

curl -s http://api.coindesk.com/v1/bpi/currentprice.json | \
        python -c "import json, sys; print(json.load(sys.stdin)['bpi']['USD']['rate'])"

Use a simple python script to communicate with the LCDd daemon

sudo apt-get install python-pip
pip install requests
#!/usr/bin/env python
import os;
import time;
import json;
import telnetlib;
import subprocess;
import requests;

host='127.0.0.1';
port='13666';

def getPrice():
    url = 'http://api.coindesk.com/v1/bpi/currentprice.json'
    resp = requests.get(url)
    usd_price = json.loads(resp.content)['bpi']['USD']['rate_float']
    eur_price = json.loads(resp.content)['bpi']['EUR']['rate_float']
    return float(usd_price), float(eur_price)

tn = telnetlib.Telnet(host, port)
tn.write("hello\r");
print(tn.read_until("\n"));
tn.write("screen_add screen\n");
print(tn.read_until("\n"));

tn.write("screen_set screen -heartbeat off -priority input\n");
print(tn.read_until("\n"));

tn.write("widget_add screen w1 scroller\n");
print(tn.read_until("\n"));
tn.write("widget_add screen w2 scroller\n");
print(tn.read_until("\n"));

title = "                Bitcoin"
while True:
    prices = getPrice()
    content = str(round(prices[0],2)) + " " + str(round(prices[1],2))
    tn.write("widget_set screen w1 1 1 16 1 m 3 \"" + title + "\"\n" );
    print(tn.read_until("\n"));
    tn.write("widget_set screen w2 1 2 16 2 m 3 \"" + content + "\"\n" );
    print(tn.read_until("\n"));
    time.sleep(60)