-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_symbol_price_to_display.py
169 lines (139 loc) · 4.88 KB
/
get_symbol_price_to_display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import network
import urequests as requests
from machine import Pin, SPI
import newframebuf
import epaper4in2 as epaper
import sys
import ntptime
import utime
import sleepscheduler as sl
sys.path.append('.')
WIFI_SSID = "HIWIFI"
WIFI_PASSWORD = "password"
miso = Pin(0)
mosi = Pin(19)
sck = Pin(18)
cs = Pin(12)
dc = Pin(0)
rst = Pin(17)
busy = Pin(32)
spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=sck, miso=miso, mosi=mosi)
e = epaper.EPD(spi, cs, dc, rst, busy)
e.init()
w = 300
h = 400
buf = bytearray(w * h // 8) # 400 * 300 / 8 = 15000 - thats a lot of pixels
fb = newframebuf.FrameBuffer(buf, h, w, newframebuf.MHMSB)
fb.rotation = 0 # 调整显示的方向,可以在0/1/2/3之间选择
def get_symbol_price():
try:
headers = {'User-Agent': 'Mozilla/5.0'}
res = requests.request('GET', 'https://hope.money/hope-index-stage-2?period=3600', headers=headers)
"""
{
"hope_price_list": [
[
0.4925987029447829,
1686139200
],
[
0.49105454893639217,
1686140724
]
],
"btc_index_price": 26801.25,
"eth_index_price": 1865.9166666666667,
"hope_index_price": 0.49105454893639217,
"k": 0.00001080180484347501
}
"""
response = res.json()
btc_price = response['btc_index_price']
eth_price = response['eth_index_price']
hope_price = response['hope_index_price']
ts = response['hope_price_list'][1][1] # 1686140724
return btc_price, eth_price, hope_price, ts
except:
return '--', '--', '--', 0
def get_lt_price():
try:
chain_id = 'ethereum'
pair_address = '0xa9ad6a54459635a62e883dc99861c1a69cf2c5b3' # LT / USDT
# pair_address = '0x1c2ad915cd67284cdbc04507b11980797cf51b22' # HOPE / USDT
# pair_address = '0x11b815efb8f581194ae79006d24e0d814b7697f6' # WETH / USDT
# pair_address = '0x9db9e0e53058c89e5b94e29621a205198648425b' # WBTC / USDT
resp = requests.request('GET',
'https://api.dexscreener.com/latest/dex/pairs/{}/{}'.format(chain_id, pair_address))
lt_last_price = resp.json()['pair']['priceUsd']
return lt_last_price
except:
return '--'
def get_bgb_price():
try:
resp = requests.get("https://api.bitget.com/api/spot/v1/market/ticker?symbol=BGBUSDT_SPBL")
last_price = resp.json()['data']['close']
return last_price
except:
return '--'
def wifi_connect():
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
print("connecting to network")
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
print(".", end="")
utime.sleep(1)
print('network config:', wlan.ifconfig())
def display():
# 检查WiFi
wifi_connect()
btc, eth, hope, ts = get_symbol_price()
lt = get_lt_price()
# todo 上面两个接口可以整合为一个,可以都从dex screener上获取,但是需要考虑到HOPE的价格在dex screener可能有延迟
bgb = get_bgb_price()
# 获取当前时间
"""
utime.localtime([secs]):
year includes the century (for example 2014).
month is 1-12
mday is 1-31
hour is 0-23
minute is 0-59
second is 0-59
weekday is 0-6 for Mon-Sun
yearday is 1-366
"""
# 这里需要的ts时间戳是以模块rtc时钟初始值以起点计算的秒数
# 如esp32模块的rtc初始时钟是 2000年1月1日
# 所以1970年时间戳 需要转换为 rtc时间戳: 1686140724 - 946656000
year, month, day, hours, minutes, seconds, weekday, yearday = utime.localtime(ts - 946656000)
# 格式化时间字符串
time_str = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}".format(year, month, day, hours, minutes)
black = 0
white = 1
fb.fill(white)
fb.text(time_str, 60, 10, black, size=3)
fb.text('BTC: {}'.format(btc), 30, 60, black, size=3)
fb.text('ETH: {}'.format(eth), 30, 100, black, size=3)
fb.text('HOPE: {}'.format(hope), 30, 140, black, size=3)
fb.text('LT: {}'.format(lt), 30, 180, black, size=3)
fb.text('BGB: {}'.format(bgb), 30, 220, black, size=3)
fb.rect(0, 40, 400, 4, black, fill=True) # line: (x, y, width, 1, color, fill=True)
# fb.circle(50, 150, 10, black)
e.display_frame(buf)
# def calibration_time():
# year, *_ = utime.localtime()
# if not str(year).startswith('202'): # default is 2000-01-01 00:00:00
# # set the time from the network
# ntptime.host = "ntp1.aliyun.com"
# ntptime.NTP_DELTA = 3155644800 # 东八区 UTC+8偏移时间(秒)
# ntptime.settime()
# print("calibration_time to: {}".format(utime.localtime()))
def init_on_cold_boot():
# configure and connect WLAN
# wifi_connect()
# the time is kept during deep sleep
# 检查时间
# calibration_time()
sl.schedule_immediately(__name__, display, 60 * 5)