#include <WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
// WiFi bilgileri
const char* ssid = "Buraya Wifi SSID";
const char* password = "Buraya Wifi Şifresi";
// NTP Sunucusu ve zaman ayarları
const char* ntpServerName = "pool.ntp.org";
const int timeZone = 3; // Türkiye: UTC+3
const unsigned long seventyYears = 2208988800UL;
// Namaz vakti API (HTTP)
const char* prayerHost = "api.aladhan.com";
int prayerPort = 80;
String prayerPath = "/v1/timingsByCity/13-12-2024?city=Ankara&country=TR&method=13";
// LCD nesnesi (I2C)
hd44780_I2Cexp lcd;
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
WiFiUDP udp;
unsigned int localPort = 2390;
String currentDate = "Yok";
String currentTime = "Yok";
String nextPrayerName = "";
int nextPrayerRemainingMinutes = -1;
const char* trMonths[13] = {
"", "Ocak", "Subat", "Mart", "Nisan", "Mayis", "Haziran",
"Temmuz", "Agustos", "Eylul", "Ekim", "Kasim", "Aralik"
};
int timeToMinutes(String timeStr) {
// Namaz vakitleri "HH:MM" formatında gelir
int idx = timeStr.indexOf(':');
if (idx < 0) return -1;
int hour = timeStr.substring(0, idx).toInt();
int min = timeStr.substring(idx+1).toInt();
return hour * 60 + min;
}
void findNextPrayer(String nowTime, String fajr, String sunrise, String dhuhr, String asr, String maghrib, String isha) {
int nowMinutes = timeToMinutes(nowTime);
if (nowMinutes < 0) {
nextPrayerRemainingMinutes = -1;
return;
}
int fajrM = timeToMinutes(fajr);
int dhuhrM = timeToMinutes(dhuhr);
int asrM = timeToMinutes(asr);
int maghribM = timeToMinutes(maghrib);
int ishaM = timeToMinutes(isha);
struct Prayer {
String name;
int timeM;
} prayers[5] = {
{"Ogle", dhuhrM},
{"Ikindi", asrM},
{"Aksam", maghribM},
{"Yatsi", ishaM},
{"Sabah", fajrM}
};
nextPrayerName = "";
nextPrayerRemainingMinutes = -1;
for (int i = 0; i < 5; i++) {
int diff = prayers[i].timeM - nowMinutes;
if (diff > 0) {
// En küçük pozitif farkı bul
if (nextPrayerRemainingMinutes < 0 || diff < nextPrayerRemainingMinutes) {
nextPrayerName = prayers[i].name;
nextPrayerRemainingMinutes = diff;
}
}
}
if (nextPrayerRemainingMinutes < 0 && fajrM >= 0) {
int diffNextDay = fajrM + 1440 - nowMinutes;
nextPrayerName = "Sabah";
nextPrayerRemainingMinutes = diffNextDay;
}
}
String getTurkishDateFromCurrent() {
if (currentDate == "Yok") return "Yok";
int firstDash = currentDate.indexOf('-');
int secondDash = currentDate.lastIndexOf('-');
if (firstDash < 0 || secondDash < 0 || firstDash == secondDash) {
return "Yok";
}
String yearStr = currentDate.substring(0, firstDash);
String monthStr = currentDate.substring(firstDash+1, secondDash);
String dayStr = currentDate.substring(secondDash+1);
int day = dayStr.toInt();
int month = monthStr.toInt();
int year = yearStr.toInt();
if (month < 1 || month > 12) return "Yok";
return String(day) + "." + String(month) + "." + String(year);
}
void sendNTPpacket(const char *address) {
byte packetBuffer[48];
memset(packetBuffer, 0, 48);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
udp.beginPacket(address, 123);
udp.write(packetBuffer, 48);
udp.endPacket();
}
void getNTPTime() {
udp.begin(localPort);
sendNTPpacket(ntpServerName);
delay(1000);
int cb = udp.parsePacket();
if (!cb) {
Serial.println("NTP yanit yok");
return;
}
byte packetBuffer[48];
udp.read(packetBuffer, 48);
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
unsigned long epoch = secsSince1900 - seventyYears;
epoch += timeZone * 3600;
int year = 1970;
unsigned long dayCount = epoch / 86400UL;
unsigned long secondsInDay = epoch % 86400UL;
int hour = secondsInDay / 3600;
int minute = (secondsInDay % 3600) / 60;
int second = secondsInDay % 60;
while (true) {
bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
int daysInYear = leap ? 366 : 365;
if (dayCount >= (unsigned long)daysInYear) {
dayCount -= daysInYear;
year++;
} else {
break;
}
}
static const int monthDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int month = 0;
bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
for (int i = 0; i < 12; i++) {
int daysInMonth = monthDays[i];
if (i == 1 && leap) daysInMonth = 29;
if ((int)dayCount >= daysInMonth) {
dayCount -= daysInMonth;
month++;
} else {
break;
}
}
int day = dayCount + 1;
month += 1;
char dateStr[11];
snprintf(dateStr, sizeof(dateStr), "%04d-%02d-%02d", year, month, day);
currentDate = dateStr;
char timeStr[9];
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", hour, minute);
currentTime = timeStr; // Saniyeye gerek yok, HH:MM yeterli
Serial.print("NTP ile Alinan Zaman: ");
Serial.print(currentDate);
Serial.print(" ");
Serial.println(currentTime);
}
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi Baglaniliyor...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi baglandi!");
delay(1000);
}
void loop() {
getNTPTime();
String turkishDate = getTurkishDateFromCurrent();
// Namaz vakitlerini al
{
WiFiClient prayerClient;
HttpClient pClient(prayerClient, prayerHost, prayerPort);
pClient.get(prayerPath);
int prayerStatusCode = pClient.responseStatusCode();
Serial.print("Namaz API Status: ");
Serial.println(prayerStatusCode);
if (prayerStatusCode == 200) {
String prayerResponse = pClient.responseBody();
Serial.println("Namaz Response:");
Serial.println(prayerResponse);
DynamicJsonDocument prayerDoc(8192);
DeserializationError pError = deserializeJson(prayerDoc, prayerResponse);
if (!pError) {
String fajr = (const char*)prayerDoc["data"]["timings"]["Fajr"];
String sunrise = (const char*)prayerDoc["data"]["timings"]["Sunrise"];
String dhuhr = (const char*)prayerDoc["data"]["timings"]["Dhuhr"];
String asr = (const char*)prayerDoc["data"]["timings"]["Asr"];
String maghrib = (const char*)prayerDoc["data"]["timings"]["Maghrib"];
String isha = (const char*)prayerDoc["data"]["timings"]["Isha"];
if (currentTime != "Yok") {
findNextPrayer(currentTime, fajr, sunrise, dhuhr, asr, maghrib, isha);
} else {
nextPrayerName = "";
nextPrayerRemainingMinutes = -1;
}
} else {
Serial.println("Namaz JSON ayristirma hatasi");
}
} else {
Serial.println("Namaz API hatasi veya baglanti sorunu");
}
}
// LCD Güncelle
lcd.clear();
// Satır 1: Konum
lcd.setCursor(0,0);
lcd.print("Konum: Ankara TR");
// Satır 2: Tarih
lcd.setCursor(0,1);
lcd.print("Tarih: ");
lcd.print(turkishDate);
// Satır 3: Saat
lcd.setCursor(0,2);
lcd.print("Saat : ");
lcd.print(currentTime);
// Satır 4: Namaz bilgisi
lcd.setCursor(0,3);
if (nextPrayerRemainingMinutes >= 0) {
lcd.print(nextPrayerName);
lcd.print(" (");
lcd.print(nextPrayerRemainingMinutes);
lcd.print(" dk)");
} else {
lcd.print("Namaz yok");
}
delay(60000);
}