Hi,
I have an Ardiuno+Ethernet shield (V1) using Wiznet W5100. My project is to control a pump on digital pin2 that is connected to a relay. The setup works great for a few days but then suddently I just cannot connect to the server anymore... This has happened twice in the last 5 days, reset of Arduino solves the problem. Both times the watchDogLed on pin2 was lid ON.
When I cannot connect to the server the small LEDs on Ethernet shield saying RX and TX are reacting/flickering but the diagnostic LED "clientConnectedLED" on A1 is just not turning on i.e. the code is not running into the client.connected() loop...
The loopLED is flickering indicating that the Arduino is looping as it should
Anyone can suggest what is the reason my code is not working for more than a few days? Thanks for your advice!!
//Olafh
My complete sketch is here:
#include <SPI.h>
#include <Ethernet.h> //note
#define CH1 2 //sets pin2 to CH1
#define loopLED A0
#define clientConnectedLED A1
#define watchDogLED A2
boolean LoopLEDON = false;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 1, 101);
unsigned long millisAtPumpOn = millis();
unsigned long durationPump = 5000; //hur lång pumpen skall vara igång
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
boolean pumpStatus = false;
void setup()
{
//for diagnostic
pinMode(watchDogLED, OUTPUT);
analogWrite(watchDogLED, 0);
//pump pin (relay)
pinMode(CH1, OUTPUT);
digitalWrite(CH1, LOW);
// Open serial communications and wait for port to open:
Serial.begin(9600);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
digitalWrite(CH1, LOW);
// start the Ethernet connection and the server:
Ethernet.init(10); // // You can use Ethernet.init(pin) to configure the CS pin, 10 for UNO mfl
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
if (LoopLEDON == true) {
analogWrite(loopLED, 0);
LoopLEDON = false;
}
else {
analogWrite(loopLED, 255);
LoopLEDON = true;
}
delay(20);
analogWrite(loopLED, 0);
if (millis() - millisAtPumpOn > durationPump && pumpStatus == true) {
digitalWrite(CH1, LOW);
pumpStatus = false;
}
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
String buffer = "";
int connectLoop = 0; //for timeout reason
while (client.connected()) {
analogWrite(clientConnectedLED, 255);
if (connectLoop > 10000)
{
// then close the connection from this end.
analogWrite(watchDogLED, 255);
client.stop();
}
connectLoop++;
if (client.available()) {
connectLoop = 0;
char c = client.read();
buffer += c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
if (pumpStatus == true) {
client.print("<head><meta http-equiv=\"refresh\" content=\"2; url=http://verysecret.duckdns.org\"></head>"); //<meta http-equiv="refresh" content="5"> inside <head>
}
client.print("<center>
<h1>Ethernet based Home Automation</h1>
");
client.print("<h1>Pumpstatus=");
client.print(pumpStatus);
client.print("</h1>");
client.print("<FORM>");
client.print("<P> <INPUT type=\"submit\" name=\"status\" value=\"S1 ON\">");
client.print("<P> <INPUT type=\"submit\" name=\"status\" value=\"S1 OFF\">");
// client.print("<P> hej");
client.print("</FORM></center>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
buffer = "";
}
else if (c == '\r') {
if (buffer.indexOf("GET /?status=S1+ON") >= 0) {
digitalWrite(CH1, HIGH);
pumpStatus = true;
millisAtPumpOn = millis();
}
if (buffer.indexOf("GET /?status=S1+OFF") >= 0) {
digitalWrite(CH1, LOW);
pumpStatus = false;
}
}
else {
currentLineIsBlank = false;
}
}
// this is a delay for the connectLoop timing
delay(1);
}
client.stop();
analogWrite(clientConnectedLED, 0);
}
}