I am having trouble with ethernet becoming non responsive after a period of time ranging from a few hours to a day or sometimes more. The hardware is an Arduino Mega 2560 with an R3 ethernet shield. I was using a previous generation of Ethernet shield and bought a new shield to resolve this issue but the problem remains. I am using windows 7 IDE = 1.0.5 . I have searched the forum for many hours but cant seem to find a solution. Ive found many people with seemingly the same problem but no concrete solution. I am posting the complete code for my sketch in hope the somebody will see something that could cause this issue. The sketch is rather long and it gives me the exact info I need when not locked up. The ethernet stuff is near the end of the sketch. I have no formal training in c programming as you will soon see from the code but it works. If any more info is needed please let me know. oh...the URL is http://67.231.227.82:8250/ I will have to post the code in two posts as it exceeds the 9500 character count.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Sensirion.h>
#include <Wire.h>
#include "RTClib.h" // Date and time functions using a Chronodot RTC connected via I2C and Wire lib
#include "Arduino.h"
#include <XBee.h>
long lasttime ; //for calculating xbee packet times
long elapsedtime; //for calculating xbee packet times
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
int modemstatus = 0;
uint8_t point_identifier = 0;
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x93, 0xB1 };
byte ip[] = {
192, 168, 1, 125 };
byte gateway[] = {
192, 168, 1, 1 };
byte subnet[] = {
255, 255, 255, 0 };
EthernetClient client;
EthernetServer monserver(8250);
RTC_DS1307 RTC;
OneWire OUTSIDE_AIR_TEMP_BUS (2) ; // Data wire is plugged into pin 2 on the Arduino
OneWire ENTERING_WATER_TEMP_BUS (3) ; // Data wire is plugged into pin 3 on the Arduino
OneWire DISCHARGE_AIR_TEMP_BUS (9) ;
OneWire DX_SUCTION_TEMP_BUS (A3) ;
DeviceAddress tempDeviceAddress;
DallasTemperature OUTSIDE_AIR_TEMP (&OUTSIDE_AIR_TEMP_BUS) ;
DallasTemperature ENTERING_WATER_TEMP (&ENTERING_WATER_TEMP_BUS) ;
DallasTemperature DISCHARGE_AIR_TEMP (&DISCHARGE_AIR_TEMP_BUS) ;
DallasTemperature DX_SUCTION_TEMP (&DX_SUCTION_TEMP_BUS) ;
unsigned long lastTempRequest = 0;
int delayInMillis = 0;
int idle = 0;
float OAT = 0.00 ; //outside air temp temperature
float EWT = 0.00; //boiler water temp entering the house
float HWT = 0.00 ; //highest water temperature
float LWT = 0.00 ; //lowest water temperature
float DAT = 0.00 ; //discharge air temperature
float DSLT = 0.00 ; //DX suction line temperature
float lastEWT = 0.00; //variable for EWT sanity check
int EC ; //sensor error count
long numReadings ; //number of time EWT sensor is read
float temp_f = 0.00; //SHT15 temp
float temp_c = 0.00;
float humidity = 0.00; //SHT15 humidity
unsigned long last_read_SHT = millis();
unsigned long max_SHT_time = 5000;
unsigned long num_SHT_reads = 0;
float max_DAT = 0.00; //maximum discharge air temperature
float OWB_OAT = 0.00; //outside aie temp at the OWB
float OWB_DWT = 0.00; // discharge water temp at the OWB
float OWB_Temp_House_DIFFERENCE = 0.00; // difference in water temp between the OWB and the temp in house
//variables from the pool controller via xbee
float OWB_supply_temp = 0.0;
float OWB_return_temp = 0.0;
float pool_unheated_temp = 0.0;
float pool_heated_temp = 0.0;
int SD_deselect = 4;
const int rTstatPin = 5; //r (power) tstat
const int yTstatPin = 6; //y (cooling) tstat
const int wTstatPin = 7; //w (heating) tstat
const int gTstatPin = 8; //g (fan) tstat
const int blowerRunning = A2; //blower running
const byte dataPin = 16; // SHTxx serial data
const byte sclkPin = 17; // SHTxx serial clock
const unsigned long TRHSTEP = 5000UL; // Sensor query period
Sensirion sht = Sensirion(dataPin, sclkPin);
unsigned int rawData;
byte measActive = false;
byte measType = TEMP;
unsigned long trhMillis = 0; // Time interval tracking
void setup()
{
// start serial port
Serial.begin(9600);
Serial1.begin(9600); // start the xbee serial port
xbee.setSerial(Serial1); // hook the xbee into the Serial1 serial port
Serial.println(" Home Monitor");
pinMode(SD_deselect, OUTPUT);
digitalWrite(SD_deselect, HIGH); //initialize the pool relay to 'not heating'
pinMode(rTstatPin, INPUT);
pinMode(yTstatPin, INPUT);
pinMode(wTstatPin, INPUT);
pinMode(gTstatPin, INPUT);
pinMode(blowerRunning, INPUT);
pinMode(A3, INPUT);
// turn on the internal pullup resistors fot tstat inputs
digitalWrite(rTstatPin, HIGH);
digitalWrite(yTstatPin, HIGH);
digitalWrite(wTstatPin, HIGH);
digitalWrite(gTstatPin, HIGH);
digitalWrite(blowerRunning, HIGH);
// Start up the librarys
//set the ds18b20 resolution to 12 bit
OUTSIDE_AIR_TEMP.begin();
OUTSIDE_AIR_TEMP.getAddress(tempDeviceAddress, 0);
OUTSIDE_AIR_TEMP.setResolution(tempDeviceAddress, 12);
ENTERING_WATER_TEMP.begin();
ENTERING_WATER_TEMP.getAddress(tempDeviceAddress, 0);
ENTERING_WATER_TEMP.setResolution(tempDeviceAddress, 12);
DISCHARGE_AIR_TEMP.begin();
DISCHARGE_AIR_TEMP.getAddress(tempDeviceAddress, 0);
DISCHARGE_AIR_TEMP.setResolution(tempDeviceAddress, 12);
DX_SUCTION_TEMP.begin();
DX_SUCTION_TEMP.getAddress(tempDeviceAddress, 0);
DX_SUCTION_TEMP.setResolution(tempDeviceAddress, 12);
OUTSIDE_AIR_TEMP.setWaitForConversion(false);
OUTSIDE_AIR_TEMP.requestTemperatures();
ENTERING_WATER_TEMP.setWaitForConversion(false);
ENTERING_WATER_TEMP.requestTemperatures();
DISCHARGE_AIR_TEMP.setWaitForConversion(false);
DISCHARGE_AIR_TEMP.requestTemperatures();
DX_SUCTION_TEMP.setWaitForConversion(false);
DX_SUCTION_TEMP.requestTemperatures();
delayInMillis = 5000 ;
lastTempRequest = millis();
delay(750);
Ethernet.begin(mac, ip, gateway, subnet);
monserver.begin();
Wire.begin();
RTC.begin();
//initalize HWT and LWT to the current entering water pemperature
EWT = ENTERING_WATER_TEMP.getTempFByIndex(0);
DAT = DISCHARGE_AIR_TEMP.getTempFByIndex(0);
DSLT = DX_SUCTION_TEMP.getTempFByIndex(0);
//initalize HWT and LWT to the current entering water pemperature
HWT = EWT;
LWT = EWT;
lastEWT = EWT;
EC = 0 ;
numReadings = 0 ;
}
//see next post
Thank you very much for your reply SurferTim. Sorry it took so long for me to reply. I merged my code into the code you suggested and uploaded to the Mega. i will let you know the outcome. Again....thanks Below is the merged code
//web stuff
EthernetClient client = monserver.available();
if(client) {
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int r,t;
char *pch;
Serial.print(F("Client request: "));
// this controls the timeout
int loopCount = 0;
while (client.connected()) {
while(client.available()) {
// if packet, reset loopCount
loopCount = 0;
char c = client.read();
if(currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response
Serial.println(tBuf);
Serial.print(F("POST data: "));
while(client.available()) Serial.write(client.read());
Serial.println();
pch = strtok(tBuf,"?");
while(pch != NULL)
{
if(strncmp(pch,"t=",2) == 0)
{
t = atoi(pch+2);
Serial.print("t=");
Serial.println(t,DEC);
}
if(strncmp(pch,"r=",2) == 0)
{
r = atoi(pch+2);
Serial.print("r=");
Serial.println(r,DEC);
}
pch = strtok(NULL,"& ");
}
Serial.println(F("Sending response"));
client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
client.print("<title>HVAC monitor</title>");
client.println("<h1>HVAC monitor</h1>");
client.println("<hr />");
client.println("<hr />");
//TOD
DateTime now = RTC.now();
client.print(now.year(), DEC);
client.print('/');
client.print(now.month(), DEC);
client.print('/');
client.print(now.day(), DEC);
client.print(' ');
client.print(now.hour(), DEC);
client.print(':');
client.print(now.minute(), DEC);
client.print(':');
client.print(now.second(), DEC);
client.println();
client.println();
client.println("<table>");
client.println("<table border='1'>");
client.print("<tr><td>OWB_Supply_Temp");
client.print("</td><td>");
client.print(OWB_supply_temp );
client.print((char)176);
client.print("F");
client.print("</td><td>");
client.print("pool heated temp");
client.print("</td><td>");
client.print(pool_heated_temp );
client.print((char)176);
client.print("F");
client.println("</td></tr>");
client.print("<tr><td>OWB_Return_Temp");
client.print("</td><td>");
client.print(OWB_return_temp );
client.print((char)176);
client.print("F");
client.print("</td><td>");
client.print("pool unheated temp");
client.print("</td><td>");
client.print(pool_unheated_temp );
client.print((char)176);
client.print("F");
client.println("</td></tr>");
client.println("</table>");
client.print("<p><font color='#339900' size=’5?>Outside air temp (front porch) = ");
client.print(OAT );
client.print((char)176);
client.print("F");
client.print("</p>");
OWB_Temp_House_DIFFERENCE = OWB_DWT - EWT;
client.print("<p><font color='#339900' size=’5?>Outside air temp (at the OWB) = ");
client.print(OWB_OAT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>Space temperature(Great room) = ");
client.print(temp_f );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>Space Humidity(Great room) = ");
client.print(humidity);
client.print("%");
client.print("</p>");
client.print("<p> Boiler water temperature (in the house) = ");
client.print(EWT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>OWB water temperature (at the OWB) = ");
client.print(OWB_DWT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>TEMP difference between the OWB and the H20 temp in the house = ");
client.print(OWB_Temp_House_DIFFERENCE );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>Number of XBee modem status messages = ");
client.print(modemstatus );
client.print("</p>");
if (digitalRead(blowerRunning) == LOW)
{
client.println("<p>The blower is running");
}
else
{
client.println("<p>The blower is off");
}
client.print("</p>");
client.print("<p>Discharge air temperature is = ");
client.print(DAT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>DX suction line temp is = ");
client.print(DSLT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.println("<hr />");
client.println("Debug info");
client.println("<hr />");
client.println();
client.println();
client.println("<p>Thermostat data");
client.print("</p>");
if (digitalRead(rTstatPin) == LOW)
{
client.println("<p>R = 24Vac");
}
else
{
client.println("<p>R = 0Vac");
}
client.print("</p>");
if (digitalRead(yTstatPin) == LOW)
{
client.println("<p>Y = 24Vac");
}
else
{
client.println("<p>Y = 0Vac");
}
client.print("</p>");
if (digitalRead(wTstatPin) == LOW)
{
client.println("<p>W = 24Vac");
}
else
{
client.println("<p>W = 0Vac");
}
client.print("</p>");
if (digitalRead(gTstatPin) == LOW)
{
client.println("<p>G = 24Vac");
}
else
{
client.println("<p>G = 0Vac");
}
client.print("</p>");
client.print("<p>Highest boiler water temperature = ");
client.print(HWT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>Lowest boiler water temperature = ");
client.print(LWT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>Highest discharge air temperature = ");
client.print(max_DAT );
client.print((char)176);
client.print("F");
client.print("</p>");
client.print("<p>EWT EC = "); //display the EWT errors
client.print(EC );
client.print("</p>");
client.print("<p>SHT15 reads = "); //display the SHT reads
client.print(num_SHT_reads );
client.print("</p>");
client.print("<p>Each sensor was read ");
client.print(numReadings);
client.print(" times");
client.print("</p>");
client.print("</HTML>");
client.stop();
}
else if (c == '\n') {
currentLineIsBlank = true;
currentLineIsGet = false;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
loopCount++;
// if 10000ms has passed since last packet
if(loopCount > 10000) {
// close connection
client.stop();
Serial.println("\r\nTimeout");
}
// delay 1ms for timeout timing
delay(1);
}
Serial.println(F("done"));
}
//end web stuff
}
Hope it works for you. If it doesn't, try to find out where in the code it is failing and let me know. The last line that appears on the serial monitor before the crash usually helps troubleshoot it.
One reason your Ethernet board might be locking up is because of the sheer size of your code. I am experiencing the same problem with my Ethernet board and was informed that the problem might be a lack of free RAM memory. User PaulS suggested this website: Arduino Playground - AvailableMemory which has a library on it that you can use to determine how much RAM you have left. I am going to go through my code and condense it in hopes that this will solve my hang up problem.
@Artie: Good suggestion normally, but it is probably not a memory problem.
The hardware is an Arduino Mega 2560 with an R3 ethernet shield.
More than likely it is a freeze in the while(client.connected()) loop. If the server code does not receive the double cr/lf (blank line) from the client, which some port scanners do not send, the code locks up without the timeout code.
edit: If the connection breaks (fails) during the request send, that will have the same effect. If the server code does not receive the blank line or the close, it freezes there.
Hi,
I had a similar problem - intermittent hanging of the Ethernet Shield - no response to ping, couldn't connect to the shield, the only solution - testing the connection from time to time and resetting the Shield if the test failed. After resetting, it would work for a random amount of time - sometimes half an hour, sometimes several hours, sometimes even half a day. I suspected my code, but no modifications would improve the situation, so I gave up.
But after some time I decided to try connecting the Ethernet Shield to a Fast Ethernet switch instead of a Gigabit Ethernet router (TP-LINK TL-WR1043ND). I suspected that there might be some incompatibility between the Wiznet chip and Gigabit Ethernet (or the specific router I used). It seems this has done the trick. No connection problems since that time (Ethernet Shield acts as a server, to which a client is constantly connected). So if you have a spare Ethernet/Fast Ethernet switch/hub/network card in the PC, any you are sure your code is fine, and cabling is fine, try to connect Ethernet Shield to it for a test, it might solve your problem (but I can't guarantee ).