Configure Series 2 Xbee (S2C) to work with the Zigbee home automation profile and connect to Samsung SmartThings
main.cpp@0:4c470d040789, 2016-09-15 (annotated)
- Committer:
- Cameron
- Date:
- Thu Sep 15 08:12:49 2016 +0000
- Revision:
- 0:4c470d040789
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cameron | 0:4c470d040789 | 1 | #include "mbed.h" |
Cameron | 0:4c470d040789 | 2 | #include "C12832.h" |
Cameron | 0:4c470d040789 | 3 | |
Cameron | 0:4c470d040789 | 4 | C12832 lcd (p5, p7, p6, p8, p11); //configure LCD |
Cameron | 0:4c470d040789 | 5 | Serial xbee (p9,p10); //Set Serial to XBee |
Cameron | 0:4c470d040789 | 6 | DigitalOut led1 (LED1); //configure onboard LEDs |
Cameron | 0:4c470d040789 | 7 | |
Cameron | 0:4c470d040789 | 8 | volatile char x; |
Cameron | 0:4c470d040789 | 9 | volatile int length; |
Cameron | 0:4c470d040789 | 10 | |
Cameron | 0:4c470d040789 | 11 | char api_frame_RE[8] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x52, 0x45, 0x5F}; // 0 = Restore Defaults |
Cameron | 0:4c470d040789 | 12 | char api_frame_EE[9] = {0x7E, 0x00, 0x05, 0x08, 0x01, 0x45, 0x45, 0x01, 0x6B}; // 1 = Encryption Enabled |
Cameron | 0:4c470d040789 | 13 | char api_frame_KY[24] = {0x7E, 0x00, 0x14, 0x08, 0x01, 0x4B, 0x59, 0x5A, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39, 0x9A}; // HA Encryption key 0x5A,69,67,42,65,65,41,6C,6C,69,61,6E,63,65,30,39 |
Cameron | 0:4c470d040789 | 14 | char api_frame_NJ[9] = {0x7E, 0x00, 0x05, 0x08, 0x01, 0x4E, 0x4A, 0x5A, 0x04}; // 5A = 90 seconds Node Join Time |
Cameron | 0:4c470d040789 | 15 | char api_frame_ZS[9] = {0x7E, 0x00, 0x05, 0x08, 0x01, 0x5A, 0x53, 0x02, 0x47}; // 2 = Zigbee PRO stack. |
Cameron | 0:4c470d040789 | 16 | char api_frame_EO[9] = {0x7E, 0x00, 0x05, 0x08, 0x01, 0x45, 0x4F, 0x01, 0x61}; // 1 = Encryption Options |
Cameron | 0:4c470d040789 | 17 | char api_frame_AO[9] = {0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x4F, 0x03, 0x63}; // API Output frame = 3 ZDO Pass through. |
Cameron | 0:4c470d040789 | 18 | char api_frame_WR[8] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D}; // Write. Write parameter values to non-volatile memory |
Cameron | 0:4c470d040789 | 19 | char api_frame_CB[8] = {0x7E,0x00,0x04,0x08,0x21,0x43,0x42,0x51}; // Create AT command to leave network. |
Cameron | 0:4c470d040789 | 20 | |
Cameron | 0:4c470d040789 | 21 | void APIsend(char API_packet[]); //Create subroutine to send API commands |
Cameron | 0:4c470d040789 | 22 | void ATsend(char AT_packet[], int Length); //Create subroutine to send API commands |
Cameron | 0:4c470d040789 | 23 | void Reset(); |
Cameron | 0:4c470d040789 | 24 | |
Cameron | 0:4c470d040789 | 25 | int main(){ |
Cameron | 0:4c470d040789 | 26 | lcd.cls(); //clear lcd screen. |
Cameron | 0:4c470d040789 | 27 | lcd.locate(0,1); //locate the cursor. |
Cameron | 0:4c470d040789 | 28 | lcd.printf(" Configure Xbee Module"); //Print to lcd screen |
Cameron | 0:4c470d040789 | 29 | |
Cameron | 0:4c470d040789 | 30 | Reset(); |
Cameron | 0:4c470d040789 | 31 | |
Cameron | 0:4c470d040789 | 32 | lcd.cls(); //clear lcd screen. |
Cameron | 0:4c470d040789 | 33 | lcd.locate(0,1); //locate the cursor. |
Cameron | 0:4c470d040789 | 34 | lcd.printf(" Configuration complete"); //Print to lcd screen |
Cameron | 0:4c470d040789 | 35 | } |
Cameron | 0:4c470d040789 | 36 | |
Cameron | 0:4c470d040789 | 37 | void APIsend(char API_packet[]) |
Cameron | 0:4c470d040789 | 38 | { |
Cameron | 0:4c470d040789 | 39 | led1 = 1; //Set onboard LED1 on for diagnostics |
Cameron | 0:4c470d040789 | 40 | length = (API_packet[2] + 4); //Calaculate packet length using packet length identifier and add the start byte, 2 byte length and checksum. |
Cameron | 0:4c470d040789 | 41 | x = 0; |
Cameron | 0:4c470d040789 | 42 | while (x < length){ //Send the command |
Cameron | 0:4c470d040789 | 43 | xbee.putc(API_packet[x]); |
Cameron | 0:4c470d040789 | 44 | x ++; |
Cameron | 0:4c470d040789 | 45 | } |
Cameron | 0:4c470d040789 | 46 | led1 = 0; //Set diagnostics LED1 to off |
Cameron | 0:4c470d040789 | 47 | } |
Cameron | 0:4c470d040789 | 48 | |
Cameron | 0:4c470d040789 | 49 | void ATsend(char AT_packet[], int Length) |
Cameron | 0:4c470d040789 | 50 | { |
Cameron | 0:4c470d040789 | 51 | led1 = 1; //Set onboard LED1 on for diagnostics |
Cameron | 0:4c470d040789 | 52 | x = 0; |
Cameron | 0:4c470d040789 | 53 | while (x < Length){ //Send the command |
Cameron | 0:4c470d040789 | 54 | xbee.putc(AT_packet[x]); |
Cameron | 0:4c470d040789 | 55 | x ++; |
Cameron | 0:4c470d040789 | 56 | } |
Cameron | 0:4c470d040789 | 57 | led1 = 0; //Set diagnostics LED1 to off |
Cameron | 0:4c470d040789 | 58 | } |
Cameron | 0:4c470d040789 | 59 | |
Cameron | 0:4c470d040789 | 60 | void Reset() |
Cameron | 0:4c470d040789 | 61 | { |
Cameron | 0:4c470d040789 | 62 | APIsend(api_frame_RE); //Restore defaults |
Cameron | 0:4c470d040789 | 63 | wait(1.5); //IMPORTANT- GUARD TIME must be > 1 second |
Cameron | 0:4c470d040789 | 64 | ATsend("+++", 3); |
Cameron | 0:4c470d040789 | 65 | wait(1.5); //IMPORTANT- GUARD TIME must be > 1 second |
Cameron | 0:4c470d040789 | 66 | ATsend("ATAP1\r", 6); //Set XBee to API mode. |
Cameron | 0:4c470d040789 | 67 | wait(0.25); |
Cameron | 0:4c470d040789 | 68 | ATsend("ATWR\r", 5); //Write command. |
Cameron | 0:4c470d040789 | 69 | wait(0.25); |
Cameron | 0:4c470d040789 | 70 | ATsend("ATCN\r", 5); //Exit command mode. |
Cameron | 0:4c470d040789 | 71 | wait(0.25); |
Cameron | 0:4c470d040789 | 72 | APIsend(api_frame_EE); //Encryption enabled. |
Cameron | 0:4c470d040789 | 73 | wait(0.1); |
Cameron | 0:4c470d040789 | 74 | APIsend(api_frame_KY); //HA Encryption key. |
Cameron | 0:4c470d040789 | 75 | wait(0.1); |
Cameron | 0:4c470d040789 | 76 | APIsend(api_frame_EO); //Encryption Options. |
Cameron | 0:4c470d040789 | 77 | wait(0.1); |
Cameron | 0:4c470d040789 | 78 | APIsend(api_frame_NJ); //Node join time. |
Cameron | 0:4c470d040789 | 79 | wait(0.1); |
Cameron | 0:4c470d040789 | 80 | APIsend(api_frame_ZS); //Zigbee Stack. |
Cameron | 0:4c470d040789 | 81 | wait(0.1); |
Cameron | 0:4c470d040789 | 82 | APIsend(api_frame_AO); //API output frame. |
Cameron | 0:4c470d040789 | 83 | wait(0.1); |
Cameron | 0:4c470d040789 | 84 | APIsend(api_frame_WR); //Write. |
Cameron | 0:4c470d040789 | 85 | wait(0.1); |
Cameron | 0:4c470d040789 | 86 | APIsend(api_frame_CB); //Leave network. |
Cameron | 0:4c470d040789 | 87 | } |