6 Minutes Delay in NTP

Hi,
I am making a project to monitor certain parameters on xively.

I am using:

  • Arduino 1.0.6 I.D.E.
  • Arduino uno R3
  • Arduino wifi shield with WiFi firmware version: 1.1.0. (upgraded firmware to last version).

My Sheild is working good just had a issue that is shows 6 minutes delay in time.
Previously it was working fine with the same codes but today it is showing 6minutes delay

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <HttpClient.h>
#include <Time.h>


// ****** WiFi Settings ******
char ssid[] = "Hitech";    // your network SSID (name) 
char pass[] = "******";    // your network password (use for WPA, or use as key for WEP)
//int keyIndex = 0;  // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;

WiFiClient client;



// ****** NTP Server Settings ******
// Local time server
IPAddress timeServer(216, 171, 112, 36);
// GMT offset (in seconds) - currently set for GMT + 5.5
const long timeZoneOffset = 19800L;   
// Syncs to NTP server every x seconds
unsigned int ntpSyncTime = 3600;         
// Local port to listen for UDP packets
unsigned int localPort = 8888;
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE= 48;      
// Buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];  
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;                    
// Keeps track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;    
// Check last time clock displayed (Not in Production)
time_t prevDisplay = 0;  
//boolean timeUpdated = false;
//int timeIncrement = 0;


void setup() {
  
  Serial.begin(9600);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass); 
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
   
  //Try to get the date and time
  int trys=0;
  while(!getTimeAndDate() && trys<10) {
    trys++;
  }
   
  // calibrate light limits

 
  }



void loop() {
  
  //Reset time once per day
  //if (timeIncrement >= 3) {
     // Update the time via NTP server as often as the time you set at the top
    if(now()-ntpLastUpdate > ntpSyncTime) {
      int trys=0;
      while(!getTimeAndDate() && trys<10){
        trys++;
      }
      if(trys<10){
        Serial.println("ntp server update success");
        //timeIncrement = 0;
      }
      else{
        Serial.println("ntp server update failed");
      }
    }
    
    // Display the time if it has changed by more than a second.
    if( now() != prevDisplay){
      prevDisplay = now();
      clockDisplay();  
    }

  //timeIncrement += 1;
  delay(60000);
 }
  
int getTimeAndDate() {
  int flag=0;
  Udp.begin(localPort);
  sendNTPpacket(timeServer);
  delay(10000); // set delay
  if (Udp.parsePacket()){
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    unsigned long highWord, lowWord, epoch;
    highWord = word(packetBuffer[40], packetBuffer[41]);
    lowWord = word(packetBuffer[42], packetBuffer[43]);  
    epoch = highWord << 16 | lowWord;
    epoch = epoch - 2208988800 + timeZoneOffset;
    flag=1;
    setTime(epoch);
    ntpLastUpdate = now();
  }
  Udp.stop();
  return flag;
}

unsigned long sendNTPpacket(IPAddress& address) {
  memset(packetBuffer, 0, NTP_PACKET_SIZE); 
  packetBuffer[0] = 0b11100011;
  packetBuffer[1] = 0;
  packetBuffer[2] = 6;
  packetBuffer[3] = 0xEC;
  packetBuffer[12]  = 49; 
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;		   
  Udp.beginPacket(address, 123);
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

void clockDisplay() {
  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) {
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

if i change the line
const long timeZoneOffset = 19800L;
to
const long timeZoneOffset = 19800L+375L; it works fine but it will some temporary solution
Is there any thing i need to change like the ntp server or any other solution

// Keeps track of how long ago we updated the NTP server

I really don't think you should be updating the NTP server. I'm guessing that you screwed one of them up doing that.

Your NTP server has probably lost its NTP server. I get about a 6 minute error using your NTP IP as a NTP server. I get the correct time using a current NTP server by resolving pool.ntp.org with nslookup. I got 149.20.68.17, and it returns the correct time.

@Paul
Its the same code i was using from beginning.
I kept my shield on testing and its was working and it was working good suddenly on 27th it started to give a delay of around 6 seconds so i added

const long timeZoneOffset = 19800L+315L

in the code and it is working as before but this is not a correct solution is there any changes in the server or anything else above my head please guide!!

@ SurferTim,
I am new to this , if u stay at different locations like me in india and u in some other part of world do we have to use other IP's for ntp.

Then use nslookup to get a local NTP server. The domain name "pool.ntp.org" should resolve to a domain you can use.

nslookup pool.ntp.org

@SurferTim,
If u stay at different locations like me in india and u in some other part of world do we have to use other IP's for ntp

I don't think so. All NTP servers return Coordinated Universal Time.

The closer the server is to the client, the less chance of a failure though.