So for some reason, my Ethernet shield stops responding after some random amount of time. When I try to access the IP address it works for a while. If I leave it for a long time and I try to access it I cant connect to it. If I open up the Arduino software and start the serial monitor, com 3, I can access the arduino again. Anyone have any ideas whats going on? Is the shield going to sleep or is my code crashing for some reason? There is a bit ore code than what is below, but it has no real effect on the web server side of things.
Thanks!
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 60
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x5A, 0xFA };
byte ip[] = { 192,168,0, 177 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
//define setpoints for fermenter temps
String inString = String(maxLength);
long previousMillis = 0;
int setferm1 = 300;
int setferm2 = 300;
int setferm3 = 300;
int setferm4 = 300;
int Posa = 0;
int Posb = 0;
int Posc = 0;
int Posd = 0;
int whichTank = -1;
int chillValue = 10;
char tempBuff[4];
int chill = 0;
int valveToOpenIdx = -1;
double volts = 0.486328125;
int TempSensor1 =0;
int TempSensor2 =0;
int TempSensor3 =0;
int TempSensor4 =0;
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
void loop()
{
//Get Temps off sensors
analogRead(1);
delay(10);
TempSensor1 = 0;
for (int i = 0; i < 10; i++) {
TempSensor1 = TempSensor1 + round(volts*analogRead(1));
}
analogRead(2);
delay(10);
TempSensor2 = 0;
for (int i = 0; i < 10; i++) {
TempSensor2 = TempSensor2 + round(volts*analogRead(2));
}
analogRead(3);
delay(10);
TempSensor3 = 0;
for (int i = 0; i < 10; i++) {
TempSensor3 = TempSensor3 + round(volts*analogRead(3));
}
analogRead(4);
delay(10);
TempSensor4 = 0;
for (int i = 0; i < 10; i++) {
TempSensor4 = TempSensor4 + round(volts*analogRead(4));
}
TempSensor1 = round(TempSensor1/10);
TempSensor2 = round(TempSensor2/10);
TempSensor3 = round(TempSensor3/10);
TempSensor4 = round(TempSensor4/10);
// Web Interface
int bufLength = 4;
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
// Serial.println(client.read());
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
inString += c;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
if (inString.indexOf("?") > -1) {
Posa = inString.indexOf("a");
Posb = inString.indexOf("b");
Posc = inString.indexOf("c");
Posd = inString.indexOf("d");
inString.substring((Posa+2), (Posb-1)).toCharArray(tempBuff, bufLength); //transfer substring to buffer
setferm1 = atoi(tempBuff);
inString.substring((Posb+2), (Posc-1)).toCharArray(tempBuff, bufLength); //transfer substring to buffer
setferm2 = atoi(tempBuff);
inString.substring((Posc+2), (Posd-1)).toCharArray(tempBuff, bufLength); //transfer substring to buffer
setferm3 = atoi(tempBuff);
inString.substring((Posd+2), (Posd+5)).toCharArray(tempBuff, bufLength); //transfer substring to buffer
setferm4 = atoi(tempBuff);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<font size=32 color=CDAD00><b><u>HGB Fermenter Control</u></b></font>
");
// output the value of each analog input pin
client.print("<font size=32>Temperature of Fermenter 1 is ");client.print(TempSensor1);client.println("</font>
");
client.print("<font size=32>Temperature of Fermenter 2 is ");client.print(TempSensor2);client.println("</font>
");
client.print("<font size=32>Temperature of Fermenter 3 is ");client.print(TempSensor3);client.println("</font>
");
client.print("<font size=32>Temperature of Fermenter 4 is ");client.print(TempSensor4);client.println("</font>
");
client.print("
<form method=get><font size=32>");
client.print("Setpoint Fermenter 1: <input type=text style=font-size:32pt size=3 name=a value="); client.print(setferm1);client.print(" />");
if (valveToOpenIdx == 0) {
client.print(" Open");
}
else {
client.print(" Closed");
}
client.print("
");
client.print("Setpoint Fermenter 2: <input type=text style=font-size:32pt size=3 name=b value="); client.print(setferm2);client.print(" />");
if (valveToOpenIdx == 1) {
client.print(" Open");
}
else {
client.print(" Closed");
}
client.print("
");
client.print("Setpoint Fermenter 3: <input type=text style=font-size:32pt size=3 name=c value="); client.print(setferm3);client.print(" />");
if (valveToOpenIdx == 2) {
client.print(" Open");
}
else {
client.print(" Closed");
}
client.print("
");
client.print("Setpoint Fermenter 4: <input type=text style=font-size:32pt size=3 name=d value="); client.print(setferm4);client.print(" />");
if (valveToOpenIdx == 3) {
client.print(" Open");
}
else {
client.print(" Closed");
}
client.print("
");
client.println("
");
client.print("<input type=submit value=Update-Temps style=height:6em; width:24em />");
client.print("</font></form>");
client.println("
");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
inString = " ";
// close the connection:
client.stop();
}