Sincronizza
Data Giorno / Mese / Anno
Giorno della Settimana
Tiene conto degli anni bisestili e dell' ora legale
Che ne pensate?
/*
Udp NTP Client
Garuti Alessandro
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define SECONDS_IN_DAY 86400
#define START_YEAR 1900
#define TIME_ZONE +1
// Array Giorni - Mesi
static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *weekNames[] = { "Dom" "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
// Velocità Seriale
const unsigned int BAUD_RATE = 9600;
// Parametri Di Rete
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE}; // Mac Address
IPAddress my_ip ( 192, 168, 1, 50); // Indirizzo Ip
IPAddress subnet ( 255, 255, 255, 0); // Subnet Mask
IPAddress gateway ( 192, 168, 1, 1); // Gateway
IPAddress dnServer ( 8, 8, 8, 8); // Dns Server
IPAddress timeServer ( 193, 204, 114, 232); // Time Server ntp1.inrim.it
unsigned int localPort = 8888; // local port to listen for UDP packets
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
EthernetUDP Udp;
void setup()
{
Serial.begin(BAUD_RATE);
Ethernet.begin(mac, my_ip, subnet, gateway, dnServer);
delay(1000);
Serial.println("Arduino Network");
Serial.print("IP ADDRESS: ");
Serial.println(Ethernet.localIP());
Serial.print("SUBNET: ");
Serial.println(subnet);
Serial.print("GATEWAY: ");
Serial.println(gateway);
Udp.begin(localPort);
}
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
delay(1000);
if ( Udp.parsePacket() ) {
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;
printDate(secsSince1900 + 3600 * TIME_ZONE);
}
}
void printDate(uint32_t timeStamp) {
unsigned int year = START_YEAR;
uint32_t calcoladay = timeStamp;
while(1) {
uint32_t seconds;
if(isLeapYear(year)) seconds = SECONDS_IN_DAY * 366;
else seconds = SECONDS_IN_DAY * 365;
if(timeStamp >= seconds) {
timeStamp -= seconds;
year++;
} else break;
}
unsigned int month = 0;
while(1) {
uint32_t seconds = SECONDS_IN_DAY * days_in_month[month];
if(isLeapYear(year) && month == 1) seconds = SECONDS_IN_DAY * 29;
if(timeStamp >= seconds) {
timeStamp -= seconds;
month++;
} else break;
}
month++;
unsigned int day = 1;
while(1) {
if(timeStamp >= SECONDS_IN_DAY) {
timeStamp -= SECONDS_IN_DAY;
day++;
} else break;
}
int giorno = ( calcoladay / SECONDS_IN_DAY ) % 7 ;
unsigned int hour = timeStamp / 3600;
unsigned int minute = (timeStamp - (uint32_t)hour * 3600) / 60;
unsigned int second = (timeStamp - (uint32_t)hour * 3600) - minute * 60;
if ( (month = 4) || (month = 5) || (month = 6) || (month = 7) || (month = 8) || (month = 9) ){
hour++;
}
else if (month = 3) {
if (weekNames[giorno] = "sab"){
if ( (day +7) > 31) {
if (hour >= 2){
hour++;
}
}
}
}
else if (month = 10) {
if (weekNames[giorno] = "sab"){
if ( (day +7) > 31) {
if (hour >= 3){
hour--;
}
}
}
}
Serial.println("Current date and time:");
Serial.print(weekNames[giorno]);
Serial.print(" ");
if(day < 10) Serial.print("0");
Serial.print(day);
Serial.print("/");
if(month < 10) Serial.print("0");
Serial.print(month);
Serial.print("/");
Serial.println(year);
if(hour < 10) Serial.print("0");
Serial.print(hour);
Serial.print(":");
if(minute < 10) Serial.print("0");
Serial.print(minute);
Serial.print(":");
if(second < 10) Serial.print("0");
Serial.println(second);
Serial.println();
}
boolean isLeapYear(unsigned int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& addess)
{
// 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(addess, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}