   // set button pin as an input
   pinMode(BUTTON, INPUT_PULLUP);
 
+  Serial.begin(115200);
+
+  Serial.println(F("Adafruit IO Example:"));
+  Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);
+
+  // Initialise the CC3000 module
+  Serial.print(F("\nInit the CC3000..."));
+  if (! cc3000.begin())
+    halt("Failed");
+
+  // attempt wifi connection
+  while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
+    Serial.println(F("Retrying WiFi"));
+    while(1);
+  }
+  Serial.println(F("Connected to WiFi!"));
+
+  // connect to adafruit io
+  connect();
+
 }
 
 void loop() {
 
-  // required for MQTT connection
-  client.loop();
+  // Make sure to reset watchdog every loop iteration!
+  Watchdog.reset();
 
-  // reconnect if we lost connection to AIO
-  if(! client.connected()) {
-    Serial.print(F("AIO connection dropped. Attempting reconnect"));
-    mqtt_connect();
+  // ping adafruit io a few times to make sure we remain connected
+  if(! mqtt.ping(3)) {
+    // reconnect to adafruit io
+    if(! mqtt.connected())
+      connect();
   }
 
   // grab the current state of the button

#include <Adafruit_CC3000.h>
#include <SPI.h>

#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
#define BUTTON                2

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,
                                         ADAFRUIT_CC3000_IRQ,
                                         ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER);

Adafruit_CC3000_Client cc3000_client = Adafruit_CC3000_Client();
PubSubClient client("io.adafruit.com", 1883, mqtt_callback, cc3000_client);

// replace with your WiFi connection info
#define WLAN_SSID "test"
#define WLAN_PASS "secretpass"
#define WLAN_SECURITY WLAN_SEC_WPA2

// replace xxxxxxxxx with your Adafruit IO key
#define AIO_KEY "xxxxxxxxx"
#define MQTT_TOPIC "api/feeds/%s/data/send.json"

// button state
int current = 0;

  if(current == last)
    return;

  // Now we can publish stuff!
  Serial.print(F("\nSending button value: "));
  Serial.print(current == LOW ? 1 : 0);
  Serial.print("... ");

  if (! button.publish(current == LOW ? 1 : 0))
    Serial.println(F("Failed."));
  else
    Serial.println(F("Success!"));

  // save the button state
  last = current;

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default:
        Serial.println(F("Connection failed"));
        CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
        break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

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

}
