1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <WiFi.h> #include <NTPClient.h> #include <WiFiUdp.h> #include <MD_MAX72xx.h> #include <MD_Parola.h> #include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW #define MAX_DEVICES 4 #define CLK_PIN 18 #define DATA_PIN 23 #define CS_PIN 5
const char* ssid = "OmaeWaMou"; const char* password = "19690824";
WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
String main_info; String hour; String minute; String year; String month; String day; String sec;
void wifiConnection(){ Serial.print("正在嘗試連線至"); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiF已連接"); }
void setTime(){ main_info=timeClient.getFormattedDate(); int find_T=main_info.indexOf('T'); hour=main_info.substring(find_T+1, find_T+3); minute=main_info.substring(find_T+4, find_T+6); sec=main_info.substring(find_T+7, find_T+9); year=main_info.substring(find_T-10, find_T-6); month=main_info.substring(find_T-5, find_T-3); day=main_info.substring(find_T-2, find_T); }
void timeDisplay(){ Display.setIntensity(5); Display.setTextAlignment(PA_CENTER); Display.print(hour+":"+minute); delay(500); Display.print(hour+" "+minute); delay(500); if(sec=="00"){ Display.print(year); delay(1000); Display.print(month); delay(1000); Display.print(day); delay(1000); } } void setup(){ Serial.begin(115200); delay(10); wifiConnection();
timeClient.begin(); timeClient.setTimeOffset(28800);
Display.begin(); Display.setIntensity(0); Display.displayClear(); } void loop(){ while(timeClient.update()){ setTime(); timeDisplay(); } }
|