Mbed switch for Samsung SmartThings using Xbee

Dependencies:   C12832 mbed

Revision:
2:e78ec8625034
Parent:
1:80fe03be2832
--- a/Main.cpp	Thu Sep 15 11:47:43 2016 +0000
+++ b/Main.cpp	Thu Sep 15 14:19:56 2016 +0000
@@ -5,7 +5,7 @@
 #define HarV 0x01
   
 C12832 lcd(p5, p7, p6, p8, p11);                                        //configure LCD
-RawSerial xbee(p9,p10);                                                    //Set Serial to XBee
+RawSerial xbee(p9,p10);                                                 //Set Serial to XBee
 Serial pc(USBTX, USBRX);                                                //Set Serial to PC.
 DigitalOut led2 (LED2);                                                 //configure onboard LEDs
 DigitalOut led1 (LED1); 
@@ -42,7 +42,8 @@
 void ActiveEPReq();                                                     //Function to create the Active end point request.
 void ClusterBasic();                                                    //Function to create the Cluster Basic.
 void OnOff();                                                           //Function to process on off commands.
-void DefRes();                               //Function to create the response for the on off command.
+void DefRes();                                                          //Function to create the response for the on off command.
+void Update(char val);
 
 int main(){
     lcd.cls();                                                          //clear lcd screen.
@@ -55,62 +56,45 @@
     while(packet[5] != 0){                                              //While Xbee has not joined a network
         wait(1);                                                        //Wait 0.1 second.
         Psend(AI);                                                      //Send network joined status again.
-        pc.printf("%x," , packet[5]);                                   //Serially print netowrk status.
-        check = 0;
         while(check == 0)
-        {}                                         //Read data.
+        {}                                                              //Read data.
     }
-    pc.printf("connected");
     lcd.cls();                                                          //Clear lcd screen.
     lcd.locate(0,1);                                                    //Locate the cursor.
     lcd.printf("        Connected");                                    //Print to lcd screen.
     
-    wait(1);                                                            //Wait 0.1 seconds.
     check = 0;
     Psend(MY);                                                          //Send AT command to get the 16bit network address.
-    while(check == 0)                                                  //Wait for packet.
-    {
-        wait(0.1);
-    }
-        LNw[0] = packet[5];                                             //Transfer 16 bit address payload.
-        pc.printf("%x",packet[5]);                                      //Serially print result.
-        LNw[1] = packet[6];                                             //Transfer 16 bit address payload.
-        pc.printf("%x \n",packet[6]);                                   //Serially print result.
+    while(check == 0)                                                   //Wait for packet.
+    {}
+    LNw[0] = packet[5];                                                 //Transfer 16 bit address payload.
+    LNw[1] = packet[6];                                                 //Transfer 16 bit address payload.
     
     check = 0;
     Psend(SH);                                                          //Send AT command to get the 64bit address MSB.
-    while(check == 0)                                                 //Wait for packet.
+    while(check == 0)                                                   //Wait for packet.
     {}
     Addr[0] = packet[5];                                                //Transfer 64 bit address payload.
-    pc.printf("%x",packet[5]);                                          //Serially print result.
     Addr[1] = packet[6];                                                //Transfer 64 bit address payload.
-    pc.printf("%x",packet[6]);                                          //Serially print result.
     Addr[2] = packet[7];                                                //Transfer 64 bit address payload.
-    pc.printf("%x",packet[7]);                                          //Serially print result.
     Addr[3] = packet[8];                                                //Transfer 64 bit address payload.
-    pc.printf("%x \n",packet[8]);                                       //Serially print result.
     
     check = 0;
     Psend(SL);                                                          //Send AT command to get the 64bit address LSB.
-    while (check == 0)                                                 //Wait for packet.
+    while (check == 0)                                                  //Wait for packet.
     {}
-        Addr[4] = packet[5];                                            //Transfer 64 bit address payload.
-        pc.printf("%x",packet[5]);                                      //Serially print result.
-        Addr[5] = packet[6];                                            //Transfer 64 bit address payload.
-        pc.printf("%x",packet[6]);                                      //Serially print result.
-        Addr[6] = packet[7];                                            //Transfer 64 bit address payload.
-        pc.printf("%x",packet[7]);                                      //Serially print result.
-        Addr[7] = packet[8];                                            //Transfer 64 bit address payload.
-        pc.printf("%x \n",packet[8]);                                   //Serially print result.
-    wait(1);                                                            //Wait 0.1 seconds.
+    Addr[4] = packet[5];                                                //Transfer 64 bit address payload.
+    Addr[5] = packet[6];                                                //Transfer 64 bit address payload.
+    Addr[6] = packet[7];                                                //Transfer 64 bit address payload.
+    Addr[7] = packet[8];                                                //Transfer 64 bit address payload.
     DevAnnc();                                                          //Send Device Announce.
     
 while(1) {   
-lcd.locate(0,21);                                                       //Locate LCD cursor.
-lcd.printf("Packets recieved = %d", z);                                 //Print to LCD display.
-check = 0;
-while(check == 0)                                                       //Wait for packet.
-{}
+    lcd.locate(0,21);                                                   //Locate LCD cursor.
+    lcd.printf("Packets recieved = %d", z);                             //Print to LCD display.
+    check = 0;
+    while(check == 0)                                                   //Wait for packet.
+    {}
     if (packet[16] == 0x00){                                            //If packet profile = 16.
         if (packet[14] == 0x04){                                        //If packet cluster = 14.
             lcd.locate(0,11);                                           //Locate LCD cursor.
@@ -143,39 +127,35 @@
             lcd.printf("     Unknown Packet");                          //Print to lcd screen.
             }
         }
-
-}                                                                       //end of while(1)
+    }                                                                   //end of while(1)
 }
 
 /********************************* Packet interupt ***************************************
 When a new Xbee packet is revieved it is sent to this function to be processed.
 
 *****************************************************************************************/
-void packet_interupt()                                                  //packet interupt begins when incoming data sensed on the rx line.
-{
+void packet_interupt(){                                                 //packet interupt begins when incoming data sensed on the rx line.
+
 if (xbee.readable())                                                    //If data is available
     x=xbee.getc();                                                      //Get data
 if (x==0x7E){                                                           //Test for start of Frame.
-    led2 =1;                                                            //Set indicator LED2 to on.
+    led2 = 1;                                                           //Set indicator LED2 to on.
     z ++;                                                               //Increment packet counter.
-    pc.printf("packet Recieved");                                       //Serially print to PC.
     memset(packet, 0, sizeof(packet));                                  //Clear packet buffer.
     while(xbee.readable() == 0);                                        //If data is available.
-        x =xbee.getc();                                                 //Get data MSB length.
+        x = xbee.getc();                                                //Get data MSB length.
     while(xbee.readable() == 0);                                        //If data is available.
-        len=xbee.getc();                                                //Get data LSB length.
-        x=0;                                                            //Counter to 0.
+        len = xbee.getc();                                              //Get data LSB length.
+        x = 0;                                                          //Counter to 0.
         sum = 0;                                                        //Set checksum calculator to 0.
-        while(x < (len +1)){                                            //Fill packet variable with entire API packet.
-            while (xbee.readable()==0);                                 //If data is available.
-            packet[x]=xbee.getc();                                      //Get data and place it in the data array.
-            //pc.printf("%x",packet[x]);
+        while(x < (len + 1)){                                           //Fill packet variable with entire API packet.
+            while (xbee.readable() == 0);                               //If data is available.
+            packet[x] = xbee.getc();                                    //Get data and place it in the data array.
             sum = (sum + packet[x]);                                    //Add value to checksum calculation.
             x++;
             }    
         sum = (0xFF - ( sum & 0xFF));                                   //Finish calculating the checksum.
-        if (packet[x]  == sum ){                                        //If the checksum and the calculated checksum are equal.
-            pc.printf(" checksum good \n");
+        if (packet[x] == sum ){                                         //If the checksum and the calculated checksum are equal.
             if (packet[0] == 0x91){                                     //If packet ID = 0x91
                 Builder();                                              //Extract packet information
                 check = 1;
@@ -187,7 +167,6 @@
                 memset(packet, 0, sizeof(packet));                      //Clear array
         }
         else{
-            pc.printf(" checksum bad \n");
             memset(packet, 0, sizeof(packet));                          //Clear array
         }
     led2 = 0;                                                           //Set indicator LED2 to off.
@@ -212,17 +191,15 @@
     x = 0;                                                              //Set counter to 0.
     while ((x+18) < len){
         pay[x] = packet[(x+18)];                                        //Copy packet payload.
-        pc.printf(" %x," ,packet[x+18]);                                //Print packet contents to PC.
         x++;
     }
-    pc.printf(" \n");
 }
 /************************************ PBuild ******************************************
 Organise the information from other functions into the correct packet format. 
 
 *****************************************************************************************/
-void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[],char PaySize)
-{
+void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[],char PaySize){
+    
     size = 0;                                                           //Set size to 0.
     x = 0;                                                              //Set counter to 0.
     memset(Opacket, 0, sizeof(Opacket));                                //Clear output buffer.
@@ -264,8 +241,8 @@
 Create the checksum for outgoing packets.
 
 *****************************************************************************************/
-void checksum()
-{
+void checksum(){
+    
     y = 0;                                                              //Set Alternatie counter to 0
     sum = 0;
     while (y < (Opacket[2])){                                           //Create the checksum.
@@ -278,26 +255,23 @@
 Send the packet out serially to the Xbee module.
 
 *****************************************************************************************/
-void Psend(char API_packet[])
-{
+void Psend(char API_packet[]){
+    
     led1 = 1;                                                           //Set onboard LED1 on for diagnostics
-    pc.printf("sending packet");
     length = (API_packet[2] + 4);                                       //Calaculate packet length using packet length identifier and add the start byte, 2 byte length and checksum.
     x = 0;
     while (x < length){                                                 //Send the command
         xbee.putc(API_packet[x]);
-        pc.printf("%x," ,API_packet[x]);
         x ++;
     }
     led1 = 0;                                                           //Set diagnostics LED1 to off
-    pc.printf("\n");
 }
 /********************************* Device Announce **************************************
 Send a Device announce packet when joining or re joining and network.
 For more informaion see ZigBee spec page 109 
 *****************************************************************************************/
-void DevAnnc()
-{
+void DevAnnc(){
+    
     char AnncFrame[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE};
     char AnncPayload[12];                                               //Create char array for payload.
     char DevClu[2] = {0x00, 0x13};                                      //Create char array for Cluster ID.
@@ -321,8 +295,8 @@
 Send a response to a Simple Description Request.
 For more informaion see ZigBee spec page 159. 
 *****************************************************************************************/
-void SimpleDesc()
-{
+void SimpleDesc(){
+    
     EP = pay[3];                                                        //Get endpoint from payload.
     char DescClu[2] = {0x80,0x04};                                      //Create char array for cluster ID.
     char DescPro[2] = {0x00,0x00};                                      //Create char array for profile ID.
@@ -363,8 +337,8 @@
 Send a response to and Active EP Request.
 For more informaion see ZigBee spec page 161. 
 *****************************************************************************************/
-void ActiveEPReq()
-{
+void ActiveEPReq(){
+    
     char EPReqClu[2] = {0x80, 0x05};                                    //Create char array for Cluster.
     char EPReqPro[2] = {0x00, 0x00};                                    //Create char array for Profile.
     memset(Buffer, 0, sizeof(Buffer));                                  //Clear array.                                                                            
@@ -380,8 +354,8 @@
 Process a Basic Cluster request.
 For more informaion see ZigBee cluster library page 78 
 *****************************************************************************************/
-void ClusterBasic()
-{
+void ClusterBasic(){
+    
     cmdID = 0;                                                          //Set command ID to 0.
     seqNum = 0;                                                         //Set seqence number to 0.
     memset(Buffer, 0, sizeof(Buffer));                                  //Clear array.
@@ -454,8 +428,7 @@
 Process and respond to a Switch cluster command.
 For more informaion see ZigBee Cluster library page 125. 
 *****************************************************************************************/
-void OnOff()
-{
+void OnOff(){
     cmdID = 0;                                                          //Set command ID to 0.
     seqNum = 0;                                                         //Set seqence number to 0.
     frmType = 0;                                                        //Set frame type to 0.
@@ -470,41 +443,50 @@
     atID[1] = pay[4];                                                   //Get attribute ID LSB.
     
     if ((frmType == 0x00) && (cmdID == 0x00) && (atID[1] == 0x00)){     //If Command to check status is sent.
-        Buffer[0] = 0x18;                                               //Frame control direction is server to client.
-        Buffer[1] = seqNum;                                             //Reply with seqence number from request.
-        Buffer[2] = 0x01;                                               //Command indetifier = 1, Read attribute response.
-        Buffer[3] = 0x00;                                               //Attribute Identfier (2 bytes) field being reported.
-        Buffer[4] = 0x00;
-        Buffer[5] = 0x00;                                               //Status 00 = success.
-        Buffer[6] = 0x10;                                               //Attribute data type 10 = boolean.
-        Buffer[7] = LED;                                                //Read LED state.
-        PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08);               //Send to packet builder.
+        Update(LED);
     }
     
     if ((frmType == 0x01) && (cmdID == 0x00) && (atID[1] == 0x00)){     //command to turn off LED.
         LED = 0;                                                        //Turn LED off.
-        DefRes();                                            //Create response.
+        DefRes();
     }
     
     if ((frmType == 0x01) && (cmdID == 0x01) && (atID[1] == 0x00)){     //Command to turn on LED.
         LED = 1;                                                        //Turn LED on.
-        DefRes();                                             //Create response.
+        DefRes();                                                       //Create response.
     }
+    
     if ((frmType == 0x01) && (cmdID == 0x02) && (atID[1] == 0x00)){     //Command to toggle LED.
         LED = !LED;                                                     //Toggle LED
-        DefRes();                                             //Create response.
+        DefRes();
     }
 }
 /********************************* Default Response *************************************
 Create a default response. 
 
 *****************************************************************************************/
-void DefRes()
-{
+void DefRes(){
+    
     Buffer[0] = 0x18;                                                   //Frame control direction is server to client.
     Buffer[1] = seqNum;                                                 //Reply with seqence number from request.
     Buffer[2] = 0x0B;                                                   //Command identifier 0x08 = default response.
     Buffer[3] = cmdID;                                                  //reply with the command ID sne to you.
-    Buffer[4] = 0x00;                                                //Send sucess 00
-    PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x05);              //Send to packet builder.
+    Buffer[4] = 0x00;                                                   //Send sucess 00
+    PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x05);                   //Send to packet builder.
+}
+/************************************** Update ******************************************
+Update LEDs status.
+
+*****************************************************************************************/
+void Update(char val){   
+    
+    Buffer[0] = 0x18;                                                   //Frame control direction is server to client.
+    Buffer[1] = seqNum;                                                 //Reply with seqence number from request.
+    Buffer[2] = 0x01;                                                   //Command indetifier = 1, Read attribute response.
+    Buffer[3] = 0x00;                                                   //Attribute Identfier (2 bytes) field being reported.
+    Buffer[4] = 0x00;
+    Buffer[5] = 0x00;                                                   //Status 00 = success.
+    Buffer[6] = 0x10;                                                   //Attribute data type 10 = boolean.
+    Buffer[7] = val;                                                    //Read LED state.
+    PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08);                   //Send to packet builder.  
 }
\ No newline at end of file