CC3000 connectTCP Hanging

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
TimAhong
 
Posts: 3
Joined: Thu Jan 29, 2015 5:16 pm

CC3000 connectTCP Hanging

Post by TimAhong »

Hi I'm using the CC3000 and the adafruit wifi library. When I make a call to connectTCP it just hangs. The code below was working back in September but after upgrading my version of mac and potentially the version of the adafruit library it has since stopped functioning.

Here is my version info:

Mac Yosemite
Arduino IDE 1.0.5
Adafruit_CC3000.cpp v1.0
Atmel 328P by tinycircuits

Here is the code that I am using to call connectTCP:

Code: Select all


Serial.print(F("Initializing..."));
	wlan_start(0);
	delay(10);
	Serial.println(F("OK"));
	Serial.print(F("SSID: "));
	Serial.println(ssid);
	Serial.print(F("Password: "));
	Serial.println(pass);
	
	if(!cc3000.connectToAP(ssid, pass, wlan_security, 0)){
		Serial.println(F("Failed to connect to AP"));
		this->_connected = false;
		return false;
	}

	Serial.println(F("connected!"));
	Serial.print(F("Requesting address from DHCP server..."));
	for(t=millis(); !cc3000.checkDHCP() && ((millis() - t) < dhcpTimeout); delay(500)) {
		Serial.println(F("....waiting"));
	}
	if(cc3000.checkDHCP()) {
		Serial.println(F("OK"));
	} 
	else {
		Serial.println(F("failed"));
		this->_connected = false;
		return false;
	}

	//delay(10);
	//from loop
	t = millis();
	do {
		Serial.println(F("trying to connect to TCP"));
		client = cc3000.connectTCP(ip, port);
	} while((!client.connected()) && ((millis() - t) < connectTimeout));

	if (client.connected()) {
		Serial.println(F("client.connected"));    
		this->_connected = true;
	} else {
		Serial.println(F("Connection failed"));    
		this->_connected = false;
	}
	return this->_connected;

Does you know why connectTCP might be hanging? Any help you could provide would be awesome!

User avatar
TimAhong
 
Posts: 3
Joined: Thu Jan 29, 2015 5:16 pm

Re: CC3000 connectTCP Hanging

Post by TimAhong »

Here is an update on my investigation. I added some debug statements in the adafruit library in it appears as though it is hanging on the call to the "socket" function, specifically the line:

Code: Select all

tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Is where it is hanging, the full function is:

Code: Select all


Adafruit_CC3000_Client Adafruit_CC3000::connectTCP(uint32_t destIP, uint16_t destPort)
{
  sockaddr      socketAddress;
  int32_t       tcp_socket;

  // Create the socket(s)
  //if (CC3KPrinter != 0) CC3KPrinter->print(F("Creating socket ... "));
  tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == tcp_socket)
  {
    Serial.println("failed to open");
    CHECK_PRINTER {
      CC3KPrinter->println(F("Failed to open socket"));
    }
    return Adafruit_CC3000_Client();
  }
  //CC3KPrinter->print(F("DONE (socket ")); CC3KPrinter->print(tcp_socket); CC3KPrinter->println(F(")"));

  closed_sockets[tcp_socket] = false; // Clear any previous closed event

  // Try to open the socket
  memset(&socketAddress, 0x00, sizeof(socketAddress));
  socketAddress.sa_family = AF_INET;
  socketAddress.sa_data[0] = (destPort & 0xFF00) >> 8;  // Set the Port Number
  socketAddress.sa_data[1] = (destPort & 0x00FF);
  socketAddress.sa_data[2] = destIP >> 24;
  socketAddress.sa_data[3] = destIP >> 16;
  socketAddress.sa_data[4] = destIP >> 8;
  socketAddress.sa_data[5] = destIP;

  CHECK_PRINTER {
    CC3KPrinter->print(F("\n\rConnect to "));
    printIPdotsRev(destIP);
    CC3KPrinter->print(':');
    CC3KPrinter->println(destPort);
  }

  //printHex((byte *)&socketAddress, sizeof(socketAddress));
  //if (CC3KPrinter != 0) CC3KPrinter->print(F("Connecting socket ... "));
  if (-1 == ::connect(tcp_socket, &socketAddress, sizeof(socketAddress)))
  {
    CHECK_PRINTER {
      CC3KPrinter->println(F("Connection error"));
    }
    closesocket(tcp_socket);
    return Adafruit_CC3000_Client();
  }
  //if (CC3KPrinter != 0) CC3KPrinter->println(F("DONE"));
  return Adafruit_CC3000_Client(tcp_socket);
}

I don't think the issue is related to the IP/server that I am using because it was working a few months ago, I never shut down the servers and I can access it by manually sending the request

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 connectTCP Hanging

Post by adafruit_support_mike »

Are you sure the CC3000 has a working wifi connection when it reaches that statement?

Try adding a call to cc3000.checkConnected() before calling any code that needs a working link to succeed.

User avatar
TimAhong
 
Posts: 3
Joined: Thu Jan 29, 2015 5:16 pm

Re: CC3000 connectTCP Hanging

Post by TimAhong »

Ya checkConnected() is called as part of connectToAP which is called before attempting to connect to TCP and it succeeds. Here is the snippet from the adafruit library:

Code: Select all

bool Adafruit_CC3000::connectToAP(const char *ssid, const char *key, uint8_t secmode, uint8_t attempts) {

  ...

  do {
    // Stop if the max number of attempts have been tried.
    if (!retryForever) {
      if (attempts == 0) {
        return checkConnected();
      }
      attempts -= 1;
    }

    ...


User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 connectTCP Hanging

Post by adafruit_support_mike »

If I'm reading the code from your first post correctly, the call to .connectToAP() happens in setup() and the code that hangs is in loop().

Wifi is much more sensitive to noise and interference than wired connections. It isn't so much a question of whether the connection will drop as when. You have to check it every time you want to use the connection.

User avatar
jellygurl
 
Posts: 1
Joined: Thu Dec 10, 2015 8:53 pm

Re: CC3000 connectTCP Hanging

Post by jellygurl »

Hi. I believe I have a similar problem. After around 5-7 loops my code hang at connectTCP()
particularly at

Code: Select all

tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
is where it hangs. And I do check if I'm still connected to the WiFi using checkConnected() before doing connectTCP. Here's that bit

Code: Select all

if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
      
      RetryMillis = millis();  // Update the time stamp
  
      ip = Wido.IP2U32(216,52,233,120);  //Get Xively IOT Server IP  
      
    if(Wido.checkConnected()){ //Are we still connected to WiFi?
      Serial.println(F("Try to connect TCP"));
      WidoClient = Wido.connectTCP(ip, 80); //if yes, connect to Xively
    
    }else{ //if not
      if(!init_WiFi()){ //reconnect to WiFi
        delay(15 * 1000);     // if we couldn't connect, try again later
        return;
      }else{ //if successfully connected, try Xively again
        ip = Wido.IP2U32(216,52,233,120);
        WidoClient = Wido.connectTCP(ip, 80);
      }
      
    }
  }
How am I supposed to fix this?

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”