I presume that I’m using server code the ‘setup’ includes
Ethernet.begin(mac,ip,localdns,gateway,subnet);
digitalWrite(ethernetChipSelectPin,HIGH); // make sure its off
// line start the Ethernet server
server.begin();
Udp.begin(localPort);
//Check presence of SD card
if (!sd.begin(sdChipSelectPin));
and in the ‘loop’ I have
// Ethernet outputs here
// listen for incoming clients
byte requestType;
char c;
client = server.available();
if (client){
while (client.connected()){
if (client.available()) {
memset(fileRequest, 0 ,sizeof(fileRequest)); //clear the inputBuffer
if (client.readBytesUntil('/',fileRequest,MAX_PAGE_NAME_LEN)){
if (strcmp(fileRequest,"GET ") == 0){
requestType = 1 ; //search for 'GET'
} else if (strcmp(fileRequest,"POST ") == 0){
requestType = 2; //search for 'POST'
}
//gather what comes after the '/'
memset(fileRequest, 0 ,sizeof(fileRequest)); //clear the inputBuffer
//EofN = 0;
if( client.find("") ){
fileBufferLen = 0;
*fileRequest = 0;
while(fileBufferLen < MAX_PAGE_NAME_LEN ){
c = client.read();
if( c == 0 ){
fileBufferLen = 0; // timeout returns 0 !
}
else if((c == 32)||(c == 63)) { // space character or ?
fileRequest[fileBufferLen] = 0; // terminate the string
break;
}
else{
fileRequest[fileBufferLen++] = c;
}
}
fileRequest[fileBufferLen] = 0;
// Note: inputBuffer full before the closing post_string encountered
}
else fileBufferLen = 0; //failed to find the prestring
}//end if(client.readBytes
client.find("\r\n\r\n"); //have to find the blank line & make sure we read to the end
//client.find(dblReturn);
if (requestType == 2){ //do a POST
actionUpDate();
}//end if (requestType == 2)
// GET or POST Give the string asked for
// or if no file name give index.htm
if (fileBufferLen == 0) {
strcpy(fileRequest,"index.htm");
}//end if
sendFile(fileRequest);
delay(1);
client.stop();
}.......
to listen for requests for info over the intranet and send the web page/file to the client.
The NTP routine is as follows
unsigned long GetNTP(byte NTP_Address[4])
{
union //Define union of unsigned long ntpEpoch
{ //to get time stamp from bigendian
unsigned long value; //bytes in the returned buffer
byte element[4];
}ntpEpoch;
ntpEpoch.value =0; // set it to zero to clear random values.
const byte ntp_BUFFER_LEN = 48;
byte ntp_Buffer[ntp_BUFFER_LEN];
//digitalWrite(sdChipSelectPin, HIGH); // make sure SD Card is OFF
// set all bytes in the inputBuffer to 0
memset(ntp_Buffer, 0, ntp_BUFFER_LEN);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
ntp_Buffer[0] = 0b11100011; // LI, Version, Mode
ntp_Buffer[1] = 0; // Stratum, or type of clock
ntp_Buffer[2] = 6; // Polling Interval
ntp_Buffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
ntp_Buffer[12] = 49;
ntp_Buffer[13] = 0x4E;
ntp_Buffer[14] = 49;
ntp_Buffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp
Udp.beginPacket(NTP_Address, 123); //NTP requests are to port 123
Udp.write(ntp_Buffer,ntp_BUFFER_LEN);
Udp.endPacket();
delay(980); //could do with being bigger but may cause problem with 'one second interupt'
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(ntp_Buffer,ntp_BUFFER_LEN); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
//long higest byte first combine the four bytes into a long integer
ntpEpoch.element[0] = ntp_Buffer[43];
ntpEpoch.element[1] = ntp_Buffer[42];
ntpEpoch.element[2] = ntp_Buffer[41];
ntpEpoch.element[3] = ntp_Buffer[40];
/* //original shift left and or the 4 bytes
for (byte i = 40; i < 44; i++){
ntpEpoch = (ntpEpoch << 8) | ntp_Buffer[i];
} */
// this is NTP time (seconds since Jan 1 1900):
// now convert NTP time into everyday time:
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
// but we are woring on a 2000 base so subtract 100 years:
ntpEpoch.value = ntpEpoch.value - 3155673600UL;// year 1970 base is 2208988800UL sec
//from 1900 ans 2000 946684800UL from 1970 Unix time:
} //end 'If ( Udp.parsePacket() )' if we didnt have a time ntpEpoch.value is still zero
delay(1);
return ntpEpoch.value;
}
All of which is a pretty standard way of doing things. Once I got the original problems of the Malloc.c out of the code including moving up to IDE 1.0.6 the unit ran 24/7 for two years without a hitch but now it is suffering hangs at irregular intervals.
Like your problem I wonder if my broadband is being sniffed for open ports, PC’s etc have their own AV software but I’m not sure how to stop the Arduino from being on the receiving end of such inquisition.
To cut down the size of the code I’ve already taken all the DHCP and other unused code out of the Ethernet librariey and also taken some stuff out of SdFat to make the thing smaller. The whole now code compiles to 27700byte under IDE 1.0.6 and to 33000 under 1.6.8 so moving up on the IDE is not an option.