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