Go Down

Topic: Solved: Ethernet Shield: Error - Client connects only 3 times .... (Read 552 times) previous topic - next topic

Zorrospapa

Feb 14, 2012, 01:42 pm Last Edit: Feb 15, 2012, 09:27 am by Zorrospapa Reason: 1
Hi,

is there something wrong with Arduino 1.0 library? I programmed a loop that should connect every 10 seconds to a server and put some data to it via GET. At the end of every loop I use Client.stop. After three loops the program cannot connect to to my server any more. Has anyone an idea?

Schematic:

Loop forever:
    Every 10 seconds do
      connect to server
      put some data: GET /data/data.php?X=100 HTTP 1.1
      Client.println();
      delay(1);
      Client.stop();
    end time loop
Loop

Is there anything wrong with logic? It seems to me that some buffer overflows or the maximum connections are getting off as the shield can handle 4 requests at one time.

It would help form me to get some sample code for a loop like above which worls.Thanks in advance

SurferTim

You are probably leaving characters in the rx buffer. The connection will not close until you empty it.

Code: [Select]
client.println("GET /data/data.php?X=100 HTTP/1.0");
client.println();

while(client.connected())
{
   while(client.available())
   {
      client.read();
   }
}
client.stop();


Search the forum for zoomkat's client code.

zoomkat

Below is some new client test code you can try and see if you can get more than three request without a failure.

Code: [Select]

//zoomkat 2-13-12
//DNS and DHCP-based web client test code
//for use with IDE 1.0
//open serial monitor and send an e to test
//and to see test result
//for use with W5100 based ethernet shields
//browser equivelant URL:
//http://web.comporium.net/~shb/arduino.txt

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){
  Serial.begin(9600);
  Serial.println("DNS and DHCP-based web client test 2/13/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Zorrospapa

Hi Zoomkat, SurferTim,

thanks a lot for your support. Last code worked for me. As I reviewed my own code I found out a problem with my timer loop (shame....). Simple but effective: comparing a int var to a unsigned long will show curious results or nothing like in my case. Arduino works fine, Ethernet library works fine, Programmer should go for different work.

Greetings

PaulS

Quote
Programmer should go for different work.

Only if Programmer won't learn from his mistakes.  8)

Go Up