esp8266 모델로 와이파이 연결되어 있으면 RTC 모듈 없이 시계 구현이 가능합니다. 본인의 와이파이 SSID와 패스워드 고쳐야 합니다.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <TM1637Display.h> #include <TimeLib.h> // Module connection pins (Digital Pins) #define CLK D3 #define DIO D4 #define GMT 9 char ssid[] = "your wifi ssid"; // change here char pass[] = "your wifi password"; // change here unsigned int localPort = 2390; IPAddress timeServerIP; const char* ntpServerName = "kr.pool.ntp.org"; const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets // A UDP instance to let us send and receive packets over UDP WiFiUDP udp; TM1637Display display(CLK, DIO); bool tick = false; //byte hour1, minute1; void setup() { Serial.begin(115200); display.setBrightness(3); Serial.println(); Serial.println(); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); setSyncProvider(ntpTimeCheck); setSyncInterval(1800); // display.showNumberDec(hour() * 100 + minute(), true); if(timeStatus()!= timeSet) Serial.println("Unable to sync"); else Serial.println("Time sets fine"); } void loop() { if (tick == true) { display.showNumberDecEx(hour() * 100 + minute(), (0x80 >> 1), true); } else { display.showNumberDecEx(hour() * 100 + minute(), 0, true); } tick = !tick; delay(500); } time_t ntpTimeCheck() { // get a random server from the pool WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); delay(1000); int cb = udp.parsePacket(); if (!cb) { Serial.println("packet failed"); return now(); } else { Serial.print("packet received, length="); Serial.println(cb); udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; Serial.print("Seconds since Jan 1 1900 = "); Serial.println(secsSince1900); Serial.print("Unix time = "); const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears; Serial.println(epoch); epoch = epoch + GMT * 3600; // minute1 = minute(epoch); // hour1 = hour(epoch); return epoch; } } // send an NTP request to the time server at the given address unsigned long sendNTPpacket(IPAddress& address) { Serial.println("sending NTP packet..."); // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum, or type of clock packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: udp.beginPacket(address, 123); //NTP requests are to port 123 udp.write(packetBuffer, NTP_PACKET_SIZE); udp.endPacket(); } |