I'm using Arduino 1.0 on a mega with the wiz5100 chip, I was checking out the NTP client that came with the IDE changed a couple things, mainly the NTP server I'm talking to, I'm at the office right now or I would post the code (really like I said its the default example)
But every once in a while the project completely freezes I added a couple of Serial.println to find out where and its Udp.write line.
Does anyone have any ideas why the Udp.write would freeze ?
I can post my code tonight but I thought that I would put this out there to see if any one had any ideas.
Thanks
Edit: Here is the code
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(199,167,198,163); // pool.ntp.org NTP server
//IPAddress timeServer = "hera.limescope.net";
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
int timeZoneHour = -4;
long timeZoneOffset = (timeZoneHour * -1) * 60 * 60 ;
int NTP_Update_Interval = 60;
void setup()
{
Serial.begin(9600);
Serial.println("Starting");
// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
Udp.begin(localPort);
Serial.print("Got IP:");
Serial.println(Ethernet.localIP());
setSyncProvider(getNTPTime);
setSyncInterval(NTP_Update_Interval);
}
void loop()
{
time_t t = now();
printDigits(hour(t));
Serial.print(":");
printDigits(minute(t));
Serial.print(":");
printDigits(second(t));
Serial.print(" ");
Serial.print(month(t));
Serial.print("/");
Serial.print(day(t));
Serial.print("/");
Serial.print(year(t));
Serial.println();
delay(10000);
}
void printDigits(int digits){
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
boolean AlreadySent = false;
unsigned long getNTPTime()
{
Serial.println("In getNTPTime");
if (!Udp.parsePacket()) {
Serial.println("In Send Packet");
if (!AlreadySent) {
sendNTPpacket(timeServer);
Serial.println("Packet Sent");
AlreadySent = true;
} else {
Serial.println("Packet Already Sent Skip");
};
} else {
Serial.println("Got Time Packet");
Udp.read(packetBuffer,NTP_PACKET_SIZE);
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL + timeZoneOffset;
unsigned long epoch = secsSince1900 - seventyYears;
AlreadySent = false;
return epoch;
}
Serial.println("No Time Packet Found");
return 0;
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
Serial.println("In sendNTPpacket");
// 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;
Serial.println("NTP packet Created");
// 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
Serial.println("UDP begin Packet");
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
Serial.println("exiting sendNTPpacket");
}