ESP8266 모델로 와이파이 연결되어 있으면 RTC 모듈 없이 시계 구현이 가능합니다. 본인의 와이파이 SSID와 패스워드 고쳐야 합니다.
#include#include #include #include // 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(); }
'ESP8266, ESP32' 카테고리의 다른 글
ESP8266 보드 여러가지 (0) | 2019.01.08 |
---|---|
ESP32 Doit 살리기 (0) | 2019.01.07 |
NTP Clock – Time & Date (0) | 2019.01.02 |
ESP8266 보드 여러가지 (0) | 2018.12.26 |
ESP8266 Wifi D1 mini 보드 살리기 (0) | 2018.12.26 |