Arduino NTP Client using EtherShield library

Print Friendly, PDF & Email

This code is snippet of a larger project I am working on. The project requires time synchronization so I decided to use NTP to retrieve time from server as the project hardware (Arduino ATMega328 board + ENC28J60 based shield) already had the the QR to execute the requirement.

The NTP server is a Windoze 2008 R2 server and my sketch is based on one published on github. However some tweaks have been done to make it compatible with my arduino board.

The sketch is for a simple network and does not cater for routers, firwall gateways and works..

Here are some of the salient changes.

  1. Static IP being used.
  2. Timestamp offset corrected to 1970 instead of 2000.
  3. set_mac() function added to set correct mac address of the destination ntp server.. Without this the packets are sent with destination mac address as 00:00:00:00:00:00
  4. A positive offset of 19800 seconds has been added to get Indian Standard Time.
#include <Wire.h>
#include <RTClib.h>
#include <EtherShield.h>

// Jan 1 
#define SECONDS_FROM_1900_TO_1970 2208988800

static uint8_t mymac[6] = {
  0x54,0x55,0x58,0x10,0x00,0x25};

// IP and netmask allocated by DHCP
static uint8_t myip[4] = { 159,12,23,149 };
static uint8_t mynetmask[4] = { 255,255,255,0 };
static uint8_t gwip[4] = {159,12,23,150};
// From http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers
byte ntpServer[4] = {159,12,23,150};
// Packet buffer, must be big enough to packet and payload
#define BUFFER_SIZE 550
static uint8_t buf[BUFFER_SIZE+1];

EtherShield es=EtherShield();
uint32_t lastUpdate = 0;
uint32_t time;
static const char day_abbrev[] PROGMEM = "SunMonTueWedThuFriSat";

void setup(){
  Serial.begin(19200);
  Serial.println("EtherShield NTP Client");

  // Initialise SPI interface
  es.ES_enc28j60SpiInit();
  // initialize enc28j60
  es.ES_enc28j60Init(mymac);
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  Serial.print( "ENC28J60 version " );
  Serial.println( es.ES_enc28j60Revision(), HEX);
  if( es.ES_enc28j60Revision() <= 0 ) {
    Serial.println( "Failed to access ENC28J60");
    while(1);    // Just loop here
  }

  // init the ethernet/ip layer
//  es.ES_init_ip_arp_udp_tcp(mymac,myip, 80);
//  es.ES_client_set_gwip(gwip);  // e.g internal IP of dsl router
  lastUpdate = millis();
}

// Output a ip address from buffer from startByte
void printIP( uint8_t *buf ) {
  for( int i = 0; i < 4; i++ ) {
    Serial.print( buf[i], DEC );
    if( i<3 )
      Serial.print( "." );
  }
}

void loop(){
  uint16_t dat_p;
  uint16_t clientPort = 123;
  char dstr[4];
  int sec = 0;
  int plen = 0;

  // Get IP Address details

  // Main processing loop now we have our addresses
  while(1) {
    // handle ping and wait for a tcp packet
    dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
    // Has unprocessed packet response
    if (dat_p > 0)
    {
     time = 0L;
      if (es.ES_client_ntp_process_answer(buf,&time,clientPort)) {
        Serial.print("Time:");
        Serial.println(time); // secs since year 1900

        if (time) {
          time -= SECONDS_FROM_1900_TO_1970; //calculate no of seconds after 2000
          time +=19800; //add offset of +5hr 30 min for IST (Indian Standard Time)

          DateTime now(time);

          Serial.print(now.year(), DEC);
          Serial.print('/');
          Serial.print(now.month(), DEC);
          Serial.print('/');
          Serial.print(now.day(), DEC);
          Serial.print(' ');
          int i=0;
          while (i<3){
            dstr[i]= pgm_read_byte(&(day_abbrev[(now.dayOfWeek()-1) * 3 + i])); 
            i++;
          }
          dstr[3]='\0';

          Serial.print( dstr );
          Serial.print(' ');
          Serial.print(now.hour(), DEC);
          Serial.print(':');
          Serial.print(now.minute(), DEC);
          Serial.print(':');
          Serial.print(now.second(), DEC);
          Serial.println();
        }
      }
    }
    // Request an update every 5s
    if( lastUpdate + 5000L < millis() ) {
      // time to send request
      lastUpdate = millis();
      Serial.print("Send NTP request ");
      if (set_mac(ntpServer))
      es.ES_client_ntp_request(buf,ntpServer,clientPort);

    }

  }
}

boolean set_mac(uint8_t *remIP){
  uint32_t endtime,dat_p;
  uint16_t timeout=2000; //timeout ping after 500 milli seconds
  endtime=millis();
  Serial.print("Senidng ARP Request to ");printIP(remIP);Serial.println();
  es.ES_client_set_gwip(remIP);
    while(1){
      if(millis()-endtime>=timeout){Serial.print("Timeout Waitin for Ping Reply ");Serial.print(endtime);Serial.println();return false;}//timeout ping
       int plen=es.ES_enc28j60PacketReceive(BUFFER_SIZE,buf);
       dat_p=es.ES_packetloop_icmp_tcp(buf,plen);
       if(dat_p==0) {
          // we are idle here
          if (es.ES_client_waiting_gw() ){
          continue;
          }

        }
        if (!es.ES_client_waiting_gw() ){Serial.println("Got ARP Reply");return true;}

}

}

John Mark Osin from Philippines went one step ahead and created a beautiful working NTP clock for his college as part of his project. Here are some snaps of his fantastic build

Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in Arduino, Arduino, Projects and tagged , , , , , , . Bookmark the permalink.

65 Responses to Arduino NTP Client using EtherShield library

  1. John Mark says:

    Hi Ishan,

    I’m John Mark from Philippines. I am also an Electronics Engineering student. I’ve tried your NTP client code with my Arduino Duemilanove with ATMega328 and ENC28J60 Ethernet Shield I’d bought from ITeadStudio. I’ve changed some parts of the code like the IP address, MAC address, default gateway address, offset time (+8), and NTP servers, etc… I am using the network on our school where my Instructor give me an IP address I can use. I’ve monitored the device via serial monitor. Unfortunately I can’t get a reply from any NTP servers including the server you’ve written in the code. I’ve tried a lot of servers I’ve found from the Internet and no one really works or give a reply to the request of our Ethernet Shield. By using the above code, Is your Ethernet Shield got a reply from an NTP server? Can you help me on these? Any suggestions Ishan?

    Regards,
    John Mark

    • Ishan Karve Ishan Karve says:

      Hi John

      Have you checked whether network card is working properly?
      You can check by using popular network sniffer Wireshark.. It is very helpful…and is available for windows and linux.
      In wireshark, set the filter for ARP.
      then see whether the ethernet shield is transmitting any arp packets.
      Once you are able to see ARP packets and their response, then you can be assured that your hardware connections are alright.

      Let me know if you have completed this stage.

  2. John Mark says:

    Hi again Ishan,

    I’ve downloaded the Wireshark. I’m really new to this so i just searched a tutorial video from youtube. I’ve set the display filter ARP and it capture the packets from my ethernet shield. It displays all the info about the packets, my MAC address, IP address, the target IP address, etc. I think the shield transmits ARP packets to its destination (159.12.23.150). I think my connection is alright… Maybe the NTP servers doesn’t respond to my ARP request.

    Regards,
    John Mark

  3. John Mark says:

    This is always the display in my serial monitor…
    It is an NTP server of Japan. I’ve tried lots of servers and no one really works.
    I don’t know what’s the problem.

    Ethernet Shield NTP Client
    ENC28J60 version 7

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 33197 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 63198 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 93198 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 123199 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 153200 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 183201 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 213202 milliseconds.

    Send NTP request…
    Sending ARP Request to 133.243.238.243
    Timeout! Waiting for Ping Reply: time = 243204 milliseconds.

    • Ishan Karve Ishan Karve says:

      Thats a descriptive debug.
      I had a similar situation like what you mentioned above for quite many a days. Wiresharking the transmitted packets helped me identify the problem.
      It was observed that the ethernet shield changes it mac address during every ntp requests may be 1 or 2 bytes. Can you verify the same and check.

  4. Ishan Karve Ishan Karve says:

    i just kept the tcpip loop simple can uemail me ur code.. i will test on my setup

  5. John Mark says:

    Ahmm. okay. Can you give me your email address? I will send it to you.

  6. John Mark says:

    I’ve already send my code. Only the addresses and the serial monitor display has been changed and added from your original code you’ve posted. But I’m hoping you that can help me. A big thanks to you Ishan.

  7. John Mark says:

    On the wireshark on the destination column sometimes it changes from Broadcast to Portwell_18:b4:ba and QuantaCo_a5:2e:fd. I don’t know what it means. Hmm, honestly I don’t really know how read and interpret the data in wireshark.

    • Ishan Karve Ishan Karve says:

      It appears that the NTP server is probably dead. Not responding to pings from my PC.
      You can get a list of time servers from here
      Secondly the script was written by me for some other related project. Hence the test setup was for an intranet scenario.
      If you see this line

      1
      2
      if (set_mac(ntpServer))
            es.ES_client_ntp_request(buf,ntpServer,clientPort);

      This is where the problem lies. In an intranet scenario (small scale) there are no gateways and you can directly request MAC address from any host by sending it an arp request.
      But in case you are behind a gateway, as is the case when you are on internet there is a need to fetch mac address of remote machine via the gateway.
      This is easily done by modifying the above code block to

      1
      2
         if (set_mac(gwip))
            es.ES_client_ntp_request(buf,ntpServer,clientPort);

      Lastly change netmask to

      1
      static uint8_t mynetmask[4] = { 255,255,255,0 };

      A sample screen shot is here Arduino NTP Console output
      Do these two changes and the code will work (fingers crossed). Also you may try playing/tweaking around with the timeout values.Keep me posted.
      In the interim, check your mailbox for the tweaked code (change network settings as per your setup)

  8. John Mark says:

    Hi,

    I tried your suggestion by sending arp request to our gateway. Unfortunately. nothing happens, still there is no reply. Do you think the problem is in our network/gateway? It seems like our request is not permitted by the administrator. I think I have to ask my instructor or any staff in our school regarding this situation.

    Regards,
    John Mark

  9. John Mark says:

    Unfortunately, I don’t have access to any linux machine. I have already inform my instructor about the situation and he contact a staff assigned in dealing with the networking in our school. Maybe the IP address he had given to us is still blocked and cannot access the Internet… We will found out later…

  10. John Mark says:

    The Technician already let our IP address/device be connected to Internet. I ping every IP address of NTP servers I could find to see if its still active and tried it in the code but still i couldn’t get any reply… I really don’t know what’s wrong with the code or the network or the servers… Hmm, can I connect the shield to a computer with an NTP time server software? 🙁

  11. John Mark says:

    Ethernet Shield NTP Client
    ENC28J60 version 7

    Send NTP request…
    Sending ARP Request to 172.27.3.7 (default gateway)
    Timeout! Waiting for Ping Reply: time = 13196 milliseconds.

    Send NTP request…
    Sending ARP Request to 172.27.3.7 (default gateway)
    Timeout! Waiting for Ping Reply: time = 23197 milliseconds.

    Send NTP request…
    Sending ARP Request to 172.27.3.7 (default gateway)
    Timeout! Waiting for Ping Reply: time = 33199 milliseconds.

    Send NTP request…
    Sending ARP Request to 172.27.3.7 (default gateway)
    Timeout! Waiting for Ping Reply: time = 43199 milliseconds.

  12. John Mark says:

    I have mailed you a snapshot of the ping results… please check if you want…

  13. John Mark says:

    Okay, I’ve emailed you a snapshot of my WireSharking. Please check. Thank you.

    • Ishan Karve Ishan Karve says:

      I saw that.. it appears that the gateway is not responding to ping requests.
      Just for troubleshooting.. Try replacing the gateway ip by your pc ip and see whether it replies arp reply

  14. John Mark says:

    This is what the serial monitor displays:

    Ethernet Shield NTP Client
    ENC28J60 version 7

    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply
    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply
    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply
    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply
    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply
    Send NTP request…
    Sending ARP Request to 172.27.3.81 (default gateway)
    Got ARP Reply

    172.27.3.81 is our pc IP address…

  15. Ishan Karve Ishan Karve says:

    It is obvious that the shield is sending and receiving arp requests. It seems that the gateway is blocking/not responding to arp requests. Can you attempt connecting beyond your institution gateway or may be at home..

    N ways .. once at home I will try to look at a solution for problem..

  16. John Mark says:

    I’m also thinking that’s the problem. Hmm, I will approach again the technician… hopefully, he can help. I will also try the shield at home… I hope this will work soon. By the way, my aim for this project is to get time from the ntp server and hopefully i will be able to display the time to my digital clock ckt (7-segment LED displays). I am using a MAX7221 LED display driver and some high-power drivers to drive multiple LEDs.

  17. Ishan Karve Ishan Karve says:

    Thats a nice project.. However I suggest that you also use a RTC chip like DS1302/7 for timekeeping. Use NTP to update the RTC time. This way you will keep the processor free for other task. I have implemented a similar setup (NTP updating RTC) ..

    Most important share your build details with teh world.

  18. John Mark says:

    Yeah, I will take that into account… Hopefully, I can make this project. Thanks.

  19. John Mark says:

    Hi Ishan,

    According to the technician, the IP address assigned to the ethernet shield has already been activated and granted all access to the network. But still the problem is there. I’m stuck with:
    “Timeout! Waiting for Ping Reply: time = . . . .”

    Is there any way that I can install a software into a pc that can make it an NTP time server (it is synchronized to external NTP servers) so that the shield can acquire time data from that local time server? Or can there be a way that instead of acquiring time data from the NTP servers, I could just get time from the local network server of our school?

  20. John Mark says:

    Can I use my laptop or any PC to install the program and be a local NTP server?

  21. John Mark says:

    Hi Ishan,

    I just wanna ask what these lines do:
    time -= SECONDS_FROM_1900_TO_1970;
    time +=28800;

    I already installed the Net Time Server & Client application you recommended which uses the NTP/SNTP time servers. I’m still unfamiliar with it but I tried what I can do. I was able to get the following display on my serial monitor:

    Ethernet Shield NTP Client
    ENC28J60 version 7

    Time:3538197575
    2042/2/13 Wed 16:39:35
    Send NTP request…
    Sending ARP Request to 172.27.3.81
    Got ARP Reply
    Time:3538197585
    2042/2/13 Wed 16:39:45
    Send NTP request…
    Sending ARP Request to 172.27.3.81
    Got ARP Reply
    Time:3538197595
    2042/2/13 Wed 16:39:55
    Send NTP request…
    Sending ARP Request to 172.27.3.81
    Got ARP Reply
    Time:3538197605
    2042/2/13 Wed 16:40:5
    Send NTP request…
    Sending ARP Request to 172.27.3.81
    Got ARP Reply
    Time:3538197615
    2042/2/13 Wed 16:40:15

    I just wonder why the time is advanced to 30 years… Can you help?

  22. Ishan Karve Ishan Karve says:

    Hurray.. congratulations John. Your efforts have paid off.. You can adjust the time skew by reducing the number of seconds from variable time it should be equal to (30*365*24*60*60)
    add this line time-=946080000;

  23. John Mark says:

    Hi Ishan,

    I was able to get time from the NTP server I have installed in my laptop. I was able to display it to my big LED display. The thing is, whenever an update is received by the shield and the microcontroller, the display would pause and jump to the updated time. I placed a loop in the program (counting up from the last update) to make it somewhat stand alone from the time update of my NTP server. So as what you have suggested, I bought a DS1307 RTC chip for timekeeping. I was able to set the time of the RTC but I notice it isn’t showing the same time as the time in my computer and the NTP server. I don’t know how to update the time of the RTC chip to the NTP time. Can you help me with the syntax? Because you have mention that you have implemented an NTP updating RTC.

    Regards,
    John Mark

  24. Ishan Karve Ishan Karve says:

    Check up my post http://karve.in/?p=255,
    Regarding the query to update RTC via NTP see the function definition for get_ntp_time().

    BTW. What time is your RTC showing. In case you have not tested your RTC, I recommend you do that first. LadyADA’s RTC blog post is a nice start.

    • John Mark says:

      When I serial monitor the chip, it’s late about 2minutes+ from my laptop’s and NTP server’s time. I followed a Tutorial and I don’t know why it’s still late since it gets the time from the time the chip is uploaded/compile…. I tried again the procedure but still it displays the late time. I will visit your post. Thanks.

  25. John Mark says:

    Hi,

    I’m just wondering how can I get the time data of the RTC so that I could display that to my LED display. This part of the code

    Serial.print(now.minute(), DEC);

    and others are used to display the hour,minutes,second,etc. derived from the seconds given by the NTP server. But we used that value (e.g. now.minute) to be displayed to our LED display, because we thought that value is coming from RTC. I really don’t know how to get RTC values. Here’s part of the code:

    void loop()
    {
    uint16_t dat_p;
    uint16_t clientPort = 123;
    char dstr[4];
    int sec = 0;
    int plen = 0;

    // Get IP Address details

    // Main processing loop now we have our addresses
    while(1)
    {
    // Handle ping and wait for a TCP packet
    dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
    // Has unprocessed packet response
    if (dat_p > 0)
    {
    time = 0L;
    if (es.ES_client_ntp_process_answer(buf,&time,clientPort))
    {
    Serial.print(“Time:”);
    Serial.println(time); // seconds since year 1900

    if (time)
    {
    time -= SECONDS_FROM_1900_TO_1970; // calculate no. of seconds after 2000
    time +=28800; // add offset e.g (+8) = 28800 seconds
    DateTime now(time);
    DateTime RTC_now = RTC.now();
    //sync RTC to NTP
    RTC.adjust(DateTime(now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second()));

    Serial.print(now.year(), DEC);
    Serial.print(‘/’);
    Serial.print(now.month(), DEC);
    Serial.print(‘/’);
    Serial.print(now.day(), DEC);
    Serial.print(‘ ‘);
    int i=0;

    while (i 12)
    {
    tenThou = (var – 12) % 10;
    hunThou = (var – 12) / 10;
    }
    else
    {
    tenThou = var % 10;
    hunThou = var / 10;
    }

    lc.clearDisplay(0);

    lc.setDigit( 0, 0, hunThou, false);
    lc.setDigit( 0, 1, tenThou, true);
    lc.setDigit( 0, 2, thou, true);
    lc.setDigit( 0, 3, huns, true);
    lc.setDigit( 0, 4, tens, true);
    lc.setDigit( 0, 5, ones, false);
    }
    }
    }

    // Request an update every 10 seconds
    if( lastUpdate + 10000L < millis() )
    {
    // time to send request
    lastUpdate = millis();
    Serial.print("Send NTP request… ");
    Serial.println();
    if (set_mac(ntpServer))
    es.ES_client_ntp_request(buf,ntpServer,clientPort);
    }

    }

    }

  26. John Mark says:

    void loop()
    {
    uint16_t dat_p;
    uint16_t clientPort = 123;
    char dstr[4];
    int sec = 0;
    int plen = 0;

    // Get IP Address details

    // Main processing loop now we have our addresses
    while(1)
    {
    // Handle ping and wait for a TCP packet
    dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
    // Has unprocessed packet response
    if (dat_p > 0)
    {
    time = 0L;
    if (es.ES_client_ntp_process_answer(buf,&time,clientPort))
    {
    Serial.print(“Time:”);
    Serial.println(time); // seconds since year 1900

    if (time)
    {
    time -= SECONDS_FROM_1900_TO_1970; // calculate no. of seconds after 2000
    time +=28800; // add offset e.g (+8) = 28800 seconds
    DateTime now(time);
    DateTime RTC_now = RTC.now();
    //sync RTC to NTP
    RTC.adjust(DateTime(now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second()));

    Serial.print(now.year(), DEC);
    Serial.print(‘/’);
    Serial.print(now.month(), DEC);
    Serial.print(‘/’);
    Serial.print(now.day(), DEC);
    Serial.print(‘ ‘);
    int i=0;

    while (i 12)
    {
    tenThou = (var – 12) % 10;
    hunThou = (var – 12) / 10;
    }
    else
    {
    tenThou = var % 10;
    hunThou = var / 10;
    }

    lc.clearDisplay(0);

    lc.setDigit( 0, 0, hunThou, false);
    lc.setDigit( 0, 1, tenThou, true);
    lc.setDigit( 0, 2, thou, true);
    lc.setDigit( 0, 3, huns, true);
    lc.setDigit( 0, 4, tens, true);
    lc.setDigit( 0, 5, ones, false);
    }
    }
    }

    // Request an update every 10 seconds
    if( lastUpdate + 10000L < millis() )
    {
    // time to send request
    lastUpdate = millis();
    Serial.print("Send NTP request… ");
    Serial.println();
    if (set_mac(ntpServer))
    es.ES_client_ntp_request(buf,ntpServer,clientPort);
    }

    }

    }

    • Ishan Karve Ishan Karve says:

      Its pretty simple .. You have already instantiated the RTC class by declaring
      DateTime RTC_now = RTC.now();
      The next step is to simply use it..
      int hour=RTC_now.hour();
      int minute=RTC_now.minute(); // and so on

      It appears that you have not declared variable ‘var’ and I do not see var being assigned any value… you may like to check that

  27. John Mark says:

    The var was already declared in the code (it seems like there was some missing part of my message yesterday) but then it still displays 00:00:00… It seems like the RTC wasn’t updated… So I tried some other revisions I found from Google… And after some struggling moment, I was able to display the time to my LED display.

    I used some function in the Wire.h library to convert DEC time data and send the BCD-converted time data derived from the NTP server to the RTC chip so that it will be updated. And also so that the RTC could send its time (converted back to DEC) to the microcontroller. There was still some problem with the drift of the RTC (it counts quite faster) compared with the server specially if it not updated for quite some time (say 30 mins). I decided to update it every 15 mins or less to minimize the clock drift.

    I’m really grateful for all the help you gave me… You really helped a lot in the development of this project. Thank you, thank you, thank you! Namaste! More power and I hope there will be more project you can share to the techno world.

    • Ishan Karve Ishan Karve says:

      Thanks for those kind words.. I am glad to be of help. You also deserve to give urself a well earned treat. Please share your build with the world and if you can mail be some snaps of your build , I would be delighted to showcase them here.

      Thanks and wish you all the best in your future endeavors.

      Ps: RTC displaying 00:00:00 is hinting towards an issue with your backup battery supply of RTC. If your RTC is drifting.. check your crystal frequency or change it

  28. John Mark says:

    Hi Ishan,

    I am wondering what could possibly be the reason why my RTC suddenly resets itself. At first it will display the current time and be updated by the server, then after around 15 or 20 mins, it suddenly resets itself and the display says 00:00:00. I tried re-initiating the RTC and upload the code again to the Arduino. It only fix the problem for a short time and then the irregularity occurs again. I hope you can give me some idea what will I do. Thanks a lot.

    Regards,
    John Mark

    • Ishan Karve Ishan Karve says:

      I think my hunch is correct .. there is a problem with your RTC battery or your RTC crystal…may be dry solder. Once the RTC sets back to 00:00:00.. does it update itself by using NTP. if not then there is definitely an issue with your RTC wiring.

  29. John Mark says:

    Hi,

    Yeah, I also think so… Whenever it resets itself, it doesn’t update itself via the NTP anymore. Do you think I should solder it again? I bought it pre-assembled (DS1307 RTC Breakout Board) from a local distributor.

  30. John Mark says:

    I’m really confused which pin (3.3V or 5V) I should place the VCC. When I tried pinning on 3.3V pin of the Arduino, it can’t get time. But I saw some examples in which the VCC is connected to the 3.3V. Does it matter where I pin the VCC (whether on the 3.3V or the 5V)?

    • Ishan Karve Ishan Karve says:

      Please read the datasheet of DS1307 carefully… i quote from the datasheet
      “VCC, GND – DC power is provided to the device on these pins. VCC is the +5V input. When 5V is applied within normal limits, the device is fully accessible and data can be written and read. When a 3Vbattery is connected to the device and VCC is below 1.25 x VBAT, reads and writes are inhibited. However, the timekeeping function continues unaffected by the lower input voltage. As VCC falls below VBAT the RAM and timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT”

      Have you connected VBAT and if yes how

  31. John Mark says:

    Ahhmm, I will send to you the datasheet of the RTC I bought. It has a separate male header connection for battery? and I don’t know what it’s for… check you’re mail.

    • Ishan Karve Ishan Karve says:

      It also says the same thing… have you connected anything to BT1 terminal header. If yes what is the battery and what voltage does it read.

      If not, are you using the on board battery ? I see there are two battery clips. They seem to be connected in series (surprising). Have you then inserted 2 batteries?

  32. John Mark says:

    Hi Ishan,

    Yes, I have inserted 2 batteries. When I bought the kit, the batteries are already there with it. Maybe you could take a look on the code I have revised and currently using. I’m wondering that maybe there’s something wrong with the program since when I turned on the digital clock yesterday morning (after turning it off the other night), it displays the current time. Then after a while, it reset itself. Maybe you could check if there are some functions in the code that could probably cause the problem. I will send it to your e-mail. Thanks.

    Regards,
    John Mark

  33. John Mark says:

    Hi Ishan,

    I found out what’s the problem with the RTC. It has some missing components (a diode and a resistor). I realized it when I bought another one today. So, since I cannot asked for replacement of the other one (’cause I already solder it), I decided to buy another one. I need two RTCs for the two units I am making. I run the two units and it works fine. My problem now is that I can’t synchronize it with my server. The two units can be updated by the server but only one at a time. I cannot update them simultaneously. When I set the IP address of one of the units, that unit will be updated and the other one will not. I am not really familiar with the server itself so I don’t really know what some of the configurations do. I am using the trial version of Net Time Server & Client V3.0.

    By the way, I tried to test the two units or the two ethernet shields earlier. I uploaded the old code with a serial print function of “Send NTP request” , Sending ARP request to…”, Got ARP reply… of course the two units have different IP address but with the same netmask of 255.255.255.0. I configured the Net Time Server, on the SNTP service, I edited the settings many times; until the Target IP I’ve set on the broadcast is 255.255.255.0, then by serial monitoring the two device from separate laptop (of course!) We observed that the two device got the broadcast ntp packet (seconds since 1900) but sometimes they differ in receiving the packet about 1 second.

    Now, when we uploaded our final program, and tried to configure again the settings of the SNTP service (Target IP: 255.255.255.0), so that we can monitor the updating of time through our LED displays BUT unfortunately, the two units did not receive any update from the server. Hmm, the final code we uploaded had no NTP or ARP requesting since the server will be the one to give or broadcast the time update periodically depending on our settings. We omitted those functions. Is it okay? or we have to put it back so that the server will know that the devices is requesting…. Do you have some ideas or suggestions regarding this issue? please help me again on these..

    Regards,
    John Mark

    • Ishan Karve Ishan Karve says:

      are you having two separate arduino’s each with its own RTC or are you having two RTC on one arduino.

      If you are having 2 seperate setups.. have both the setups different IP and MAC adresses?

      • John Mark says:

        Yeah, I do have two separate setups with two different sets of IP & MAC addresses.

        • Ishan Karve Ishan Karve says:

          I’ve set on the broadcast is 255.255.255.0, then by serial monitoring the two device from separate laptop (of course!) We observed that the two device got the broadcast ntp packet (seconds since 1900) but sometimes they differ in receiving the packet about 1 second. This is ok.. peculiar to SNTP.. however there should not be any time variation between both the devices after update.

          I am not able to guess the problem. May be you can test each unit individually and then connect them together.

  34. John Mark says:

    Hi Ishan,

    I’ve emailed you last night about my problem in supplying my arduino. Please check. Thanks.

    Regards,
    John Mark

    • Ishan Karve Ishan Karve says:

      Hey John was away from home… saw your mail.. however I cannot comment much on that as I am not familiar with the inner working of PoE.. However I will still try to have a look at your schematic..

  35. John Mark says:

    Hello Ishan,

    My clock is now fine I already traced up the problem. It seems like a bad case of soldering is causing all the trouble in the device (the reason why it resets itself). There was a miss in soldering in the female headers soldered in my Ethernet shield. I placed some headers to my Ethernet shield so that I could connect the RTC to it. When I tried to operate it without the shield, it works well, so I realized maybe there’s something wrong with the shield and there I found it. Now it works quite well.

    About the heating up of the regulators, I tried to put some remedy by placing a larger heatsink on the voltage regulator of the Arduino and adding heatsink on the voltage regulators (that is, there is a heatsink connected to the individual heatsinks of the 3 voltage regulators: 9V, 5V, 15V). Somehow it minimizes the heating up.

    I already displayed one of the units near our school gate so that students can view the time as they enter the school. I will send you some images of the prototype in your email. I hope you could check it out.

    Thank you so much for your help. You are one of the greatest contributor of this project. I could have never done it without your help. Thank you, thank you. Namaste!

    • Ishan Karve Ishan Karve says:

      Congratulations John.. The snaps are absolutely amazing and I would be proudly posting here on my site.. Your prototype is leagues ahead of mine… And that is the power of Open Source… Keep designing .. Keep the electrons flowing… Hurray!!

      BTW: Submit your project to Hackaday.com and electronics-lab.com

  36. Miguel says:

    Hi Ishan,

    I’ try make a project like you but i can´t send packet NTP out from my PC. I use the Wireshark to get the packet, it stay this way in first 6 bytes :

    00 00 a2 00 00 00

    I thing this is the problem because, via software NTP PC, this 6 bytes stay this :
    90 f6 52 fc 92 7c

    It´s possible?

    Regards,
    Miguel

    ps : my english é very poor.

Leave a Reply

Your email address will not be published. Required fields are marked *