06/10/2024
// phien ban NodeMCU 1.0
// thay đổi thông tin wifi qua Smartphone
ESP32
int pinCS = D4; // Attach CS to this pin, DIN to MOSI and CLK to SCK
int numberOfHorizontalDisplays = 4; // SL MODULE CHIEU DAI LED
int numberOfVerticalDisplays = 1; // SL MODULE CHIEU CAP LED
char time_value[20];
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
int wait = 50; // In milliseconds
int spacer = 1;
int width = 6 + spacer; // The font width is 5 pixels
bool blink = true; // Biến để kiểm soát việc nhấp nháy của dấu ":"
void setup() {
Serial.begin(115200);
WiFi_Setup();
configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");
setenv("TZ", "-7", 1);
matrix.setIntensity(5);
matrix.setRotation(0, 1);
matrix.setRotation(1, 1);
matrix.setRotation(2, 1);
matrix.setRotation(3, 1);
}
void loop() {
matrix.fillScreen(LOW);
time_t now = time(nullptr);
String time = String(ctime(&now));
time.trim();
// Trích xuất giờ, phút, giây
time.substring(11, 19).toCharArray(time_value, 10);
// Hiển thị giờ
matrix.drawChar(2, 0, time_value[0], HIGH, LOW, 1); // H
matrix.drawChar(8, 0, time_value[1], HIGH, LOW, 1); // H
// Nhấp nháy dấu ":" mỗi giây
if (blink) {
matrix.drawChar(14, 0, ':', HIGH, LOW, 1); // Nếu biến blink là true thì hiển thị ":"
} else {
matrix.drawChar(14, 0, ' ', HIGH, LOW, 1); // Nếu biến blink là false thì không hiển thị gì
}
// Hiển thị phút
matrix.drawChar(20, 0, time_value[3], HIGH, LOW, 1); // M
matrix.drawChar(26, 0, time_value[4], HIGH, LOW, 1); // M
matrix.write(); // Gửi bitmap đến hiển thị
blink = !blink; // Đảo ngược trạng thái của biến blink
delay(1000); // Cập nhật mỗi giây
// Cuộn giờ và phút lên để hiện ngày tháng
display_date_time();
}
void display_date_time() {
time_t now = time(nullptr);
String date = String(ctime(&now));
date.trim();
// Hiển thị ngày tháng
String day_month = date.substring(8, 16); // Lấy ngày tháng
for (int i = 0; i < day_month.length(); i++) {
matrix.drawChar(i * 6, 8, day_month[i], HIGH, LOW, 1); // Căn giữa văn bản theo chiều dọc
}
matrix.write(); // Gửi bitmap để hiển thị
}
void WiFi_Setup() {
WiFiManager wifiManager;
wifiManager.setTimeout(180);
if (!wifiManager.autoConnect("XTDIY_CLOCK")) {
Serial.println(F("failed to connect and timeout occurred"));
delay(6000);
ESP.restart();
}
Serial.println(F("WiFi connected..."));
Serial.println(F("Use this URL to connect: http://"));
Serial.println(WiFi.localIP().toString() + "/");
}