/*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
/*
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const String url = "https://v2.jokeapi.dev/joke/Programming";
*/
//===========================================
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//#include <Arduino_JSON.h>
// Replace with your WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Replace with your Alpha Vantage API key
const char* apiKey = "4OTQ3C8G33T6FRXI";
#define TFT_DC 2
#define TFT_CS 15
int BTN_PIN = 5;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// TFT Display setup
//#define TFT_CS 5
//#define TFT_DC 4
//#define TFT_RST 22
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Stock data variables
const char* symbol = "AAPL";
const int windowSize = 10;
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.begin(115200);
// Connect to Wi-Fi
connectToWiFi();
// Initialize TFT display
tft.begin();
tft.setRotation(1); // Adjust the rotation if needed
// Fetch and display stock data
fetchAndDisplayStockData();
}
void loop() {
// Code in loop if needed
if(digitalRead(BTN_PIN) == LOW){
fetchAndDisplayStockData();
Serial.println("Button Pressed, Fetching Data");
}
delay(1000);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
void fetchAndDisplayStockData() {
// Make HTTP request to Alpha Vantage
//String url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + String(symbol) + "&apikey=" + String(apiKey);
String url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=AAPL&apikey=4OTQ3C8G33T6FRXI";
//'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo'
HTTPClient http;
http.begin(url);
Serial.println("Making HTTP request to Alpha Vantage...");
Serial.print("URL: ");
Serial.println(url);
int httpCode = http.GET();
Serial.print("HTTP Response code: ");
Serial.println(httpCode);
//int httpCode = http.GET();
if (httpCode > 0 ) {
String payload = http.getString();
// Parse JSON response using ArduinoJson
const size_t bufferSize = 2 * JSON_ARRAY_SIZE(windowSize) + JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
// Extract closing prices
float closingPrices[windowSize];
for (int i = 0; i < windowSize; i++) {
String date = doc["Time Series (Daily)"][i]["4. close"];
closingPrices[i] = date.toFloat();
}
// Calculate moving average
float movingAverage = 0;
for (int i = 0; i < windowSize; i++) {
movingAverage += closingPrices[i];
}
movingAverage /= windowSize;
// Display graph on TFT
displayGraph(closingPrices, movingAverage);
}
else {
Serial.println("Error parsing JSON");
}
}
else {
Serial.println("Error in HTTP request");
}
http.end();
}
void displayGraph(float closingPrices[], float movingAverage) {
tft.fillScreen(ILI9341_BLACK);
int graphWidth = 200;
int graphHeight = 200;
int margin = 10;
int yAxisOffset = 10;
// Draw x-axis
tft.drawLine(margin, graphHeight + margin, graphWidth + margin, graphHeight + margin, ILI9341_WHITE);
// Draw y-axis
tft.drawLine(margin, margin, margin, graphHeight + margin, ILI9341_WHITE);
// Plot closing prices
int barWidth = (graphWidth - 2 * margin) / windowSize;
for (int i = 0; i < windowSize; i++) {
int barHeight = map(closingPrices[i], 0, 300, 0, graphHeight);
tft.fillRect(i * barWidth + margin, graphHeight + margin - barHeight, barWidth, barHeight, ILI9341_GREEN);
Serial.print("Bar ");
Serial.print(i);
Serial.print(": Height=");
Serial.println(barHeight);
}
// Plot moving average
int averageHeight = map(movingAverage, 0, 300, 0, graphHeight);
tft.drawLine(margin, graphHeight + margin - averageHeight, graphWidth + margin, graphHeight + margin - averageHeight, ILI9341_RED);
Serial.print("Moving Average Height=");
Serial.println(averageHeight);
// Display labels
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(margin / 2, graphHeight + margin + yAxisOffset);
tft.print("0");
tft.setCursor(margin / 2, margin);
tft.print("300");
tft.setCursor(graphWidth + margin / 2, graphHeight + margin + yAxisOffset);
tft.print("Date");
tft.setCursor(margin / 2, margin - 10);
tft.print("Stock Price");
// Display legend
tft.setCursor(graphWidth + margin + 10, margin);
tft.setTextColor(ILI9341_GREEN);
tft.print("Closing Prices");
tft.setCursor(graphWidth + margin + 10, margin + 20);
tft.setTextColor(ILI9341_RED);
tft.print("Moving Average");
}