-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266_StockTicker.ino
163 lines (140 loc) · 4.87 KB
/
ESP8266_StockTicker.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
/*
ESP STOCK TICKER
CREATED BY BILLUPS TILLMAN
https://github.com/BeeTillman
FOR INSTRUCTIONS ON HOW TO USE,
NAVIGATE TO THE README
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <Math.h>
#include "lcd_functions.h"
#include "keys.h"
#define TEST_HOST "www.finnhub.io"
#define ssid ssidKey // Network SSID (Defined in keys header)
#define password passwordKey // Network Password (Defined in keys header)
#define TEST_HOST_FINGERPRINT fingerprintKey // Chrome fingerprint (Defined in keys header)
#define apikey apiKey // Finnhub.io API Key (Defined in keys header)
#define SYMBOL1 "FIVN" // You can add as many symbol as you want. Simply copy the line, increment SYMBOL#, and add a getData function followed by a delay in the loop
#define SYMBOL2 "AMZN"
#define SYMBOL3 "NVDA"
#define SYMBOL4 "AAPL"
#define SYMBOL5 "TSLA"
#define stockRotationDelay 17500 // Delay between switching of stock ticker display
WiFiClientSecure client;
void setup() {
Serial.begin(112500); //esp8266 default baud rate
delay(500);
initLCD();
displayInitialStartup(); // Display welcome screen
client.setInsecure(); // Resolves wifi error due to bad port
}
void loop() {
connectToWiFi(); // Periodically Reconnect to WiFi. This will display the "Reconnecting" notice.
getData(SYMBOL1); // Display Ticker for input symbol
delay(stockRotationDelay); // Delay between stocks
getData(SYMBOL2);
delay(stockRotationDelay);
getData(SYMBOL3);
delay(stockRotationDelay);
getData(SYMBOL4);
delay(stockRotationDelay);
getData(SYMBOL5);
delay(stockRotationDelay);
}
void getData(String ticker){
Serial.println("Making a HTTP Request");
if (!client.connect(TEST_HOST, 443)) // Opening connection to server (Use 80 as port if HTTP)
{
Serial.println(F("Connection failed"));
}
yield(); // Give the ESP8266 a break
client.print(F("GET ")); // Send HTTP request
client.print("/api/v1/quote?symbol="+ticker+"&token="+apikey); // This is the second half of a request (everything that comes after the base URL)
client.println(F(" HTTP/1.1"));
//Headers
client.print(F("Host: "));
client.println(TEST_HOST);
client.println(F("Cache-Control: no-cache"));
if (client.println() == 0)
{
Serial.println(F("Failed to send request"));
}
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "HTTP/1.1 200 OK") != 0)
{
Serial.print(F("Unexpected response: "));
Serial.println(status);
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders))
{
Serial.println(F("Invalid response"));
}
// This is probably not needed for most, but I had issues
// with the Tindie api where sometimes there were random
// characters coming back before the body of the response.
// This will cause no hard to leave it in
// peek() will look at the character, but not take it off the queue
while (client.available() && client.peek() != '{')
{
char c = 0;
client.readBytes(&c, 1);
Serial.print(c);
Serial.println("BAD");
}
// While the client is still availble read each
// byte and print to the serial monitor
// This is kind of unnecessary but I added the for loop
// to limit API calls that return huge amounts of data
String apiResult = "";
for (int i=0; i<600; i++){
if (client.available()) {
char c = 0;
client.readBytes(&c, 1);
Serial.print(c);
apiResult+=c;
}
}
// We now have a String called apiResult, which contains the first
// 600 characters that were returned from our API call.
// We now throw apiResult into a JSON document so we can index it.
Serial.println(apiResult);
DynamicJsonDocument doc(1024);
deserializeJson(doc, apiResult);
JsonObject obj = doc.as<JsonObject>();
String currentPrice = obj["c"].as<String>();
String pchange = obj["dp"].as<String>();
float float_pchange = pchange.toFloat();
displayStockResult(ticker, float_pchange, currentPrice);
}
void connectToWiFi(){
if (WiFi.status() != WL_CONNECTED) {
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // Wireless network not connected
Serial.println("");
Serial.println("Awaiting WiFi Connection.");
Serial.println("Update Environment Variables if Necessary");
displayReconnectingCycle(); // Display reconnecting notice on LCD
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
displayWifiSuccess(); // Display wifi connection success on LCD
delay(1000);
IPAddress ip = WiFi.localIP();
Serial.println(ip);
client.setFingerprint(TEST_HOST_FINGERPRINT);
}
else {
Serial.println("Network Test Successful.");
}
}