#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String apiKey = "H70JWWMLB2ZA2NEJ";
String stockSymbol = "RELIANCE.BSE";
// Graph settings
#define MAX_POINTS 120
float prices[MAX_POINTS];
int indexPos = 0;
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
float getStockPrice() {
HTTPClient http;
String url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=" + stockSymbol + "&apikey=" + apiKey;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
String priceStr = doc["Global Quote"]["05. price"];
return priceStr.toFloat();
}
http.end();
return 0;
}
void drawGraph() {
display.clearDisplay();
// Find min & max
float minVal = prices[0];
float maxVal = prices[0];
for (int i = 0; i < MAX_POINTS; i++) {
if (prices[i] < minVal) minVal = prices[i];
if (prices[i] > maxVal) maxVal = prices[i];
}
// Draw graph
for (int i = 0; i < MAX_POINTS - 1; i++) {
int x1 = i;
int x2 = i + 1;
int y1 = map(prices[i] * 100, minVal * 100, maxVal * 100, 63, 0);
int y2 = map(prices[i + 1] * 100, minVal * 100, maxVal * 100, 63, 0);
display.drawLine(x1, y1, x2, y2, WHITE);
}
display.display();
}
void loop() {
float price = getStockPrice();
if (price > 0) {
prices[indexPos] = price;
indexPos++;
if (indexPos >= MAX_POINTS) {
// Shift left
for (int i = 0; i < MAX_POINTS - 1; i++) {
prices[i] = prices[i + 1];
}
indexPos = MAX_POINTS - 1;
}
drawGraph();
}
delay(30000); // update every 30 sec
}