4 years, 11 months ago.

WiflyInterface : Issue with int TCPSocketConnection::connect (const char* host, const int port)

Hi,

Clarification

Tried the Socket - int TCPSocketConnection::connect (const char* host, const int port) function that came with David WifiInterface Rev 70:83e420b0d3e1 and was not connecting, (David, thanks for the great work).

The original code from Rev 70:83e420b0d3e1 was :

int TCPSocketConnection::connect(const char* host, const int port)
{  
    if (!wifi->connect(host, port))
        return -1;
    wifi->flush();
    return 0;
} 

Was then replaced with that from Rev 11:002225507f44 - and it able to connect

int TCPSocketConnection::connect(const char* host, const int port)
{
    //open the connection
    char cmd[30];
    char rcv[40];

    // if we cannot enter in cmd mode, maybe we have a tcp connection opened.
    // in this case we need to wait at least 0.25s to enter in cmd mode
    if (!wifi->cmdMode()) {
        wait(0.25);
        if (!wifi->cmdMode()) {
            wifi->send("\r", 1);
            wait(0.1);
            wifi->exit();
            return -1;
        }
    }

    if (wifi->gethostbyname(host, rcv)) {
        sprintf(cmd, "set ip host %s\r", rcv);
        if (!wifi->sendCommand(cmd, "AOK"))
            return -1;
    } else {
        return -1;
    }

    sprintf(cmd, "set ip remote %d\r", port);
    if (!wifi->sendCommand(cmd, "AOK"))
        return -1;

    if (wifi->sendCommand("open\r", NULL, rcv)) {
        if (strstr(rcv, "OPEN") == NULL) {
            if (strstr(rcv, "Connected") != NULL) {
                if (!wifi->sendCommand("close\r", "CLOS"))
                    return -1;
                if (!wifi->sendCommand(cmd, "OPEN"))
                    return -1;
            } else {
                return -1;
            }
        }
    } else {
        return -1;
    }

    wifi->flush();
    return 0;
}

Thanks

Hi,

Sorry, forgotten to post, my finding in consultation with Dave. There is a logic error in the TCPSocketConnection.cpp.

int TCPSocketConnection::connect(const char* host, const int port)
{  
    // if (!wifi->connect(host, port)) -> CODING ERROR
        
    if (wifi->connect(host, port))  // connected - True not connected - False
        return -1;
    wifi->flush();
    return 0;
}

Thanks Dave

posted by Boy Crystal 23 Dec 2015

1 Answer

4 years, 11 months ago.

Hi Boy,

Well, for sure it was not my plans to reduce the capability. I'd like to ask two things -

  1. Edit your note above, and wrap it with <<code>> <</code>>, one just above your code, and the other just below.
  2. Can you share the smallest sample program to demonstrate the behavior? I've got a project underway with the WiflyInterface module right now, so if I can replicate your issue, I will solve it.

Thanks, Dave

Accepted Answer

Hi Dave,

Thanks for the response.

Done as per item 1.

For Item 2, don't know is this what you are looking for.

     wifly.flush();  // during first transmit
     while(ThingSock.connect(IPThingSpeak, 80)!= 0)    // wait till TCP connect successful - 0
        {
         wait(0.1);
         wifly.flush();  // during retransmit - to see still got missing gap
         z++;
         if(z == 5)
           {
            // *** Check is wifly still connected to wireless router *** //
            while(!wifly.is_connected())  // if not connected
              {
               wifly.disconnect();
               wait(0.1);
               wifly.connect();
               wait(0.1);
              }
            // *** End of auto recovery from lost association *** //
              
            z=-1;  // -1 don;t sent data
            error = 1;
            
            // *** Debug Message *** //
            ClearScn(2.0);
            lcd.printf("\nTCP FAIL Count %d",ivalue);
            
            break;  // Break from While Loop
           }
        };
     wait(0.1); // wait for connection to settle down - to see still got missing gaps
     if(z != -1)  // TCP connected and is within 5 tries
       {
        ThingSock.send_all(strPOST, (int)strlen(strPOST));
        ThingSock.close();  // CLOSE HERE

Dave, I am not a real programmer, appreciate if you could patch your lib, and once again thanks for allowing me to use your lib, (great work). Oh, by the way, taking the hint from you, I have successfully include codes to re-connect wireless connection between Wifly and wireless router (association ???). Thanks !!!

Boy

posted by Boy Crystal 20 Dec 2015