1212#include " freertos/FreeRTOS.h"
1313#include " freertos/timers.h"
1414#include < WiFiClientSecure.h>
15+ #include " API_TOKENS.h"
1516
1617const char *rootCACertificate =
1718 " -----BEGIN CERTIFICATE-----\n "
@@ -38,6 +39,21 @@ const char *rootCACertificate =
3839 " +OkuE6N36B9K\n "
3940 " -----END CERTIFICATE-----\n " ;
4041
42+ const char *FINN_HUB_ROOT_CA =
43+ " -----BEGIN CERTIFICATE-----\n "
44+ " MIICCTCCAY6gAwIBAgINAgPlwGjvYxqccpBQUjAKBggqhkjOPQQDAzBHMQswCQYD\n "
45+ " VQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEUMBIG\n "
46+ " A1UEAxMLR1RTIFJvb3QgUjQwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAwMDAw\n "
47+ " WjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2Vz\n "
48+ " IExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjQwdjAQBgcqhkjOPQIBBgUrgQQAIgNi\n "
49+ " AATzdHOnaItgrkO4NcWBMHtLSZ37wWHO5t5GvWvVYRg1rkDdc/eJkTBa6zzuhXyi\n "
50+ " QHY7qca4R9gq55KRanPpsXI5nymfopjTX15YhmUPoYRlBtHci8nHc8iMai/lxKvR\n "
51+ " HYqjQjBAMA4GA1UdDwEB/wQEAwIBhjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW\n "
52+ " BBSATNbrdP9JNqPV2Py1PsVq8JQdjDAKBggqhkjOPQQDAwNpADBmAjEA6ED/g94D\n "
53+ " 9J+uHXqnLrmvT/aDHQ4thQEd0dlq7A/Cr8deVl5c1RxYIigL9zC2L7F8AjEA8GE8\n "
54+ " p/SgguMh1YQdc4acLa/KNJvxn7kjNuK8YAOdgLOaVsjh4rsUecrNIdSUtUlD\n "
55+ " -----END CERTIFICATE-----\n " ;
56+
4157TaskHandle_t httpTaskHandle = NULL ;
4258// Add your includes here
4359// UI event
@@ -53,6 +69,7 @@ static TimerHandle_t http_timer = NULL;
5369String get_stock_price_yahoo (const char *symbol);
5470void get_stock_price_all_yahoo (void );
5571void connect_to_wifi_async (const char *ssid, const char *password);
72+ void http_get_all_stock_prices_finnhub (void );
5673
5774static uint8_t _gui_hide_loading = 0 ;
5875static uint8_t _gui_show_loading = 0 ;
@@ -235,12 +252,84 @@ void http_get_all_stock_prices(void)
235252{
236253 LV_LOG_USER (" HTTP GET all stock prices" );
237254 _gui_show_loading = 1 ;
238- get_stock_price_all_yahoo ();
255+ // get_stock_price_all_yahoo();
256+ http_get_all_stock_prices_finnhub ();
239257 _gui_hide_loading = 1 ;
240258 _gui_update_price = 1 ;
241259
242260}
243261
262+ void http_get_all_stock_prices_finnhub (void )
263+ {
264+ String sprice = " N/A" ; // Default value if fetching fails
265+ char (*symbols)[5 ] = gui_get_symbol_list ();
266+ char (*prices)[10 ] = gui_get_price_list ();
267+ if (WiFi.status () == WL_CONNECTED)
268+ {
269+ httpsClient->setCACert (FINN_HUB_ROOT_CA); // Set the root CA certificate
270+ httpsClient->setHandshakeTimeout (8000 ); // Set handshake timeout
271+ // httpsClient->setInsecure(); // Disable certificate validation for testing purposes (not recommended for production)
272+ for (int i = 0 ; i < 4 ; ++i)
273+ {
274+
275+ String symbol = String (symbols[i]); // Adjust based on your actual data structure
276+ Serial.printf (" Fetching price for symbol: %s\n " , symbol.c_str ());
277+ _http_status = " Fetching " + symbol + " ... " ;
278+ _gui_update_http_status = 1 ;
279+
280+ String url = String (" https://finnhub.io/api/v1/quote?symbol=" ) + symbol + " &token=" + String (FINN_HUB_API_TOKENS);
281+ Serial.println (url.c_str ());
282+ s_http.begin (*httpsClient, url);
283+ s_http.setUserAgent (" EchoapiRuntime/1.1.0" );
284+ s_http.setTimeout (5000 );
285+ // Start external timeout timer
286+ // start_http_timeout_timer(10000);
287+
288+ int httpCode = s_http.GET ();
289+ stop_http_timeout_timer ();
290+ if (httpCode == 200 )
291+ {
292+
293+ String payload = s_http.getString ();
294+ // Serial.println("Response: " + payload);
295+
296+ // Parse JSON
297+ JsonDocument doc;
298+ DeserializationError error = deserializeJson (doc, payload);
299+ if (!error)
300+ {
301+ float price = doc[" c" ];
302+ sprice = " $" + String (price, 2 ); // Format price to 2 decimal places
303+ strncpy (prices[i], sprice.c_str (), 10 ); // Store in prices array
304+ }
305+ else
306+ {
307+ Serial.println (" Failed to parse JSON." );
308+ }
309+ }
310+ else
311+ {
312+ _http_status = " HTTP GET failed, error:" + httpCode;
313+ _gui_update_http_status = 1 ;
314+ // gui_update_loading_status(status.c_str());
315+ Serial.printf (" HTTP GET failed, error: %d\n " , httpCode);
316+ }
317+ }
318+ s_http.end ();
319+ Serial.println (" Updated stock prices:" );
320+ for (int i = 0 ; i < 4 ; ++i)
321+ {
322+ Serial.printf (" Symbol: %s, Price: %s\n " , symbols[i], prices[i]);
323+ }
324+ }
325+ else
326+ {
327+ Serial.println (" WiFi not connected" );
328+ }
329+ }
330+
331+
332+
244333void https_get_all_stock_prices (void )
245334{
246335 String sprice = " N/A" ; // Default value if fetching fails
@@ -429,7 +518,6 @@ void http_request_task(void *parameter)
429518 ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
430519 LV_LOG_USER (" Starting HTTP request task" );
431520 // Perform HTTP request
432- // get_stock_price_yahoo("AAPL");
433521 http_get_all_stock_prices ();
434522
435523 }
0 commit comments