 // This #include statement was automatically added by the Particle IDE.
 #include "S3B.h"
 
-SYSTEM_THREAD(ENABLED);
+unsigned long tOut = 3000;
+
+void commandHandler(const char *event, const char *data);
 
 S3B module;
 
-byte S3BAddress[7][8] = {{0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0xD8}, 
-                        {0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0x43}, 
-                        {0x00, 0x13, 0xA2, 0x00, 0x40, 0xF2, 0x74, 0x79}, 
-                        {0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0xD8}, 
-                        {0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0xD8}, 
-                        {0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0xD8},
-                        {0x00, 0x13, 0xA2, 0x00, 0x41, 0x02, 0x20, 0xD8}};
+byte receiveBuffer[256];
                         
-int relayStatus = 0;
 
 void setup() {
     Particle.function("deviceComm", gatewayCommand);
+    Particle.subscribe("deviceCom", commandHandler);
+    Particle.function("clearInit", clearInit);
     Serial1.begin(115200);
     Wire.begin();
-    
-  String initCommands = "i2c,32,write,0,0|6,0";
-	byte initCommandBytes[initCommands.length()+1];
-	initCommands.getBytes(initCommandBytes, initCommands.length()+1);
-    module.transmit(S3BAddress[0], initCommandBytes, initCommands.length());
+    int initCommandsRan = runInitCommands();
+    Serial.printf("Ran %i init commands \n", initCommandsRan);
+}
+
+void commandHandler(const char *event, const char *data){
+    Serial.println("Got Event");
+    String newCommand = String(data);
+    gatewayCommand(newCommand);
 }
 
 void loop() {


            }
            String stringCommand = String(receivedData);
            gatewayCommand(stringCommand);
        }else{
            Serial.printf("First byte not valid, it was: 0x%x \n", startDelimiter);

// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"

// This #include statement was automatically added by the Particle IDE.
#include "ncd_gateway.h"

