From 16a433240cddc1ecfd86684a87eab6526fa48865 Mon Sep 17 00:00:00 2001 From: Valerio Vaccaro Date: Sat, 30 May 2020 00:53:37 +0200 Subject: [PATCH 1/2] Migrate to coingeko API via PHP filtering page This is an updated version using coingecko.com API via a simple PHP page hosted at URL https://m5stack.it/btc PHP code: 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\"}"; ?> --- examples/BTCTicker/BTCTicker_request.py | 70 ++++++++++++------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/examples/BTCTicker/BTCTicker_request.py b/examples/BTCTicker/BTCTicker_request.py index 0895943..4e7e7c8 100644 --- a/examples/BTCTicker/BTCTicker_request.py +++ b/examples/BTCTicker/BTCTicker_request.py @@ -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: +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 * @@ -28,10 +26,10 @@ 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): # with open(filename, 'a') as f: @@ -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(): @@ -49,7 +47,7 @@ def main(): lcd.setBrightness(800) lcd.setTextColor(lcd.WHITE, lcd.BLACK) lcd.font('SFArch_48.fon') - lcd.print('BTC Price', lcd.CENTER, 25, lcd.ORANGE) + lcd.print('BTC Price', lcd.CENTER, 30, lcd.ORANGE) prev_price = '' timereset = time.ticks_ms() + (60*1000) while True: @@ -59,35 +57,32 @@ 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.rect(0, 105, 320, 48, lcd.BLACK, lcd.BLACK) lcd.font('SFArch_48.fon') - lcd.print('$ '+price, lcd.CENTER, 100, color=lcd.WHITE) + lcd.print('$ '+price, lcd.CENTER, 105, color=lcd.WHITE) # Symbol _offset = 175 @@ -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() - From c9e587d512154c66c3a31bc8cd26230119bbaa93 Mon Sep 17 00:00:00 2001 From: Valerio Vaccaro Date: Sat, 30 May 2020 00:57:08 +0200 Subject: [PATCH 2/2] fix differences --- examples/BTCTicker/BTCTicker_request.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/BTCTicker/BTCTicker_request.py b/examples/BTCTicker/BTCTicker_request.py index 4e7e7c8..bef0197 100644 --- a/examples/BTCTicker/BTCTicker_request.py +++ b/examples/BTCTicker/BTCTicker_request.py @@ -29,7 +29,7 @@ def timeout_reset(timer): import machine machine.reset() - + # def save_price_csv(filename, timestamp, price): # if utils.exists(filename): # with open(filename, 'a') as f: @@ -47,7 +47,7 @@ def main(): lcd.setBrightness(800) lcd.setTextColor(lcd.WHITE, lcd.BLACK) lcd.font('SFArch_48.fon') - lcd.print('BTC Price', lcd.CENTER, 30, lcd.ORANGE) + lcd.print('BTC Price', lcd.CENTER, 25, lcd.ORANGE) prev_price = '' timereset = time.ticks_ms() + (60*1000) while True: @@ -80,9 +80,9 @@ def main(): # Last Price price = btc_data['current_price'] if not price == prev_price: - lcd.rect(0, 105, 320, 48, lcd.BLACK, lcd.BLACK) + lcd.rect(0, 100, 320, 48, lcd.BLACK, lcd.BLACK) lcd.font('SFArch_48.fon') - lcd.print('$ '+price, lcd.CENTER, 105, color=lcd.WHITE) + lcd.print('$ '+price, lcd.CENTER, 100, color=lcd.WHITE) # Symbol _offset = 175