Here is another example of a UDP hang during NTP request. This is built on the UDPNTPClient code in Arduino 1.0. Sketch was working before code was added to service the LCD117 board and Dallas Temp sensors. I am looking to add NTP update of ds1307 RTC when this gets working. Any help appreciated. Entire sketch does not fit. Trimmed version may not help much.
[code// Compile with Arduino IDE version 1.0 Comments removed to save space
// Libraries :
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <Time.h>
#include <DS1307RTC.h>
//#include <DS1307new.h>
#include <Wire.h>
#include <OneWire.h>
#include <SoftwareSerial.h>
#include <Ping.h>
// Valve Position defines
#define ON HIGH
#define OFF LOW
#define recirculate LOW
#define heating HIGH
// This is the control pin for the valve actuator relay
const int Valve_Pin = 12;
const int SolarValve = 15;
// pin 13 LED
const int LEDpin = 13 ; // Winker LED on pin 13
//
#define txLCDPin 14
#define rx1WPin 10
#define tx1WPin 8
//
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
//
// Setup a OneWire bus instance to communicate with any OneWire
// devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
//
// Pass our OneWire reference to Dallas Temperature Library.
DallasTemperature sensors(&oneWire);
SoftwareSerial myLCDSerial = SoftwareSerial(rx1WPin, txLCDPin);
// Arrays to hold device addresses
// uint8_t poolThermometer[8], airThermometer[8], roofThermometer[8];
// Assign address manually. The addresses below will need to be changed
// to valid device addresses on your buss. Device addresses can be retrieved
// by using either the sensors.search(deviceAddresses) or individually via
// sensors.getAddress(deviceAddress, index)
uint8_t poolThermometer[8] = {0x28, 0x26, 0x84, 0x8f, 0x2, 0x0, 0xc2, 0x0};
uint8_t airThermometer[8] = {0x28, 0xd1, 0xa7, 0x8f, 0x2, 0x0, 0x0, 0x83};
uint8_t roofThermometer[8] = {0x28, 0xca, 0x7b, 0x8f, 0x2, 0x0, 0x0, 0x5};
//used by Print Double routine
double x;
double y;
double z;
double LastPoolTemp;
double LastRoofTemp;
byte ValvePositionCurrent;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xfE, 0xeD, 0xad, 0x06, 0xF0, 0x0D }; // feed a dog food
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
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;
//=========================================================================
//
//=========================================================================
void setup()
{
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
pinMode(rx1WPin, INPUT);
pinMode(txLCDPin, OUTPUT);
pinMode(tx1WPin, OUTPUT);
// Solar Valve Actuator
pinMode(Valve_Pin, OUTPUT);
// Initialize valve to recirculate
digitalWrite(SolarValve, recirculate);
// ValvePositionCurrent = recirculate;
// Start up the Temp Sensor library
sensors.begin();
Wire.begin();
myLCDSerial.begin(9600);
delay(100);
myLCDSerial.print("?G4x20"); // display is 4 lines of 20 char each
delay(100);
myLCDSerial.print("?B40"); // set backlight to half intensity on
delay(100);
//
//
Serial.println("Solar Pool Controler Version 1.2 02-09-2012CJT");
delay(3000);
//
// locate devices on buss
myLCDSerial.print("?F");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" thermal sensors.");
delay(1000);
// report parasitic power requirements
Serial.print("Parasitic power: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
delay(500);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
//
Serial.println("Getting Ethernet IP via DHCP");
// 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);
} // End of Setup
//===========================================================================
// M A I N
//===========================================================================
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
int packetSize = Udp.parsePacket();
if(Udp.available())
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch %60); // print the second
}
// wait ten seconds before asking for the time again
delay(10000);
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
// 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;
// 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
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
//==========================================================================
// Service Routines
//==========================================================================
//==============================================================================================
// function to print a device address
void printAddress(uint8_t deviceAddress[])
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(" "); //space at end
}
}
]