Bitcoin Clock



  • Hey Onioneers,
    Today I wanted to share a project : the Bitcoin Clock.

    This Omega2+ based project displays Bitcoin price on a LCD. A strip of LEDs blinks red when price drops, and green when it goes up.

    You'll need a LCD i2c display (1602), an Onion Omega 2+ with a dock (I've got a Power Dock so I'll be able to power it with a LiPo battery), and a LED strip.
    I used to make projects with addressable LEDs (like WS2811…) but this time, I choose a simple RGB strip working like a RGB LED.

    I installed FireI2CLCD : http://davidstein.cz/onion-omega-firei2clcd-lib/
    As the i2cdetect command doesn’t work well, I tried a few i2c addresses, mine was 0x27

    When everything was ok, I just put my python file in /etc/rc.local to run my command on boot.

           # Put your custom commands here that should be executed once
           # the system init finished. By default this file does nothing.
            
           python /root/helloBitcoin.py
           python /root/bitcoin.py
              
           exit 0
    

    The command helloBitcoin.py displays "Bitcoin Clock" on boot before the principal command.

           import time 
           import omega_gpio
           import lcddriver
           import i2c_lib
    
           lcd = lcddriver.lcd(0x27)
           lcd.lcd_clear()
           lcd.backlightOn()
           lcd.lcd_display_string(" BITCOIN CLOCK ",1)
           time.sleep(7)
           lcd.backlightOff()
    

    Here is the idea : (Don't pay attention to the scale, I drew this with Paint)

    0_1504791124314_Projet 2.png

    First try (without LCD) :

    0_1504790760094_LEDS Proto 1.jpg

    Laser Cutting :

    0_1504959553918_IMG_58052.jpg

    And it works !!

    0_1504960311823_IMG_5842.JPG
    0_1504960440948_IMG_5845.JPG

    Here is the code :

    # BITCOIN CLOCK - A Dorize - OnionIoT - Powered by coindesk.com API 
    
    import time							
    import urllib                                                                                                         
    import json                                                                                                                  
    import omega_gpio
    import lcddriver
    import i2c_lib
    
    lcd = lcddriver.lcd(0x27)			# Init LCD
    
    omega_gpio.initpin(3,'out')			# Init LEDs
    omega_gpio.initpin(2,'out')
    omega_gpio.setoutput(3, 1)
    omega_gpio.setoutput(2, 1)
    
    prevprice = 4500.000                      	# Nevermind, first blinking
    
    while True:
    
    	lcd.lcd_clear()
    	lcd.backlightOn()									
    	x = 0
    	site="https://api.coindesk.com/v1/bpi/currentprice.json" # JSONrequest
    	jfile=urllib.urlopen(site)                                                                                                   
    	jsfile=jfile.read()
    	a=json.loads(jsfile)
    	price=a['bpi']['USD']['rate'] 	      # need number in bpi>USD>rate
    	price=price.replace(",","")	# Some cleaning
    	priceEUR=a['bpi']['EUR']['rate']
    	priceEUR=priceEUR.replace(",","")
    
    	info="1 BTC = " + str(price) + " USD"		# Serial info
    	print info
    
    	lcd.lcd_display_string("1 BTC =",1)		# LCD info
    	lcd.lcd_display_string(str(price)+" USD",2)
    
    
    	if float(price)>prevprice:			# Green blinking
    		for x in range(0, 7):
    			omega_gpio.setoutput(3, 0)
    			time.sleep(0.2)
    			omega_gpio.setoutput(3, 1)
    			time.sleep(0.2)
    			x +=1
    		time.sleep(15)
    	
    	elif float(price)<prevprice:			# Red blinking
    		for x in range(0, 7):
    		
    			omega_gpio.setoutput(2, 0)
    			time.sleep(0.2)
    			omega_gpio.setoutput(2, 1)
    			time.sleep(0.2)
    			x += 1
    		time.sleep(15)	
    	
    	elif float(price)==prevprice:		# backlightOff can be removed
    		lcd.backlightOff()
    		time.sleep(15)
    	
    	infoEUR="1 BTC = " + str(priceEUR) + " EUR"     # EUR Serial info
    	print infoEUR
    
    	lcd.lcd_clear()
    	lcd.lcd_display_string("1 BTC = ",1)		# EUR LCD info
    	lcd.lcd_display_string(str(priceEUR)+" EUR",2)	
    
    	time.sleep(15)
    
    	prevprice = float(price)			# Price update
    

    It's a prototype yet, but it works. Next step I will build another "clock" for Litecoin, maybe 3D printed.

    I hope you enjoyed this tutorial πŸ™‚

    BTC Tips : 3JRf2GaHsGs9c1naxAn8TN3HN4S1y5hUoG

    Have a nice day Onioneers πŸ™‚



  • Very nice! I wouldn't mind having one of these. I think it would be cool if you made the face of the 'bitcoin' a pie chart, and had different crypto currencies with their own LEDs under each piece of the pie. Then you could use one omega to report a bunch of different crypto currency rates.

    How many watts is your laser cutter? That looks like its gotta be pretty powerful, 100 watts?



  • @James-Harding Good idea ! πŸ™‚ Thank you, I'll think about it.
    It's not my laser cutter, I use a Trotec Speedy 400 so it's very powerful πŸ˜‰



  • Don't need to do all that cleaning...

    you should be able to just select the first item in the list to get clean JSON data which is recognised as a dictionary.

    a=json.loads(jsfile)[0]

    should work... have only been using python for 2 hours tho!



  • @Rongomai-Bailey Cleaning is just to translate a X,XXX.XXXX price into a float XXXX.XXXX



  • @Antoine-Dorize oh yes now I see!

    my bad - was thinking of the stock ticker example



  • I get a following error:

    Traceback (most recent call last):
    File "bitcoin.py", line 27, in <module>
    a=json.loads(jsfile)
    File "/usr/lib/python2.7/json/init.py", line 339, in loads
    return _default_decoder.decode(s)
    File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

    I modify the line27 - but the error is same:

    Traceback (most recent call last):
    File "bitcoin.py", line 27, in <module>
    a=json.loads(jsfile)[0]

    Can you help for me, how could I fix it?

    Thanks a lot!



Looks like your connection to Community was lost, please wait while we try to reconnect.