A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE
Fork of xbee_lib by
telegesis.cpp@11:18ff088287ea, 2013-10-15 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Tue Oct 15 09:55:41 2013 +0000
- Revision:
- 11:18ff088287ea
- Parent:
- 9:c8e4339ccc29
- Child:
- 12:debf76f0c0bf
starting to work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 7:45511c3d2950 | 1 | #include "telegesis.h" |
tristanjph | 0:2656fb225c5d | 2 | |
tristanjph | 0:2656fb225c5d | 3 | |
gert_lauritsen | 11:18ff088287ea | 4 | zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) { |
tristanjph | 0:2656fb225c5d | 5 | _tx = tx; |
tristanjph | 0:2656fb225c5d | 6 | _rx = rx; |
gert_lauritsen | 11:18ff088287ea | 7 | |
gert_lauritsen | 11:18ff088287ea | 8 | _zbee.baud(19200); |
tristanjph | 0:2656fb225c5d | 9 | } |
tristanjph | 0:2656fb225c5d | 10 | |
gert_lauritsen | 7:45511c3d2950 | 11 | zigbee::~zigbee() |
tristanjph | 0:2656fb225c5d | 12 | { |
tristanjph | 0:2656fb225c5d | 13 | } |
tristanjph | 0:2656fb225c5d | 14 | |
gert_lauritsen | 11:18ff088287ea | 15 | int zigbee::wait4OK() { |
gert_lauritsen | 11:18ff088287ea | 16 | readPacket(); |
gert_lauritsen | 11:18ff088287ea | 17 | if (strstr(_responseFrameString,"OK")) return 1; else return 0; |
gert_lauritsen | 11:18ff088287ea | 18 | } |
tristanjph | 0:2656fb225c5d | 19 | |
gert_lauritsen | 11:18ff088287ea | 20 | int zigbee::GetSerial() |
gert_lauritsen | 8:4682155753ec | 21 | { |
gert_lauritsen | 11:18ff088287ea | 22 | /** comes with something like this |
gert_lauritsen | 8:4682155753ec | 23 | Telegesis ETRX357-LRS |
gert_lauritsen | 8:4682155753ec | 24 | R305C |
gert_lauritsen | 8:4682155753ec | 25 | 000D6F0000D5F06A |
gert_lauritsen | 8:4682155753ec | 26 | OK |
gert_lauritsen | 8:4682155753ec | 27 | */ |
gert_lauritsen | 11:18ff088287ea | 28 | _zbee.printf("ATI\r"); |
gert_lauritsen | 11:18ff088287ea | 29 | readPacket(); readPacket(); //commando |
gert_lauritsen | 11:18ff088287ea | 30 | readPacket(); |
gert_lauritsen | 11:18ff088287ea | 31 | sscanf(_responseFrameString,"Telegesis %s",HWType); |
gert_lauritsen | 11:18ff088287ea | 32 | readPacket(); readPacket(); |
gert_lauritsen | 11:18ff088287ea | 33 | sscanf (_responseFrameString,"%s",LocalID); |
tristanjph | 0:2656fb225c5d | 34 | return 1; |
tristanjph | 0:2656fb225c5d | 35 | } |
tristanjph | 0:2656fb225c5d | 36 | |
gert_lauritsen | 7:45511c3d2950 | 37 | int zigbee::SetKey(char* key) |
tristanjph | 0:2656fb225c5d | 38 | { |
tristanjph | 0:2656fb225c5d | 39 | return 1; |
tristanjph | 0:2656fb225c5d | 40 | } |
tristanjph | 0:2656fb225c5d | 41 | |
tristanjph | 0:2656fb225c5d | 42 | |
tristanjph | 0:2656fb225c5d | 43 | |
gert_lauritsen | 7:45511c3d2950 | 44 | void zigbee::RecieveData(char *data_buf, int numchar) |
tristanjph | 1:c3d9bdcb0b03 | 45 | { |
tristanjph | 1:c3d9bdcb0b03 | 46 | int count=0; |
tristanjph | 2:cb627ea9b817 | 47 | if(numchar == 0) { |
tristanjph | 2:cb627ea9b817 | 48 | numchar = sizeof(data_buf); |
tristanjph | 2:cb627ea9b817 | 49 | } |
gert_lauritsen | 11:18ff088287ea | 50 | |
tristanjph | 1:c3d9bdcb0b03 | 51 | while(numchar!=count) { |
gert_lauritsen | 11:18ff088287ea | 52 | if(_zbee.readable()) { |
gert_lauritsen | 11:18ff088287ea | 53 | *data_buf = _zbee.getc(); |
tristanjph | 3:682615a0717e | 54 | data_buf+=1; |
tristanjph | 3:682615a0717e | 55 | count++; |
tristanjph | 1:c3d9bdcb0b03 | 56 | } |
tristanjph | 1:c3d9bdcb0b03 | 57 | |
tristanjph | 1:c3d9bdcb0b03 | 58 | } |
tristanjph | 2:cb627ea9b817 | 59 | } |
tristanjph | 1:c3d9bdcb0b03 | 60 | |
gert_lauritsen | 7:45511c3d2950 | 61 | int zigbee::ATI() |
gert_lauritsen | 7:45511c3d2950 | 62 | { |
gert_lauritsen | 11:18ff088287ea | 63 | _zbee.printf("ATI\r"); |
gert_lauritsen | 11:18ff088287ea | 64 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 65 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 66 | } |
gert_lauritsen | 7:45511c3d2950 | 67 | |
gert_lauritsen | 7:45511c3d2950 | 68 | |
gert_lauritsen | 7:45511c3d2950 | 69 | int zigbee::PingOut() |
tristanjph | 2:cb627ea9b817 | 70 | { |
gert_lauritsen | 11:18ff088287ea | 71 | _zbee.printf("AT+ANNCE\r"); |
gert_lauritsen | 11:18ff088287ea | 72 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 73 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 74 | } |
gert_lauritsen | 7:45511c3d2950 | 75 | |
gert_lauritsen | 7:45511c3d2950 | 76 | |
gert_lauritsen | 7:45511c3d2950 | 77 | int zigbee::PanScan() |
gert_lauritsen | 7:45511c3d2950 | 78 | { |
gert_lauritsen | 11:18ff088287ea | 79 | |
gert_lauritsen | 11:18ff088287ea | 80 | |
gert_lauritsen | 11:18ff088287ea | 81 | _zbee.printf("AT+PANSCAN\r"); |
gert_lauritsen | 11:18ff088287ea | 82 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 83 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 84 | } |
gert_lauritsen | 7:45511c3d2950 | 85 | |
gert_lauritsen | 7:45511c3d2950 | 86 | |
gert_lauritsen | 7:45511c3d2950 | 87 | int zigbee::Establish_Network() |
gert_lauritsen | 7:45511c3d2950 | 88 | { |
gert_lauritsen | 11:18ff088287ea | 89 | _zbee.printf("AT+EN\r"); |
gert_lauritsen | 11:18ff088287ea | 90 | _zbee.scanf ("%*s"); |
tristanjph | 2:cb627ea9b817 | 91 | return 1; |
tristanjph | 2:cb627ea9b817 | 92 | } |
tristanjph | 3:682615a0717e | 93 | |
gert_lauritsen | 7:45511c3d2950 | 94 | |
gert_lauritsen | 7:45511c3d2950 | 95 | int zigbee::JoinNetwork() |
gert_lauritsen | 7:45511c3d2950 | 96 | { |
gert_lauritsen | 11:18ff088287ea | 97 | _zbee.printf("AT+JN\r"); |
gert_lauritsen | 11:18ff088287ea | 98 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 99 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 100 | } |
gert_lauritsen | 7:45511c3d2950 | 101 | |
gert_lauritsen | 7:45511c3d2950 | 102 | int zigbee::ScanNetwork() |
gert_lauritsen | 7:45511c3d2950 | 103 | { |
gert_lauritsen | 11:18ff088287ea | 104 | _zbee.printf("AT+SN\r"); |
gert_lauritsen | 11:18ff088287ea | 105 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 106 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 107 | } |
gert_lauritsen | 7:45511c3d2950 | 108 | |
gert_lauritsen | 7:45511c3d2950 | 109 | int zigbee::NetworkInfo() |
gert_lauritsen | 7:45511c3d2950 | 110 | { |
gert_lauritsen | 7:45511c3d2950 | 111 | //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522" |
gert_lauritsen | 11:18ff088287ea | 112 | _zbee.printf("AT+N\r"); |
gert_lauritsen | 11:18ff088287ea | 113 | _zbee.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID); |
gert_lauritsen | 7:45511c3d2950 | 114 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 115 | } |
gert_lauritsen | 7:45511c3d2950 | 116 | |
gert_lauritsen | 9:c8e4339ccc29 | 117 | int zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string |
gert_lauritsen | 7:45511c3d2950 | 118 | { |
gert_lauritsen | 11:18ff088287ea | 119 | _zbee.printf("AT+UCAST:%s=%s\r",adr,payload); |
gert_lauritsen | 11:18ff088287ea | 120 | _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata); |
gert_lauritsen | 7:45511c3d2950 | 121 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 122 | } |
gert_lauritsen | 7:45511c3d2950 | 123 | |
gert_lauritsen | 9:c8e4339ccc29 | 124 | int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format |
gert_lauritsen | 7:45511c3d2950 | 125 | { |
gert_lauritsen | 11:18ff088287ea | 126 | _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize); |
gert_lauritsen | 11:18ff088287ea | 127 | _zbee.scanf ("%*s"); |
gert_lauritsen | 7:45511c3d2950 | 128 | return 1; |
gert_lauritsen | 7:45511c3d2950 | 129 | } |
gert_lauritsen | 7:45511c3d2950 | 130 | |
gert_lauritsen | 7:45511c3d2950 | 131 | //-----------------------Håndtere indkommende data------------------------------ |
tristanjph | 3:682615a0717e | 132 | |
gert_lauritsen | 11:18ff088287ea | 133 | unsigned long zigbee::hextolong(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 134 | { |
gert_lauritsen | 7:45511c3d2950 | 135 | //return 32 bit |
gert_lauritsen | 7:45511c3d2950 | 136 | unsigned long result = 0; |
gert_lauritsen | 7:45511c3d2950 | 137 | while (*hex) { //så længe det ikke er null |
gert_lauritsen | 7:45511c3d2950 | 138 | if (*hex >= '0' && *hex <= '9') |
gert_lauritsen | 7:45511c3d2950 | 139 | result += (*hex - '0'); |
gert_lauritsen | 7:45511c3d2950 | 140 | else if (*hex >= 'A' && *hex <= 'F') |
gert_lauritsen | 7:45511c3d2950 | 141 | result += (*hex - 'A' +10); |
gert_lauritsen | 7:45511c3d2950 | 142 | else if (*hex >= 'a' && *hex <= 'f') |
gert_lauritsen | 7:45511c3d2950 | 143 | result += (*hex - 'a'+ 10); |
gert_lauritsen | 7:45511c3d2950 | 144 | |
gert_lauritsen | 7:45511c3d2950 | 145 | if (*++hex) //hvis den næstee ikke er null |
gert_lauritsen | 7:45511c3d2950 | 146 | result <<= 4; |
gert_lauritsen | 7:45511c3d2950 | 147 | } |
gert_lauritsen | 7:45511c3d2950 | 148 | return result; |
gert_lauritsen | 7:45511c3d2950 | 149 | } |
gert_lauritsen | 7:45511c3d2950 | 150 | |
gert_lauritsen | 7:45511c3d2950 | 151 | |
gert_lauritsen | 11:18ff088287ea | 152 | unsigned int zigbee::hextoint(const char *hex) |
gert_lauritsen | 7:45511c3d2950 | 153 | { |
gert_lauritsen | 7:45511c3d2950 | 154 | //return 16 bit |
gert_lauritsen | 7:45511c3d2950 | 155 | unsigned int 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 | 7:45511c3d2950 | 164 | if (*++hex) //hvis den næstee ikke er 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 | 11:18ff088287ea | 170 | void zigbee::readPacket() { |
gert_lauritsen | 11:18ff088287ea | 171 | //read and waits for the packet |
gert_lauritsen | 11:18ff088287ea | 172 | while (SeePacket()!='\n') { |
gert_lauritsen | 11:18ff088287ea | 173 | } |
gert_lauritsen | 11:18ff088287ea | 174 | } |
gert_lauritsen | 7:45511c3d2950 | 175 | |
gert_lauritsen | 11:18ff088287ea | 176 | |
gert_lauritsen | 11:18ff088287ea | 177 | uint8_t zigbee::SeePacket() { |
gert_lauritsen | 11:18ff088287ea | 178 | char *p; |
gert_lauritsen | 11:18ff088287ea | 179 | _pos=0; b=0; |
gert_lauritsen | 11:18ff088287ea | 180 | if (_zbee.readable()) { |
gert_lauritsen | 11:18ff088287ea | 181 | b = _zbee.getc(); |
gert_lauritsen | 11:18ff088287ea | 182 | if ((b!=CR) & (b!=LF)) _responseFrameString[_pos++]=b; |
gert_lauritsen | 11:18ff088287ea | 183 | if (b==CR) { |
gert_lauritsen | 11:18ff088287ea | 184 | _responseFrameString[_pos]=0; //Nul terminate |
gert_lauritsen | 11:18ff088287ea | 185 | if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something |
gert_lauritsen | 11:18ff088287ea | 186 | //Do something |
gert_lauritsen | 11:18ff088287ea | 187 | } else |
gert_lauritsen | 11:18ff088287ea | 188 | if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data |
gert_lauritsen | 11:18ff088287ea | 189 | Zdat=1; |
gert_lauritsen | 11:18ff088287ea | 190 | p=strstr(_responseFrameString,"="); p++; |
gert_lauritsen | 11:18ff088287ea | 191 | strcpy(Zdata,p); |
gert_lauritsen | 11:18ff088287ea | 192 | } |
gert_lauritsen | 11:18ff088287ea | 193 | if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0; //Local node has left the Pan |
gert_lauritsen | 11:18ff088287ea | 194 | |
gert_lauritsen | 11:18ff088287ea | 195 | if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID> |
gert_lauritsen | 11:18ff088287ea | 196 | //new node on the pan |
gert_lauritsen | 11:18ff088287ea | 197 | } |
gert_lauritsen | 11:18ff088287ea | 198 | |
gert_lauritsen | 11:18ff088287ea | 199 | if (strstr(_responseFrameString,"OK")) { |
gert_lauritsen | 11:18ff088287ea | 200 | //if (ScriptState) ConnectScript(); |
gert_lauritsen | 11:18ff088287ea | 201 | //Ok=1; Cmd=0; LineNo=0; |
gert_lauritsen | 11:18ff088287ea | 202 | } |
gert_lauritsen | 11:18ff088287ea | 203 | |
gert_lauritsen | 11:18ff088287ea | 204 | if (strstr(_responseFrameString,"ACK")) { // |
gert_lauritsen | 11:18ff088287ea | 205 | PacketAck=1; |
gert_lauritsen | 11:18ff088287ea | 206 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 11:18ff088287ea | 207 | SeqNumber=hextoint(p); |
gert_lauritsen | 11:18ff088287ea | 208 | } |
gert_lauritsen | 11:18ff088287ea | 209 | if (strstr(_responseFrameString,"NAK")) { |
gert_lauritsen | 11:18ff088287ea | 210 | PacketAck=0; |
gert_lauritsen | 11:18ff088287ea | 211 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 11:18ff088287ea | 212 | SeqNumber=hextoint(p); |
gert_lauritsen | 11:18ff088287ea | 213 | } |
gert_lauritsen | 11:18ff088287ea | 214 | if (strstr(_responseFrameString,"SEQ")) { // |
gert_lauritsen | 11:18ff088287ea | 215 | PacketAck=1; |
gert_lauritsen | 11:18ff088287ea | 216 | p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer |
gert_lauritsen | 11:18ff088287ea | 217 | SeqNumber=hextoint(p); |
gert_lauritsen | 11:18ff088287ea | 218 | } |
gert_lauritsen | 11:18ff088287ea | 219 | } |
gert_lauritsen | 11:18ff088287ea | 220 | } |
gert_lauritsen | 11:18ff088287ea | 221 | return b; |
gert_lauritsen | 11:18ff088287ea | 222 | } |