I found some code to get the utc time (maybe also the local time from another NTP-Server with some modification) I want to share with the community, so it’s not really a libary. (modified code from http://www.dl8ma.de/Arduino/web-utc-uhr/ , German language)
Unfortunately I realized while compiling in web IDE that the UPD libary in the Firmware is still not available. @zach : Is there a schedule for that? Is there another way to add UDP in the web IDE right now, or another way to poll that data ? How can I put the result to the onboard-RTC? (I’m an Arduino beginner, sorry!)
char string[ 17 ] = { "" };
int stunde, minute, sekunde; //hour, minute, second
int led = D7;
unsigned int localPort = 8888; // local port to listen for UDP packets
byte timeServer[] = { 192, 43, 244, 18}; // time.nist.gov NTP server
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
void setup()
{
pinMode(led, OUTPUT);
Udp.begin(localPort);
Serial.begin(9600);
delay(1000);
serial.printIn("NTP-Uhr");
}
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if ( Udp.available() ) {
Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into everyday time:
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
stunde = (epoch % 86400L) / 3600;
minute = (epoch % 3600) / 60;
sekunde = (epoch % 60);
serial.print (stunde);
serial.print (minute);
serial.println (sekunde);
}
// wait ten seconds before asking for the time again
digitalWrite(led, HIGH); // Turn ON the LED
delay(1000); // Wait for 1000mS = 1 second
digitalWrite(led, LOW); // Turn OFF the LED
delay(9000); // Wait for 2 seconds
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(byte *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.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
}