Mbed temperature sensor for Samsung SmartThings.

Dependencies:   C12832 LM75B mbed

Committer:
Cameron
Date:
Fri Sep 16 10:38:54 2016 +0000
Revision:
0:9d5aac2f5d0a
V1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cameron 0:9d5aac2f5d0a 1 #include "mbed.h"
Cameron 0:9d5aac2f5d0a 2 #include "C12832.h"
Cameron 0:9d5aac2f5d0a 3 #include "LM75B.h"
Cameron 0:9d5aac2f5d0a 4
Cameron 0:9d5aac2f5d0a 5 #define AppV 0x01
Cameron 0:9d5aac2f5d0a 6 #define HarV 0x01
Cameron 0:9d5aac2f5d0a 7 #define MaxTemp 125
Cameron 0:9d5aac2f5d0a 8 #define MinTemp -55
Cameron 0:9d5aac2f5d0a 9
Cameron 0:9d5aac2f5d0a 10 C12832 lcd(p5, p7, p6, p8, p11); //configure LCD
Cameron 0:9d5aac2f5d0a 11 LM75B tmp (p28,p27); //Temperature Sensor
Cameron 0:9d5aac2f5d0a 12 RawSerial xbee(p9,p10); //Set Serial to XBee
Cameron 0:9d5aac2f5d0a 13 Serial pc(USBTX, USBRX); //Set Serial to PC.
Cameron 0:9d5aac2f5d0a 14 DigitalOut led2 (LED2); //configure onboard LEDs
Cameron 0:9d5aac2f5d0a 15 DigitalOut led1 (LED1);
Cameron 0:9d5aac2f5d0a 16 DigitalOut LED (LED4);
Cameron 0:9d5aac2f5d0a 17 volatile char x,y,z,len,sum; //some useful internal variables
Cameron 0:9d5aac2f5d0a 18 volatile int length, size, temp, check;
Cameron 0:9d5aac2f5d0a 19 char packet[60];
Cameron 0:9d5aac2f5d0a 20 char PacketAddr[10];
Cameron 0:9d5aac2f5d0a 21 char Opacket[60];
Cameron 0:9d5aac2f5d0a 22 char AI[8] = {0x7E,0x00,0x04,0x08,0x21,0x41,0x49,0x4C}; //create AT command to querey network joined?
Cameron 0:9d5aac2f5d0a 23 char MY[8] = {0x7E,0x00,0x04,0x08,0x21,0x4D,0x59,0x30}; //create AT command to get my network ID.
Cameron 0:9d5aac2f5d0a 24 char SL[8] = {0x7E,0x00,0x04,0x08,0x21,0x53,0x4C,0x37}; //create AT command to get 64bit LSB.
Cameron 0:9d5aac2f5d0a 25 char SH[8] = {0x7E,0x00,0x04,0x08,0x21,0x53,0x48,0x3B}; //create AT command to get 64bit MSB.
Cameron 0:9d5aac2f5d0a 26 char CB[8] = {0x7E,0x00,0x04,0x08,0x21,0x43,0x42,0x51}; //Create AT command to leave network.
Cameron 0:9d5aac2f5d0a 27 char LNw[2];
Cameron 0:9d5aac2f5d0a 28 char Addr[8];
Cameron 0:9d5aac2f5d0a 29 char pay[101];
Cameron 0:9d5aac2f5d0a 30 char Buffer[101];
Cameron 0:9d5aac2f5d0a 31 char EP, DEP, SEP, seqNum, cmdID, frmType;
Cameron 0:9d5aac2f5d0a 32 char atID[2];
Cameron 0:9d5aac2f5d0a 33 char Clu[2];
Cameron 0:9d5aac2f5d0a 34 char Pro[2];
Cameron 0:9d5aac2f5d0a 35 char Temperature[2];
Cameron 0:9d5aac2f5d0a 36 char Man[3] = {'A','R','M'}; //Product manufacturer
Cameron 0:9d5aac2f5d0a 37 char Dev[10] = {'N','X','P','L','P','C','1','7','6','8'}; //Device Name
Cameron 0:9d5aac2f5d0a 38
Cameron 0:9d5aac2f5d0a 39 void packet_interupt(); //Create subroutine to deal with incoming packet.
Cameron 0:9d5aac2f5d0a 40 void Psend(char API_packet[]); //Create subroutine to send API commands
Cameron 0:9d5aac2f5d0a 41 void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[], char PaySize); //function to create packets to send.
Cameron 0:9d5aac2f5d0a 42 void Builder(); //Get usefull infromation from packets.
Cameron 0:9d5aac2f5d0a 43 void checksum(); //Create the checksum for the packet.
Cameron 0:9d5aac2f5d0a 44 void DevAnnc(); //function to send the device announce command.
Cameron 0:9d5aac2f5d0a 45 void SimpleDesc(); //Function to create the Simple description packet
Cameron 0:9d5aac2f5d0a 46 void ActiveEPReq(); //Function to create the Active end point request.
Cameron 0:9d5aac2f5d0a 47 void ClusterBasic(); //Function to create the Cluster Basic.
Cameron 0:9d5aac2f5d0a 48 void Temp(); //Function to process on off commands.
Cameron 0:9d5aac2f5d0a 49
Cameron 0:9d5aac2f5d0a 50 int main(){
Cameron 0:9d5aac2f5d0a 51 lcd.cls(); //clear lcd screen.
Cameron 0:9d5aac2f5d0a 52 lcd.locate(0,1); //locate the cursor.
Cameron 0:9d5aac2f5d0a 53 lcd.printf(" Joining network..."); //Print to lcd screen
Cameron 0:9d5aac2f5d0a 54 memset(packet, 0, sizeof(packet)); //Clear array
Cameron 0:9d5aac2f5d0a 55
Cameron 0:9d5aac2f5d0a 56 xbee.attach(&packet_interupt, Serial::RxIrq); //attach interupt when recieving serial information.
Cameron 0:9d5aac2f5d0a 57
Cameron 0:9d5aac2f5d0a 58 while(packet[5] != 0){ //While Xbee has not joined a network
Cameron 0:9d5aac2f5d0a 59 wait(1); //Wait 0.1 second.
Cameron 0:9d5aac2f5d0a 60 Psend(AI); //Send network joined status again.
Cameron 0:9d5aac2f5d0a 61 while(check == 0)
Cameron 0:9d5aac2f5d0a 62 {} //Read data.
Cameron 0:9d5aac2f5d0a 63 }
Cameron 0:9d5aac2f5d0a 64 lcd.cls(); //Clear lcd screen.
Cameron 0:9d5aac2f5d0a 65 lcd.locate(0,1); //Locate the cursor.
Cameron 0:9d5aac2f5d0a 66 lcd.printf(" Connected"); //Print to lcd screen.
Cameron 0:9d5aac2f5d0a 67
Cameron 0:9d5aac2f5d0a 68 check = 0;
Cameron 0:9d5aac2f5d0a 69 Psend(MY); //Send AT command to get the 16bit network address.
Cameron 0:9d5aac2f5d0a 70 while(check == 0) //Wait for packet.
Cameron 0:9d5aac2f5d0a 71 {}
Cameron 0:9d5aac2f5d0a 72 LNw[0] = packet[5]; //Transfer 16 bit address payload.
Cameron 0:9d5aac2f5d0a 73 LNw[1] = packet[6]; //Transfer 16 bit address payload.
Cameron 0:9d5aac2f5d0a 74
Cameron 0:9d5aac2f5d0a 75 check = 0;
Cameron 0:9d5aac2f5d0a 76 Psend(SH); //Send AT command to get the 64bit address MSB.
Cameron 0:9d5aac2f5d0a 77 while(check == 0) //Wait for packet.
Cameron 0:9d5aac2f5d0a 78 {}
Cameron 0:9d5aac2f5d0a 79 Addr[0] = packet[5]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 80 Addr[1] = packet[6]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 81 Addr[2] = packet[7]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 82 Addr[3] = packet[8]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 83
Cameron 0:9d5aac2f5d0a 84 check = 0;
Cameron 0:9d5aac2f5d0a 85 Psend(SL); //Send AT command to get the 64bit address LSB.
Cameron 0:9d5aac2f5d0a 86 while (check == 0) //Wait for packet.
Cameron 0:9d5aac2f5d0a 87 {}
Cameron 0:9d5aac2f5d0a 88 Addr[4] = packet[5]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 89 Addr[5] = packet[6]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 90 Addr[6] = packet[7]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 91 Addr[7] = packet[8]; //Transfer 64 bit address payload.
Cameron 0:9d5aac2f5d0a 92 DevAnnc(); //Send Device Announce.
Cameron 0:9d5aac2f5d0a 93
Cameron 0:9d5aac2f5d0a 94 while(1) {
Cameron 0:9d5aac2f5d0a 95 lcd.locate(0,21); //Locate LCD cursor.
Cameron 0:9d5aac2f5d0a 96 lcd.printf("Packets recieved = %d", z); //Print to LCD display.
Cameron 0:9d5aac2f5d0a 97 check = 0;
Cameron 0:9d5aac2f5d0a 98 while(check == 0) //Wait for packet.
Cameron 0:9d5aac2f5d0a 99 {}
Cameron 0:9d5aac2f5d0a 100 if (packet[16] == 0x00){ //If packet profile = 16.
Cameron 0:9d5aac2f5d0a 101 if (packet[14] == 0x04){ //If packet cluster = 14.
Cameron 0:9d5aac2f5d0a 102 lcd.locate(0,11); //Locate LCD cursor.
Cameron 0:9d5aac2f5d0a 103 lcd.printf(" simple dec");
Cameron 0:9d5aac2f5d0a 104 SimpleDesc(); //Process Simple Desciption request.
Cameron 0:9d5aac2f5d0a 105 }
Cameron 0:9d5aac2f5d0a 106 else if (packet[14] ==0x05){ //If packet cluster = 15.
Cameron 0:9d5aac2f5d0a 107 lcd.locate(0,11); //Locate LCD display
Cameron 0:9d5aac2f5d0a 108 lcd.printf(" Active EP req");
Cameron 0:9d5aac2f5d0a 109 ActiveEPReq(); //Process active EP Request.
Cameron 0:9d5aac2f5d0a 110 }
Cameron 0:9d5aac2f5d0a 111 else{
Cameron 0:9d5aac2f5d0a 112 lcd.locate(0,11); //Locate LCD cursor.
Cameron 0:9d5aac2f5d0a 113 lcd.printf(" Unknown Packet"); //Print to lcd screen.
Cameron 0:9d5aac2f5d0a 114 }
Cameron 0:9d5aac2f5d0a 115 }
Cameron 0:9d5aac2f5d0a 116 else{ //Else if packet profile is not = 00.
Cameron 0:9d5aac2f5d0a 117 if (packet[14] == 0x00){ //If packet Cluster = 00.
Cameron 0:9d5aac2f5d0a 118 lcd.locate(0,11); //Locate LCD cursor.
Cameron 0:9d5aac2f5d0a 119 lcd.printf(" Cluster Basic");
Cameron 0:9d5aac2f5d0a 120 ClusterBasic(); //Process Cluster Basic request.
Cameron 0:9d5aac2f5d0a 121 }
Cameron 0:9d5aac2f5d0a 122 else if (packet[14] == 0x02){ //Else if packet profile is not = 06.
Cameron 0:9d5aac2f5d0a 123 lcd.locate(0,11); //Locate LCD cursor.
Cameron 0:9d5aac2f5d0a 124 lcd.printf(" Temperature ");
Cameron 0:9d5aac2f5d0a 125 Temp(); //Process On/Off.
Cameron 0:9d5aac2f5d0a 126 }
Cameron 0:9d5aac2f5d0a 127 else{
Cameron 0:9d5aac2f5d0a 128 lcd.locate(0,11); //Locate the cursor.
Cameron 0:9d5aac2f5d0a 129 lcd.printf(" Unknown Packet"); //Print to lcd screen.
Cameron 0:9d5aac2f5d0a 130 }
Cameron 0:9d5aac2f5d0a 131 }
Cameron 0:9d5aac2f5d0a 132 } //end of while(1)
Cameron 0:9d5aac2f5d0a 133 }
Cameron 0:9d5aac2f5d0a 134
Cameron 0:9d5aac2f5d0a 135 /********************************* Packet interupt ***************************************
Cameron 0:9d5aac2f5d0a 136 When a new Xbee packet is revieved it is sent to this function to be processed.
Cameron 0:9d5aac2f5d0a 137
Cameron 0:9d5aac2f5d0a 138 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 139 void packet_interupt(){ //packet interupt begins when incoming data sensed on the rx line.
Cameron 0:9d5aac2f5d0a 140
Cameron 0:9d5aac2f5d0a 141 if (xbee.readable()) //If data is available
Cameron 0:9d5aac2f5d0a 142 x=xbee.getc(); //Get data
Cameron 0:9d5aac2f5d0a 143 if (x==0x7E){ //Test for start of Frame.
Cameron 0:9d5aac2f5d0a 144 led2 = 1; //Set indicator LED2 to on.
Cameron 0:9d5aac2f5d0a 145 z ++; //Increment packet counter.
Cameron 0:9d5aac2f5d0a 146 memset(packet, 0, sizeof(packet)); //Clear packet buffer.
Cameron 0:9d5aac2f5d0a 147 while(xbee.readable() == 0); //If data is available.
Cameron 0:9d5aac2f5d0a 148 x = xbee.getc(); //Get data MSB length.
Cameron 0:9d5aac2f5d0a 149 while(xbee.readable() == 0); //If data is available.
Cameron 0:9d5aac2f5d0a 150 len = xbee.getc(); //Get data LSB length.
Cameron 0:9d5aac2f5d0a 151 x = 0; //Counter to 0.
Cameron 0:9d5aac2f5d0a 152 sum = 0; //Set checksum calculator to 0.
Cameron 0:9d5aac2f5d0a 153 while(x < (len + 1)){ //Fill packet variable with entire API packet.
Cameron 0:9d5aac2f5d0a 154 while (xbee.readable() == 0); //If data is available.
Cameron 0:9d5aac2f5d0a 155 packet[x] = xbee.getc(); //Get data and place it in the data array.
Cameron 0:9d5aac2f5d0a 156 sum = (sum + packet[x]); //Add value to checksum calculation.
Cameron 0:9d5aac2f5d0a 157 x++;
Cameron 0:9d5aac2f5d0a 158 }
Cameron 0:9d5aac2f5d0a 159 sum = (0xFF - ( sum & 0xFF)); //Finish calculating the checksum.
Cameron 0:9d5aac2f5d0a 160 if (packet[x] == sum ){ //If the checksum and the calculated checksum are equal.
Cameron 0:9d5aac2f5d0a 161 if (packet[0] == 0x91){ //If packet ID = 0x91
Cameron 0:9d5aac2f5d0a 162 Builder(); //Extract packet information
Cameron 0:9d5aac2f5d0a 163 check = 1;
Cameron 0:9d5aac2f5d0a 164 }
Cameron 0:9d5aac2f5d0a 165 else if(packet[0] == 0x88){ //Else if packet ID = 0x88.
Cameron 0:9d5aac2f5d0a 166 check = 1;
Cameron 0:9d5aac2f5d0a 167 }
Cameron 0:9d5aac2f5d0a 168 else
Cameron 0:9d5aac2f5d0a 169 memset(packet, 0, sizeof(packet)); //Clear array
Cameron 0:9d5aac2f5d0a 170 }
Cameron 0:9d5aac2f5d0a 171 else{
Cameron 0:9d5aac2f5d0a 172 memset(packet, 0, sizeof(packet)); //Clear array
Cameron 0:9d5aac2f5d0a 173 }
Cameron 0:9d5aac2f5d0a 174 led2 = 0; //Set indicator LED2 to off.
Cameron 0:9d5aac2f5d0a 175 }
Cameron 0:9d5aac2f5d0a 176 }
Cameron 0:9d5aac2f5d0a 177 /************************************* Builder ******************************************
Cameron 0:9d5aac2f5d0a 178 Extract all the important inforamtion from the packet and assoicate it to the relevant variable.
Cameron 0:9d5aac2f5d0a 179 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 180 void Builder()
Cameron 0:9d5aac2f5d0a 181 {
Cameron 0:9d5aac2f5d0a 182 x = 0; //Set the counter to 0.
Cameron 0:9d5aac2f5d0a 183 while (x < 10){ //send the command
Cameron 0:9d5aac2f5d0a 184 PacketAddr[x] = packet[(x+1)]; //Copy the packets 16 and 64bit address.
Cameron 0:9d5aac2f5d0a 185 x ++;
Cameron 0:9d5aac2f5d0a 186 }
Cameron 0:9d5aac2f5d0a 187 DEP = packet[11]; //Get the source endpoint.
Cameron 0:9d5aac2f5d0a 188 SEP = packet[12]; //Get the destination endpoint.
Cameron 0:9d5aac2f5d0a 189 Clu[0] = packet[13]; //Get Cluster ID MSB.
Cameron 0:9d5aac2f5d0a 190 Clu[1] = packet[14]; //Get Cluster ID LSB.
Cameron 0:9d5aac2f5d0a 191 Pro[0] = packet[15]; //Get profile ID MSB.
Cameron 0:9d5aac2f5d0a 192 Pro[1] = packet[16]; //Get profile ID LSB.
Cameron 0:9d5aac2f5d0a 193 x = 0; //Set counter to 0.
Cameron 0:9d5aac2f5d0a 194 while ((x+18) < len){
Cameron 0:9d5aac2f5d0a 195 pay[x] = packet[(x+18)]; //Copy packet payload.
Cameron 0:9d5aac2f5d0a 196 x++;
Cameron 0:9d5aac2f5d0a 197 }
Cameron 0:9d5aac2f5d0a 198 }
Cameron 0:9d5aac2f5d0a 199 /************************************ PBuild ******************************************
Cameron 0:9d5aac2f5d0a 200 Organise the information from other functions into the correct packet format.
Cameron 0:9d5aac2f5d0a 201
Cameron 0:9d5aac2f5d0a 202 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 203 void PBuild(char FrameAddr[], char sEP, char dEP, char Profile[], char Cluster[], char Payload[],char PaySize){
Cameron 0:9d5aac2f5d0a 204
Cameron 0:9d5aac2f5d0a 205 size = 0; //Set size to 0.
Cameron 0:9d5aac2f5d0a 206 x = 0; //Set counter to 0.
Cameron 0:9d5aac2f5d0a 207 memset(Opacket, 0, sizeof(Opacket)); //Clear output buffer.
Cameron 0:9d5aac2f5d0a 208
Cameron 0:9d5aac2f5d0a 209 size = 20 +(PaySize); //Find out the size of the out going packet.
Cameron 0:9d5aac2f5d0a 210
Cameron 0:9d5aac2f5d0a 211 Opacket[0] = 0x7E; //Packet start bit.
Cameron 0:9d5aac2f5d0a 212 Opacket[1] = 0x00; //Packet length MSB.
Cameron 0:9d5aac2f5d0a 213 Opacket[2] = size; //Packet length LSB.
Cameron 0:9d5aac2f5d0a 214 Opacket[3] = 0x11; //Packet Type
Cameron 0:9d5aac2f5d0a 215 Opacket[4] = 0x00; //No response needed.
Cameron 0:9d5aac2f5d0a 216 Opacket[5] = FrameAddr[0]; //64 bit address High
Cameron 0:9d5aac2f5d0a 217 Opacket[6] = FrameAddr[1];
Cameron 0:9d5aac2f5d0a 218 Opacket[7] = FrameAddr[2];
Cameron 0:9d5aac2f5d0a 219 Opacket[8] = FrameAddr[3];
Cameron 0:9d5aac2f5d0a 220 Opacket[9] = FrameAddr[4]; //64 bit address Low
Cameron 0:9d5aac2f5d0a 221 Opacket[10] = FrameAddr[5];
Cameron 0:9d5aac2f5d0a 222 Opacket[11] = FrameAddr[6];
Cameron 0:9d5aac2f5d0a 223 Opacket[12] = FrameAddr[7];
Cameron 0:9d5aac2f5d0a 224 Opacket[13] = FrameAddr[8]; //16 bit PAN address
Cameron 0:9d5aac2f5d0a 225 Opacket[14] = FrameAddr[9];
Cameron 0:9d5aac2f5d0a 226 Opacket[15] = sEP; //Source Endpoint
Cameron 0:9d5aac2f5d0a 227 Opacket[16] = dEP; //Destinaton Endpoint
Cameron 0:9d5aac2f5d0a 228 Opacket[17] = Cluster[0]; //Cluster ID
Cameron 0:9d5aac2f5d0a 229 Opacket[18] = Cluster[1];
Cameron 0:9d5aac2f5d0a 230 Opacket[19] = Profile[0]; //Profile ID
Cameron 0:9d5aac2f5d0a 231 Opacket[20] = Profile[1];
Cameron 0:9d5aac2f5d0a 232 Opacket[21] = 0x00; //Broadcast radius
Cameron 0:9d5aac2f5d0a 233 Opacket[22] = 0x00; //Trasmit option bitfield
Cameron 0:9d5aac2f5d0a 234 while (x < PaySize){
Cameron 0:9d5aac2f5d0a 235 Opacket[(23 + x)] = Payload[x]; //Copy packet payload
Cameron 0:9d5aac2f5d0a 236 x ++;
Cameron 0:9d5aac2f5d0a 237 }
Cameron 0:9d5aac2f5d0a 238
Cameron 0:9d5aac2f5d0a 239 checksum(); //Create the check sum.
Cameron 0:9d5aac2f5d0a 240 Psend(Opacket); //Send the packet.
Cameron 0:9d5aac2f5d0a 241 }
Cameron 0:9d5aac2f5d0a 242 /************************************ Checksum ******************************************
Cameron 0:9d5aac2f5d0a 243 Create the checksum for outgoing packets.
Cameron 0:9d5aac2f5d0a 244
Cameron 0:9d5aac2f5d0a 245 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 246 void checksum(){
Cameron 0:9d5aac2f5d0a 247
Cameron 0:9d5aac2f5d0a 248 y = 0; //Set Alternatie counter to 0
Cameron 0:9d5aac2f5d0a 249 sum = 0;
Cameron 0:9d5aac2f5d0a 250 while (y < (Opacket[2])){ //Create the checksum.
Cameron 0:9d5aac2f5d0a 251 sum = (sum + Opacket[y + 3]);
Cameron 0:9d5aac2f5d0a 252 y++;
Cameron 0:9d5aac2f5d0a 253 }
Cameron 0:9d5aac2f5d0a 254 Opacket[23 + x] = (0xFF - ( sum & 0xFF));
Cameron 0:9d5aac2f5d0a 255 }
Cameron 0:9d5aac2f5d0a 256 /************************************* Psend ********************************************
Cameron 0:9d5aac2f5d0a 257 Send the packet out serially to the Xbee module.
Cameron 0:9d5aac2f5d0a 258
Cameron 0:9d5aac2f5d0a 259 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 260 void Psend(char API_packet[]){
Cameron 0:9d5aac2f5d0a 261
Cameron 0:9d5aac2f5d0a 262 led1 = 1; //Set onboard LED1 on for diagnostics
Cameron 0:9d5aac2f5d0a 263 length = (API_packet[2] + 4); //Calaculate packet length using packet length identifier and add the start byte, 2 byte length and checksum.
Cameron 0:9d5aac2f5d0a 264 x = 0;
Cameron 0:9d5aac2f5d0a 265 while (x < length){ //Send the command
Cameron 0:9d5aac2f5d0a 266 xbee.putc(API_packet[x]);
Cameron 0:9d5aac2f5d0a 267 x ++;
Cameron 0:9d5aac2f5d0a 268 }
Cameron 0:9d5aac2f5d0a 269 led1 = 0; //Set diagnostics LED1 to off
Cameron 0:9d5aac2f5d0a 270 }
Cameron 0:9d5aac2f5d0a 271 /********************************* Device Announce **************************************
Cameron 0:9d5aac2f5d0a 272 Send a Device announce packet when joining or re joining and network.
Cameron 0:9d5aac2f5d0a 273 For more informaion see ZigBee spec page 109
Cameron 0:9d5aac2f5d0a 274 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 275 void DevAnnc(){
Cameron 0:9d5aac2f5d0a 276
Cameron 0:9d5aac2f5d0a 277 char AnncFrame[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE};
Cameron 0:9d5aac2f5d0a 278 char AnncPayload[12]; //Create char array for payload.
Cameron 0:9d5aac2f5d0a 279 char DevClu[2] = {0x00, 0x13}; //Create char array for Cluster ID.
Cameron 0:9d5aac2f5d0a 280 char DevPro[2] = {0x00, 0x00}; //Create char array for Profile ID.
Cameron 0:9d5aac2f5d0a 281 AnncPayload[0] = 0x22; //Set seqence ID to 0x22.
Cameron 0:9d5aac2f5d0a 282 AnncPayload[1] = LNw[1]; //Copy 16bit PAN address little endian.
Cameron 0:9d5aac2f5d0a 283 AnncPayload[2] = LNw[0];
Cameron 0:9d5aac2f5d0a 284 AnncPayload[3] = Addr[7]; //Copy 64bit address little endian.
Cameron 0:9d5aac2f5d0a 285 AnncPayload[4] = Addr[6];
Cameron 0:9d5aac2f5d0a 286 AnncPayload[5] = Addr[5];
Cameron 0:9d5aac2f5d0a 287 AnncPayload[6] = Addr[4];
Cameron 0:9d5aac2f5d0a 288 AnncPayload[7] = Addr[3];
Cameron 0:9d5aac2f5d0a 289 AnncPayload[8] = Addr[2];
Cameron 0:9d5aac2f5d0a 290 AnncPayload[9] = Addr[1];
Cameron 0:9d5aac2f5d0a 291 AnncPayload[10] = Addr[0];
Cameron 0:9d5aac2f5d0a 292 AnncPayload[11] = 0x8C; //Set device parameters.
Cameron 0:9d5aac2f5d0a 293
Cameron 0:9d5aac2f5d0a 294 PBuild(AnncFrame,0x00,0x00,DevPro,DevClu, AnncPayload, 12); //Send packet to builder.
Cameron 0:9d5aac2f5d0a 295 }
Cameron 0:9d5aac2f5d0a 296 /******************************** Simple Description ************************************
Cameron 0:9d5aac2f5d0a 297 Send a response to a Simple Description Request.
Cameron 0:9d5aac2f5d0a 298 For more informaion see ZigBee spec page 159.
Cameron 0:9d5aac2f5d0a 299 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 300 void SimpleDesc(){
Cameron 0:9d5aac2f5d0a 301
Cameron 0:9d5aac2f5d0a 302 EP = pay[3]; //Get endpoint from payload.
Cameron 0:9d5aac2f5d0a 303 char DescClu[2] = {0x80,0x04}; //Create char array for cluster ID.
Cameron 0:9d5aac2f5d0a 304 char DescPro[2] = {0x00,0x00}; //Create char array for profile ID.
Cameron 0:9d5aac2f5d0a 305
Cameron 0:9d5aac2f5d0a 306 if( EP == 0x76){ //If Endpoint = 38.
Cameron 0:9d5aac2f5d0a 307 memset(Buffer, 0, sizeof(Buffer)); //Clear array
Cameron 0:9d5aac2f5d0a 308 Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number
Cameron 0:9d5aac2f5d0a 309 Buffer[1] = 0x00; //Status $00 = success Table 2.93 on page 159 of ZBSpec
Cameron 0:9d5aac2f5d0a 310 Buffer[2] = pay[1]; //Set Network address little endian order
Cameron 0:9d5aac2f5d0a 311 Buffer[3] = pay[2];
Cameron 0:9d5aac2f5d0a 312 Buffer[4] = 0x0E; //Length in bytes of the Simple Descriptor to Follow
Cameron 0:9d5aac2f5d0a 313 Buffer[5] = 0x76; //Endpoint of the simple descriptor Table 2.38 on page 88 of ZBSpec
Cameron 0:9d5aac2f5d0a 314 Buffer[6] = 0x04; //Application Profile ID 2 Bytes Little endian. $0104 = Home Automation Profile
Cameron 0:9d5aac2f5d0a 315 Buffer[7] = 0x01;
Cameron 0:9d5aac2f5d0a 316 Buffer[8] = 0x02; //Device type 2 Bytes Little endian, $0002 = On/Off Output see page 42 of ZigBee Home Automation Profile
Cameron 0:9d5aac2f5d0a 317 Buffer[9] = 0x00;
Cameron 0:9d5aac2f5d0a 318 Buffer[10] = 0x00; //App Dev Version 4bits + reserved 4bits
Cameron 0:9d5aac2f5d0a 319 Buffer[11] = 0x02; //Input cluster count in this case we only have $02 input clusters
Cameron 0:9d5aac2f5d0a 320 Buffer[12] = 0x00; //Input cluster list 2 bytes each little endian. $0000 = Basic Cluster
Cameron 0:9d5aac2f5d0a 321 Buffer[13] = 0x00;
Cameron 0:9d5aac2f5d0a 322 Buffer[14] = 0x02; //Output cluster 2 bytes each little endian. $0006 = On / Off Cluster
Cameron 0:9d5aac2f5d0a 323 Buffer[15] = 0x04;
Cameron 0:9d5aac2f5d0a 324 Buffer[16] = 0x00; //Output cluster list. No output cluster.
Cameron 0:9d5aac2f5d0a 325 PBuild(PacketAddr,0x00,0x00,DescPro,DescClu, Buffer, 0x11);
Cameron 0:9d5aac2f5d0a 326 }
Cameron 0:9d5aac2f5d0a 327
Cameron 0:9d5aac2f5d0a 328 else{
Cameron 0:9d5aac2f5d0a 329 memset(Buffer, 0, sizeof(Buffer)); //Clear array.
Cameron 0:9d5aac2f5d0a 330 Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number.
Cameron 0:9d5aac2f5d0a 331 Buffer[1] = 0x82; //Status $82 = Invalid_EP page 212 of ZigBee Specification.
Cameron 0:9d5aac2f5d0a 332 Buffer[2] = pay[1]; //Set Network address little endian order.
Cameron 0:9d5aac2f5d0a 333 Buffer[3] = pay[2];
Cameron 0:9d5aac2f5d0a 334 Buffer[4] = 0x00; //Length in bytes of simple descriptor to follow.
Cameron 0:9d5aac2f5d0a 335 PBuild(PacketAddr,0x00,0x00,DescPro,DescClu, Buffer, 0x05);
Cameron 0:9d5aac2f5d0a 336 }
Cameron 0:9d5aac2f5d0a 337 }
Cameron 0:9d5aac2f5d0a 338 /******************************** Active EP Request ************************************
Cameron 0:9d5aac2f5d0a 339 Send a response to and Active EP Request.
Cameron 0:9d5aac2f5d0a 340 For more informaion see ZigBee spec page 161.
Cameron 0:9d5aac2f5d0a 341 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 342 void ActiveEPReq(){
Cameron 0:9d5aac2f5d0a 343
Cameron 0:9d5aac2f5d0a 344 char EPReqClu[2] = {0x80, 0x05}; //Create char array for Cluster.
Cameron 0:9d5aac2f5d0a 345 char EPReqPro[2] = {0x00, 0x00}; //Create char array for Profile.
Cameron 0:9d5aac2f5d0a 346 memset(Buffer, 0, sizeof(Buffer)); //Clear array.
Cameron 0:9d5aac2f5d0a 347 Buffer[0] = pay[0]; //Set Transcation Seq number to match inbound packets seq number.
Cameron 0:9d5aac2f5d0a 348 Buffer[1] = 0x00; //Status $00 = success Table 2.93 on page 159 of ZBSpec.
Cameron 0:9d5aac2f5d0a 349 Buffer[2] = pay[1]; //Set Network address little endian order.
Cameron 0:9d5aac2f5d0a 350 Buffer[3] = pay[2];
Cameron 0:9d5aac2f5d0a 351 Buffer[4] = 0x01; //Active end point count in this case 1.
Cameron 0:9d5aac2f5d0a 352 Buffer[5] = 0x76; //Endpoint 38.
Cameron 0:9d5aac2f5d0a 353 PBuild(PacketAddr,0x00,0x00,EPReqPro,EPReqClu, Buffer, 0x06);
Cameron 0:9d5aac2f5d0a 354 }
Cameron 0:9d5aac2f5d0a 355 /********************************** Cluster Basic ***************************************
Cameron 0:9d5aac2f5d0a 356 Process a Basic Cluster request.
Cameron 0:9d5aac2f5d0a 357 For more informaion see ZigBee cluster library page 78
Cameron 0:9d5aac2f5d0a 358 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 359 void ClusterBasic(){
Cameron 0:9d5aac2f5d0a 360
Cameron 0:9d5aac2f5d0a 361 cmdID = 0; //Set command ID to 0.
Cameron 0:9d5aac2f5d0a 362 seqNum = 0; //Set seqence number to 0.
Cameron 0:9d5aac2f5d0a 363 memset(Buffer, 0, sizeof(Buffer)); //Clear array.
Cameron 0:9d5aac2f5d0a 364 memset(atID, 0, sizeof(atID)); //Clear array.
Cameron 0:9d5aac2f5d0a 365
Cameron 0:9d5aac2f5d0a 366 seqNum = pay[1]; //Copy seqence number from payload.
Cameron 0:9d5aac2f5d0a 367 cmdID = pay[2]; //Copy command ID from payload.
Cameron 0:9d5aac2f5d0a 368 atID[0] = pay[3]; //Copy attribute ID LSB from payload.
Cameron 0:9d5aac2f5d0a 369 atID[1] = pay[4]; //Copy attribute ID MSB from payload.
Cameron 0:9d5aac2f5d0a 370
Cameron 0:9d5aac2f5d0a 371 if ((cmdID == 0x00) && (atID[0] == 0x01) && (atID[1] == 0x00)){ //If Application version is requested.
Cameron 0:9d5aac2f5d0a 372 Buffer[0] = 0x18; //Frame control direction is server to client.
Cameron 0:9d5aac2f5d0a 373 Buffer[1] = seqNum; //Reply with seqence number from request.
Cameron 0:9d5aac2f5d0a 374 Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response.
Cameron 0:9d5aac2f5d0a 375 Buffer[3] = 0x01; //Attribute Identfier (2 bytes) field being reported.
Cameron 0:9d5aac2f5d0a 376 Buffer[4] = 0x00;
Cameron 0:9d5aac2f5d0a 377 Buffer[5] = 0x00; //status 00 success.
Cameron 0:9d5aac2f5d0a 378 Buffer[6] = 0x20; //Attribute data type 0x20 = unsigned 8 bit integer.
Cameron 0:9d5aac2f5d0a 379 Buffer[7] = AppV; //Application version.
Cameron 0:9d5aac2f5d0a 380 PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08); //Send data to packet builder.
Cameron 0:9d5aac2f5d0a 381 }
Cameron 0:9d5aac2f5d0a 382
Cameron 0:9d5aac2f5d0a 383 if ((cmdID == 0x00) && (atID[0] == 0x03) && (atID[1] == 0x00)){ //If Hardware version is requeted.
Cameron 0:9d5aac2f5d0a 384 Buffer[0] = 0x18; //Frame control direction is server to client.
Cameron 0:9d5aac2f5d0a 385 Buffer[1] = seqNum; //Reply with seqence number from request.
Cameron 0:9d5aac2f5d0a 386 Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response.
Cameron 0:9d5aac2f5d0a 387 Buffer[3] = 0x03; //Attribute Identfier (2 bytes) field being reported.
Cameron 0:9d5aac2f5d0a 388 Buffer[4] = 0x00;
Cameron 0:9d5aac2f5d0a 389 Buffer[5] = 0x00; //status 00 success.
Cameron 0:9d5aac2f5d0a 390 Buffer[6] = 0x20; //Attribute data type 0x20 = unsigned 8 bit integer.
Cameron 0:9d5aac2f5d0a 391 Buffer[7] = HarV; //Hardware version.
Cameron 0:9d5aac2f5d0a 392 PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x08); //Send data to packet builder.
Cameron 0:9d5aac2f5d0a 393 }
Cameron 0:9d5aac2f5d0a 394
Cameron 0:9d5aac2f5d0a 395 if ((cmdID == 0x00) && (atID[0] == 0x04) && (atID[1] == 0x00)){ //If device Manufacturer is requested
Cameron 0:9d5aac2f5d0a 396 Buffer[0] = 0x18; //Frame control direction is server to client.
Cameron 0:9d5aac2f5d0a 397 Buffer[1] = seqNum; //Reply with seqence number from request.
Cameron 0:9d5aac2f5d0a 398 Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response.
Cameron 0:9d5aac2f5d0a 399 Buffer[3] = 0x04; //Attribute Identfier (2 bytes) field being reported.
Cameron 0:9d5aac2f5d0a 400 Buffer[4] = 0x00;
Cameron 0:9d5aac2f5d0a 401 Buffer[5] = 0x00; //status 00 success.
Cameron 0:9d5aac2f5d0a 402 Buffer[6] = 0x42; //Attribute data type 0x42 = character string.
Cameron 0:9d5aac2f5d0a 403 Buffer[7] = sizeof(Man); //Size of string to follow.
Cameron 0:9d5aac2f5d0a 404 x = 0;
Cameron 0:9d5aac2f5d0a 405 while( x < (sizeof(Man))){ //Send sting byte by byte.
Cameron 0:9d5aac2f5d0a 406 Buffer[(x+8)] = Man[x];
Cameron 0:9d5aac2f5d0a 407 x++;
Cameron 0:9d5aac2f5d0a 408 }
Cameron 0:9d5aac2f5d0a 409 PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, (0x08 + Buffer[7])); //Send data to packet builder.
Cameron 0:9d5aac2f5d0a 410 }
Cameron 0:9d5aac2f5d0a 411
Cameron 0:9d5aac2f5d0a 412 if ((cmdID == 0x00) && (atID[0] == 0x05) && (atID[1] == 0x00)){ //If Device devoloper is requested.
Cameron 0:9d5aac2f5d0a 413 Buffer[0] = 0x18; //Frame control direction is server to client.
Cameron 0:9d5aac2f5d0a 414 Buffer[1] = seqNum; //Reply with seqence number from request.
Cameron 0:9d5aac2f5d0a 415 Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response.
Cameron 0:9d5aac2f5d0a 416 Buffer[3] = 0x05; //Attribute Identfier (2 bytes) field being reported.
Cameron 0:9d5aac2f5d0a 417 Buffer[4] = 0x00;
Cameron 0:9d5aac2f5d0a 418 Buffer[5] = 0x00; //status 00 success.
Cameron 0:9d5aac2f5d0a 419 Buffer[6] = 0x42; //Attribute data type 0x42 = character string.
Cameron 0:9d5aac2f5d0a 420 Buffer[7] = sizeof(Dev) ; //Size of string to follow.
Cameron 0:9d5aac2f5d0a 421 x = 0;
Cameron 0:9d5aac2f5d0a 422 while( x < (sizeof(Dev))){ //Send sting byte by byte.
Cameron 0:9d5aac2f5d0a 423 Buffer[(x+8)] = Dev[x];
Cameron 0:9d5aac2f5d0a 424 x++;
Cameron 0:9d5aac2f5d0a 425 }
Cameron 0:9d5aac2f5d0a 426 PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, (0x08 + Buffer[7])); //Send data to packet builder.
Cameron 0:9d5aac2f5d0a 427 }
Cameron 0:9d5aac2f5d0a 428 }
Cameron 0:9d5aac2f5d0a 429 /******************************** Switch Cluster ************************************
Cameron 0:9d5aac2f5d0a 430 Process and respond to a Switch cluster command.
Cameron 0:9d5aac2f5d0a 431 For more informaion see ZigBee Cluster library page 125.
Cameron 0:9d5aac2f5d0a 432 *****************************************************************************************/
Cameron 0:9d5aac2f5d0a 433
Cameron 0:9d5aac2f5d0a 434 void Temp()
Cameron 0:9d5aac2f5d0a 435 {
Cameron 0:9d5aac2f5d0a 436 cmdID = 0; //Set command ID to 0.
Cameron 0:9d5aac2f5d0a 437 seqNum = 0; //Set seqence number to 0.
Cameron 0:9d5aac2f5d0a 438 frmType = 0; //Set frame type to 0.
Cameron 0:9d5aac2f5d0a 439 memset(Buffer, 0, sizeof(Buffer)); //Clear array
Cameron 0:9d5aac2f5d0a 440 memset(atID, 0, sizeof(atID)); //Clear array
Cameron 0:9d5aac2f5d0a 441
Cameron 0:9d5aac2f5d0a 442 frmType = pay[0]; //Get frame type from payload.
Cameron 0:9d5aac2f5d0a 443 frmType = frmType & 0x01; //Bitwise & with 0x03 to make sure you are looking at the first 2 bits.
Cameron 0:9d5aac2f5d0a 444 seqNum = pay[1]; //Get Seqence Number for payload.
Cameron 0:9d5aac2f5d0a 445 cmdID = pay[2]; //Get command ID from payload.
Cameron 0:9d5aac2f5d0a 446 atID[0] = pay[3]; //Get attribute ID MSB.
Cameron 0:9d5aac2f5d0a 447 atID[1] = pay[4];
Cameron 0:9d5aac2f5d0a 448
Cameron 0:9d5aac2f5d0a 449 if ((frmType == 0x00) && (cmdID == 0x00) && (atID[0] == 0x00)){ //Read current temperature.
Cameron 0:9d5aac2f5d0a 450 temp = tmp.read();
Cameron 0:9d5aac2f5d0a 451 temp = temp - 8;
Cameron 0:9d5aac2f5d0a 452 Temperature[0] = (temp >> 8) & 0xFF;
Cameron 0:9d5aac2f5d0a 453 Temperature[1] = temp & 0xFF;
Cameron 0:9d5aac2f5d0a 454 }
Cameron 0:9d5aac2f5d0a 455
Cameron 0:9d5aac2f5d0a 456 if ((frmType == 0x00) && (cmdID == 0x00) && (atID[0] == 0x01)){ //Read minimum temperature.
Cameron 0:9d5aac2f5d0a 457 Temperature[0] = (MinTemp >> 8) & 0xFF;
Cameron 0:9d5aac2f5d0a 458 Temperature[1] = MinTemp & 0xFF;
Cameron 0:9d5aac2f5d0a 459 }
Cameron 0:9d5aac2f5d0a 460
Cameron 0:9d5aac2f5d0a 461 if ((frmType == 0x00) && (cmdID == 0x00) && (atID[0] == 0x02)){ //Read maximum temperature.
Cameron 0:9d5aac2f5d0a 462 Temperature[0] = (MaxTemp >> 8) & 0xFF;
Cameron 0:9d5aac2f5d0a 463 Temperature[1] = MaxTemp & 0xFF;
Cameron 0:9d5aac2f5d0a 464 }
Cameron 0:9d5aac2f5d0a 465
Cameron 0:9d5aac2f5d0a 466 Buffer[0] = 0x18; //Frame control direction is server to client.
Cameron 0:9d5aac2f5d0a 467 Buffer[1] = seqNum; //Reply with seqence number from request.
Cameron 0:9d5aac2f5d0a 468 Buffer[2] = 0x01; //Command indetifier = 1, Read attribute response.
Cameron 0:9d5aac2f5d0a 469 Buffer[3] = atID[0]; //Attribute Identfier (2 bytes) field being reported.
Cameron 0:9d5aac2f5d0a 470 Buffer[4] = atID[1];
Cameron 0:9d5aac2f5d0a 471 Buffer[5] = 0x00; //Status 00 = success.
Cameron 0:9d5aac2f5d0a 472 Buffer[6] = 0x29; //Attribute data type 29 = signed 16-bit interger.
Cameron 0:9d5aac2f5d0a 473 Buffer[7] = Temperature[1]; //MSB temperature.
Cameron 0:9d5aac2f5d0a 474 Buffer[8] = Temperature[0]; //LSB temperature.
Cameron 0:9d5aac2f5d0a 475 PBuild(PacketAddr,SEP,DEP,Pro,Clu, Buffer, 0x09); //Send to packet builder.
Cameron 0:9d5aac2f5d0a 476 }