#include <WiFi.h>
#include <WiFiUdp.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// Set your WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// NTP Server details
const char* ntpServer = "pool.ntp.org";
const int ntpPort = 123;
// Time offset in seconds for your timezone
const int timeZoneOffset = 3600; // Example: GMT+1 (1 hour = 3600 seconds)
// LED Matrix configuration
const uint8_t numOfModules = 1; // Number of MAX7219 modules connected
const uint8_t dataPin = 23; // GPIO23 pin on ESP32
const uint8_t clockPin = 18; // GPIO18 pin on ESP32
const uint8_t csPin = 5; // GPIO5 pin on ESP32
// Create instances of necessary objects
WiFiUDP udp;
MD_MAX72XX mx = MD_MAX72XX(dataPin, clockPin, csPin, numOfModules);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// 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");
// Initialize LED Matrix
mx.begin();
mx.setIntensity(0); // Set the brightness level (0-15)
mx.clear();
// Initialize UDP for NTP
udp.begin(ntpPort);
// Set time offset
configTime(timeZoneOffset, 0, ntpServer);
}
void loop() {
// Update the time from NTP server every 60 seconds
static unsigned long lastUpdate = 0;
if (millis() - lastUpdate >= 60000) {
updateTime();
lastUpdate = millis();
}
// Display the current time
displayTime();
}
void updateTime() {
// Get the current time from the NTP server
udp.flush();
IPAddress ntpServerIP;
while (udp.parsePacket() > 0)
; // Discard any previously received packets
if (WiFi.hostByName(ntpServer, ntpServerIP)) {
Serial.println("Sending NTP request...");
byte packetBuffer[48];
memset(packetBuffer, 0, 48);
packetBuffer[0] = 0xE3;
udp.beginPacket(ntpServerIP, ntpPort);
udp.write(packetBuffer, 48);
udp.endPacket();
}
while (udp.parsePacket() == 0)
; // Wait until a valid NTP packet is received
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 ntpTime = highWord << 16 | lowWord;
// Set the system time to the received NTP time
struct timeval tv;
tv.tv_sec = ntpTime - 2208988800UL; // Convert NTP time to UNIX timestamp
tv.tv_usec = 0;
settimeofday(&tv, nullptr);
}
void displayTime() {
// Get the current time
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
// Extract individual time components
int hour = timeinfo.tm_hour;
int minute = timeinfo.tm_min;
int second = timeinfo.tm_sec;
// Format the time as a string
String timeString = String(hour) + ":" +
(minute < 10 ? "0" : "") + String(minute) + ":" +
(second < 10 ? "0" : "") + String(second);
// Display the time on the LED Matrix
mx.clear();
mx.setChar(0, 0, timeString.charAt(0));
mx.setChar(1, 0, timeString.charAt(1));
mx.setChar(2, 0, timeString.charAt(3));
mx.setChar(3, 0, timeString.charAt(4));
mx.setChar(4, 0, timeString.charAt(6));
mx.setChar(5, 0, timeString.charAt(7));
mx.commit();
}