Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions examples/BTCTicker/BTCTicker_request.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'''
Arduino version:
https://github.com/dankelley2/M5Stack_BTCTicker
This is an updated version using coingecko.com API via a simple PHP page hosted at URL https://m5stack.it/btc

Response (JSON)
last Last BTC price.
high Last 24 hours price high.
low Last 24 hours price low.
vwap Last 24 hours volume weighted average price.
volume Last 24 hours volume.
bid Highest buy order.
ask Lowest sell order.
timestamp Unix timestamp date and time.
open First price of the day.

m5stack.com/api/btc/ticker/btcusd.json
PHP code:
<?php
$url = "https://api.coingecko.com/api/v3/coins/bitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false";
$json = file_get_contents($url);
$json = json_decode($json);
$high_24h = $json->market_data->high_24h->usd;
$low_24h = $json->market_data->low_24h->usd;
$current_price = $json->market_data->current_price->usd;
$last_updated = $json->last_updated;
echo "{\"last_updated\":\"$last_updated\",\"high_24h\":\"$high_24h\",\"low_24h\":\"$low_24h\",\"current_price\":\"$current_price\"}";
?>
'''

from m5stack import *
Expand All @@ -28,9 +26,9 @@
import gc


# def timeout_reset(timer):
# import machine
# machine.reset()
def timeout_reset(timer):
import machine
machine.reset()

# def save_price_csv(filename, timestamp, price):
# if utils.exists(filename):
Expand All @@ -39,9 +37,9 @@
# else:
# with open(filename, 'w') as f:
# f.write(timestamp+','+price+'\n')
# t1 = machine.Timer(2)
# t1.init(period=60*1000*60, mode=t1.PERIODIC, callback=timeout_reset)

t1 = machine.Timer(2)
t1.init(period=60*1000*60*6, mode=t1.PERIODIC, callback=timeout_reset)


def main():
Expand All @@ -59,31 +57,28 @@ def main():
# btc_data = get_btc_price()
gc.collect()
lcd.triangle(300,0, 319,0, 319,19, lcd.YELLOW, lcd.YELLOW)
r = urequests.get("http://api.m5stack.com/btc")
# r = urequests.get("http://api.coindesk.com/v1/bpi/currentprice/usd.json")
r = urequests.get('https://m5stack.it/btc.php')
lcd.triangle(300,0, 319,0, 319,19, lcd.BLUE, lcd.BLUE)
btc_data = ujson.loads(r.text)
print(btc_data)
print('')
if btc_data:
# Max price
high = btc_data['high']
high = btc_data['high_24h']
high = high[:(high.find('.')+3)]
lcd.font(lcd.FONT_DejaVu18)
lcd.print('Max:', 20, 192, lcd.GREEN)
lcd.print(high, 5, 215, lcd.GREEN)

# Min price
low = btc_data['low']
low = btc_data['low_24h']
low = low[:(low.find('.')+3)]
lcd.font(lcd.FONT_DejaVu18)
lcd.print('Min:', 255, 192, lcd.RED)
lcd.print(low, lcd.RIGHT, 215, lcd.RED)

# Last Price
price = btc_data['last']
# price = btc_data['bpi']['USD']['rate_float']
# price = '%.2f' % price
price = btc_data['current_price']
if not price == prev_price:
lcd.rect(0, 100, 320, 48, lcd.BLACK, lcd.BLACK)
lcd.font('SFArch_48.fon')
Expand All @@ -99,12 +94,15 @@ def main():
lcd.triangle(160,_offset+25, 140,_offset, 180,_offset, lcd.RED, lcd.RED)
prev_price = price

# # updated time
# lcd.font(lcd.FONT_Default)
# lcd.print('Updated:'+btc_data['time']['updated'], lcd.CENTER, 222, 0x999999)
except:
# update time
tstr = 'Update: %s' % (btc_data['last_updated'])
lcd.font(lcd.FONT_Default)
lcd.print(tstr, 3, 3, 0x666666)

except Exception as e:
sys.print_exception(e)
pass

time.sleep(5)

main()