Network failure

=(
Can some one help me with a Ethernet connection problem. I had a program running which collected data and sent it to my web site using the GET function. all was good for a year and then i decided to change my domain name and carried out other maintenance and now i have a major issue with the arduino connecting to the network.

I cant see what has gone wrong.

my arduino code is below which has been stripped back from simplicity.

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //mac address for the ethernet card
char serverName[] = "www.stevesolarhome.com";  // my web address name

EthernetClient client;

int panelin = 20;// variable to store panel temperature
int lowerin = 20;// variable to store lower cylinder temperature
int upperin = 20;// variable to store upper cylinder

void setup(){
  
  Serial.println("Start setup");
  
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    //Ethernet.begin (mac, ip);
    while(true);
  }
Serial.begin(9600);
   
   Serial.println ("setup end");
}

void loop (){
  
  
  Serial.println ("start loop");
  for (int x=0; x<2; x++)
  {
Serial.println (x);

delay (1000);
  
  }
  Serial.println ("End loop go to Get function");
  sendGET();
}

void sendGET() //client function to send/receive GET request data.
{
  Serial.println ("send data");
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.print("GET /php_page");//send panel = to server
    client.print(panelin);//add panelin temperature to above line note no println in code
    client.print("&lower=");
    client.print(lowerin);
    client.print("&upper=");
    client.print(upperin);
    client.println(" HTTP/1.0");// add to end of GET send with println
      
    client.println("Host: www.stevesolarhome.com");// my web host address
    client.println(); //end of get request
    Serial.println ("end send data");
    
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
   int noCharCount = 0;
  
  while(client.connected())
  {
    while(client.available())
    {
      char c = client.read();
      Serial.write(c);
      noCharCount = 0;
    }

    delay(10);
    noCharCount++;

    // change this value to suit you. this is 10 seconds    
    if(noCharCount > 1000)
    {
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    
  }
}

this is what is coming from the serial monitor is attached

it fails the first connection then connects the second but the connection takes a long time. After that the connection keeps failing very quickly.

if you need the php code i will up load it.

i will try and upload more of the serial monitor text.

is there any think i can try

It sounds like a DNS problem. It could be the dns server used to resolve the domain to an ip is taking too long to respond. This is common with slow/cheap/busy dns resolvers.

One alternative is resolve the domain to an ip with nslookup from a command prompt, then use it instead of the domain name. This usually works for hosting services that use the same ip all the time, and it saves time. You should notice a decrease in the time required to establish the connection.

Hello SurferTim

You are most probably right as it is now taking a long time on connect. this has only just started to cause a problem since ive changed the domain name on the site.

I don't know how to get the ip address to go straight to the site.

I have an ip address but when entered into the address line of IE it come up with page ok. is this the ip address i should use.

I've changed the following line from

char serverName[] ="stevesolarhome.com";  // my web address name

to

char serverName[] ="184.168.138.128";  // my web address name

which i believe is the ip address for my server, but i still get the same issue

www.stevesolarhome.com is resolved to 184.168.138.128. That is the ip of your hosting company. You can't enter that in your web browser and get your web page, because it does not set the "Host:" value in the request header.

Use this instead

IPAddress serverName(184,168,138,128);  // my web address ip

Use that ip to connect to your server, but insure you include this in the header. This is what gets you to your website on that server.

    client.println("Host: www.stevesolarhome.com");// my web host address

I have done this but still get the same issue. I really don't know what to try next

the first time to tries to connect it fails straight away, the second time it takes a very long time to connect. once connected all goes well. the data is sent to my web page and a return to the arduino saying successful from my php code is received.

after this is goes through a very quick trying to connect and failure to connect with no pausing.

I think the server address is all ok. maybe its taking to long to connect but why then go through all the connects and fails quickly as if its not even trying to connect.

I know some disagree here, but my routers have a problem if they are not given a couple seconds to set up your connections. Try waiting a couple seconds before trying any connections.

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    //Ethernet.begin (mac, ip);
    while(true);
  }

  delay(2000);

ive tried the delay but still the same.

I think the Ethernet configuration must be setting up as it connects once then it keeps failing.

I did not notice this before, but you are not closing the connection on the client end. Try these mods on your sendGet function.

void sendGET() //client function to send/receive GET request data.
{
  Serial.println ("send data");
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.print("GET /php_page");//send panel = to server
    client.print(panelin);//add panelin temperature to above line note no println in code
    client.print("&lower=");
    client.print(lowerin);
    client.print("&upper=");
    client.print(upperin);
    client.println(" HTTP/1.0");// add to end of GET send with println
      
    client.println("Host: www.stevesolarhome.com");// my web host address
    client.println(); //end of get request
    Serial.println ("end send data");
    
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
    return;
  }

  int noCharCount = 0;
  
  while(client.connected())
  {
    while(client.available())
    {
      char c = client.read();
      Serial.write(c);
      noCharCount = 0;
    }

    delay(10);
    noCharCount++;

    // change this value to suit you. this is 10 seconds    
    if(noCharCount > 1000)
    {
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    
  }
  client.stop();
  Serial.println("disconnected");
}

:D :D :D :D :D :D

SurferTim you are amazing, it works

I had a while(client.connected()) in the code twice and the disconnect was not present as you said. now its working all ok.

I have tested it twice and its still working.

I will have to start rebuilding all my solarpanel code now and check my web code as ive been pulling it all apart all day. I should have came hear first.

this is the second time you have got me out of a hole. i was beginning to give up.

thank you :D :D :D