-
Notifications
You must be signed in to change notification settings - Fork 2
/
code.py
183 lines (156 loc) · 9.13 KB
/
code.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import time
import board
import busio
import displayio
from digitalio import DigitalInOut
import microcontroller
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
from adafruit_bitmap_font import bitmap_font
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
#from adafruit_pyportal import PyPortal
################### Global vars ########################################################
WHITE = 0xFFFFFF
BLACK = 0x000000
GREEN = 0x00FF00
RED = 0xFF0000
DARKRED = 0xBB0000
URL_BASE = "https://eodhistoricaldata.com/api/real-time/"
URL_APIKEY = "?api_token="
URL_FORMAT = "&fmt=json"
URL_ADDL_STOCKS = "&s="
URL1 = "https://eodhistoricaldata.com/api/real-time/AAPL?api_token=5e3667b5434e12.89123274&fmt=json"
URL2 = "https://eodhistoricaldata.com/api/real-time/GOOG?api_token=5e3667b5434e12.89123274&fmt=json"
URL3 = "https://eodhistoricaldata.com/api/real-time/TSLA?api_token=5e3667b5434e12.89123274&fmt=json"
NUM_LOOPS = 0 # Useful for debugging, tracks how many times we've successfully requested data
######## Get info from secrets.py ######################################################
try:
from secrets import secrets
except ImportError:
print("WiFi credentials, API keys, and assets tracked are kept in secrets.py, please add them there!")
raise
########### Set up display and load screen UI ##########################################
display = board.DISPLAY
display.rotation = 90
font = bitmap_font.load_font("fonts/LeagueSpartan-Bold-16.bdf")
loadscreen_group = displayio.Group()
loadscreen_label = label.Label(font, text="Connecting to WiFi...", scale=1, color=WHITE, x=50, y=150)
loadscreen_group.append(loadscreen_label)
display.show(loadscreen_group)
######## Wi-Fi setup ###################################################################
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp32 = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
requests.set_socket(socket, esp32)
if esp32.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Authenticating with WiFi...")
while not esp32.is_connected:
try:
esp32.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to WiFi, retrying: ", e)
continue
api_key = secrets['eodhistoricaldata_api_key']
######## UI setup ##############################################################
indent_top = 48
indent_label = 24
indent_price = 112
indent_change = 240
vert_spacing = 48
background_rect = Rect(0, 0, display.width, display.height, fill=BLACK)
pricedata_group = displayio.Group()
label_stockname1 = label.Label(font, text=secrets["stock1"], color=WHITE, x=indent_label, y=indent_top )
label_stockname2 = label.Label(font, text=secrets["stock2"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing) )
label_stockname3 = label.Label(font, text=secrets["stock3"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*2) )
label_stockname4 = label.Label(font, text=secrets["stock4"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*3) )
label_stockname5 = label.Label(font, text=secrets["stock5"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*4) )
label_stockname6 = label.Label(font, text=secrets["stock6"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*5) )
label_stockname7 = label.Label(font, text=secrets["stock7"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*6) )
label_stockname8 = label.Label(font, text=secrets["stock8"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*7) )
label_stockname9 = label.Label(font, text=secrets["stock9"], color=WHITE, x=indent_label, y=(indent_top + vert_spacing*8) )
label_stock1_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=indent_top)
label_stock2_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing))
label_stock3_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*2) )
label_stock4_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*3) )
label_stock5_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*4) )
label_stock6_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*5) )
label_stock7_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*6) )
label_stock8_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*7) )
label_stock9_price = label.Label(font, text="wait...", scale=1, color=WHITE, x=indent_price, y=(indent_top + vert_spacing*8) )
label_stock1_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=indent_top)
label_stock2_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing))
label_stock3_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*2) )
label_stock4_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*3) )
label_stock5_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*4) )
label_stock6_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*5) )
label_stock7_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*6) )
label_stock8_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*7) )
label_stock9_change = label.Label(font, text=str(""), scale=1, color=WHITE, x=indent_change, y=(indent_top + vert_spacing*8) )
pricedata_group.append(background_rect)
pricedata_group.append(label_stockname1)
pricedata_group.append(label_stockname2)
pricedata_group.append(label_stockname3)
pricedata_group.append(label_stockname4)
pricedata_group.append(label_stockname5)
pricedata_group.append(label_stockname6)
pricedata_group.append(label_stockname7)
pricedata_group.append(label_stockname8)
pricedata_group.append(label_stockname9)
pricedata_group.append(label_stock1_price)
pricedata_group.append(label_stock2_price)
pricedata_group.append(label_stock3_price)
pricedata_group.append(label_stock4_price)
pricedata_group.append(label_stock5_price)
pricedata_group.append(label_stock6_price)
pricedata_group.append(label_stock7_price)
pricedata_group.append(label_stock8_price)
pricedata_group.append(label_stock9_price)
pricedata_group.append(label_stock1_change)
pricedata_group.append(label_stock2_change)
pricedata_group.append(label_stock3_change)
pricedata_group.append(label_stock4_change)
pricedata_group.append(label_stock5_change)
pricedata_group.append(label_stock6_change)
pricedata_group.append(label_stock7_change)
pricedata_group.append(label_stock8_change)
pricedata_group.append(label_stock9_change)
display.show(pricedata_group)
def getprice(asset, pricelabel, changelabel):
try:
response = requests.get(URL_BASE + asset + URL_APIKEY + api_key + URL_FORMAT)
response_json = response.json()
price_unformatted = float(response_json["close"])
price_delta_unformatted = float(response_json["change_p"]) # get the percentage change, which can be many decimal places
price = "%.2f" % price_unformatted # Create a string from a float and round the price to 2 decimal places
price_delta = "%.1f" % price_delta_unformatted # Create a string from a float and round the price to 2 decimal places
if price_delta_unformatted >= 0:
pricelabel.color = GREEN
changelabel.color = GREEN
else:
pricelabel.color = RED
changelabel.color = RED
pricelabel.text = price
changelabel.text = price_delta + "%"
except (ValueError, RuntimeError) as e:
print("Error: ", e)
background_rect.fill = DARKRED
microcontroller.reset()
######## MAIN LOOP #####################################################################
while True:
getprice(secrets["stock1"], label_stock1_price, label_stock1_change)
getprice(secrets["stock2"], label_stock2_price, label_stock2_change)
getprice(secrets["stock3"], label_stock3_price, label_stock3_change)
getprice(secrets["stock4"], label_stock4_price, label_stock4_change)
getprice(secrets["stock5"], label_stock5_price, label_stock5_change)
getprice(secrets["stock6"], label_stock6_price, label_stock6_change)
getprice(secrets["stock7"], label_stock7_price, label_stock7_change)
getprice(secrets["stock8"], label_stock8_price, label_stock8_change)
getprice(secrets["stock9"], label_stock9_price, label_stock9_change)
NUM_LOOPS += 1
print("Loops=" + str(NUM_LOOPS) )
time.sleep(4400)