Wifi Shield hangs out after 60 minutes

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 after every 60 mintues it got hangs.
I Googled and found that if i update firmware this will be fixed but after firmware update it issue still persists

#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");
}

Ntp report.txt (4.45 KB)

I've not used UDP but I think it should begin only once in setup, not repeatedly in your function call.

unsigned int ntpSyncTime = 3600;

This should be unsigned long since you are using it in a calculation involving unsigned long. Don't take chances. Same as delay(60000);. Should be 60000UL

Your programming style needs improvement ;)

You should update ntpSyncTime in loop, not in a function call getTimeAndDate. The function call should do what the name says, get time and date and return sucess or failure. Let the caller decide whether to update ntpSyncTime . The function has no use for ntpSyncTime so why updating it in the function? It's a mess to define important variables as global variables and then update them whenever you see fit.

But that code is for getting the time from the server so as to sync it with the network time anyways i have changed it 100days and it s now working fine Along with that i have added a watchdog timer for every 3 hours so problem is solved but can it be done by keeping it synching it every hour.

amirchaudhary: But that code is for getting the time from the server so as to sync it with the network time anyways i have changed it 100days and it s now working fine Along with that i have added a watchdog timer for every 3 hours so problem is solved but can it be done by keeping it synching it every hour.

Try to begin UDP twice in a row and see if you are immediately screwed up. I suspect that's the real cause. What happens after 100 days? Do you care if it screws up again or is 100 days beyond the scope of your project's run time? I thought that code was to get synced every hour. Cause there's no reason it won't. You didn't change anything.

Does the watchdog timer begin UDP?