#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
// Constants for OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Constants for Wi-Fi
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// Weather API Settings
char weatherAPI[255]; // Store the weather API URL
char apiKey[50]; // Store your API key here
char location[50]; // Store the location here
void setup() {
Serial.begin(9600);
// Set your API key and location here
strcpy(apiKey, "Tomorrow.io API"); // Replace with your Tomorrow.io API key
strcpy(location, "location"); // Replace with your desired location
sprintf(weatherAPI, "https://api.tomorrow.io/v4/weather/realtime?location=%s&apikey=%s", location, apiKey);
// Initialize Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) {
// Don't proceed, loop forever
}
}
// Clear the display buffer
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setTextSize(2); // Set text size
}
void loop() {
// Fetch weather data from the API
HTTPClient http;
http.begin(weatherAPI);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
// Parsing successful, you can access JSON data here
const char* data_time = doc["data"]["time"];
JsonObject data_values = doc["data"]["values"];
float data_values_temperature = data_values["temperature"];
int data_values_humidity = data_values["humidity"];
int data_values_precipitationProbability = data_values["precipitationProbability"];
float data_values_windSpeed = data_values["windSpeed"];
// And so on for other fields...
// Clear the display buffer
display.clearDisplay();
// Display data on OLED with improved styling
display.setCursor(0, 0);
display.setTextColor(SSD1306_WHITE);
display.print("Temp: ");
display.setTextSize(2);
display.print(data_values_temperature, 1);
display.setTextSize(1);
display.println("°C");
display.print("Humidity: ");
display.print(data_values_humidity);
display.println("%");
display.print("Precipitation: ");
display.print(data_values_precipitationProbability);
display.println("%");
display.print("Wind Speed: ");
display.print(data_values_windSpeed, 1);
display.println("k/h");
display.setTextSize(1);
display.print("Last Update: ");
display.println(data_time);
// Display other data fields as needed
// Display the content
display.display();
} else {
Serial.println("JSON parsing failed");
}
} else {
Serial.println("HTTP request failed");
}
http.end();
// Wait for some time before checking the API again (e.g., every 30 minutes)
delay(1800000);
}