Adafruit CC3000 wifi breakout board code issue "while" commands

Hello everyone,

I have built a fish tank controller using the Arduino Mega. Up until adding the CC3000 is has perfomed exactly as expected. The issue I'm having is all the "while" commands with in the code for the CC3000.

For example, using the below code, if the DHCP isn't able to resolve the whole thing just hangs waiting for it to. This causes a huge issue say if the heater comes on, then the controller hangs because it can't resolve the DHCP, the heater will not shut off when the preset temp is reached because the controller is "hung".

while (!cc3000.checkDHCP())  
{    delay(100);  }
ip = 0;
Serial.print(WEBSITE); 
Serial.print(F(" -> "));
while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) 
{  
Serial.println(F("Couldn't resolve!")); 
} 
delay(500);}cc3000.printIPdotsRev(ip);

FULL CODE

if (timer0 > interval)
       {
        // connect to WiFi network
        cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
         Serial.println(F("Connected!"));
  
         // wait for DHCP to complete
        Serial.println(F("Request DHCP"));
        while (!cc3000.checkDHCP())
          {
            delay(100);
          }
        
        // get the website IP & print it
        ip = 0;
        Serial.print(WEBSITE); Serial.print(F(" -> "));
        while (ip == 0) 
          {
            if (!cc3000.getHostByName(WEBSITE, &ip)) 
            {
              Serial.println(F("Couldn't resolve!"));
            }
            delay(500);
          }
        cc3000.printIPdotsRev(ip);      
  
         // get data 
  
         int length = 0;

          String data = "";
          data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Water_Temp\",\"current_value\" : \"" + String(temp1) + "\"}," 
          + "{\"id\" : \"Box_Temp\",\"current_value\" : \"" + String(temp0) + "\"}]}";  
          length = data.length();
          Serial.print("Data length");
          Serial.println(length);
          Serial.println();
    
          // Print request for debug purposes
          Serial.print("PUT /v2/feeds/");
          Serial.print(feedID);
          Serial.println(".json HTTP/1.0");
          Serial.println("Host: api.xively.com");
          Serial.print("X-ApiKey: ");
          Serial.println(API_key);
          Serial.print("Content-Length: ");
          Serial.println(length, DEC);
          Serial.print("Connection: close");
          Serial.println();
          Serial.print(data);
          Serial.println();
  
            // Send request
          Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
          if (client.connected()) {
          Serial.println("Connected!");
          client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
          client.println("Host: api.xively.com");
          client.println("X-ApiKey: " + String(API_key));
          client.println("Content-Length: " + String(length));
          client.print("Connection: close");
          client.println();
          client.print(data);
          client.println();
          } else 
            {
            Serial.println(F("Connection failed"));    
            return;
            }
  
          Serial.println(F("-------------------------------------"));
          while (client.connected()) 
            {
              while (client.available()) 
              {
                char c = client.read();
                Serial.print(c);
              }
            }
          client.close();
          Serial.println(F("-------------------------------------"));  
          Serial.println(F("\n\nDisconnecting"));
          cc3000.disconnect();
          timer0 = 0;   //resets the time to 0 so the counting starts over

}

Is there any way to write the code removing all the "while" commands?

the code can be found here. GitHub - makecademy/arduino-cc3000-xively

It would be better to put the system into a safe state before checking the DHCP or to change the while to an if and put the system into a safe state if the DHCP could not be resolved.

It would be easier to see your code if you posted it here. As it is, I have not looked at it.

Bob,

Thank you very much for the response. I appreciate you taking a look at it.

I'm at work now but I will post the code when I get home this evening.

I don't know about that device library, but in the wifi ethernet shield from Arduino, it returns zero if it is not time to renew the dhcp lease. Zero means "no action taken".

while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) 
{  
Serial.println(F("Couldn't resolve!")); 
} 
delay(500);}cc3000.printIPdotsRev(ip);

Please find the name for this style. I call it piss-poor.

No standard style allows for the curly brace on the same line as any other code.
No standard style allows for anything after the }.
No standard style allows for anything after the {.

PaulS:

while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) 


Serial.println(F("Couldn't resolve!"));
}
delay(500);}cc3000.printIPdotsRev(ip);



Please find the name for this style. I call it piss-poor.

No standard style allows for the curly brace on the same line as any other code.
No standard style allows for anything after the }.
No standard style allows for anything after the {.

Paul I'm not sure how your comment helps my situation? If you have any insight that would help correct my issue, please share it. Thank you in advance.

Is this failing and staying in this loop?

while (!cc3000.checkDHCP())  
{    delay(100);  }

I can't find the description of these functions and the return values. Can you post a link to that?

I have added the full code to my original message.

Thank all of you who take the time to look at it and help me out.

MSGCampbellD:
FULL CODE

I think not....

That was all of the code for the CC3000?

I cannot find the whole code of the program either posted here or attached to a message.
Have I missed it ?