-
Notifications
You must be signed in to change notification settings - Fork 0
/
vix_ticker.ino
239 lines (196 loc) · 6.2 KB
/
vix_ticker.ino
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**************
PINOUT ESP32 -> ST7735
GND -> GND
VCC -> 3.3
SCL -> D18 (SCLK)
SDA -> D23 (MISO)
RES -> D16
DC -> D15
CS -> D2
BL -> D12
PINOUT ESP32 -> BUTTON
LEG1 -> GND
LEG2 -> D13
**************/
// Network libraries
#include <WiFi.h>
#include <HTTPClient.h>
// Graphic libraries
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <esp_sleep.h>
// Pin definition
#define TFT_CS 2
#define TFT_DC 15
#define TFT_RST 16
// WIFI credentials
const char* ssid = "<SSID>";
const char* password = "<PASSWORD>";
// VIX values variables
String change = "0.00%";
double change_double;
int change_index;
String sell_price = "0.00";
double sell_price_double;
int sell_price_index;
String buy_price = "0.00";
double buy_price_double;
int buy_price_index;
String high_price = "0.00";
int high_price_index;
String low_price = "0.00";
int low_price_index;
// Site to get data
String serverPath = "https://www.plus500.com/pl/instruments/vix/";
// LCD intialization
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
const int backlightPin = 12;
bool backlightState = 1;
const int buttonPin = 13;
const int longPressDuration = 1000;
unsigned long buttonPressStartTime = 0;
int16_t x, y;
uint16_t w, h;
// Internal timer
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(backlightPin, OUTPUT);
digitalWrite(backlightPin, HIGH);
setup_lcd();
setup_wifi();
}
void loop() {
get_vix();
int reading = digitalRead(buttonPin);
if (reading == LOW) {
if (buttonPressStartTime == 0) {
buttonPressStartTime = millis();
}
if (millis() - buttonPressStartTime >= longPressDuration) {
delay(1000);
if (backlightState == 1) {
digitalWrite(backlightPin, LOW);
backlightState = 0;
} else {
digitalWrite(backlightPin, HIGH);
backlightState = 1;
}
}
} else {
buttonPressStartTime = 0;
}
}
void setup_wifi(){
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void setup_lcd(){
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST77XX_BLACK);
//Header
tft.setTextColor(ST77XX_WHITE);
tft.setFont(&FreeSansBold12pt7b);
tft.getTextBounds("VIX", 0, 0, &x, &y, &w, &h);
tft.setCursor((tft.width() - w) / 2, 25);
tft.print("VIX");
//Initialize
tft.setCursor(3, 140);
tft.print("Initializing");
}
void get_vix(){
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED) {
HTTPClient http;
http.begin(serverPath.c_str());
http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
//Scrape buy price
buy_price_index = payload.indexOf("buyPrice");
buy_price = payload.substring(buy_price_index, buy_price_index+20);
buy_price = buy_price.substring(buy_price.indexOf(">")+1, buy_price.indexOf("<"));
// Scrape sell price
sell_price_index = payload.indexOf("sellPrice");
sell_price = payload.substring(sell_price_index, sell_price_index+20);
sell_price = sell_price.substring(sell_price.indexOf(">")+1, sell_price.indexOf("<"));
//Scrape daily change
change_index = payload.indexOf("changePercentText");
change = payload.substring(change_index, change_index+40);
change = change.substring(change.indexOf(">")+1, change.indexOf("<"));
//Scrape high price
high_price_index = payload.indexOf("highPrice");
high_price = payload.substring(high_price_index, high_price_index+40);
high_price = high_price.substring(high_price.indexOf(">")+1, high_price.indexOf("<"));
//Scrape low price
low_price_index = payload.indexOf("lowPrice");
low_price = payload.substring(low_price_index, low_price_index+40);
low_price = low_price.substring(low_price.indexOf(">")+1, low_price.indexOf("<"));
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
//Buy banner
tft.drawRect(0, 35, 54, 40, ST77XX_GREEN);
tft.fillRect(0, 35, 54, 40, ST77XX_GREEN);
tft.drawTriangle(54, 35, 54, 74, 93, 35, ST77XX_GREEN);
tft.fillTriangle(54, 35, 54, 74, 93, 35, ST77XX_GREEN);
//Sell banner
tft.drawRect(74, 70, 54, 40, ST77XX_RED);
tft.fillRect(74, 70, 54, 40, ST77XX_RED);
tft.drawTriangle(73, 70, 73, 109, 34, 109, ST77XX_RED);
tft.fillTriangle(73, 70, 73, 109, 34, 109, ST77XX_RED);
tft.drawRect(0, 115, 128, 45, ST77XX_BLACK);
tft.fillRect(0, 115, 128, 45, ST77XX_BLACK);
tft.setFont(&FreeSansBold12pt7b);
//Buy price
tft.getTextBounds(buy_price.c_str(), 0, 0, &x, &y, &w, &h);
tft.setCursor((tft.width()/2 - w) / 2, 35+h+((40-h)/2));
tft.print(buy_price.c_str());
//Sell price
tft.getTextBounds(sell_price.c_str(), 0, 0, &x, &y, &w, &h);
tft.setCursor(tft.width()/2 + (tft.width()/2 - w)/2-3, 109-(40-h)/2);
tft.print(sell_price.c_str());
//Change
if (change.indexOf("-") == 0) {
tft.setTextColor(ST77XX_RED);
} else if (change == "0.00%") {
tft.setTextColor(ST77XX_WHITE);
} else {
tft.setTextColor(ST77XX_GREEN);
}
tft.getTextBounds(change.c_str(), 0, 0, &x, &y, &w, &h);
tft.setCursor((tft.width() - w) / 2, 135);
tft.print(change.c_str());
tft.setTextColor(ST77XX_WHITE);
tft.setFont();
//High text
tft.setFont();
tft.setCursor(3, 140);
tft.print("HIGH");
//Low text
tft.setCursor(108, 140);
tft.print("LOW");
//High Value
tft.setCursor(2, 150);
tft.print(high_price.c_str());
//Low Value
tft.getTextBounds(low_price.c_str(), 0, 0, &x, &y, &w, &h);
tft.setCursor(tft.width()-w-1, 150);
tft.print(low_price.c_str());
} else {
WiFi.reconnect();
}
lastTime = millis();
}
}