Problem on YunClient

We’re using the latest version (1.5.8) and the latest openwrt-yun firmware and we think there’s a problem with the connect method of the YunClient.

Problem is quite simple, after 256 connect failures, the Python bridge enters an infinite loop and stop responding at all (you can check the method “connect” of the SocketServer bridge class). This is a source code that can replicate the problem:

#include
#include
#include

#define CM24_SOCKET_URL “www.anyaddress.local”
#define CM24_SOCKET_PORT 3503

long loopCounter = 0;

YunClient *yunClient = new YunClient();

void setup()
{
Serial.begin(9600);
while(!Serial); // wait for Serial port to connect.

Bridge.begin();
Serial.println("* SETUP DONE *");
}

void loop()
{
loopCounter++;

Serial.println("Loop " + String(loopCounter));
//Try connect
yunClient->connect(CM24_SOCKET_URL, CM24_SOCKET_PORT);

}

There’s no way to fix the problem, because Arduino thinks that client is not connected and issuing a “stop” doesn’t led to any solution.

Does anybody know a possibile fix to this problem?

I think you spotted a bug, can you open an issue on Github so we can track it?

Done! Already filed a bug!

Actually the solution is quite simple, but need a rework on the Bridge or the YunClient class.
It would be nice to find a workaround also for current and previous versions...

A solution is to replace a line inside the YunClient.cpp file.

This is the line https://github.com/arduino/Arduino/blob/ide-1.5.x/libraries/Bridge/src/YunClient.cpp#L163

You have to replace at line 163

opened = false;

With

stop();

I made a pull request with this fix and tomorrow it will be available in the nightly build IDE.
In the meanwhile you can replace this line by yourself and start testing it.

I'm having a similar problem but AFTER 256 CONNECT Success... it fails?

I'm running Arduion 1.6.5. I checked the cpp file and it looks correct as per above.

I added a client.stop(); before reaching 256 sends and it seemed to work for a minute and then failed again as follows... It freezes the Arduino at "connecting...." and does not go to to the else statement and print "failed"... manually resetting (pressing the atmel 32u4 button doesn't fix) nor does power cycling the board. Reprogramming the board works.

I was having an oddly similar issue using an ethernet shield communicating to the same server although it was a time based crash (after 25.25 minutes). As a cheap fix I included a softwareReboot() routine that seemed to fix it but I'd rather not drop the data.

Any help? thank you,

byte postData(char* page, String dataPackage) {
 
  Serial.print(F("connecting..."));
  if (client.connect(server, server_port)) {  
    Serial.println(F("connected"));
    printHeaders("POST", page, dataPackage.length());
    // send the body (variables)
    client.print(dataPackage);
    n++;
    Serial.println(n);
  } else {
    Serial.println(F("failed"));
    //Serial.println("rebooting software");
    //n = 0;
    //softwareReboot();
    return 0;
  }

  if(n== 250){
  client.stop();
  n = 0;
  } 
  
}

You close only the 250th instance of the connection, but the other 249 remains active. You should stop the client everytime.

Alternatively you can check if the client is already connected so you can avoid creating another connection if there is one available.