Mbed switch for Samsung SmartThings using Xbee

Dependencies:   C12832 mbed

Revision:
1:80fe03be2832
Parent:
0:1431a301c379
Child:
2:e78ec8625034
--- a/Main.cpp	Thu Sep 15 10:15:59 2016 +0000
+++ b/Main.cpp	Thu Sep 15 11:47:43 2016 +0000
@@ -42,52 +42,45 @@
 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(char success, char endpoint);                               //Function to create the response for the on off command.
+void DefRes();                               //Function to create the response for the on off command.
 
 int main(){
     lcd.cls();                                                          //clear lcd screen.
     lcd.locate(0,1);                                                    //locate the cursor.
     lcd.printf("         Joining network...");                          //Print to lcd screen
-
-    //xbee.attach(&packet_interupt, Serial::RxIrq);                                  //attach interupt when recieving serial information.
+    memset(packet, 0, sizeof(packet));                                  //Clear array
+    
+    xbee.attach(&packet_interupt, Serial::RxIrq);                       //attach interupt when recieving serial information.
     
     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){
-            while (!xbee.readable())
-            {}
-            if (xbee.readable())                                        //If data is available.
-            packet_interupt();                                          //Read data.
-        }
+        while(check == 0)
+        {}                                         //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.
-    check = 0;
-    while( check == 0){                                                 //Wait for packet.
-        while (!xbee.readable())                                        //Wait till something is available on the Serial port.
-        {}
-        if (xbee.readable())                                            // if data is available
-        packet_interupt();                                              //Read data.
+    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.
-    }
     
-    Psend(SH);                                                          //Send AT command to get the 64bit address MSB.
     check = 0;
-    while (check == 0){                                                 //Wait for packet.
-        while (!xbee.readable())                                        //Wait till something is available on the Serial port.
-        {}
-        if (xbee.readable())                                            //If data is available
-        packet_interupt();                                              //Read data.
+    Psend(SH);                                                          //Send AT command to get the 64bit address MSB.
+    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.
@@ -96,15 +89,11 @@
     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.
-    }
     
-    Psend(SL);                                                          //Send AT command to get the 64bit address LSB.
     check = 0;
-    while (check == 0){                                                 //Wait for packet.
-        while (!xbee.readable())                                        //Wait till something is available on the Serial port.
-        {}  
-        if (xbee.readable())                                            //If data is available
-        packet_interupt();                                              //Read data.
+    Psend(SL);                                                          //Send AT command to get the 64bit address LSB.
+    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.
@@ -113,7 +102,6 @@
         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.
     DevAnnc();                                                          //Send Device Announce.
     
@@ -121,12 +109,8 @@
 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.
-    while(!xbee.readable())                                             //Wait till something is available on the Serial port.
-    {}
-    if (xbee.readable())                                                //If data is available.
-        packet_interupt();                                              //Read data.
-}
+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.
@@ -185,6 +169,7 @@
         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]);
             sum = (sum + packet[x]);                                    //Add value to checksum calculation.
             x++;
             }    
@@ -195,8 +180,9 @@
                 Builder();                                              //Extract packet information
                 check = 1;
             }
-            else if(packet[0] == 0x88)                                  //Else if packet ID = 0x88.
+            else if(packet[0] == 0x88){                                  //Else if packet ID = 0x88.
                 check = 1;
+            }
             else
                 memset(packet, 0, sizeof(packet));                      //Clear array
         }
@@ -497,28 +483,28 @@
     
     if ((frmType == 0x01) && (cmdID == 0x00) && (atID[1] == 0x00)){     //command to turn off LED.
         LED = 0;                                                        //Turn LED off.
-        DefRes( 0x00, 0x76);                                            //Create response.
+        DefRes();                                            //Create response.
     }
     
     if ((frmType == 0x01) && (cmdID == 0x01) && (atID[1] == 0x00)){     //Command to turn on LED.
         LED = 1;                                                        //Turn LED on.
-        DefRes(0x00, 0x76);                                             //Create response.
+        DefRes();                                             //Create response.
     }
     if ((frmType == 0x01) && (cmdID == 0x02) && (atID[1] == 0x00)){     //Command to toggle LED.
         LED = !LED;                                                     //Toggle LED
-        DefRes(0x00, 0x76);                                             //Create response.
+        DefRes();                                             //Create response.
     }
 }
 /********************************* Default Response *************************************
 Create a default response. 
 
 *****************************************************************************************/
-void DefRes(char success, char endpoint)
+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] = success;                                                //Send sucess 00
-    PBuild(PacketAddr,endpoint,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.
 }
\ No newline at end of file