[RESOLU ] Synchoniser RTC DS1307 par NTP sur mega2506 ethernet2

Salut ,

je recois l ' heure d ' un serveur NTP sous 3 formes differentes :

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, 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;
    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);
}

dans la bibliotheque RTClib , j ' ai reperer 2 fonctions qui pourrait peut etre convenir :

/*!
    @brief  Set the current date/time of the RTC_Millis clock.
    @param dt DateTime object with the desired date and time
*/
/**************************************************************************/
void RTC_Millis::adjust(const DateTime& dt) {
  lastMillis = millis();
  lastUnix = dt.unixtime();
}



/*!
    @brief  Set the current date/time of the RTC_Micros clock.
    @param dt DateTime object with the desired date and time
*/
/**************************************************************************/
void RTC_Micros::adjust(const DateTime& dt) {
  lastMicros = micros();
  lastUnix = dt.unixtime();
}

j ' avais vu sur un post que je ne retrouve plus sur le forum , un truc avec RTCadjust qui prenait en parametre timestamp unix .

Du coup je suis paumé :confused:

un petit coup de pouce svp ?

regarde en utilisant cette lib

en particulier avec cet exemple

moi c'est cette lib que j'utilise pour mettre à jour mon horloge à partir d'un GPS, ça fonctionne très bien

Bonsoir Bricofoy et merci pour votre reponse :wink: :wink:

Cependant je ne peux pas utiliser cet example , c ' est de ma faute je n' ai pas preciser dans mon programme principal quelles classe j' utilisais :kissing:

je rectifie desuite le tir donc pour le momen je suis avec ces lib là :

#include <SPI.h>
#include <Ethernet2.h>
#include "AsyncTask.h"
#include "RTClib.h"
#include <EthernetUdp2.h>

je ne vois pas en quoi ce serait incompatible ?

il suffit de remplacer rtclib.h par timelib.h et d'adapter la fonction qui lit le ntp

voilà le code de l'exemple en question, la librairie est installable directement depuis le gestionnaire :

/*
 * Time_NTP.pde
 * Example showing time sync to NTP time source
 *
 * This sketch uses the Ethernet library
 */
 
#include <TimeLib.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov


const int timeZone = 1;     // Central European Time
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)


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

void setup() 
{
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  delay(250);
  Serial.println("TimeNTP Example");
  if (Ethernet.begin(mac) == 0) {
    // no point in carrying on, so do nothing forevermore:
    while (1) {
      Serial.println("Failed to configure Ethernet using DHCP");
      delay(10000);
    }
  }
  Serial.print("IP number assigned by DHCP is ");
  Serial.println(Ethernet.localIP());
  Udp.begin(localPort);
  Serial.println("waiting for sync");
  setSyncProvider(getNtpTime);
}

time_t prevDisplay = 0; // when the digital clock was displayed

void loop()
{  
  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
      prevDisplay = now();
      digitalClockDisplay();  
    }
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);
  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;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  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();
}

bricofoy:
je ne vois pas en quoi ce serait incompatible ?

il suffit de remplacer rtclib.h par timelib.h et d'adapter la fonction qui lit le ntp

voilà le code de l'exemple en question, la librairie est installable directement depuis le gestionnaire :

/*
  • Time_NTP.pde
  • Example showing time sync to NTP time source
  • This sketch uses the Ethernet library
    */

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov

const int timeZone = 1;     // Central European Time
//const int timeZone = -5;  // Eastern Standard Time (USA)
//const int timeZone = -4;  // Eastern Daylight Time (USA)
//const int timeZone = -8;  // Pacific Standard Time (USA)
//const int timeZone = -7;  // Pacific Daylight Time (USA)

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

void setup()
{
 Serial.begin(9600);
 while (!Serial) ; // Needed for Leonardo only
 delay(250);
 Serial.println("TimeNTP Example");
 if (Ethernet.begin(mac) == 0) {
   // no point in carrying on, so do nothing forevermore:
   while (1) {
     Serial.println("Failed to configure Ethernet using DHCP");
     delay(10000);
   }
 }
 Serial.print("IP number assigned by DHCP is ");
 Serial.println(Ethernet.localIP());
 Udp.begin(localPort);
 Serial.println("waiting for sync");
 setSyncProvider(getNtpTime);
}

time_t prevDisplay = 0; // when the digital clock was displayed

void loop()
{  
 if (timeStatus() != timeNotSet) {
   if (now() != prevDisplay) { //update the display only if time has changed
     prevDisplay = now();
     digitalClockDisplay();  
   }
 }
}

void digitalClockDisplay(){
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(month());
 Serial.print(" ");
 Serial.print(year());
 Serial.println();
}

void printDigits(int digits){
 // utility for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

/-------- NTP code ----------/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
 while (Udp.parsePacket() > 0) ; // discard any previously received packets
 Serial.println("Transmit NTP Request");
 sendNTPpacket(timeServer);
 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;
     // convert four bytes starting at location 40 to a long integer
     secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
     secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
     secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
     secsSince1900 |= (unsigned long)packetBuffer[43];
     return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
   }
 }
 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();
}

ca ne marche pas , j ' ai juste ajouté les libs manquantes et quand je tente la compilation :

home/iznobe/Arduino/SKETCH/serveur volets/serveur_volet_arrosage_RTC.v6.21/serveur_volet_arrosage_RTC.v6.21.ino:829:105: warning: invalid conversion from 'void (*)(byte, long unsigned int) {aka void (*)(unsigned char, long unsigned int)}' to 't_callback {aka void (*)(unsigned int)}' [-fpermissive]
   gestionnaireDeTache.registerAsyncCommand( numRelais, CalculEspaceJoursArrosage(zone) , MarcheArrosage ); // tourne en boucle
                                                                                                         ^
In file included from /home/iznobe/Arduino/SKETCH/serveur volets/serveur_volet_arrosage_RTC.v6.21/serveur_volet_arrosage_RTC.v6.21.ino:3:0:
/home/iznobe/Arduino/libraries/AsyncTask/AsyncTask.h:35:10: note:   initializing argument 3 of 'bool AsyncTask<queueLength>::registerAsyncCommand(t_commandID, uint32_t, t_callback) [with unsigned int queueLength = 10; t_commandID = unsigned int; uint32_t = long unsigned int; t_callback = void (*)(unsigned int)]'
     bool registerAsyncCommand(t_commandID id, uint32_t deltaT, t_callback cb) {

          ^~~~~~~~~~~~~~~~~~~~
Plusieurs bibliothèque trouvées pour "Ethernet.h"
Utilisé : /home/iznobe/Arduino/libraries/Ethernet
Non utilisé : /home/iznobe/arduino-1.8.12/libraries/Ethernet
Plusieurs bibliothèque trouvées pour "RTClib.h"
Utilisé : /home/iznobe/Arduino/libraries/RTClib
Non utilisé : /home/iznobe/Arduino/libraries/RTCLib_by_NeiroN
Non utilisé : /home/iznobe/Arduino/libraries/arduino_576780
Utilisation de la bibliothèque SPI version 1.0 dans le dossier: /home/iznobe/arduino-1.8.12/hardware/arduino/avr/libraries/SPI 
Utilisation de la bibliothèque Ethernet2-master version 1.0.4 dans le dossier: /home/iznobe/Arduino/libraries/Ethernet2-master 
Utilisation de la bibliothèque AsyncTask prise dans le dossier : /home/iznobe/Arduino/libraries/AsyncTask (legacy)
Utilisation de la bibliothèque RTClib version 1.4.0 dans le dossier: /home/iznobe/Arduino/libraries/RTClib 
Utilisation de la bibliothèque Time version 1.6 dans le dossier: /home/iznobe/Arduino/libraries/Time 
Utilisation de la bibliothèque Wire version 1.0 dans le dossier: /home/iznobe/arduino-1.8.12/hardware/arduino/avr/libraries/Wire 
Utilisation de la bibliothèque DS1307RTC version 1.4.1 dans le dossier: /home/iznobe/Arduino/libraries/DS1307RTC 
Utilisation de la bibliothèque Ethernet version 2.0.0 dans le dossier: /home/iznobe/Arduino/libraries/Ethernet 
exit status 1
Erreur de compilation pour la carte Arduino Mega or Mega 2560

les lib ethernet et ethernet2 n ' ont jamais ete compatible , pour la lib EthernetUdp et EthernetUdp2 , je ne sais pas , mais je sais que je n' ai pas pu installer l ' udp classique , il fallait celle que j ' ai mis avec Ethernet2 .

faite un essai avec ce simple fichier :

#include <Ethernet.h>
#include <Ethernet2.h>

void setup() {
    // put your setup code here, to run once:
}
void loop() {
    // put your main code here, to run repeatedly:
}

mais l'exemple seul, il fonctionne ? il faut commencer par là.

là c'est en le greffant dans ton code que ça foire, vu les messages d'erreur

Hello

Y a aussi ma simpleRTC avec une méthode qui règle directement le RTC à partir d'un timestamp unix NTP.

bricofoy:
mais l'exemple seul, il fonctionne ? il faut commencer par là.

là c'est en le greffant dans ton code que ça foire, vu les messages d'erreur

as tu teste le dernier fichier que j ' ai mis entre balise code ?

enleve ensuite une des deux lib et tu verras bien !

Salut ,

suite a l ' intervention de henry @hbachetti et avecl ' aidee de brocofoy et bricoleau , j ' ai reussi a faire un mix de tout ca pour faire fonctionner la recuperation de l ' heure sur serveur NTP pour mettre a jour la RTC avec les bibliotheques ethernet2 , ethernetUDP2 , et donc j ' ai utilisé la bibliotheque de Mr bricoleau : simpleRTC .

ensuite j ' ai fait un mic mac avec les fonctions des exemples de chacune et ca marche enfin , non pas sans mal !!

Merci a vous 3 :wink:

voici donc le code pour ceux qui serait dans le meme cas que moi :

les libs :

#include <EthernetUdp2.h>
#include <Wire.h>
#include "simpleRTC.h"

pour les declarations de variables suivre les exemples des libs concernées , mais le minimum pour l ' UDP est :

char timeServer[] = "ntp.com.univ-mrs.fr"; // time.nist.gov NTP server
const unsigned int localPort = 8888;       // local port to listen for UDP packets
const uint8_t NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
uint8_t packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

les fonctions utilisées :

unsigned long sendNTPpacket(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();
}

void reglerHeureByNTP () {
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  sendNTPpacket(timeServer);

  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
      //the timestamp starts at uint8_t 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:
		Serial.print("Unix time = ");
		// Unix time starts on Jan 1 1970. In seconds, that's 2208988800 :  subtract seventy years:
		unsigned long epoch = secsSince1900 - 2208988800UL;
		Serial.println(epoch);

		if ( epoch > 1585951635UL ) {  // => date du 04 / 04 / 2020 .
			RTC.reglerNTP(epoch); // met a jour la RTC par le serveur NTP
    }
  }
}

voilà !