A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE
Fork of xbee_lib by
telegesis.cpp@14:dcf2390f89e2, 2013-10-18 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Fri Oct 18 06:18:20 2013 +0000
- Revision:
- 14:dcf2390f89e2
- Parent:
- 12:debf76f0c0bf
- Child:
- 15:8d4990362a80
Ny med zigbee fungerende
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:45511c3d2950 | 46 | int zigbee::SetKey(char* key) |
tristanjph | 0:2656fb225c5d | 47 | { |
tristanjph | 0:2656fb225c5d | 48 | return 1; |
tristanjph | 0:2656fb225c5d | 49 | } |
tristanjph | 0:2656fb225c5d | 50 | |
tristanjph | 0:2656fb225c5d | 51 | |
tristanjph | 0:2656fb225c5d | 52 | |
gert_lauritsen | 7:45511c3d2950 | 53 | void zigbee::RecieveData(char *data_buf, int numchar) |
tristanjph | 1:c3d9bdcb0b03 | 54 | { |
tristanjph | 1:c3d9bdcb0b03 | 55 | int count=0; |
tristanjph | 2:cb627ea9b817 | 56 | if(numchar == 0) { |
tristanjph | 2:cb627ea9b817 | 57 | numchar = sizeof(data_buf); |
tristanjph | 2:cb627ea9b817 | 58 | } |
gert_lauritsen | 11:18ff088287ea | 59 | |
tristanjph | 1:c3d9bdcb0b03 | 60 | while(numchar!=count) { |
gert_lauritsen | 11:18ff088287ea | 61 | if(_zbee.readable()) { |
gert_lauritsen | 11:18ff088287ea | 62 | *data_buf = _zbee.getc(); |
tristanjph | 3:682615a0717e | 63 | data_buf+=1; |
tristanjph | 3:682615a0717e | 64 | count++; |
tristanjph | 1:c3d9bdcb0b03 | 65 | } |
tristanjph | 1:c3d9bdcb0b03 | 66 | |
tristanjph | 1:c3d9bdcb0b03 | 67 | } |
tristanjph | 2:cb627ea9b817 | 68 | } |
tristanjph | 1:c3d9bdcb0b03 | 69 | |
gert_lauritsen | 12:debf76f0c0bf | 70 | int zigbee::Reset() |
gert_lauritsen | 12:debf76f0c0bf | 71 | { |
gert_lauritsen | 12:debf76f0c0bf | 72 | _zbee.printf("ATZ\r"); |
gert_lauritsen | 12:debf76f0c0bf | 73 | wait4OK(); |
gert_lauritsen | 12:debf76f0c0bf | 74 | return 1; |
gert_lauritsen | 12:debf76f0c0bf | 75 | } |
gert_lauritsen | 12:debf76f0c0bf | 76 | |
gert_lauritsen | 7:45511c3d2950 | 77 | int zigbee::ATI() |
gert_lauritsen | 7:45511c3d2950 | 78 | { |
gert_lauritsen | 12:debf76f0c0bf | 79 | GetSerial(); |
gert_lauritsen | 7:45511c3d2950 | 80 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 81 | } |
gert_lauritsen | 7:45511c3d2950 | 82 | |
gert_lauritsen | 7:45511c3d2950 | 83 | |
gert_lauritsen | 7:45511c3d2950 | 84 | int zigbee::PingOut() |
gert_lauritsen | 12:debf76f0c0bf | 85 | { //just return a OK (sends it ID out out the pan) |
gert_lauritsen | 11:18ff088287ea | 86 | _zbee.printf("AT+ANNCE\r"); |
gert_lauritsen | 12:debf76f0c0bf | 87 | wait4OK(); |
gert_lauritsen | 7:45511c3d2950 | 88 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 89 | } |
gert_lauritsen | 7:45511c3d2950 | 90 | |
gert_lauritsen | 7:45511c3d2950 | 91 | |
gert_lauritsen | 7:45511c3d2950 | 92 | int zigbee::PanScan() |
gert_lauritsen | 12:debf76f0c0bf | 93 | {// |
gert_lauritsen | 11:18ff088287ea | 94 | _zbee.printf("AT+PANSCAN\r"); |
gert_lauritsen | 7:45511c3d2950 | 95 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 96 | } |
gert_lauritsen | 7:45511c3d2950 | 97 | |
gert_lauritsen | 7:45511c3d2950 | 98 | |
gert_lauritsen | 7:45511c3d2950 | 99 | int zigbee::Establish_Network() |
gert_lauritsen | 12:debf76f0c0bf | 100 | {// |
gert_lauritsen | 11:18ff088287ea | 101 | _zbee.printf("AT+EN\r"); |
tristanjph | 2:cb627ea9b817 | 102 | return 1; |
tristanjph | 2:cb627ea9b817 | 103 | } |
tristanjph | 3:682615a0717e | 104 | |
gert_lauritsen | 7:45511c3d2950 | 105 | |
gert_lauritsen | 7:45511c3d2950 | 106 | int zigbee::JoinNetwork() |
gert_lauritsen | 7:45511c3d2950 | 107 | { |
gert_lauritsen | 11:18ff088287ea | 108 | _zbee.printf("AT+JN\r"); |
gert_lauritsen | 7:45511c3d2950 | 109 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 110 | } |
gert_lauritsen | 7:45511c3d2950 | 111 | |
gert_lauritsen | 7:45511c3d2950 | 112 | int zigbee::ScanNetwork() |
gert_lauritsen | 7:45511c3d2950 | 113 | { |
gert_lauritsen | 11:18ff088287ea | 114 | _zbee.printf("AT+SN\r"); |
gert_lauritsen | 7:45511c3d2950 | 115 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 116 | } |
gert_lauritsen | 7:45511c3d2950 | 117 | |
gert_lauritsen | 7:45511c3d2950 | 118 | int zigbee::NetworkInfo() |
gert_lauritsen | 7:45511c3d2950 | 119 | { |
gert_lauritsen | 7:45511c3d2950 | 120 | //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522" |
gert_lauritsen | 11:18ff088287ea | 121 | _zbee.printf("AT+N\r"); |
gert_lauritsen | 7:45511c3d2950 | 122 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 123 | } |
gert_lauritsen | 7:45511c3d2950 | 124 | |
gert_lauritsen | 9:c8e4339ccc29 | 125 | int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string |
gert_lauritsen | 7:45511c3d2950 | 126 | { |
gert_lauritsen | 11:18ff088287ea | 127 | _zbee.printf("AT+UCAST:%s=%s\r",adr,payload); |
gert_lauritsen | 12:debf76f0c0bf | 128 | // _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata); |
gert_lauritsen | 7:45511c3d2950 | 129 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 130 | } |
gert_lauritsen | 7:45511c3d2950 | 131 | |
gert_lauritsen | 9:c8e4339ccc29 | 132 | int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format |
gert_lauritsen | 7:45511c3d2950 | 133 | { |
gert_lauritsen | 11:18ff088287ea | 134 | _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize); |
gert_lauritsen | 12:debf76f0c0bf | 135 | //_zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 136 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 137 | } |
gert_lauritsen | 7:45511c3d2950 | 138 | |
gert_lauritsen | 12:debf76f0c0bf | 139 | //-----------------------Handle incoming data------------------------------ |
tristanjph | 3:682615a0717e | 140 | |
gert_lauritsen | 11:18ff088287ea | 141 | unsigned long zigbee::hextolong(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 142 | { |
gert_lauritsen | 7:45511c3d2950 | 143 | //return 32 bit |
gert_lauritsen | 7:45511c3d2950 | 144 | unsigned long result = 0; |
gert_lauritsen | 7:45511c3d2950 | 145 | while (*hex) { //så længe det ikke er null |
gert_lauritsen | 7:45511c3d2950 | 146 | if (*hex >= '0' && *hex <= '9') |
gert_lauritsen | 7:45511c3d2950 | 147 | result += (*hex - '0'); |
gert_lauritsen | 7:45511c3d2950 | 148 | else if (*hex >= 'A' && *hex <= 'F') |
gert_lauritsen | 7:45511c3d2950 | 149 | result += (*hex - 'A' +10); |
gert_lauritsen | 7:45511c3d2950 | 150 | else if (*hex >= 'a' && *hex <= 'f') |
gert_lauritsen | 7:45511c3d2950 | 151 | result += (*hex - 'a'+ 10); |
gert_lauritsen | 7:45511c3d2950 | 152 | |
gert_lauritsen | 12:debf76f0c0bf | 153 | if (*++hex) //if the neext isn't a null |
gert_lauritsen | 7:45511c3d2950 | 154 | result <<= 4; |
gert_lauritsen | 7:45511c3d2950 | 155 | } |
gert_lauritsen | 7:45511c3d2950 | 156 | return result; |
gert_lauritsen | 7:45511c3d2950 | 157 | } |
gert_lauritsen | 7:45511c3d2950 | 158 | |
gert_lauritsen | 7:45511c3d2950 | 159 | |
gert_lauritsen | 11:18ff088287ea | 160 | unsigned int zigbee::hextoint(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 161 | { |
gert_lauritsen | 7:45511c3d2950 | 162 | //return 16 bit |
gert_lauritsen | 7:45511c3d2950 | 163 | unsigned int result = 0; |
gert_lauritsen | 7:45511c3d2950 | 164 | while (*hex) { //så længe det ikke er null |
gert_lauritsen | 7:45511c3d2950 | 165 | if (*hex >= '0' && *hex <= '9') |
gert_lauritsen | 7:45511c3d2950 | 166 | result += (*hex - '0'); |
gert_lauritsen | 7:45511c3d2950 | 167 | else if (*hex >= 'A' && *hex <= 'F') |
gert_lauritsen | 7:45511c3d2950 | 168 | result += (*hex - 'A' +10); |
gert_lauritsen | 7:45511c3d2950 | 169 | else if (*hex >= 'a' && *hex <= 'f') |
gert_lauritsen | 7:45511c3d2950 | 170 | result += (*hex - 'a'+ 10); |
gert_lauritsen | 7:45511c3d2950 | 171 | |
gert_lauritsen | 12:debf76f0c0bf | 172 | if (*++hex) //if the neext isn't a null |
gert_lauritsen | 7:45511c3d2950 | 173 | result <<= 4; |
gert_lauritsen | 7:45511c3d2950 | 174 | } |
gert_lauritsen | 7:45511c3d2950 | 175 | return result; |
gert_lauritsen | 7:45511c3d2950 | 176 | } |
gert_lauritsen | 7:45511c3d2950 | 177 | //--------------------------------------------------------------- |
gert_lauritsen | 11:18ff088287ea | 178 | void zigbee::readPacket() { |
gert_lauritsen | 11:18ff088287ea | 179 | //read and waits for the packet |
gert_lauritsen | 12:debf76f0c0bf | 180 | Timer t; |
gert_lauritsen | 12:debf76f0c0bf | 181 | t.reset(); |
gert_lauritsen | 12:debf76f0c0bf | 182 | t.start(); |
gert_lauritsen | 12:debf76f0c0bf | 183 | while ((GotFrame==0) & (t.read_ms() < 500)){} |
gert_lauritsen | 12:debf76f0c0bf | 184 | t.stop(); |
gert_lauritsen | 12:debf76f0c0bf | 185 | GotFrame=0; |
gert_lauritsen | 11:18ff088287ea | 186 | } |
gert_lauritsen | 7:45511c3d2950 | 187 | |
gert_lauritsen | 11:18ff088287ea | 188 | |
gert_lauritsen | 12:debf76f0c0bf | 189 | void zigbee::SeePacket() { |
gert_lauritsen | 11:18ff088287ea | 190 | char *p; |
gert_lauritsen | 12:debf76f0c0bf | 191 | b = _zbee.getc(); |
gert_lauritsen | 12:debf76f0c0bf | 192 | if ((b!=CR) & (b!=LF)) _responseFrameData[_pos]=b; |
gert_lauritsen | 12:debf76f0c0bf | 193 | _pos=(_pos+1) % MAX_FRAME_DATA_SIZE; |
gert_lauritsen | 12:debf76f0c0bf | 194 | if (b==CR) { |
gert_lauritsen | 12:debf76f0c0bf | 195 | memcpy(&_responseFrameString,&_responseFrameData,_pos); |
gert_lauritsen | 12:debf76f0c0bf | 196 | GotFrame=1; |
gert_lauritsen | 12:debf76f0c0bf | 197 | _responseFrameString[_pos]=0; //Nul terminate |
gert_lauritsen | 12:debf76f0c0bf | 198 | _pos=0; |
gert_lauritsen | 12:debf76f0c0bf | 199 | if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something |
gert_lauritsen | 12:debf76f0c0bf | 200 | //Do something |
gert_lauritsen | 11:18ff088287ea | 201 | } else |
gert_lauritsen | 12:debf76f0c0bf | 202 | if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data |
gert_lauritsen | 12:debf76f0c0bf | 203 | //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0) |
gert_lauritsen | 12:debf76f0c0bf | 204 | p=strstr(_responseFrameString,"="); |
gert_lauritsen | 12:debf76f0c0bf | 205 | if (p) { |
gert_lauritsen | 12:debf76f0c0bf | 206 | p++; |
gert_lauritsen | 12:debf76f0c0bf | 207 | strcpy(Zdata,p); |
gert_lauritsen | 12:debf76f0c0bf | 208 | Zdat=1; |
gert_lauritsen | 12:debf76f0c0bf | 209 | } |
gert_lauritsen | 11:18ff088287ea | 210 | } |
gert_lauritsen | 12:debf76f0c0bf | 211 | if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 11:18ff088287ea | 212 | |
gert_lauritsen | 12:debf76f0c0bf | 213 | if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> |
gert_lauritsen | 12:debf76f0c0bf | 214 | //new node on the pan |
gert_lauritsen | 12:debf76f0c0bf | 215 | } |
gert_lauritsen | 12:debf76f0c0bf | 216 | |
gert_lauritsen | 12:debf76f0c0bf | 217 | if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) { |
gert_lauritsen | 12:debf76f0c0bf | 218 | //if (ScriptState) ConnectScript(); |
gert_lauritsen | 12:debf76f0c0bf | 219 | //Ok=1; Cmd=0; LineNo=0; |
gert_lauritsen | 12:debf76f0c0bf | 220 | } |
gert_lauritsen | 12:debf76f0c0bf | 221 | if (strstr(_responseFrameString,"+N=")) { |
gert_lauritsen | 12:debf76f0c0bf | 222 | sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID); |
gert_lauritsen | 12:debf76f0c0bf | 223 | } |
gert_lauritsen | 12:debf76f0c0bf | 224 | if (strstr(_responseFrameString,"ACK")) { // |
gert_lauritsen | 12:debf76f0c0bf | 225 | PacketAck=1; |
gert_lauritsen | 12:debf76f0c0bf | 226 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 227 | SeqNumber=hextoint(p); |
gert_lauritsen | 12:debf76f0c0bf | 228 | } |
gert_lauritsen | 12:debf76f0c0bf | 229 | if (strstr(_responseFrameString,"NAK")) { |
gert_lauritsen | 12:debf76f0c0bf | 230 | PacketAck=0; |
gert_lauritsen | 12:debf76f0c0bf | 231 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 232 | SeqNumber=hextoint(p); |
gert_lauritsen | 12:debf76f0c0bf | 233 | } |
gert_lauritsen | 12:debf76f0c0bf | 234 | if (strstr(_responseFrameString,"SEQ")) { // |
gert_lauritsen | 12:debf76f0c0bf | 235 | PacketAck=1; |
gert_lauritsen | 12:debf76f0c0bf | 236 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 12:debf76f0c0bf | 237 | SeqNumber=hextoint(p); |
gert_lauritsen | 12:debf76f0c0bf | 238 | } |
gert_lauritsen | 12:debf76f0c0bf | 239 | } |
gert_lauritsen | 11:18ff088287ea | 240 | } |