Skip to content

Instantly share code, notes, and snippets.

Created December 2, 2013 05:39
Show Gist options
  • Save anonymous/7745593 to your computer and use it in GitHub Desktop.
Save anonymous/7745593 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 2, 2013.
    46 changes: 46 additions & 0 deletions btc_hue.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #BTC Ticker Light
    This script updates one of your Philip's Hue lights to be either red or green depending on if the current price of bitcoin is below or above the 24 hour weighted price.

    ![The light](http://i.imgur.com/LkwQKMBl.jpg "The endproduct")

    ##Install Dependencies
    easy_install install beautifulhue

    ##The Script
    Save this to a python file after customizing your Philip's Hue Bridge ip, username, and light_id.
    ```python
    from urllib import urlopen
    import json
    import time
    from beautifulhue.api import Bridge

    # CONFIGURE HERE
    bridge = Bridge(device={'ip':'192.168.1.28'}, user={'name': 'newdeveloper'})
    light_id = 1

    while True:
    active_light = bridge.light.get({'which': light_id})
    previous_color = -1
    if active_light['resource']['state']['on']:
    weightedprices_url= "http://api.bitcoincharts.com/v1/weighted_prices.json"
    weightedprices = urlopen(weightedprices_url)
    weightedprices = json.loads(weightedprices.read())
    weighted_24 = float(weightedprices["USD"]["24h"])

    currentprices_url = "https://www.bitstamp.net/api/ticker/"
    currentprices = urlopen(currentprices_url)
    currentprices = json.loads(currentprices.read())
    currentprice = float(currentprices["last"])

    color = 0 # red
    if currentprice > weighted_24:
    color = 25500 # green
    if color != previous_color:
    bridge.light.update({'which': light_id, 'data': {'state': {'hue': color}}})
    previous_color = color
    print "updating light color -- current=%s, avg=%s" % (currentprice, weighted_24)

    time.sleep(60 * 1000)
    ```
    ##To Run
    python script_name.py