Go Down

Topic: [Resolved]Ethershield GET problems (Read 365 times) previous topic - next topic

firestorm_v1

Aug 26, 2012, 08:19 pm Last Edit: Aug 26, 2012, 09:00 pm by firestorm_v1 Reason: 1
Hello Arduino Forum!

I hope someone can help shed some light into the issue I'm having with the Arduino ethershield.  I'm using Arduino 1.0.1 and have been having several problems with getting a response from the server despite the server working.

The API I'm going to be calling always returns a #0 for now (a #1 should be returned if everything's working when I get past this issue.)  It's simply a PHP page that says "echo "#0" but later on will be fleshed out to actually do something.

The issue I'm having is two-fold:

1) The client is reporting error however the server shows the expected GET request and does not report an error.  TCPDUMP  validates that the response is being sent to the ethershield but it's not there?
2) After connecting to the server and posting the GET request, I need to parse the client's buffer for the "#" (which I kind of have figured out) and get the return value afterward.  The "0" and "1" will be used by the Arduino to determine the success or failure of the GET request. but I can't get any client data so this one's kind of shelved for now.

So far I have never had a problem starting the Ethernet library (using DHCP) however this client issue is really bugging me

Affected Arduino Code:

Quote

 Serial.println("Conn Srv");

 if (client.connect(server, 80)) {
   Serial.println("connected");
   // Make a HTTP request:
   client.println("GET /ardurfid/commander.php HTTP/1.0");
   client.println();
 }
 else {
   // kf you didn't get a connection to the server:
   Serial.println("connection failed");
 }  
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 else
 {
   Serial.println("Something wrong with client.");
   delay(10000);
 }
 Serial.println("Done with client.");
 if (!client.connected())
 {
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }



Arduino Serial Console output:
Code: [Select]

Station ID:9
DHCP OK
Conn Srv
connected
Something wrong with client.
Done with client.


TCPDUMP GET and server response:
(Arduino to Server)
Code: [Select]
13:12:03.748842 IP (tos 0x0, ttl 128, id 7, offset 0, flags [DF], proto TCP (6), length 76)
   192.168.0.36.1025 > 192.168.0.39.80: Flags [P.], cksum 0xc83d (correct), seq 1:37, ack 1, win 2048, length 36
0x0000:  4500 004c 0007 4000 8006 7909 c0a8 0024  E..L..@...y....$
0x0010:  c0a8 0027 0401 0050 74d9 d225 971b 1abf  ...'...Pt..%....
0x0020:  5018 0800 c83d 0000 4745 5420 2f61 7264  P....=..GET./ard
0x0030:  7572 6669 642f 636f 6d6d 616e 6465 722e  urfid/commander.
0x0040:  7068 7020 4854 5450 2f31 2e30            php.HTTP/1.0


(Server to Arduino)
Code: [Select]
13:12:03.758678 IP (tos 0x0, ttl 64, id 32993, offset 0, flags [DF], proto TCP (6), length 253)
   192.168.0.39.80 > 192.168.0.36.1025: Flags [P.], cksum 0xf7f0 (correct), seq 1:214, ack 41, win 14600, length 213
0x0000:  4500 00fd 80e1 4000 4006 377e c0a8 0027  E.....@.@.7~...'
0x0010:  c0a8 0024 0050 0401 971b 1abf 74d9 d24d  ...$.P......t..M
0x0020:  5018 3908 f7f0 0000 4854 5450 2f31 2e31  P.9.....HTTP/1.1
0x0030:  2032 3030 204f 4b0d 0a44 6174 653a 2053  .200.OK..Date:.S
0x0040:  756e 2c20 3236 2041 7567 2032 3031 3220  un,.26.Aug.2012.
0x0050:  3138 3a31 323a 3033 2047 4d54 0d0a 5365  18:12:03.GMT..Se
0x0060:  7276 6572 3a20 4170 6163 6865 2f32 2e32  rver:.Apache/2.2
0x0070:  2e31 3720 2855 6275 6e74 7529 0d0a 582d  .17.(Ubuntu)..X-
0x0080:  506f 7765 7265 642d 4279 3a20 5048 502f  Powered-By:.PHP/
0x0090:  352e 332e 352d 3175 6275 6e74 7537 2e31  5.3.5-1ubuntu7.1
0x00a0:  300d 0a56 6172 793a 2041 6363 6570 742d  0..Vary:.Accept-
0x00b0:  456e 636f 6469 6e67 0d0a 436f 6e74 656e  Encoding..Conten
0x00c0:  742d 4c65 6e67 7468 3a20 320d 0a43 6f6e  t-Length:.2..Con
0x00d0:  6e65 6374 696f 6e3a 2063 6c6f 7365 0d0a  nection:.close..
0x00e0:  436f 6e74 656e 742d 5479 7065 3a20 7465  Content-Type:.te
0x00f0:  7874 2f68 746d 6c0d 0a0d 0a23 30         xt/html....#0



Access log on Server shows:
192.168.0.36 - - [26/Aug/2012:13:12:03 -0500] "GET /ardurfid/commander.php HTTP/1.0" 200 213 "-" "-"


I know it's a wall of text, but it appears this is a pretty technical issue.  Any suggestions are welcome!  Thank you.

SurferTim

You are not waiting for the server to return a response. The server will close the connection when it is finished sending.
Code: [Select]

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


firestorm_v1

Hello SurferTim:

Thank you for your response, I think that was the swift kick in the posterior that I needed to get moving.   I incorporated your code example into my code and it now recognizes the response from the API perfectly. 




If anyone else runs into the problem, here's what my code looks like after the modification.  This will parse the client response for "#" and then look for the real value (in my example, "1" or non-"1").

My code includes control for LEDs to indicate status and where a non-1 response should endlessly loop (as it requires user intervention on the server to mitigate).  Your application may be different.

Code: [Select]
 
  Serial.println("Conn Srv");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    String getrequest = "GET /ardurfid/commander.php";
    client.println(getrequest);
    client.println();
    Serial.println("Attn Reg");
    digitalWrite(redled,HIGH);
    digitalWrite(greenled,LOW);
    Serial.println("Client Output here.");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  } 
  while(client.connected())
  {
    while(client.available())
    {
      char c = client.read();
      if ( c == '#' )
      {
        Serial.println("Found response");
        c = client.read();
        if ( c == '1' )
        {
          Serial.println("Reg OK");
          digitalWrite(redled,LOW);
          digitalWrite(greenled,HIGH);
          delay(1000);
        }
        else
        {
          Serial.println("Reg Fail");
          digitalWrite(greenled,HIGH);
          for(;;)
          {
            digitalWrite(redled,HIGH);
            delay(500);
            digitalWrite(redled,LOW);
            delay(500);
          }
        }
      }
    }
  }
  client.stop();
 

  Serial.println("Done with client."); 




PaulS

Code: [Select]
    while(client.available())
    {
      char c = client.read();
      if ( c == '#' )
      {
        Serial.println("Found response");
        c = client.read();

While there is at least one character available to read, read two of them. This is not a recommended approach.

Go Up