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

Fork of xbee_lib by Tristan Hughes

Committer:
gert_lauritsen
Date:
Sun Oct 13 10:13:35 2013 +0000
Revision:
9:c8e4339ccc29
Parent:
8:4682155753ec
Child:
11:18ff088287ea
It compiles...:)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 7:45511c3d2950 1 #include "telegesis.h"
tristanjph 0:2656fb225c5d 2
tristanjph 0:2656fb225c5d 3
gert_lauritsen 7:45511c3d2950 4 zigbee::zigbee(PinName tx, PinName rx, PinName reset)
tristanjph 0:2656fb225c5d 5 {
tristanjph 0:2656fb225c5d 6 _tx = tx;
tristanjph 0:2656fb225c5d 7 _rx = rx;
tristanjph 3:682615a0717e 8 _reset = reset;
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
tristanjph 0:2656fb225c5d 15
gert_lauritsen 7:45511c3d2950 16 int zigbee::GetSerial(int *serial_no)
gert_lauritsen 8:4682155753ec 17 {
gert_lauritsen 8:4682155753ec 18 /** comes with something like this
gert_lauritsen 8:4682155753ec 19 Telegesis ETRX357-LRS
gert_lauritsen 8:4682155753ec 20 R305C
gert_lauritsen 8:4682155753ec 21 000D6F0000D5F06A
gert_lauritsen 8:4682155753ec 22 OK
gert_lauritsen 8:4682155753ec 23 */
tristanjph 0:2656fb225c5d 24 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 25 wait_ms(50);
gert_lauritsen 8:4682155753ec 26 DATA.printf("ATI \r");
gert_lauritsen 9:c8e4339ccc29 27 DATA.scanf("Telegesis ",HWType);
gert_lauritsen 8:4682155753ec 28 DATA.scanf ("%*s");
gert_lauritsen 8:4682155753ec 29 DATA.scanf ("%X",serial_no);
tristanjph 0:2656fb225c5d 30 return 1;
tristanjph 0:2656fb225c5d 31 }
tristanjph 0:2656fb225c5d 32
gert_lauritsen 7:45511c3d2950 33 int zigbee::SetKey(char* key)
tristanjph 0:2656fb225c5d 34 {
tristanjph 0:2656fb225c5d 35 return 1;
tristanjph 0:2656fb225c5d 36 }
tristanjph 0:2656fb225c5d 37
tristanjph 0:2656fb225c5d 38
tristanjph 0:2656fb225c5d 39
gert_lauritsen 7:45511c3d2950 40 void zigbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 41 {
tristanjph 1:c3d9bdcb0b03 42 int count=0;
tristanjph 2:cb627ea9b817 43 if(numchar == 0) {
tristanjph 2:cb627ea9b817 44 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 45 }
tristanjph 1:c3d9bdcb0b03 46 Serial DATA(_tx,_rx);
tristanjph 1:c3d9bdcb0b03 47 while(numchar!=count) {
tristanjph 1:c3d9bdcb0b03 48 if(DATA.readable()) {
tristanjph 2:cb627ea9b817 49 *data_buf = DATA.getc();
tristanjph 3:682615a0717e 50 data_buf+=1;
tristanjph 3:682615a0717e 51 count++;
tristanjph 1:c3d9bdcb0b03 52 }
tristanjph 1:c3d9bdcb0b03 53
tristanjph 1:c3d9bdcb0b03 54 }
tristanjph 2:cb627ea9b817 55 }
tristanjph 1:c3d9bdcb0b03 56
gert_lauritsen 7:45511c3d2950 57 int zigbee::ATI()
gert_lauritsen 7:45511c3d2950 58 {
gert_lauritsen 7:45511c3d2950 59 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 60 wait_ms(5);
gert_lauritsen 7:45511c3d2950 61 DATA.printf("ATI\r");
gert_lauritsen 7:45511c3d2950 62 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 63 return 1;
gert_lauritsen 7:45511c3d2950 64 }
gert_lauritsen 7:45511c3d2950 65
gert_lauritsen 7:45511c3d2950 66
gert_lauritsen 7:45511c3d2950 67 int zigbee::PingOut()
tristanjph 2:cb627ea9b817 68 {
tristanjph 2:cb627ea9b817 69 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 70 wait_ms(5);
gert_lauritsen 7:45511c3d2950 71 DATA.printf("AT+ANNCE\r");
gert_lauritsen 7:45511c3d2950 72 DATA.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 7:45511c3d2950 79 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 80 wait_ms(5);
gert_lauritsen 7:45511c3d2950 81 DATA.printf("AT+PANSCAN\r");
gert_lauritsen 7:45511c3d2950 82 DATA.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 7:45511c3d2950 89 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 90 wait_ms(5);
gert_lauritsen 7:45511c3d2950 91 DATA.printf("AT+EN\r");
tristanjph 2:cb627ea9b817 92 DATA.scanf ("%*s");
tristanjph 2:cb627ea9b817 93 return 1;
tristanjph 2:cb627ea9b817 94 }
tristanjph 3:682615a0717e 95
gert_lauritsen 7:45511c3d2950 96
gert_lauritsen 7:45511c3d2950 97 int zigbee::JoinNetwork()
gert_lauritsen 7:45511c3d2950 98 {
gert_lauritsen 7:45511c3d2950 99 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 100 wait_ms(5);
gert_lauritsen 7:45511c3d2950 101 DATA.printf("AT+JN\r");
gert_lauritsen 7:45511c3d2950 102 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 103 return 1;
gert_lauritsen 7:45511c3d2950 104 }
gert_lauritsen 7:45511c3d2950 105
gert_lauritsen 7:45511c3d2950 106 int zigbee::ScanNetwork()
gert_lauritsen 7:45511c3d2950 107 {
gert_lauritsen 7:45511c3d2950 108 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 109 wait_ms(5);
gert_lauritsen 7:45511c3d2950 110 DATA.printf("AT+SN\r");
gert_lauritsen 7:45511c3d2950 111 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 112 return 1;
gert_lauritsen 7:45511c3d2950 113 }
gert_lauritsen 7:45511c3d2950 114
gert_lauritsen 7:45511c3d2950 115 int zigbee::NetworkInfo()
gert_lauritsen 7:45511c3d2950 116 {
gert_lauritsen 7:45511c3d2950 117 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
gert_lauritsen 7:45511c3d2950 118 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 119 wait_ms(5);
gert_lauritsen 7:45511c3d2950 120 DATA.printf("AT+N\r");
gert_lauritsen 7:45511c3d2950 121 DATA.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
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 7:45511c3d2950 127 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 128 wait_ms(5);
gert_lauritsen 7:45511c3d2950 129 DATA.printf("AT+UCAST:%s=%s\r",adr,payload);
gert_lauritsen 7:45511c3d2950 130
gert_lauritsen 7:45511c3d2950 131 DATA.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
gert_lauritsen 7:45511c3d2950 132 return 1;
gert_lauritsen 7:45511c3d2950 133 }
gert_lauritsen 7:45511c3d2950 134
gert_lauritsen 9:c8e4339ccc29 135 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
gert_lauritsen 7:45511c3d2950 136 {
gert_lauritsen 7:45511c3d2950 137 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 138 wait_ms(5);
gert_lauritsen 7:45511c3d2950 139 DATA.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
gert_lauritsen 7:45511c3d2950 140 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 141 return 1;
gert_lauritsen 7:45511c3d2950 142 }
gert_lauritsen 7:45511c3d2950 143
gert_lauritsen 7:45511c3d2950 144 void zigbee::Reset()
tristanjph 3:682615a0717e 145 {
tristanjph 6:6455a079bdb3 146 DigitalOut rst(_reset);
tristanjph 6:6455a079bdb3 147 rst = 0;
tristanjph 3:682615a0717e 148 wait_ms(10);
tristanjph 6:6455a079bdb3 149 rst = 1;
tristanjph 3:682615a0717e 150 wait_ms(1);
tristanjph 3:682615a0717e 151 }
tristanjph 3:682615a0717e 152
gert_lauritsen 7:45511c3d2950 153 //-----------------------Håndtere indkommende data------------------------------
tristanjph 3:682615a0717e 154
gert_lauritsen 7:45511c3d2950 155 unsigned long zigbee::hexToLong(const char *hex)
gert_lauritsen 7:45511c3d2950 156 {
gert_lauritsen 7:45511c3d2950 157 //return 32 bit
gert_lauritsen 7:45511c3d2950 158 unsigned long result = 0;
gert_lauritsen 7:45511c3d2950 159 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 160 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 161 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 162 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 163 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 164 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 165 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 166
gert_lauritsen 7:45511c3d2950 167 if (*++hex) //hvis den næstee ikke er null
gert_lauritsen 7:45511c3d2950 168 result <<= 4;
gert_lauritsen 7:45511c3d2950 169 }
gert_lauritsen 7:45511c3d2950 170 return result;
gert_lauritsen 7:45511c3d2950 171 }
gert_lauritsen 7:45511c3d2950 172
gert_lauritsen 7:45511c3d2950 173
gert_lauritsen 7:45511c3d2950 174 unsigned int zigbee::hexToInt(const char *hex)
gert_lauritsen 7:45511c3d2950 175 {
gert_lauritsen 7:45511c3d2950 176 //return 16 bit
gert_lauritsen 7:45511c3d2950 177 unsigned int result = 0;
gert_lauritsen 7:45511c3d2950 178 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 179 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 180 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 181 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 182 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 183 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 184 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 185
gert_lauritsen 7:45511c3d2950 186 if (*++hex) //hvis den næstee ikke er null
gert_lauritsen 7:45511c3d2950 187 result <<= 4;
gert_lauritsen 7:45511c3d2950 188 }
gert_lauritsen 7:45511c3d2950 189 return result;
gert_lauritsen 7:45511c3d2950 190 }
gert_lauritsen 7:45511c3d2950 191 //---------------------------------------------------------------
gert_lauritsen 7:45511c3d2950 192