A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE

Fork of xbee_lib by Tristan Hughes

Committer:
gert_lauritsen
Date:
Fri Oct 18 09:05:43 2013 +0000
Revision:
17:5a3f81b10a61
Parent:
15:8d4990362a80
Child:
18:10fcddf238c9
Add errorcode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 7:45511c3d2950 1 #include "telegesis.h"
gert_lauritsen 14:dcf2390f89e2 2 #define CR 0x0D
gert_lauritsen 14:dcf2390f89e2 3 #define LF 0x0A
tristanjph 0:2656fb225c5d 4
tristanjph 0:2656fb225c5d 5
gert_lauritsen 11:18ff088287ea 6 zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) {
tristanjph 0:2656fb225c5d 7 _tx = tx;
tristanjph 0:2656fb225c5d 8 _rx = rx;
gert_lauritsen 12:debf76f0c0bf 9 _pos=0;
gert_lauritsen 11:18ff088287ea 10 _zbee.baud(19200);
gert_lauritsen 12:debf76f0c0bf 11 _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq);
tristanjph 0:2656fb225c5d 12 }
tristanjph 0:2656fb225c5d 13
gert_lauritsen 7:45511c3d2950 14 zigbee::~zigbee()
tristanjph 0:2656fb225c5d 15 {
tristanjph 0:2656fb225c5d 16 }
tristanjph 0:2656fb225c5d 17
gert_lauritsen 12:debf76f0c0bf 18 bool zigbee::wait4OK() {
gert_lauritsen 12:debf76f0c0bf 19 Timer t;
gert_lauritsen 12:debf76f0c0bf 20 t.reset();
gert_lauritsen 12:debf76f0c0bf 21 t.start();
gert_lauritsen 11:18ff088287ea 22 readPacket();
gert_lauritsen 12:debf76f0c0bf 23 while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500)) readPacket();
gert_lauritsen 12:debf76f0c0bf 24 return (strstr(_responseFrameString,"OK")>0);
gert_lauritsen 11:18ff088287ea 25 }
tristanjph 0:2656fb225c5d 26
gert_lauritsen 11:18ff088287ea 27 int zigbee::GetSerial()
gert_lauritsen 8:4682155753ec 28 {
gert_lauritsen 12:debf76f0c0bf 29
gert_lauritsen 11:18ff088287ea 30 /** comes with something like this
gert_lauritsen 8:4682155753ec 31 Telegesis ETRX357-LRS
gert_lauritsen 8:4682155753ec 32 R305C
gert_lauritsen 8:4682155753ec 33 000D6F0000D5F06A
gert_lauritsen 8:4682155753ec 34 OK
gert_lauritsen 8:4682155753ec 35 */
gert_lauritsen 11:18ff088287ea 36 _zbee.printf("ATI\r");
gert_lauritsen 11:18ff088287ea 37 readPacket(); readPacket(); //commando
gert_lauritsen 11:18ff088287ea 38 readPacket();
gert_lauritsen 11:18ff088287ea 39 sscanf(_responseFrameString,"Telegesis %s",HWType);
gert_lauritsen 11:18ff088287ea 40 readPacket(); readPacket();
gert_lauritsen 12:debf76f0c0bf 41 LocalID=hextoint(_responseFrameString);
gert_lauritsen 12:debf76f0c0bf 42 wait_ms(5);
tristanjph 0:2656fb225c5d 43 return 1;
tristanjph 0:2656fb225c5d 44 }
tristanjph 0:2656fb225c5d 45
gert_lauritsen 15:8d4990362a80 46 int zigbee::SetKey(char* key){
gert_lauritsen 15:8d4990362a80 47 /**
gert_lauritsen 15:8d4990362a80 48 S09: the link key. Write the same 128-bit number (32 hexadecimal characters) into every device
gert_lauritsen 15:8d4990362a80 49
gert_lauritsen 15:8d4990362a80 50 S0A: security configuration. You must set bit 4 in the coordinator, and set bit 8 in all other devices.
gert_lauritsen 15:8d4990362a80 51 For more security set bit 2 also in the coordinator. The devices ignore the bits that are not relevant
gert_lauritsen 15:8d4990362a80 52 for them, so it is easiest to just set bits 8, 4, and 2 in all devices which gives S0A=0114.
gert_lauritsen 15:8d4990362a80 53 */
gert_lauritsen 15:8d4990362a80 54 char psw[50];
gert_lauritsen 15:8d4990362a80 55 sprintf(psw,"ATS09=%s;password\r",key);
gert_lauritsen 15:8d4990362a80 56 _zbee.printf(psw);
gert_lauritsen 15:8d4990362a80 57 wait4OK();
gert_lauritsen 15:8d4990362a80 58 _zbee.printf("ATS0A=0114;password\r");
tristanjph 0:2656fb225c5d 59 return 1;
tristanjph 0:2656fb225c5d 60 }
tristanjph 0:2656fb225c5d 61
tristanjph 0:2656fb225c5d 62
tristanjph 0:2656fb225c5d 63
gert_lauritsen 7:45511c3d2950 64 void zigbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 65 {
tristanjph 1:c3d9bdcb0b03 66 int count=0;
tristanjph 2:cb627ea9b817 67 if(numchar == 0) {
tristanjph 2:cb627ea9b817 68 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 69 }
gert_lauritsen 11:18ff088287ea 70
tristanjph 1:c3d9bdcb0b03 71 while(numchar!=count) {
gert_lauritsen 11:18ff088287ea 72 if(_zbee.readable()) {
gert_lauritsen 11:18ff088287ea 73 *data_buf = _zbee.getc();
tristanjph 3:682615a0717e 74 data_buf+=1;
tristanjph 3:682615a0717e 75 count++;
tristanjph 1:c3d9bdcb0b03 76 }
tristanjph 1:c3d9bdcb0b03 77
tristanjph 1:c3d9bdcb0b03 78 }
tristanjph 2:cb627ea9b817 79 }
tristanjph 1:c3d9bdcb0b03 80
gert_lauritsen 12:debf76f0c0bf 81 int zigbee::Reset()
gert_lauritsen 12:debf76f0c0bf 82 {
gert_lauritsen 12:debf76f0c0bf 83 _zbee.printf("ATZ\r");
gert_lauritsen 12:debf76f0c0bf 84 wait4OK();
gert_lauritsen 12:debf76f0c0bf 85 return 1;
gert_lauritsen 12:debf76f0c0bf 86 }
gert_lauritsen 12:debf76f0c0bf 87
gert_lauritsen 7:45511c3d2950 88 int zigbee::ATI()
gert_lauritsen 7:45511c3d2950 89 {
gert_lauritsen 12:debf76f0c0bf 90 GetSerial();
gert_lauritsen 7:45511c3d2950 91 return 1;
gert_lauritsen 7:45511c3d2950 92 }
gert_lauritsen 7:45511c3d2950 93
gert_lauritsen 7:45511c3d2950 94
gert_lauritsen 7:45511c3d2950 95 int zigbee::PingOut()
gert_lauritsen 12:debf76f0c0bf 96 { //just return a OK (sends it ID out out the pan)
gert_lauritsen 11:18ff088287ea 97 _zbee.printf("AT+ANNCE\r");
gert_lauritsen 12:debf76f0c0bf 98 wait4OK();
gert_lauritsen 7:45511c3d2950 99 return 1;
gert_lauritsen 7:45511c3d2950 100 }
gert_lauritsen 7:45511c3d2950 101
gert_lauritsen 7:45511c3d2950 102
gert_lauritsen 7:45511c3d2950 103 int zigbee::PanScan()
gert_lauritsen 12:debf76f0c0bf 104 {//
gert_lauritsen 11:18ff088287ea 105 _zbee.printf("AT+PANSCAN\r");
gert_lauritsen 7:45511c3d2950 106 return 1;
gert_lauritsen 7:45511c3d2950 107 }
gert_lauritsen 7:45511c3d2950 108
gert_lauritsen 7:45511c3d2950 109
gert_lauritsen 7:45511c3d2950 110 int zigbee::Establish_Network()
gert_lauritsen 12:debf76f0c0bf 111 {//
gert_lauritsen 11:18ff088287ea 112 _zbee.printf("AT+EN\r");
tristanjph 2:cb627ea9b817 113 return 1;
tristanjph 2:cb627ea9b817 114 }
tristanjph 3:682615a0717e 115
gert_lauritsen 7:45511c3d2950 116
gert_lauritsen 7:45511c3d2950 117 int zigbee::JoinNetwork()
gert_lauritsen 7:45511c3d2950 118 {
gert_lauritsen 11:18ff088287ea 119 _zbee.printf("AT+JN\r");
gert_lauritsen 7:45511c3d2950 120 return 1;
gert_lauritsen 7:45511c3d2950 121 }
gert_lauritsen 7:45511c3d2950 122
gert_lauritsen 7:45511c3d2950 123 int zigbee::ScanNetwork()
gert_lauritsen 7:45511c3d2950 124 {
gert_lauritsen 11:18ff088287ea 125 _zbee.printf("AT+SN\r");
gert_lauritsen 7:45511c3d2950 126 return 1;
gert_lauritsen 7:45511c3d2950 127 }
gert_lauritsen 7:45511c3d2950 128
gert_lauritsen 7:45511c3d2950 129 int zigbee::NetworkInfo()
gert_lauritsen 7:45511c3d2950 130 {
gert_lauritsen 7:45511c3d2950 131 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
gert_lauritsen 11:18ff088287ea 132 _zbee.printf("AT+N\r");
gert_lauritsen 7:45511c3d2950 133 return 1;
gert_lauritsen 7:45511c3d2950 134 }
gert_lauritsen 7:45511c3d2950 135
gert_lauritsen 9:c8e4339ccc29 136 int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string
gert_lauritsen 7:45511c3d2950 137 {
gert_lauritsen 11:18ff088287ea 138 _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
gert_lauritsen 12:debf76f0c0bf 139 // _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
gert_lauritsen 7:45511c3d2950 140 return 1;
gert_lauritsen 7:45511c3d2950 141 }
gert_lauritsen 7:45511c3d2950 142
gert_lauritsen 9:c8e4339ccc29 143 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
gert_lauritsen 7:45511c3d2950 144 {
gert_lauritsen 11:18ff088287ea 145 _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
gert_lauritsen 12:debf76f0c0bf 146 //_zbee.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 147 return 1;
gert_lauritsen 7:45511c3d2950 148 }
gert_lauritsen 7:45511c3d2950 149
gert_lauritsen 12:debf76f0c0bf 150 //-----------------------Handle incoming data------------------------------
tristanjph 3:682615a0717e 151
gert_lauritsen 11:18ff088287ea 152 unsigned long zigbee::hextolong(const char *hex)
gert_lauritsen 7:45511c3d2950 153 {
gert_lauritsen 7:45511c3d2950 154 //return 32 bit
gert_lauritsen 7:45511c3d2950 155 unsigned long result = 0;
gert_lauritsen 7:45511c3d2950 156 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 157 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 158 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 159 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 160 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 161 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 162 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 163
gert_lauritsen 12:debf76f0c0bf 164 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 165 result <<= 4;
gert_lauritsen 7:45511c3d2950 166 }
gert_lauritsen 7:45511c3d2950 167 return result;
gert_lauritsen 7:45511c3d2950 168 }
gert_lauritsen 7:45511c3d2950 169
gert_lauritsen 7:45511c3d2950 170
gert_lauritsen 11:18ff088287ea 171 unsigned int zigbee::hextoint(const char *hex)
gert_lauritsen 7:45511c3d2950 172 {
gert_lauritsen 7:45511c3d2950 173 //return 16 bit
gert_lauritsen 7:45511c3d2950 174 unsigned int result = 0;
gert_lauritsen 7:45511c3d2950 175 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 176 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 177 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 178 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 179 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 180 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 181 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 182
gert_lauritsen 12:debf76f0c0bf 183 if (*++hex) //if the neext isn't a null
gert_lauritsen 7:45511c3d2950 184 result <<= 4;
gert_lauritsen 7:45511c3d2950 185 }
gert_lauritsen 7:45511c3d2950 186 return result;
gert_lauritsen 7:45511c3d2950 187 }
gert_lauritsen 7:45511c3d2950 188 //---------------------------------------------------------------
gert_lauritsen 11:18ff088287ea 189 void zigbee::readPacket() {
gert_lauritsen 11:18ff088287ea 190 //read and waits for the packet
gert_lauritsen 12:debf76f0c0bf 191 Timer t;
gert_lauritsen 12:debf76f0c0bf 192 t.reset();
gert_lauritsen 12:debf76f0c0bf 193 t.start();
gert_lauritsen 12:debf76f0c0bf 194 while ((GotFrame==0) & (t.read_ms() < 500)){}
gert_lauritsen 12:debf76f0c0bf 195 t.stop();
gert_lauritsen 12:debf76f0c0bf 196 GotFrame=0;
gert_lauritsen 11:18ff088287ea 197 }
gert_lauritsen 7:45511c3d2950 198
gert_lauritsen 11:18ff088287ea 199
gert_lauritsen 12:debf76f0c0bf 200 void zigbee::SeePacket() {
gert_lauritsen 11:18ff088287ea 201 char *p;
gert_lauritsen 12:debf76f0c0bf 202 b = _zbee.getc();
gert_lauritsen 12:debf76f0c0bf 203 if ((b!=CR) & (b!=LF)) _responseFrameData[_pos]=b;
gert_lauritsen 12:debf76f0c0bf 204 _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
gert_lauritsen 12:debf76f0c0bf 205 if (b==CR) {
gert_lauritsen 12:debf76f0c0bf 206 memcpy(&_responseFrameString,&_responseFrameData,_pos);
gert_lauritsen 12:debf76f0c0bf 207 GotFrame=1;
gert_lauritsen 12:debf76f0c0bf 208 _responseFrameString[_pos]=0; //Nul terminate
gert_lauritsen 12:debf76f0c0bf 209 _pos=0;
gert_lauritsen 12:debf76f0c0bf 210 if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
gert_lauritsen 12:debf76f0c0bf 211 //Do something
gert_lauritsen 11:18ff088287ea 212 } else
gert_lauritsen 12:debf76f0c0bf 213 if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data
gert_lauritsen 12:debf76f0c0bf 214 //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0)
gert_lauritsen 12:debf76f0c0bf 215 p=strstr(_responseFrameString,"=");
gert_lauritsen 12:debf76f0c0bf 216 if (p) {
gert_lauritsen 12:debf76f0c0bf 217 p++;
gert_lauritsen 12:debf76f0c0bf 218 strcpy(Zdata,p);
gert_lauritsen 12:debf76f0c0bf 219 Zdat=1;
gert_lauritsen 12:debf76f0c0bf 220 }
gert_lauritsen 11:18ff088287ea 221 }
gert_lauritsen 12:debf76f0c0bf 222 if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan
gert_lauritsen 17:5a3f81b10a61 223 if (strstr(_responseFrameString,"LostPAN:")) PanOnline=0; //Local node has left the Pan
gert_lauritsen 17:5a3f81b10a61 224 if (strstr(_responseFrameString,"ERROR:")) {
gert_lauritsen 17:5a3f81b10a61 225 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 17:5a3f81b10a61 226 ErrorCode=hextoint(p);
gert_lauritsen 17:5a3f81b10a61 227 }
gert_lauritsen 11:18ff088287ea 228
gert_lauritsen 12:debf76f0c0bf 229 if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
gert_lauritsen 12:debf76f0c0bf 230 //new node on the pan
gert_lauritsen 12:debf76f0c0bf 231 }
gert_lauritsen 12:debf76f0c0bf 232
gert_lauritsen 12:debf76f0c0bf 233 if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) {
gert_lauritsen 12:debf76f0c0bf 234 //if (ScriptState) ConnectScript();
gert_lauritsen 12:debf76f0c0bf 235 //Ok=1; Cmd=0; LineNo=0;
gert_lauritsen 12:debf76f0c0bf 236 }
gert_lauritsen 12:debf76f0c0bf 237 if (strstr(_responseFrameString,"+N=")) {
gert_lauritsen 12:debf76f0c0bf 238 sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
gert_lauritsen 12:debf76f0c0bf 239 }
gert_lauritsen 12:debf76f0c0bf 240 if (strstr(_responseFrameString,"ACK")) { //
gert_lauritsen 12:debf76f0c0bf 241 PacketAck=1;
gert_lauritsen 12:debf76f0c0bf 242 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 243 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 244 }
gert_lauritsen 12:debf76f0c0bf 245 if (strstr(_responseFrameString,"NAK")) {
gert_lauritsen 12:debf76f0c0bf 246 PacketAck=0;
gert_lauritsen 12:debf76f0c0bf 247 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 248 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 249 }
gert_lauritsen 12:debf76f0c0bf 250 if (strstr(_responseFrameString,"SEQ")) { //
gert_lauritsen 12:debf76f0c0bf 251 PacketAck=1;
gert_lauritsen 12:debf76f0c0bf 252 p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
gert_lauritsen 12:debf76f0c0bf 253 SeqNumber=hextoint(p);
gert_lauritsen 12:debf76f0c0bf 254 }
gert_lauritsen 12:debf76f0c0bf 255 }
gert_lauritsen 11:18ff088287ea 256 }