Ethernet Shield (Wiz5100) Probles with reoccuring requests

Hello,

Im new to EtherNet problems. Im IT literate, but I lack understanding low level electronics. (eq.which way electrons really go + or -)

Im setting up home automation based on Arduino Uno r3 (orginal); Ethernet (china); Attiny84/NRF24L01+ nodes with DS18b20.

Problem Im facing is related to hosting/EhterNet Shield.

FatCow Shared Hosting giving me really poor support with HTTP debugging, and honestly I believe they block my WIZ5100 requests.

I'm using PuTTy on ubuntu to diagnose open ports "80", and got really strange results that don't help.

When connecting via EtherNet, using standard arduino sketches for URL/DNS Server host(i dont have ip number), everything works fine first time. I get trough server, send lines of GET, read the file in serial monitor.

BUT when test things and try few GET requests(one at the time.. no loops yet) I get this never ending "connecting..." on my second or third attempt! Dont know how to move forward.

To sum up:
Using this code:
http://arduino.cc/en/Tutorial/WebClient
My hosting shuts down or after 3rd attempt within 2minutes from firs one.

Please help.

Tom.

I have no problems with this example sketch in the playground.
http://playground.arduino.cc/Code/WebClient

SurferTim:
I have no problems with this example sketch in the playground.
Arduino Playground - HomePage

Thanks.
Im happy for you.

...on the side line, any one else with post I could benefit?

My bad. Here is "Perfect World" code to modify the webclient turtorial.

void loop()
{
  while(client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }

  client.stop();
  Serial.println(F("disconnected"));
  while(true);
}

arduino_node:
To sum up:
Using this code:
http://arduino.cc/en/Tutorial/WebClient
My hosting shuts down or after 3rd attempt within 2minutes from firs one.

Please help.

Can you post the url you are trying to retrieve?
Better still copy and paste your sketch, or at least the parts you changed.

Server is being hosted by fatcow.com
It is shared but its not free.
I dont have SSH.
Telnet should be able test its port 80 butreturns garbage.

First attempt ok, rest hangs. No reply from server, even after 3min.

ill send code later.

so you are using the WebClient-sketch that connects a single time and then waits forever in while(true)?
Do you reset to execute the next request-attempt?

If so it could be the local-port is allways the same after reset so you cannot connect because the other side drops the packets as duplicates.

  • Norbert

Thanks Norbert,
Thats the clues Im looking for! I think on my end Hardware is ok , I managed to do all test stuff on my local ip.

so you are using the WebClient-sketch that connects a single time and then waits forever in while(true)?

Yes. This is how I test if it works. Yes while loop is there too. But it freezes on "connecting..." even after I hard reset the board, unplugg it and plug back on (power+ether)

Do you reset to execute the next request-attempt?

No. how to reset?
How to instruct not cashing of packets?
PHP string request is different every time (different tempC), but it is probable to have identical requests(time stamp is in PHP itself)

it could be the local-port is always the same after reset

Can I change ports from my Ethernet shield?
What is the solution if this is the case? modify my .htaccess ?

Im not fluent on HTTP/1.1 protocol, but please pint me in right stuff. (HTTP Made Really Easy).

Thank you.

If your code fails after 3 times, then your connection to the server is probably not being closed properly in your code. Below is some simple client test code you can try to see if you can make repeated connections to my ISP's web server.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#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(){

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

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/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
}

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.1"); //download text
    client.println("Host: web.comporium.net");
    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

}

Hello,

I've used your code and its brilliant.

but here fatcow reply to your code:

==================

connected
HTTP/1.1 200 OK
Date: Tue, 26 Nov 2013 23:13:34 GMT
Content-Type: text/html
Content-Length: 84
Connection: keep-alive
Server: Apache/2
X-Powered-By: PHP/5.2.17

Timestamp is 31/12/1969 19:00:00unix epoh is 1385507614Your table has been populatedÿÿÿÿÿÿÿÿÿÿÿÿÿÿ<.....>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
disconnecting.
==================

Timestamp is 31/12/1969 19:00:00unix epoh is 1385507614Your table has been populated this is PHP confirming.
There are 28625 of those "ÿ",
ive just put <...> to save some screen :wink:

i believe those would go on forever but ethernet buffer is only 2kb.
it takes 20 seconds on uno

after unix timestamp my server returns som wired stuff as you can see.

i was trying to get some code together to truncate that string, but no luck.

do you know what are they sending?

do you know what are they sending?

You might try making the same request with a web browser and see what is returned.

arduino_node:

Do you reset to execute the next request-attempt?

No. how to reset?

I meant 'do you press the reset-button on your arduino to start the next attempt?'

arduino_node:

it could be the local-port is always the same after reset

Can I change ports from my Ethernet shield?

you cannot without modifying the Ethernet-library. It's in EthernetClient.cpp Line 53. The problem is: whenever you reset the Arduino, the _localport is reset to 1024 as well.

arduino_node:
What is the solution if this is the case? modify my .htaccess ?

If reuse of local port is the issue then there is absolutely nothing you can do on the http-protocoll-level (which is application) as this happens in tcp (which is transport).

What you can do: Make sure you don't have to reset, but write some logic that issues multiple-requests with some delay in between. (This ensures you don't reuse local port). Then make sure your connection is closed properly on each request so you don't run out of sockets. Your sketch waits for the other side to close, which just might not happen, so your client would stay in the 'while(client.connected()) {'-loop until you press reset. Add some kind of timeout here - if the reception of request takes longer than some seconds leave the loop and call client.stop().

arduino_node:
Timestamp is 31/12/1969 19:00:00unix epoh is 1385507614Your table has been populated this is PHP confirming.
There are 28625 of those "ÿ",

If this is the debug-output your arduino prints on the serial-line than you've run out of memory corrupting the Serial-devices buffer. Make sure you don't allocate memory for the whole response, but read the request in small chunks (calling returnsize = client.read(buffer,size) returns the number of bytes read, so you can use this for the next call to Serial.write(buffer,returnsize);

Thanks,

ntruchsess

(calling returnsize = client.read(buffer,size) returns the number of bytes read, so you can use this for the next call to Serial.write(buffer,returnsize);

I will reasearch this. thank you. if you have link handy, please post, if not .. google is my friend. (isnt really)
have you tested it somewhere, i was thinking that, if I cannot get this server stop sending garbage, I would truncate the string only to essential query.
But this is only when I know the query lenght.
What about other uses? Im planning to adde weather xml pharsing, or logging through mysql. I dont know how long reply might get.

Thank you,

ntruchsess:

arduino_node:

Do you reset to execute the next request-attempt?

No. how to reset?

I meant 'do you press the reset-button on your arduino to start the next attempt?'

Yes I was reseting with button.

arduino_node:

it could be the local-port is always the same after reset

Can I change ports from my Ethernet shield?

you cannot without modifying the Ethernet-library. It's in EthernetClient.cpp Line 53. The problem is: whenever you reset the Arduino, the _localport is reset to 1024 as well.
Noted. Thank you.

arduino_node:
What is the solution if this is the case? modify my .htaccess ?

If reuse of local port is the issue then there is absolutely nothing you can do on the http-protocoll-level (which is application) as this happens in tcp (which is transport).
Noted. Thanks.

ntruchsess:
What you can do: Make sure you don't have to reset, but write some logic that issues multiple-requests with some delay in between. (This ensures you don't reuse local port). Then make sure your connection is closed properly on each request so you don't run out of sockets. Your sketch waits for the other side to close, which just might not happen, so your client would stay in the 'while(client.connected()) {'-loop until you press reset. Add some kind of timeout here - if the reception of request takes longer than some seconds leave the loop and call client.stop().

noted.Thank you.
Your script was running since 00:00GMT and still report ok every 15minutes.

The headers returned by the server are telling you what you want to know.

content-length: 84

There will be 84 bytes of content following the headers

connection: keep-alive

The server will not end the connection after the content has been sent.

Change the request you send to include the header

Connection: close
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
   clinet.println("Connection: close");    // <---connection control 
   client.println(); //end of get request
  }

Dear Guys,
I managed to get this working with two DS18B20 on one bus.

Unfortunately it stopped after overnight test run, at 07:52, unknown reason.
Possibly FatCow blocked MySql user again. (it happened before)
This Uno is powered of USB wall powersuply, so I cannot access the Serial monitor.

This give me good prognosis for weekend tinkering session! :wink:

MattS, Thanks for this. I have not resolved the 28k of "y" ant the end of connection. Maybe this will help. It hangs the Serial Monitor, and possibly Uno for some time, but data is passed to server. Im not happy with this and I will resolve this.

I'll post code later, when i clean it from all the // comments and junk.

Sorry for horrible CSS, I savaged it form some other project.

This section of zoomkat's code has a bit of a problem. If the client is connected, it reads the client connection, no matter if a character is available or not. If not, you get the funny 'y'.

  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 
  }

I like this better. It is the "Perfect World" version, but good if the connection does not break.

while(client.connected()) {
   while(client.available()) {
      char c = client.read(); //gets byte from ethernet buffer
      Serial.print(c); //prints byte to serial monitor 
   }
}

edit: If you want something a bit more bulletproof, here is the "Real World" version.

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      char ch = client.read();
      Serial.write(ch);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();
  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

Not to make you feel bad about wasting all this time, but you blew off this solution back at reply #2.
http://forum.arduino.cc/index.php?topic=201186.msg1482954#msg1482954
You were happy for me because I was using it. I was sad for you because you weren't.

I like this better. It is the "Perfect World" version, but good if the connection does not break.

I think in the past there were concerns with data still remaining in the input buffer after a connection was closed, which resulted in the "//connected or data available" approach.

There was occasions when other code did not empty the rx buffer, but the server will not close the connection until the rx buffer is empty after the last packet is sent.

The challenge is the OR operator. If the connection is still open, your code reads from the rx buffer, even if there is no characters in it. That results in the client.read() call returning an int -1 (char -1 or byte 255) meaning "no characters available". That is the reason for the 'ÿ' character. It is character 255.

Also it will lock up in that while loop if the server stalls or the connection fails (breaks). It becomes an endless loop because the server can't send the connection close message, so the client thinks the connection is still open. I didn't discover that bug, but I did find a fix for it. It was brought to my attention by another user whose sketch kept locking in that loop.