/*
@
*/
void setup(void)
{
  Serial.begin(115200);
  Serial.print(F("Starting up, this is version "));Serial.println(VERSION); 

  Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);

  /* Initialise the module */
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
  MAC = getMACAddress();
  MAC.toCharArray(MACbuff,MAC.length());
  Serial.print(F("the MAC is "));Serial.println(MACbuff);
}

void loop(void)
{
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  Serial.println(F("Failed!"));
  while(1);
  }

  Serial.println(F("Connected!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
  delay(1000); // ToDo: Insert a DHCP timeout!
  }  
  int DNSReset = 0;
  /* Display the IP address DNS, Gateway, etc. */  
  while (! displayConnectionDetails()) {
    delay(1000);
    if (DNSReset++>10){
      softwareReset();
    }
  }

  cc3000.printIPdotsRev(ip);

  /* Try connecting to the website.
   Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
  */
  while (true)
  {
    // according to Adafruit, we might lose the connection with the AP so we must check and reconnect
    if (!cc3000.checkConnected())
    {
      Serial.println("##################################### AP connection failure detected, force reconnect...");
      break;
    }
    // connect->send data->disconnect->sleep->repeat
    sendData();
    Serial.println(F("\n\nSleeping"));
    delay(SLEEP);
  }
  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();
}
/*
*@
*/

 

 

void sendData(void)
{
  Adafruit_CC3000_Client  www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(F("?temp="));
    int t = getTemperature();
    sprintf(charBuf,"%d",t);
    www.fastrprint(charBuf);
    Serial.print("Measured temperature ");Serial.println(charBuf);
    Serial.print("SRAM: ");Serial.println(getFreeRam(), DEC);
    www.fastrprint(F("&ioti="));
    www.fastrprint(MACbuff);
    Serial.print("MAC: ");Serial.println(MACbuff);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); 
    www.fastrprint(WEBSITE); 
    www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
    Serial.println(F("---------------done writing----------------------"));

    /* Read data until either the connection is closed, or the idle timeout is reached. */ 
    unsigned long lastRead = millis();
    while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) 
    {
      while (www.available()) 
      {
        char c = www.read();
        Serial.print(c);
        lastRead = millis();
      }
    }
    www.close();
    delay(200);
    Serial.println(F("\r\n----------done reading ---------------------------"));  
  }
  else {
    Serial.println(F("Connection failed, trying again later..."));    
  }
}