#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
unsigned long previousMillis = 0;
const long interval = 5000;
bool showTime = true;
const int RECV_PIN = 15; // IR Receiver pin
IRrecv irrecv(RECV_PIN);
decode_results results;
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0, 1);
lcd.print("Connection Err");
return;
}
lcd.setCursor(0, 0);
char timeStr[9];
snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
lcd.print(timeStr);
lcd.setCursor(0, 1);
char dateStr[17];
snprintf(dateStr, sizeof(dateStr), "%02d/%02d/%04d", timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900);
lcd.print(dateStr);
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("HELLO ARYAN");
delay(2000); // Show "HELLO ARYAN" for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connecting to");
lcd.setCursor(0, 1);
lcd.print("WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Online");
lcd.setCursor(0, 1);
lcd.print("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
if (irrecv.decode(&results)) {
unsigned long key_value = results.value;
irrecv.resume(); // Receive the next value
lcd.clear();
if (key_value >= 0xFFA25D) { // Example IR remote code for button 1
lcd.setCursor(0, 0);
lcd.print("Number: 1");
} else if (key_value >= 0xFF629D) { // Example IR remote code for button 2
lcd.setCursor(0, 0);
lcd.print("Number: 2");
}
// Add more conditions for other numbers as needed
} else {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
lcd.clear();
if (showTime) {
printLocalTime();
} else {
lcd.setCursor(0, 0);
lcd.print("HELLO ARYAN");
}
showTime = !showTime;
}
}
}