+// 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"
 

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

SYSTEM_THREAD(ENABLED);

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}};
                        
int relayStatus = 0;

void setup() {
    Particle.function("deviceComm", gatewayCommand);
    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());
}

void loop() {

            byte lenMSB = Serial1.read();
            byte lenLSB = Serial1.read();
            int newDataLength = (lenMSB*256)+lenLSB;
            
            int count = 0;
            while(count != newDataLength+1 && millis() <= tOut+startTime){
                if(Serial1.available() != 0){
                    receiveBuffer[count] = Serial1.read();
                    count++;
                }
            }
            if(count < newDataLength+1){
                Serial.println("Timeout2");
                Serial.printf("Received Bytes: %i, expected %i \n", count, newDataLength+1);
                return;
            }
            Serial.printf("Received %i bytes \n", count);
            //We have all our data.
            byte newData[newDataLength+4];
            newData[0] = startDelimiter;
            newData[1] = lenMSB;
            newData[2] = lenLSB;
            for(int i = 3; i < newDataLength+4; i++){
                newData[i] = receiveBuffer[i-3];
            }
            Serial.print("Received: ");
            for(int i = 0; i < sizeof(newData); i++){
                Serial.printf("%x, ", newData[i]);
            }
            Serial.println();
            //validate data
            if(!module.validateReceivedData(newData, newDataLength+4)){
                Serial.println("Invalid Data");
