Implementing NTP Time Synchronization Stops code from working?

Hello all,

I have an Arduino Mega 2560 with an ethernet shield (w5100) connected to a local database. My end goal is to send date time stamps whenever an input is detected, but for the sake of testing I’m sending data every 5 seconds. Attached is my code. “testWorks.ino” successfully sends a date time stamp to my local database, but once I implement NTP time synchronization (“testSimple.ino”), the Arduino stops connecting after 4 times. By this, I mean “Connection Established” prints to the serial monitor 4 times (goes through the loop block 4 times), and the database successfully receives the data. But it stops working after the 4th time, printing “Connection failed.” Does the 4th time have anything to do with max sockets equaling 4 in the Ethernet library?

Because “testWorks.ino” works, but “testSimple.ino” doe not work, I know there must be an issue with the NTP synchronization code, but I am new to all this and any help/suggestions will be appreciated. Thank you for your time.

testWorks.ino (1.55 KB)

testSimple.ino (4.95 KB)

Does the 4th time have anything to do with max sockets equaling 4 in the Ethernet library?

Seems like a reasonable starting point for investigation.

      client.print("GET /info.php?"); //GET request to write data to the database.
      client.print("request=");
      //client.print(mod);
       client.print(year()); if(month() < 10) { client.print("0"); }
       client.print(month()); if(day() < 10) { client.print("0"); } 
       client.print(day()); if(hour() < 10) { client.print("0"); }
       client.print(hour()); if(minute() < 10) { client.print("0"); }
       client.print(minute()); if(second() < 10) { client.print("0"); }
       client.print(second()); 
       client.println(""); 
      client.println(" HTTP/1.1");

The entire GET request is supposed to be on one line. println() is used ONLY to send the HTTP/1.1 terminator.

If you don’t call setSyncProvider(), so the getNtpTime() function never gets called, do you still lock up after 4 calls?

I was testing my code, and added the following lines in my setup loop: pinMode(4, OUTPUT); digitalWrite(4, HIGH);

And for some reason it now doesn't lock up after 4 calls. I even tested the code after commenting "pinMode(4, OUTPUT); digitalWrite(4, HIGH);" out, and it still worked (didn't lock up after 4 calls).

I also tested my code with and without calling setSyncProvider(), with both times working (no lock ups after 4 calls).

So far, the code is working, so I will post if I need any more assistance. Thanks for your help!