Ethernet Shield - Randomly failing to connect

Hello,

I am working on a little project where I use my Arduino Uno + Ethernet Shield to write/read data from a MySQL database, hosted on a webserver.
I am new to Arduino, but after a few days of reading I think I could get a basic understanding of how to use the Ethernet library.
The code I came up with works. The Arduino will read from a .php page and will write to another .php page. The SerialMonitor displays status messages for debuging.

Before I try to explain my problem, here is the code:

#include <SPI.h>
#include <Ethernet.h>


byte mac[] = {
  0x90, 0xA1, 0xDA, 0x0E, 0xF2, 0x77
};
char serverName[] = "www.***********.at";

EthernetClient client;

int Red = 0;
int Green = 0;
int Blue = 0;
int Bright = 0;
byte PPower = 0;
byte SPower = 0;

int pinRed = 5;
int pinGreen = 6;
int pinBlue = 3;
int pinPPower = 7;
int pinSPower = 8;

long refreshrate = 2000;
long lasttime = 0 - refreshrate;

void setup()
{
  Serial.begin(115200);
  Serial.println("Remote Control #1 Initializing");

  client.setTimeout(200);

  pinMode(pinRed, OUTPUT);
  pinMode(pinGreen, OUTPUT);
  pinMode(pinBlue, OUTPUT);
  pinMode(pinPPower, OUTPUT);
  pinMode(pinSPower, OUTPUT);

  pinMode(2, INPUT_PULLUP);

  pinMode(A0, INPUT);
  pinMode(9, OUTPUT);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  delay(5000);
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
  Serial.print(millis());
  Serial.println(": connecting...");
}

void loop()
{

  if (millis() - lasttime >= refreshrate)
  {
    Serial.print(millis());
    Serial.println(": Starting");
    lasttime = millis();
    Request();
    Send();
    Serial.println("---------------------");
    Serial.println();
  }

  digitalWrite(pinPPower, PPower);
  digitalWrite(pinSPower, SPower);

}

void Request()
{

  if (client.connect(serverName, 80) > 0) {
    Serial.print(millis());
    Serial.print(": connected... ");
    Serial.println(" reciving... ");
    client.println("GET http://***********.at/Arduino/PCControll/Status.php HTTP/1.0");
    client.println("Host: www.***********.at");
    client.println("Connection: close");
    client.println();
    client.println();
    Serial.print(millis());
    Serial.println(": finished reciving!");
  }
  else {
    Serial.print(millis());
    Serial.println(": connection failed");
  }
  if (client.find("Start")) {
    if (client.find("Red: ")) {
      Red = client.parseInt();
      Serial.print(millis());
      Serial.print(": Red is: " );
      Serial.println(Red);
    }
    if (client.find("Green: ")) {
      Green = client.parseInt();
      Serial.print(millis());
      Serial.print(": Green is: " );
      Serial.println(Green);
    }
    if (client.find("Blue: ")) {
      Blue = client.parseInt();
      Serial.print(millis());
      Serial.print(": Blue is: " );
      Serial.println(Blue);
    }
    if (client.find("Bright: ")) {
      Bright = client.parseInt();
      Serial.print(millis());
      Serial.print(": Brightness is: " );
      Serial.println(Bright);
    }
    if (client.find("PPower: ")) {
      PPower = client.parseInt();
      Serial.print(millis());
      Serial.print(": Primary-Power is: " );
      Serial.println(PPower);
    }
    if (client.find("SPower: ")) {
      SPower = client.parseInt();
      Serial.print(millis());
      Serial.print(": Secondary-Power is: " );
      Serial.println(SPower);
    }
  }
  else {
    Serial.print(millis());
    Serial.println(": result not found");
  }

  client.stop();
  while (client.status() != 0) {
    delay(5);
  }

  Serial.print(millis());
  Serial.println(": outputting");

  Red = constrain(Red, 0, 255);
  Green = constrain(Green, 0, 255);
  Blue = constrain(Blue, 0, 255);
  Bright = constrain(Bright, 0, 255);

  double Reddim = Red * Bright / 255;
  double Greendim = Green * Bright / 255;
  double Bluedim = Blue * Bright / 255;

  analogWrite(pinRed, Reddim);
  analogWrite(pinGreen, Greendim);
  analogWrite(pinBlue, Bluedim);

  Serial.print(millis());
  Serial.println(": outputting - finished");
}

void Send()
{
  double value = analogRead(A0);
  value = (value * 100) / 1025;

  if (client.connect(serverName, 80) > 0) {
    Serial.print(millis());
    Serial.println(": sending... ");
    client.print("GET http://***********.at/Arduino/PCControll/Back.php");
    client.print("?T1=");
    client.print(value);
    client.print("&key=MEINPASSWORT123");
    client.println(" HTTP/1.0");
    client.println("Host: www.***********.at");
    client.println();
    client.println("User-Agent: Arduino");;
    client.println("Connection: close");
    client.println();
    client.println();
    Serial.print(millis());
    Serial.println(": finished sending!");
  } else {
    Serial.print(millis());
    Serial.println(": sending failed");
  }
  client.stop();
  while (client.status() != 0) {
    delay(5);
  }
}

The normal Serial output looks like that:

250088: Starting
250243: connected...  reciving... 
250247: finished reciving!
250359: Red is: 0
250363: Green is: 255
250366: Blue is: 0
250371: Brightness is: 255
250374: Primary-Power is: 0
250377: Secondary-Power is: 0
250422: outputting
250423: outputting - finished
250577: sending... 
250585: finished sending!
---------------------

However, from time to time (maybe one in twenty cases), the client.connect(serverName, 80) > 0 of the Request() seems to stop working for about 30 seconds. The serial output looks like that:

252088: Starting
284356: connection failed
284556: result not found
284556: outputting
284557: outputting - finished
284713: sending... 
284721: finished sending!
---------------------

After that the programm continues to run normal.
If the board would only be used to do those requests it wouldn’t be a problem. However, as I am planning to have a couple of switches connected to it as well, it is a little bit problematic to wait for 30 seconds until the board reacts to the change of an input.

I couldn’t find a way to manipulate the timeout for that. It always seems to take about 30 seconds before it continues. Lowering the timeout to 500 milliseconds would also be a solution I could live with. I just couldn’t figure out how to do it.

Your help is appricated.

Thanks,

Jonathan

Try to change that line:

long lasttime = 0 - refreshrate;

to

uint32_t lasttime = 0;

The code you are using (millis() - lasttime > timeout) is only meant to work with unsigned integers because only there the overflow does create the expected behaviour. In the signed version you have jumps that gives you an unexpected behaviour. Although I would expect that to last longer than 40 seconds.

The below part of your code looks a little different than what is normally used.

client.print("GET http://***********.at/Arduino/PCControll/Back.php");

would normally be

client.print("GET /Arduino/PCControll/Back.php");

Thank you both pylon and zoomkat!

I changed it to uint32_t. That made no diffrence in the behaviour of the web request, but is still good to know!

However, changing from:

client.print("GET http://***********.at/Arduino/PCControll/Back.php");

to:

client.print("GET /Arduino/PCControll/Back.php");

stoped that problem I described! Intresting, that a wrong GET request would work, however with throwing up some problems from time to time.

Thank you very much for your help!

Jonathan