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.
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;
//
// }
}
}
}
}