Go Down

Topic: Problem with multpile (fast) client-requests (Eth) (Read 4 times) previous topic - next topic

Douwe Homans

Hello!

I'm new to Arduino, and want to do the following.
I want simple board with a button, when i press the button i want it to do a http-request, and display contents on the mac using serial connection.

This shouldn't be too hard..

I thought I'd best approach this problem one sub-problem at the time.
My first step was to create the http-request and serial display.

I found the code below, and understand what it does..
I inserted the delay(500); pieces myself.

Problem with code below is that sometimes it can't connect to the server, I don't understand why cause it really is up. When I increase the amount of delay it seems to go better. But I want to be able to push the button (in the final) project multpile times per second....

This should be possible, shouldn't is? Could the problem be serverside? If anyone can help, it would help me a lot.

Thanks!

Code: [Select]
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,0,0,7 };
byte server[] = {10,0,0,1};


Client client(server, 80);

enum CLIENT_STATE
{  
     STEP1, STEP2, STEP3, STEP4
};

static CLIENT_STATE client_state;

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 
 delay(1000);
}

void loop()
{
 
 if (client_state==STEP1)
 {
   Serial.println("connecting...");
 
 if (client.connect()) {
   Serial.println("connected");
   
   client.println("GET /index.html HTTP/1.0");
   client.println("Host: localhost");
   client.println("User-Agent: AVR ethernet");
   client.println("Accept: text/html");

   client.println();
 

   client_state=STEP2;
   delay(500);
 } else {
   Serial.println("connection failed");
   delay(500);
   client_state=STEP1;
 }
 }
 
   if (client_state==STEP2)
 {
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
   client_state=STEP2;
 }
 else if (!client.connected())
 {    Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   delay(500);
     client_state=STEP1;
   }

 }
}

jerrya.net

I've been having a similar problem, but not related to the speed of requests.

I have mine set up to pull a web page every 60 sec, and I have an LED indicate when there was a connection failure. Maybe I should log the failures so I know for sure how often they happen, but it seems to have a fair number of them throughout the day.

The server is of course up and responding, but I'm not sure what to look at in my Arduino to see what the problem is.
- Jerry Adlersfluegel

Douwe Homans

I've rewritten the code. Now it is only the essence...
I do a request every second with the doRequest function... It works a couple of times, but then it suddenly can't connect anymore (i keep getting "Can't get a connection!").

After a while it suddenly can connect again, and i get results from te server again. Most of the time it works once or twice, and then the procees repeats... (can't get a connection, and after a while CAN get a connection.)

Does anyone has an idea where the problem could be?
Code: [Select]
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 12 };
byte server[] = {10,0,0,1 }; // local ip

Client client(server, 80);

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
}

void loop()
{
 doRequest();
 delay(1000);
}

void doRequest(){
   
 Serial.println("doRequest Function");
 while(!client.connect()) {
   Serial.println("Can't get a connection!");
 }
 Serial.println("(Finally) got a connection, so send get-request.....");
  client.println("GET /index.html HTTP/1.0");
  client.println();
 Serial.println("Reading incoming data");
 incoming();
 client.stop();
 Serial.println('socket stop');
}

void incoming(){
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println("Disconnected.");
 }
}



Mark Bramwell

Any chance the connection is not fully stopped between tries?

Instead of relying on a delay(), try checking the status of the connection.

something like so:

while ( client.connected )
{
client.stop();
Serial.println('stopping the connection... ');
}

Serial.println('socket stop');


I have not played with the ethernet lib yet. Does the status change to disconnected right away or does it takes a few moments depending on the response of the remote machine?
Does the Stopping....  message always print exactly once?

In vb6, I have found that it takes time for the connection to fully stop after I issue the disconnect request. I have not yet played with my ethernet shield. Ideally you would say  while ( !client.disconnected ) but I have not verified the types of 'states' that the library provides.

example:  'not connected' is not the same as 'disconnected' because we could be in transition of states.


NJPaul

Mark,

Looks like you've found the solution. I was having the same exact issue. Putting in a delay is not reliable. Instead, looping until the connection ends works like a charm. Thanks!

Paul


Go Up