#if defined(ESP8266) #include #else #include #endif //Library used in projects #include #include #include #include #include RtcDS3231 Rtc(Wire); #define countof(a) (sizeof(a) / sizeof(a[0])) const char ssid[] = "netis"; // your network SSID (name) const char pass[] = "password"; // your network password unsigned int localPort = 2390; // local port to listen for UDP packets IPAddress timeServerIP; // time.nist.gov NTP server address const char* ntpServerName = "time.nist.gov"; 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; //Function used in this projects time_t getNtpTime(); void digitalClockDisplay(); void printDigits(int digits); void sendNTPpacket(IPAddress &address); //Setup file void setup() { Serial.begin(115200); delay(250); Rtc.Begin(); Serial.println("TimeNTP Example"); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" "); Serial.print("IP number assigned by DHCP is "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); Serial.println("waiting for sync"); setSyncProvider(getNtpTime); // Set the time using NTP if(timeStatus() != timeNotSet){ digitalClockDisplay(); Serial.println("here is another way to set rtc"); time_t t = now(); char d_mon_yr[12]; snprintf_P(d_mon_yr, countof(d_mon_yr), PSTR("%s %02u %04u"), monthShortStr(month(t)), day(t), year(t)); // get month, day and year Serial.println(d_mon_yr); char tim_set[9]; snprintf_P(tim_set, countof(tim_set), PSTR("%02u:%02u:%02u"), hour(t), minute(t), second(t)); // get time Serial.println(tim_set); Serial.println("Now its time to set up rtc"); RtcDateTime compiled = RtcDateTime(d_mon_yr, tim_set); printDateTime(compiled); Serial.println(""); if (!Rtc.IsDateTimeValid()) { Serial.println("RTC lost confidence in the DateTime!"); } Rtc.SetDateTime(compiled); RtcDateTime now = Rtc.GetDateTime(); if (now < compiled) { Serial.println("RTC is older than compile time! (Updating DateTime)"); Rtc.SetDateTime(compiled); } else if (now > compiled) { Serial.println("RTC is newer than compile time. (this is expected)"); } else if (now == compiled) { Serial.println("RTC is the same as compile time! (not expected but all is fine)"); } Rtc.Enable32kHzPin(false); Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); } } // Loop Function void loop() { if (!Rtc.IsDateTimeValid()) { Serial.println("RTC lost confidence in the DateTime!"); } Serial.println("ready to get date time"); // get date and time repeatedly in every five second from DS3231 RtcDateTime now = Rtc.GetDateTime(); printDateTime(now); Serial.println(); delay(5000); } void digitalClockDisplay() { Serial.print(hour());//print hour in serial monitor printDigits(minute());//print minutes in serial monitor printDigits(second()); //print second in serial monitor Serial.print(" "); Serial.print(day()); //print day in serial monitor Serial.print("."); Serial.print(month()); //print month in serial monitor Serial.print("."); Serial.print(year()); //print year in serial monitor Serial.println(); } void printDigits(int digits) { Serial.print(":"); if (digits < 10) Serial.print('0'); Serial.print(digits); } //This function get time from NTP Server time_t getNtpTime() { IPAddress ntpServerIP; // NTP server's ip address while (udp.parsePacket() > 0) ; // discard any previously received packets Serial.println("Transmit NTP Request"); WiFi.hostByName(ntpServerName, ntpServerIP); Serial.print(ntpServerName); Serial.print(": "); Serial.println(ntpServerIP); sendNTPpacket(ntpServerIP); uint32_t beginWait = millis(); while (millis() - beginWait < 1500) { int size = udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { Serial.println("Receive NTP Response"); udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer unsigned long secsSince1900; secsSince1900 = (unsigned long)packetBuffer[40] << 24; secsSince1900 |= (unsigned long)packetBuffer[41] << 16; secsSince1900 |= (unsigned long)packetBuffer[42] << 8; secsSince1900 |= (unsigned long)packetBuffer[43]; // change you GMT time zones here; For nepal its GMT +5:45 //So you have to add the you GMT zones in second i.e. 5 hour 45 minutes = 20700 seconds //for that 5*Second for hours and 2700 seonds for 45 minutes return secsSince1900 - 2208988800UL + 5 * SECS_PER_HOUR + 2700; } } Serial.println("No NTP Response :-("); return 0; // return 0 if unable to get the time } // send an NTP request to the time server at the given address void sendNTPpacket(IPAddress &address) { // 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(); } //Print Time Function void printDateTime(const RtcDateTime& dt) { char datestring[20]; snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"), dt.Month(), dt.Day(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() ); Serial.print(datestring); }