Created
October 21, 2025 09:20
-
-
Save maxpromer/4163ce0e4e572a6db7bda8c70e124cc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <WiFi.h> | |
| #include <WiFiMulti.h> | |
| #include <Wire.h> | |
| #include <time.h> | |
| #include <ArtronShop_RX8130CE.h> | |
| #include <ESP32-HUB75-MatrixPanel-I2S-DMA.h> | |
| #define PANEL_RES_X 64 // Number of pixels wide of each INDIVIDUAL panel module. | |
| #define PANEL_RES_Y 32 // Number of pixels tall of each INDIVIDUAL panel module. | |
| #define PANEL_CHAIN 1 // Total number of panels chained one to another | |
| // MatrixPanel_I2S_DMA dma_display; | |
| MatrixPanel_I2S_DMA *dma_display = nullptr; | |
| ArtronShop_RX8130CE rtc(&Wire); | |
| WiFiMulti wifiMulti; | |
| // กำหนด NTP Servers (ใช้ได้พร้อมกัน 3 ตัว) | |
| const char* ntpServer1 = "pool.ntp.org"; | |
| const char* ntpServer2 = "time.nist.gov"; | |
| const char* ntpServer3 = "th.pool.ntp.org"; | |
| // กำหนดข้อมูลเชื่อมต่อ WiFi | |
| const char * ssid = "WiFi Name"; | |
| const char * password = "WiFi Password"; | |
| const long gmtOffset_sec = 7 * 3600; // GMT+7 (เวลาไทย) | |
| const int daylightOffset_sec = 0; | |
| void setup() { | |
| Serial.begin(115200); | |
| // เพิ่มเครือข่าย WiFi หลายเครือข่าย | |
| wifiMulti.addAP(ssid, password); | |
| Wire.begin(); // เริ่มต้นใช้งาน I2C | |
| while(!rtc.begin()) { // เริ่มใช้งาน RTC | |
| Serial.println("RX8130CE init error !"); | |
| delay(1000); | |
| } | |
| // Display Setup | |
| HUB75_I2S_CFG mxconfig( | |
| PANEL_RES_X, // module width | |
| PANEL_RES_Y, // module height | |
| PANEL_CHAIN // Chain length | |
| ); | |
| dma_display = new MatrixPanel_I2S_DMA(mxconfig); | |
| dma_display->begin(); | |
| dma_display->setBrightness8(90); //0-255 | |
| dma_display->clearScreen(); | |
| dma_display->setTextSize(1); // size 1 == 8 pixels high | |
| dma_display->setTextWrap(false); // Don't wrap at end of line - will do ourselves | |
| xTaskCreate([](void*) { // WiFi Task | |
| while(1) { // วนรอบไม่จำกัด | |
| if (!WiFi.isConnected()) { // ถ้า WiFi ไม่ได้เชื่อมต่ออยู่ | |
| Serial.println("Connecting to WiFi..."); | |
| if (wifiMulti.run() != WL_CONNECTED) { // เชื่อมต่อ WiFi | |
| Serial.println("Connect Fail !"); | |
| continue; | |
| } | |
| // ถ้าเชื่อม WiFi สำเร็จ ให้แสดงข้อมูลเชื่อมต่อ WiFi ใน Serial Monitor | |
| Serial.println("\nConnected to WiFi!"); | |
| Serial.print("IP Address: "); | |
| Serial.println(WiFi.localIP()); | |
| // ตั้งค่าเวลา NTP (หลายเซิร์ฟเวอร์) | |
| configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2, ntpServer3); | |
| } | |
| delay(500); | |
| } | |
| vTaskDelete(NULL); | |
| }, "WiFiTask", 32 * 1024, NULL, 10, NULL); | |
| } | |
| unsigned long timer = 0; | |
| void loop() { | |
| if (WiFi.isConnected()) { // ถ้าเชื่อมต่อ WiFi อยู่ | |
| if ((timer == 0) || (millis() < timer) || ((millis() - timer) > (5 * 60 * 1000))) { // อยู่ในช่วงเวลาอัพเดทเวลาจาก NTP | |
| struct tm timeinfo; | |
| if (getLocalTime(&timeinfo)) { // ดึงเวลาจาก NTP | |
| if (rtc.setTime(timeinfo)) { // บันทึกเวลาลง RTC | |
| Serial.println("Set time to RTC successful!"); // แสดงข้อความออก Serial Monitor | |
| timer = millis(); | |
| } else { // ถ้าบันทึกเวลาไม่สำเร็จ | |
| Serial.println("Set time to RTC fail !"); // แสดงข้อความออก Serial Monitor | |
| } | |
| } else { // ถ้าดึงเวลาจาก NTP ไม่สำเร็จ | |
| Serial.println("Failed to obtain time"); // แสดงข้อความใน Serial Monitor | |
| } | |
| } | |
| } | |
| // ดึงค่าเวลาจาก RTC | |
| struct tm timeinfo; | |
| if (!rtc.getTime(&timeinfo)) { | |
| Serial.println("Get time from RTC fail !"); | |
| return; | |
| } | |
| timeinfo.tm_year += 543; // แปลงเลขปี คศ. เป็น พศ. | |
| Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // แสดงวัน-เวลาปัจจุบันใน Serial Monitor | |
| dma_display->fillScreen(dma_display->color444(0, 0, 0)); // เทสีดำทั้งจอ | |
| if (WiFi.isConnected()) { // ถ้าเชื่อมต่อ WiFi อยู่ | |
| dma_display->drawRect(0, 0, 64, 13, dma_display->color565(0, 255, 0)); // วาดกรอบสี่เหลี่ยมเริ่มพิกัด (0, 0) ขนาดกว้าง 64 สูง 13 สีเขียว | |
| } else { // ถ้าไม่ได้เชื่อมต่อ WiFi | |
| dma_display->drawRect(0, 0, 64, 13, dma_display->color565(0, 0, 255)); // วาดกรอบสี่เหลี่ยมเริ่มพิกัด (0, 0) ขนาดกว้าง 64 สูง 13 สีน้ำเงิน | |
| } | |
| dma_display->setCursor(2, 3); // กำหนดพิกัดเริ่มวาด (2, 3) | |
| dma_display->setTextSize(1); // กำหนดขนาดตัวอักษร 1 | |
| dma_display->setTextColor(dma_display->color565(255, 255, 255)); // กำหนดตัวอักษรสีขาว | |
| dma_display->print(&timeinfo, "%d/%m/%Y"); // แสดงวันที่บนจอ | |
| dma_display->setTextSize(2); // กำหนดขนาดตัวอักษร 2 | |
| dma_display->setTextColor(dma_display->color565(255, 0, 0)); // กำหนดตัวอักษรสีแดง | |
| dma_display->setCursor(7 + 0, 16); // กำหนดพิกัดเริ่มวาด (7, 16) | |
| dma_display->print(&timeinfo, "%H"); // แสดงเลขชั่วโมงบนหน้าจอ | |
| dma_display->setCursor(7 + 28, 16); // กำหนดพิกัดเริ่มวาด (7 + 28, 16) | |
| dma_display->print(&timeinfo, "%M"); // แสดงเลขนาทีบนหน้าจอ | |
| { | |
| static uint8_t i = 1; | |
| if (i == 1) { // ถ้า i เท่ากับ 1 | |
| // แสดง : บนหน้าจอ | |
| dma_display->drawRect(7 + 24, 20, 2, 2, dma_display->color565(255, 0, 0)); | |
| dma_display->drawRect(7 + 24, 24, 2, 2, dma_display->color565(255, 0, 0)); | |
| } | |
| i = 1 - i; // สลับค่า i เป็น 0 หรือ 1 | |
| } | |
| delay(500); // หน่วงเวลา 500 ms | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment