#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* weatherAPIKey = "ee6ba6810020699060c2af52ff377857";
const char* weatherAPIURL = "http://api.openweathermap.org/data/2.5/weather?q=Mumbai,IN,&appid=";

void setup() {
  Serial.begin(115200);
  delay(2000); // Delay for serial connection to be established
  Serial.println("Connecting to WiFi...");
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  
  Serial.println("Connected to WiFi");

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
}

void loop() {
  display.clearDisplay();

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = String(weatherAPIURL) + String(weatherAPIKey);
    
    http.begin(url);
    int httpResponseCode = http.GET();
    
    if (httpResponseCode > 0) {
      String payload = http.getString();
      JSONVar data = JSON.parse(payload);
      
      double temperature = data["main"]["temp"];
      temperature -= 273.15; // Convert from Kelvin to Celsius

      display.setCursor(0, 0);
      display.print("Temperature: ");
      display.print(temperature);
      display.print(" C");
    } else {
      display.setCursor(0, 0);
      display.println("Error getting weather");
    }
    
    http.end();
  } else {
    display.setCursor(0, 0);
    display.println("WiFi Disconnected");
  }

  display.display();
  delay(30000); // Delay for 30 seconds before fetching weather again
}
Loading
esp32-c3-devkitm-1