# Giorno mese anno su NTPClient e ethernet shield

**URL:** <https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712>\
**Category:** Software\
**Created:** [January 3, 2024, 8:43pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712 "2024-01-03T20:43:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 3, 2024, 8:43pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/1 "2024-01-03T20:43:18Z")

</div>

Salve a tutti, sto usando Arduino UNO, la ethernet shield e lo sketch ufficiale per avere l'orario esatto ed utilizzarlo. Il problema è che mi servirebbe anche giorno mese e anno ed è davvero un rompicapo.

Di seguito il codice che non è altro che quello ufficiale:

```arduino
/*
 Udp NTP Client

 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket
 For more on NTP time servers and the messages needed to communicate with them,
 see https://en.wikipedia.org/wiki/Network_Time_Protocol

 created 4 Sep 2010
 by Michael Margolis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

 This code is in the public domain.

 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

unsigned int localPort = 8888; // local port to listen for UDP packets

const char timeServer[] = "time.nist.gov"; // 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

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10); // Most Arduino shields
  //Ethernet.init(5); // MKR ETH Shield
  //Ethernet.init(0); // Teensy 2.0
  //Ethernet.init(20); // Teensy++ 2.0
  //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // no point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
  Udp.begin(localPort);
}

void loop() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server

  // wait to see if a reply is available
  delay(1000);
  if (Udp.parsePacket()) {
    // We've received a packet, read the data from it
    Udp.read(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, extract 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;
    Serial.print("Seconds since Jan 1 1900 = ");
    Serial.println(secsSince1900);

    // now convert NTP time into everyday time:
    Serial.print("Unix 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;
    // print Unix time:
    Serial.println(epoch);

    // print the hour, minute and second:
    Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');
    if (((epoch % 3600) / 60) < 10) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ((epoch % 60) < 10) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch % 60); // print the second
  }
  // wait ten seconds before asking for the time again
  delay(10000);
  Ethernet.maintain();
}

// send an NTP request to the time server at the given address
void sendNTPpacket(const char * 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();
}

```

Funziona tutto ma mi manca la data con giorno mese è anno.  
Non riesco a trovare la documentazione (se esiste) relativa a questo sketch.  
Grazie Alberto

---

<div class="post-metadata">

**Author:** ![Salvorhardin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/salvorhardin/32/469189_2.png) [@Salvorhardin](https://forum.arduino.cc/u/Salvorhardin)\
**Post date:** [January 3, 2024, 9:25pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/2 "2024-01-03T21:25:30Z")

</div>

Prova a guardare qui

> **[ESP32 NTP Client-Server: Get Date and Time (Arduino IDE) | Random Nerd Tutorials](https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/)**
>
> Request date and time from an NTP Server using the ESP32 with Arduino IDE. With an Internet connection, you can make data logging projects with timestamps.

Oppure qui

> [@gestione data/ora da ntp](https://forum.arduino.cc/t/gestione-data-ora-da-ntp/600125):
>
> Buongiorno a tutti,attualmente ho vari nodemcu installati nella mia abitazione, e su quasi tutti ho installato la funzionalità ntp, vorrei centralizzarla su un unico node e poi da questo inviare comandi centralizzati a tutti gli altri, inoltre ho trovato una libreria che gestisce l'ora legale e vorrei utilizzarla. Il problema è che questo codice; /\* &nbsp; This sketch shows an example of sending a reading to data.sparkfun.com once per day. &nbsp; It uses the Sparkfun testing stream so the only custom…

---

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 3, 2024, 9:40pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/3 "2024-01-03T21:40:43Z")

</div>

Ho visto questi sketch ma sono per esp32.

Io ho la ethernet shield classica.

---

<div class="post-metadata">

**Author:** ![Salvorhardin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/salvorhardin/32/469189_2.png) [@Salvorhardin](https://forum.arduino.cc/u/Salvorhardin)\
**Post date:** [January 3, 2024, 10:21pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/4 "2024-01-03T22:21:49Z")

</div>

Contento tu contenti tutti

---

<div class="post-metadata">

**Author:** ![gpb01](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gpb01/32/830685_2.png) [@gpb01](https://forum.arduino.cc/u/gpb01)\
**Post date:** [January 4, 2024, 3:03am UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/5 "2024-01-04T03:03:54Z")

</div>

> [@maredentro72](#):
>
> Ho visto questi sketch ma sono per esp32.

Vero, ma ... uno "_Unix time_" come quello che viene usato negli esempi per ESP è lo stesso che recupera il tuo programma ovvero è un numero intero, non segnato a 32 bit che indica il numero di secondi passati dal 1 Gennaio 1970, sapendo il quale, usando le varie librerie, si ricava tutto quello che a te occorre.

Quindi, se **ti studi** come fanno su ESP, puoi sicuramente fare una cosa simile su Arduino UNO.

Guglielmo

---

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 4, 2024, 5:30am UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/6 "2024-01-04T05:30:26Z")

</div>

Ok grazie Guglielmo, ci provo.

Alberto

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [January 4, 2024, 9:17am UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/7 "2024-01-04T09:17:42Z")

</div>

Per prima cosa ti consiglio di usare la libreria "[NTPClient](https://github.com/arduino-libraries/NTPClient)". Come ti ha suggerito Guglielmo, devi ottenere un "epoch time" e se leggi nel README della libreria, in fondo trovi il riferimento alla funzione getEpochTime(). Quindi una volta ottenuto l'"epoch time" puoi ottenere la data chiamando la funzione [gmtime()](https://cplusplus.com/reference/ctime/gmtime/) che restituisce una [struct tm](https://mikaelpatel.github.io/Arduino-RTC/d8/d5a/structtm.html).  
Esempio:

```arduino
    struct tm *ptm = gmtime ((time_t *)&epochTime);
    // Giorno = (int)ptm->tm_mday;  
    // Mese = (int)ptm->tm_mon+1;  
    // Anno = (int)ptm->tm_year-100+2000;

```

Altrimenti devi comunque tu calcolare la data partendo da "epochTime" che se non ricordo male è un valore unsigned long che rappresenta l'offset dal 1 Gennaio 1970.

Prova a scrivere un tuo codice di test che alla partenza mostri la data corrente (dopo ovviamente aveer aggiornato il "timeClient") e facci sapere se ti funziona.

---

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 4, 2024, 8:26pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/8 "2024-01-04T20:26:05Z")

</div>

docdoc la libreria alla quale ti riferisci è la prima che ho provato purtroppo però è legata ai moduli ESP8266 o ESP32 che io non ho mai usato e comunque in questo caso sono vincolato alla ethernet shield.

Alberto.

---

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 4, 2024, 8:59pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/9 "2024-01-04T20:59:54Z")

</div>

Problema risolto e come sempre era dentro l'IDE.

Ho usato la TimeLib e lo sketch TimeNTP.

All'interno c'è tutto. Bisogna aver cura di modificare l'IP del server NTP. Io ho usato questo:  
(132, 163, 96, 6); [//time-e-b.nist.gov](https://time-e-b.nist.gov)

Alberto

---

<div class="post-metadata">

**Author:** ![gpb01](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gpb01/32/830685_2.png) [@gpb01](https://forum.arduino.cc/u/gpb01)\
**Post date:** [January 4, 2024, 9:01pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/10 "2024-01-04T21:01:24Z")

</div>

Guarda la [time.h](https://www.nongnu.org/avr-libc/user-manual/group __avr__ time.html) che fa parte di [AVRlibc](https://www.nongnu.org/avr-libc/user-manual/modules.html) (_... libreria che è sempre disponibile in Arduino AVR_) ... trovi tutto quello che serve.

Guglielmo

_P.S.: Ok, hai scritto di aver risolto mentre io scrivevo questo 😁 😁 😁_

---

<div class="post-metadata">

**Author:** ![maredentro72](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maredentro72/32/1058375_2.png) [@maredentro72](https://forum.arduino.cc/u/maredentro72)\
**Post date:** [January 4, 2024, 9:50pm UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/11 "2024-01-04T21:50:34Z")

</div>

Grazie Guglielmo, ho dato un'occhiata veloce e domani darò un'occhiata più approfondita.

Alberto

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [January 8, 2024, 8:25am UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/12 "2024-01-08T08:25:18Z")

</div>

> [@maredentro72](#):
>
> la libreria alla quale ti riferisci è la prima che ho provato purtroppo però è legata ai moduli ESP8266 o ESP32

Hai ragione, davo per scontato che bastasse l'UDP, invece serve un ESP o WiFi shield o una MKR1000.

Comunque per la Ethernet shield hai provato a vedere anche [QUI](https://docs.arduino.cc/tutorials/ethernet-shield-rev2/udp-ntp-client)? Una volta che ottieni l'epoch, puoi calcolare la data con il metodo che ti avevo indicato.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 6, 2024, 8:26am UTC](https://forum.arduino.cc/t/giorno-mese-anno-su-ntpclient-e-ethernet-shield/1206712/13 "2024-07-06T08:26:06Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
