 void setup() {
   Serial.begin(9600);
   Serial.println(F("The Day of the Tentacle has begun!"));
-  debugPhoton();
+  setupDebug();
+}
+
+void setupDebug() {
+  Spark.function("wifiInfo", wifiInfoCmd);
+  Spark.function("connect", connectToServerCmd);
+  Spark.function("connectlocal", connectToLocalServerCmd);
+  Spark.function("disconnect", disconnectFromServerCmd);
+  Spark.function("authenticate", authenticateCmd);
+  Spark.function("config", requestConfigCmd);
+  Spark.function("sendPins", sendPinsCmd);
+  Spark.function("dfu", dfuCmd);
+}
+
+int wifiInfoCmd(String cmd) {
+  wifiInfo();
+  return 0;
+}
+
+int connectToServerCmd(String cmd) {
   connectToServer();
+  return 0;
+}
+
+int connectToLocalServerCmd(String cmd) {
+  connectToLocalServer();
+  return 0;
+}
+
+int disconnectFromServerCmd(String cmd) {
+  disconnectFromServer();
+  return 0;
+}
+
+int authenticateCmd(String cmd) {
+  return authenticate();
+}
+
+int requestConfigCmd(String cmd) {
+  return pseudopod.requestConfiguration();
+}
+
+int sendPinsCmd(String cmd) {
+  return pseudopod.sendConfiguredPins();
+}
+
+
+int dfuCmd(String cmd) {
+  System.dfu();
+  return 0;
 }
 
 void loop() {
+  Serial.println(F("In loop."));
+
+  if(!hasConnectedToServer) {
+    Serial.println(F("I haven't been told to connect yet. Waiting."));
+    delay(1000);
+    Serial.flush();
+    return;
+  }
+
+  if(!hasAuthenticated) {
+    Serial.println(F("I haven't been told to authenticate yet. Waiting."));
+    delay(1000);
+    Serial.flush();
+    return;
+  }
+
   if (!conn.connected()) {
+    Serial.println(F("I wasn't connected to the server!"));
     conn.stop();
     connectToServer();
   }
-  Serial.println(F("In loop."));
+
   Serial.flush();
   delay(2000);
   readData();
+
+  if(!pseudopod.isConfigured()) {
+    Serial.println(F("I'm not configured. Requesting configuration."));
+    delay(100);
+    pseudopod.requestConfiguration();
+  }
 
   if(pseudopod.shouldBroadcastPins() ) {
     delay(pseudopod.getBroadcastInterval());

// This #include statement was automatically added by the Spark IDE.
#include "tentacle-particle.h"

/*#define server "tentacle.octoblu.com"
#define port 80*/


IPAddress server(54,186,61,91);
#define port 80

static const char uuid[]  = "ff12c403-04c7-4e63-9073-2e3b1f8e4450";
static const char token[] = "28d2c24dfa0a5289799a345e683d570880a3bc41";

TCPClient conn;

TentacleArduino tentacle;
Pseudopod pseudopod(conn, conn, tentacle);

  Serial.println(WiFi.subnetMask());
  Serial.println(WiFi.gatewayIP());
  Serial.println(WiFi.SSID());

}
