Pages: [1]   Go Down
Author Topic: Help with Heartbeat client code  (Read 1587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a project that I need my arduino uno connected to the wifi shield to open up multiple TCP connections with a webserver I have created and send and receive some information via HTTP. The TCP connections do not have to be pipelined they are just one after another waiting for the first to finish before the second. The problem that I am having is that my arduino times out before it has received all of the information that is sent back from the webserver(if it takes longer than ~10 seconds to connect and recieve it all). When it does this it freezes up the whole arduino and I have to reset it. From some research it appears this a known issue/bug that is in the wifi shield firmware. I have seen that people are implementing hearbeat clients in order to keep the connection alive so that the wifishield does not terminate. I'm unsure of how to implement this in C on the arduino. I have searched for some code on how to do this but am unaware of how to implement it practically. Can anyone help me with how I could go around writing this code for the arduino or point me in the direction of where I could find help. I also saw the people were updating the firmware on their shield but I read that this was only a fix for it if it is a server and not a client?
Here is the main part of the code that I am working with, the code that I have wrote seems to be working fine the problem is that I cannot always get all of the information back.

Code:
void loop() {
   while (connection_estabalished == false){//check to see if the connection has been estabalished yet, if it hasn't then try to connect
    Serial.println("connecting...");
  if (client.connect(server,80)){
    Serial.println("making get request for proifile"); // this is where we make a get request for the home page this basically simulates typing remotlet.com into your browser
      client.println("GET / HTTP/1.1");// we are doing this becase we want to get a cookie from remotlet.com
      client.println("Host:www.remotlet.com");
      client.println("Connection: close");
      client.println();
      connection_estabalished =true;
  }
  }
 
  // if there are incoming bytes available
  // from the server, read them and put them into a string
  if(cookie_read==false){
  while (client.available()) {
    while(headercounter<350){ // we only want to copy the header that is sent back which is ~350 chars that is where the cookie is, we dont want to waste memory or time on the entire site
    char c = client.read(); // read the bytes into c
    currentLine+= c; //store c into currentline
    headercounter++;
    if (headercounter==349){ //once we have the whole header print it for trouble shooting purposes
      //Serial.println("Current Line; "); //dont need these lines they are just for trouble shooting
      //Serial.println(currentLine);
      delay(1000);
      client.stop();
    }
   
   
    if ( currentLine.endsWith("Set-Cookie: ")) { // check to see if the current line ends with set cookie this way we will know that the cookie info is coming up
        // cookieis beginning. set reading flag:
        Serial.println("setting read info");
        readingInfo = true;
      }
      // if you're currently reading the bytes of a cookie,
      // add them to the cookie String:
      if (readingInfo) {
        if (c != ';') {
          cookie += c;
        }
        else {
          // if you got a ";" character,
          // you've reached the end of the cookie:
          readingInfo = false;
          Serial.println("Cookie:");
          Serial.println(cookie);
          cookie_read=true;
          Serial.println("cookie_read status: ");
          Serial.println(cookie_read);
        }
      }
    }
  }
  } // this is where we will login to remotlet.com so we can have access to the user page
    if(cookie_read==true){ // check to make sure that we have the cookie before we try to login
    while(scnd_cnctn_est==false){ // check to make sure we have estabalished a new connection with the server
    if (client.connect(server, 80)) { //make the connection to the server
          scnd_cnctn_est=true;
          Serial.println("in the post portion");
          Serial.println("connected to server");
//        // Make a HTTP post:
          client.println("POST /index.php/component/comprofiler/login HTTP/1.1");
          client.println("Host:remotlet.com");
          client.println("Content-Type: application/x-www-form-urlencoded");
          client.println("Content-Length: 225");
          client.println("Connection: keep-alive");
          client.print("Cookie:");
          client.println(cookie);
          client.println();
              client.println("username=user&passwd=secretpassword&op2=login&lang=english&force_session=1&return=B%3AaHR0cDovL3JlbW90bGV0LmNvbS8%3D&message=0&loginfrom=loginmodule&cbsecuritym3=cbm_732e97f9_40ef70af_b80a1e5829f76aabe4c61f5c28dbc001&Submit=Login");
          while (client.available()) { //this is all for trouble shooting
          //char d= client.read();
          //Serial.write(d);
          writecounter++;
          if(writecounter==100){
            client.stop(); //client.stop(); is always needed do not remove
            Serial.println("connection closed");
            login_complete=true; //end of trouble shooting
            delay(1000);
          }
          }
    }
    }
    } //this where we will get the userdetails and eventually from this get the device status
    if(login_complete==true){ // check to make sure that we have actually logged into the wesbite before we try to get user details
      while(thrd_cnctn_est==false){// make sure that we have established a 3rd connection
         if (client.connect(server,80)){
          Serial.println("making get request for user status"); // this is working to get the right page but... we need are not receiving the whole page back consistently.  Sometimes get nothing...
          client.println("GET /index.php/profile/userdetails HTTP/1.1");
          client.println("Host:www.remotlet.com");
          client.println("Connection: keep-alive");
          client.println("Referer: http://remotlet.com/index.php/profile");
          client.print("Cookie:");
          client.println(cookie);
          client.println();
          thrd_cnctn_est =true;
          while (client.available()) {
          char e= client.read();
        //  userdata+=e;
          //usercounter++;
          //Serial.println(usercounter);
          Serial.write(e);
          //delay(10);
         
         
    }
//    if(flag==false){
//          Serial.println();   
//          Serial.println("Userdata");
//          Serial.println(userdata);
//          flag=true;
//
//    }
    }
    }
    }
   
}




Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you find where in the library code the timeout is processed?
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After you send the request, try reading the entire server response. The server will close the connection when finished sending packets.

This is a simple example. There is no timeout if the connection breaks. Note the addition of a "while(client.connected())" loop. It should read the entire response.
Code:
while(client.connected()) {
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}
client.stop();
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bill thank you for the reply, I am not sure where in the library the time out is being caused, I am fairly new to working with the arduino so I'm not sure how I would go to look for that, it would appear others more talented than I have tried to fix this and failed. I am basing my guess that my issue is from a time out because of cases like this one http://code.google.com/p/arduino/issues/detail?id=1024 that are being reported by other users.

Tim thank you for the suggestion, I tried adding in the while loop that you recommended and tested it on a basic get request for a wikipedia page here is the code that I used:

Code:
while(flag==false)//*addition from revision 2*
  {
    Serial.println("connecting");//addition from revision 2 this is purely for trouble shooting
    if (client.connect(server, 80))
    {
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /wiki/Code HTTP/1.1");//changed to wikipedia for revision 4 of code
      client.println("Host:en.wikipedia.org");
      client.println("Connection: close");
      client.println();
      flag=true;
        while(client.connected()){
          while (client.available()) {
          char c = client.read();
          Serial.write(c);
          }
        }
      }
  }
client.stop();

I also tried this:
Code:
  while(flag==false)//*addition from revision 2*
  {
    Serial.println("connecting");//addition from revision 2 this is purely for trouble shooting
    if (client.connect(server, 80))
    {
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /wiki/Code HTTP/1.1");//changed to wikipedia for revision 4 of code
      client.println("Host:en.wikipedia.org");
      client.println("Connection: close");
      client.println();
      flag=true;
    }
  }
  while(client.connected()){
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
  }
  }
  client.stop();

With both of these after testing a few times for each I get the same result as before, the client drops the connection mid way through receiving the whole response, each time I get a different amount of the response but can never get a full request unless the response is for a very short page.
Logged

Pages: [1]   Go Up
Jump to: