Skip to content

Commit d016c80

Browse files
author
ladyada
committed
stonx
1 parent e89b43e commit d016c80

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

CircuitStonks/code.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
from adafruit_esp32spi import adafruit_esp32spi
6+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
7+
from adafruit_featherwing import minitft_featherwing
8+
import terminalio
9+
from adafruit_display_text import label
10+
import displayio
11+
12+
minitft = minitft_featherwing.MiniTFTFeatherWing()
13+
14+
# Get wifi details and more from a secrets.py file
15+
try:
16+
from secrets import secrets
17+
except ImportError:
18+
print("WiFi secrets are kept in secrets.py, please add them there!")
19+
raise
20+
21+
# If you are using a board with pre-defined ESP32 Pins:
22+
esp32_cs = DigitalInOut(board.D13)
23+
esp32_ready = DigitalInOut(board.D11)
24+
esp32_reset = DigitalInOut(board.D12)
25+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
26+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
27+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
28+
29+
# Symbol "INX" for S&P500, "DJIA" for Dow
30+
DATA_SOURCE = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&apikey="
31+
DATA_SOURCE += secrets['alphavantage_key']
32+
symbols = ["DJIA", "INX", "AAPL", "TSLA", "MSFT"]
33+
34+
# Set text, font, and color
35+
group = displayio.Group()
36+
symbol_text = label.Label(terminalio.FONT, text="WiFi", color=0xFFFFFF,
37+
scale=3, max_glyphs=10)
38+
symbol_text.anchor_point = (0.0, 0.0)
39+
symbol_text.anchored_position = (0, 10)
40+
41+
price_text = label.Label(terminalio.FONT, text="Connect...", color=0xFFFF00,
42+
scale=3, max_glyphs=10)
43+
price_text.anchor_point = (0, 1)
44+
price_text.anchored_position = (0, 75)
45+
46+
change_text = label.Label(terminalio.FONT, text="", color=0xFFFF00,
47+
scale=2, max_glyphs=10)
48+
change_text.anchor_point = (0, 0)
49+
change_text.anchored_position = (80, 10)
50+
51+
group.append(symbol_text)
52+
group.append(price_text)
53+
group.append(change_text)
54+
minitft.display.show(group)
55+
56+
refresh = None
57+
i = 0
58+
while True:
59+
# only query the api every 10 sec (and on first run)
60+
if (not refresh) or ((time.monotonic() - refresh) > 10):
61+
try:
62+
symbol = symbols[i]
63+
i = (i + 1) % len(symbols) # go to the next symbol
64+
response = wifi.get(DATA_SOURCE+"&symbol="+symbol).json()
65+
print("Response is", response)
66+
symbol_text.text = response['Global Quote']['01. symbol']
67+
spot = round(float(response['Global Quote']['05. price']))
68+
price_text.text = '${:,d}'.format(spot)
69+
change = float(response['Global Quote']['09. change'])
70+
print("Price is $", spot, "Change: $", change)
71+
if change >= 0:
72+
change_text.text = '+${:,d}'.format(round(change))
73+
change_text.color = 0x00FF00
74+
else:
75+
change_text.text = '-${:,d}'.format(abs(round(change)))
76+
change_text.color = 0xFF0000
77+
refresh = time.monotonic()
78+
except RuntimeError as e:
79+
print("Some error occured, retrying! -", e)
80+
time.sleep(5)
81+
continue
82+
time.sleep(5)

CircuitStonks/secrets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
secrets = {
3+
'ssid' : 'myssid',
4+
'password' : 'mypassword',
5+
'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
6+
'alphavantage_key' : 'GRABAFREEKEYONLINE'
7+
}

0 commit comments

Comments
 (0)