#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Time API endpoint
const char* timeApiUrl = "http://worldtimeapi.org/api/timezone/Europe/Bucharest";
void setup() {
Serial.begin(115200);
Wire.begin(13, 14);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
display.clearDisplay();
// Fetch current time from the time API
String currentTimeStr = getTimeFromApi();
// Parse the JSON response
const size_t capacity = JSON_OBJECT_SIZE(15) + 400;
// JSON_OBJECT_SIZE(2) + 100;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, currentTimeStr);
// Extract time values
String datetime = doc["datetime"];
String zian = doc["day_of_year"];
String week = doc["week_number"];
// Serial.print(datetime);
//Serial.println(timezone);
// Adjust the time to the Karachi time zone (GMT+5)
const char* karachiTimeZone = "Europe/Bucharest";
// int hours = datetime.substring(11, 13).toInt();
// int minutes = datetime.substring(14, 16).toInt();
// int seconds = datetime.substring(17, 19).toInt();
int hours, minutes, seconds;
parseTime(datetime, hours, minutes, seconds);
Serial.print("Hours: ");
Serial.println(hours);
Serial.print("Minutes: ");
Serial.println(minutes);
Serial.print("Seconds: ");
Serial.println(seconds);
Serial.print("Ziua din an: ");
Serial.println(zian);
Serial.print("Nr.sapt: ");
Serial.println(week);
// Display hours and minutes with text size 3
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5, 10); // Adjust the position as needed
if (hours < 10) {
display.print("0"); // Add leading zero for single-digit hours
}
display.print(hours);
display.print(":");
if (minutes < 10) {
display.print("0"); // Add leading zero for single-digit minutes
}
display.print(minutes);
// Display seconds with text size 2
display.setTextSize(2);
display.setCursor(100, 15); // Adjust the position as needed
if (seconds < 10) {
display.print("0"); // Add leading zero for single-digit seconds
}
display.print(seconds);
display.display();
delay(10000); // Update the display every second
}
String getTimeFromApi() {
WiFiClient client;
// Make a GET request to the time API
HTTPClient http;
http.begin(client, timeApiUrl); // Use begin with WiFiClient
//Serial.print('timeApiUrl');
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
//Serial.println(payload);
http.end();
return payload;
}
} else {
Serial.println("Failed to connect to time API");
}
http.end();
return "";
}
void parseTime(const String &timeStr, int &hours, int &minutes, int &seconds) {
// Find the position of 'T' in the string
int tIndex = timeStr.indexOf('T');
if (tIndex != -1) {
// Extract the time substring after 'T'
String timeSubStr = timeStr.substring(tIndex + 1);
// Find the position of '+' in the time substring
int plusIndex = timeSubStr.indexOf('+');
if (plusIndex != -1) {
// Extract the time part before '+'
String timePart = timeSubStr.substring(0, plusIndex);
// Split the time part into hours, minutes, and seconds
hours = timePart.substring(0, 2).toInt();
minutes = timePart.substring(3, 5).toInt();
seconds = timePart.substring(6, 8).toInt();
}
}
}