CC3000 AP connection timeout exit.

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
unbound
 
Posts: 18
Joined: Sun Jan 12, 2014 9:51 pm

CC3000 AP connection timeout exit.

Post by unbound »

I am having a problem where the CC3000 hangs if it is unable to connect to the designated AP.
It works fine if I pass it a correct SSID and password, but in testing if I pass it an intentionally wrong SSID or password, the CC3000 hangs hard with no timeout.

I am using the following simple code:

Code: Select all

   
if (WiFiPresent){
    Serial.print("Trying to bind to "); Serial.println (WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.print("Unable to bind to "); Serial.println (WLAN_SSID);
    WiFiPresent = 0;
    WiFiStatus = 4; //Unable to bind to WiFi 
  }
 }
Using the serial monitor, I get the "Trying to bind to " message, but it hangs and I never get to the "Unable to bind to " message when it fails.

Help?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: CC3000 AP connection timeout exit.

Post by Franklin97355 »

Could we see the complete code?

User avatar
unbound
 
Posts: 18
Joined: Sun Jan 12, 2014 9:51 pm

Re: CC3000 AP connection timeout exit.

Post by unbound »

Using Arduino Mega256.
Binary sketch size: 47,474 bytes (of a 258,048 byte maximum)
RAM available when full sketch is running correctly about 4.4k.

To avoid confusion, I should clarify that the valid name of the SSID is "Unbound".
This is not urgent, I am just adding some error checking to the base code.

The code works OK if I have a valid SSID "Unbound".
Serial output sample of valid SSID:

Code: Select all

MAC Address : 0x08 0x00 0x28 0x59 0x85 0x9C

Deleting old connection profiles
Trying to bind to Unbound
Connected!
Request DHCP

IP Addr: 192.168.0.119
Netmask: 255.255.255.0
Gateway: 192.168.0.1
DHCPsrv: 192.168.0.1
DNSserv: 192.168.0.1
Locating time server...connected!
Issuing request...
Awaiting response...OK
18:47:23 26 7 2014
But it comes to a screeching halt if the SSID is bad, such as "Unbound1".

Code: Select all

MAC Address : 0x08 0x00 0x28 0x59 0x85 0x9C

Deleting old connection profiles
Trying to bind to Unbound1
That is as far as it gets. NOTHING happens no matter how long I wait.

The full code is looong, here is the most pertinent piece:

Code: Select all

void startWIFI (){
  if (!cc3000.begin())
  {
    Serial.println("CC3000 Wifi not responding");
    WiFiPresent = 0;
    WiFiStatus = 2; //CC3000 Wifi not responding
  }
  
if (WiFiPresent) displayMACAddress();
      
  
  /* Delete any old connection data on the module */
  Serial.println("\nDeleting old connection profiles");
  if (!cc3000.deleteProfiles()) {
    Serial.println("Unable to clear WiFi Cache");
    WiFiPresent = 0;
    WiFiStatus = 3; // Unable to clear WiFi Cache
  }

  /* Attempt to connect to an access point */
  char *ssid = WLAN_SSID;             /* Max 32 chars */
  
  /* NOTE: Secure connections are not available in 'Tiny' mode! */
  if (WiFiPresent){
    Serial.print("Trying to bind to "); Serial.println (WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.print("Unable to bind to "); Serial.println (WLAN_SSID);
    WiFiPresent = 0;
    WiFiStatus = 4; //Unable to bind to WiFi 
  }
  }
  
  if (WiFiPresent) Serial.println("Connected!");
  
  /* Wait for DHCP to complete */
  if (WiFiPresent){
    Serial.println("Request DHCP");
    uint32_t ip = 0L, t;
    for(t=millis(); !cc3000.checkDHCP() && ((millis() - t) < dhcpTimeout); delay(100));
    if(!cc3000.checkDHCP()){
    Serial.print("Unable to obtain DHCP");
    WiFiPresent = 0;
    WiFiStatus = 5; //Unable to obtain DHCP     
    }
    displayConnectionDetails(); 
  }  
}

User avatar
unbound
 
Posts: 18
Joined: Sun Jan 12, 2014 9:51 pm

Re: CC3000 AP connection timeout exit.

Post by unbound »

Update.

It looks to me like the no-timeout issue when trying to connect to an nonexistent AP is a hole in the Adafruit library, because it also happens with the "buildtest" example.

User avatar
luatntother
 
Posts: 3
Joined: Fri Jan 02, 2015 2:42 pm

Re: CC3000 AP connection timeout exit.

Post by luatntother »

Hi guys!

I had gotten the same Unbound's problem when i worked with Adafruit CC3000 wifi breakout and Arduino UNO R3 but i found out solution for this problem. In fact, this is not a CC3000's problem, it's just that Adafruit's engineers do not show us the best way to use Adafruit_CC3000::connectToAP api.

The Adafruit_CC3000::connectToAP api, in the original, has 4 parameters, you can see this in "Adafruit_CC3000.cpp" file at following folder path: C:\Users\<your_pc's_user_name>\Documents\Arduino\libraries\Adafruit_CC3000_Library-master

If you open this file up, you'll see that the Adafruit_CC3000::connectToAP api's prototype is:

Code: Select all

bool Adafruit_CC3000::connectToAP(const char *ssid, const char *key, uint8_t secmode, uint8_t attempts)
But when you use this api in your sketch, you (and Adafruit's engineers) bind only 3 parameters to the api, those are the "ssid", the "key" and the "secmode" and the api still works! So, what is "attempts" parameter for and why does not it appear in “connectToAP” api of ”buildtest” sketch from Adafruit?

At first, what is this parameter for?
It’s the times that cc3000 will attempt to connect to an AP and this is the key of the problem that Unbound and I got (maybe you did). By default, this parameter’s value is “0”, it means that cc3000 module will attempt to connect to an AP forever until it connects to the AP successfully – this is not good in almost cases.

Secondly, why does not “attempts” parameter appear in “connectToAP” api of ”buildtest” sketch from Adafruit?
The reason for this is, as I mentioned above, this parameter’s value is “0” by default so we don’t need to use it when we call the api (in fact, maybe sometimes we want to use it, but we don’t know how to do that :D ). This is done in the api’s prototype declaration in “Adafruit_CC3000.h” file, which is in the same folder with "Adafruit_CC3000.cpp" file. In this file, connectToAP api’s prototype is declared as the following:

Code: Select all

bool     connectToAP(const char *ssid, const char *key, uint8_t secmode, uint8_t attempts = 0);
As you can see, by default, attempts = 0

So, at last, how to solve the problem?
All things we have to do are just open “Adafruit_CC3000.h” file, change this line

Code: Select all

bool     connectToAP(const char *ssid, const char *key, uint8_t secmode, uint8_t attempts = 0);
to

Code: Select all

bool     connectToAP(const char *ssid, const char *key, uint8_t secmode, uint8_t attempts);
then save the file and use "connectToAP" api like this: cc3000.connectToAP(ssid, key, secmode, n); instead of connectToAP(ssid, key, secmode); in our sketch. “n” in the function cc3000.connectToAP(ssid, key, secmode, n) is the times that you want the cc3000 to attempt to connect to your AP, it can be 1, 2, 3, … or any value that you want.

As I tested, if n = 2, It takes about 46 seconds from the moment cc3000 starts connecting to the AP to the moment the api return “false” value if it cannot connect to the AP. If n = 3, it’s about 66 seconds and if n = 5, it’s about 115 seconds. You can try changing “n”’s value to find out which it’s value is suitable for your sketch :)

Sorry for any inconvenience because of my bad English!

Thanks & Best Regards,
LuatNT

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: CC3000 AP connection timeout exit.

Post by tdicola »

Thanks for diving in and exploring how the retry attempt parameter works! You actually shouldn't have to modify the header file to change or remove its default value of 0. Try running your code that uses the extra parameter as is and it should still compile and work.

If you're curious we added the optional parameter in this pull request: https://github.com/adafruit/Adafruit_CC ... ry/pull/89 The rationale for making it optional was just to preserve backwards compatibility, so if someone had a sketch that expected connectToAP to retry forever (like a long running monitoring application) then they wouldn't be broken by the change.

Great point though that it should be documented a little more clearly. I'll update the comments in the buildtest example to make a note of it. Thanks!

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

Return to “Other Arduino products from Adafruit”