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 09:46:49 2013 +0000
Revision:
7:45511c3d2950
Parent:
xbee.cpp@6:6455a079bdb3
Child:
8:4682155753ec
First test of zigbee

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)
tristanjph 0:2656fb225c5d 17 {
tristanjph 0:2656fb225c5d 18 int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
tristanjph 0:2656fb225c5d 19 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 20 wait_ms(50);
tristanjph 0:2656fb225c5d 21 DATA.printf("ATSL \r");
tristanjph 0:2656fb225c5d 22 DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
tristanjph 0:2656fb225c5d 23 wait_ms(500);
tristanjph 0:2656fb225c5d 24 DATA.printf("ATSH \r");
tristanjph 0:2656fb225c5d 25 DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
tristanjph 0:2656fb225c5d 26
tristanjph 0:2656fb225c5d 27 serial_no[0] = sh1;
tristanjph 0:2656fb225c5d 28 serial_no[1] = sh2;
tristanjph 0:2656fb225c5d 29 serial_no[2] = sh3;
tristanjph 0:2656fb225c5d 30 serial_no[3] = sl1;
tristanjph 0:2656fb225c5d 31 serial_no[4] = sl2;
tristanjph 0:2656fb225c5d 32 serial_no[5] = sl3;
tristanjph 0:2656fb225c5d 33 serial_no[6] = sl4;
tristanjph 0:2656fb225c5d 34
tristanjph 0:2656fb225c5d 35 return 1;
tristanjph 0:2656fb225c5d 36 }
tristanjph 0:2656fb225c5d 37
gert_lauritsen 7:45511c3d2950 38 int zigbee::SetKey(char* key)
tristanjph 0:2656fb225c5d 39 {
tristanjph 0:2656fb225c5d 40 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 41 DATA.printf("ATEE 1 \r");
tristanjph 0:2656fb225c5d 42
tristanjph 0:2656fb225c5d 43 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 44 wait_ms(1);
tristanjph 5:714651141a83 45 DATA.printf("ATKY %s \r",key);
tristanjph 0:2656fb225c5d 46 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 47 return 1;
tristanjph 0:2656fb225c5d 48 }
tristanjph 0:2656fb225c5d 49
gert_lauritsen 7:45511c3d2950 50 int zigbee::WriteSettings()
tristanjph 0:2656fb225c5d 51 {
tristanjph 0:2656fb225c5d 52 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 53 wait_ms(5);
tristanjph 0:2656fb225c5d 54 DATA.printf("ATWR \r");
tristanjph 0:2656fb225c5d 55 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 56 return 1;
tristanjph 0:2656fb225c5d 57 }
tristanjph 0:2656fb225c5d 58
tristanjph 0:2656fb225c5d 59
gert_lauritsen 7:45511c3d2950 60 void zigbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 61 {
tristanjph 1:c3d9bdcb0b03 62 int count=0;
tristanjph 2:cb627ea9b817 63 if(numchar == 0) {
tristanjph 2:cb627ea9b817 64 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 65 }
tristanjph 1:c3d9bdcb0b03 66 Serial DATA(_tx,_rx);
tristanjph 1:c3d9bdcb0b03 67 while(numchar!=count) {
tristanjph 1:c3d9bdcb0b03 68 if(DATA.readable()) {
tristanjph 2:cb627ea9b817 69 *data_buf = DATA.getc();
tristanjph 3:682615a0717e 70 data_buf+=1;
tristanjph 3:682615a0717e 71 count++;
tristanjph 1:c3d9bdcb0b03 72 }
tristanjph 1:c3d9bdcb0b03 73
tristanjph 1:c3d9bdcb0b03 74 }
tristanjph 2:cb627ea9b817 75 }
tristanjph 1:c3d9bdcb0b03 76
gert_lauritsen 7:45511c3d2950 77 int zigbee::ATI()
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("ATI\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::PingOut()
tristanjph 2:cb627ea9b817 88 {
tristanjph 2:cb627ea9b817 89 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 90 wait_ms(5);
gert_lauritsen 7:45511c3d2950 91 DATA.printf("AT+ANNCE\r");
gert_lauritsen 7:45511c3d2950 92 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 93 return 1;
gert_lauritsen 7:45511c3d2950 94 }
gert_lauritsen 7:45511c3d2950 95
gert_lauritsen 7:45511c3d2950 96
gert_lauritsen 7:45511c3d2950 97 int zigbee::PanScan()
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+PANSCAN\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
gert_lauritsen 7:45511c3d2950 107 int zigbee::Establish_Network()
gert_lauritsen 7:45511c3d2950 108 {
gert_lauritsen 7:45511c3d2950 109 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 110 wait_ms(5);
gert_lauritsen 7:45511c3d2950 111 DATA.printf("AT+EN\r");
tristanjph 2:cb627ea9b817 112 DATA.scanf ("%*s");
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 7:45511c3d2950 119 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 120 wait_ms(5);
gert_lauritsen 7:45511c3d2950 121 DATA.printf("AT+JN\r");
gert_lauritsen 7:45511c3d2950 122 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 123 return 1;
gert_lauritsen 7:45511c3d2950 124 }
gert_lauritsen 7:45511c3d2950 125
gert_lauritsen 7:45511c3d2950 126 int zigbee::ScanNetwork()
gert_lauritsen 7:45511c3d2950 127 {
gert_lauritsen 7:45511c3d2950 128 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 129 wait_ms(5);
gert_lauritsen 7:45511c3d2950 130 DATA.printf("AT+SN\r");
gert_lauritsen 7:45511c3d2950 131 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 132 return 1;
gert_lauritsen 7:45511c3d2950 133 }
gert_lauritsen 7:45511c3d2950 134
gert_lauritsen 7:45511c3d2950 135 int zigbee::NetworkInfo()
gert_lauritsen 7:45511c3d2950 136 {
gert_lauritsen 7:45511c3d2950 137 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
gert_lauritsen 7:45511c3d2950 138 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 139 wait_ms(5);
gert_lauritsen 7:45511c3d2950 140 DATA.printf("AT+N\r");
gert_lauritsen 7:45511c3d2950 141 DATA.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
gert_lauritsen 7:45511c3d2950 142 return 1;
gert_lauritsen 7:45511c3d2950 143 }
gert_lauritsen 7:45511c3d2950 144
gert_lauritsen 7:45511c3d2950 145 void zigbee::UniCast(char *adr,char *payload) //Ascii mode with null terminated string
gert_lauritsen 7:45511c3d2950 146 {
gert_lauritsen 7:45511c3d2950 147 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 148 wait_ms(5);
gert_lauritsen 7:45511c3d2950 149 DATA.printf("AT+UCAST:%s=%s\r",adr,payload);
gert_lauritsen 7:45511c3d2950 150
gert_lauritsen 7:45511c3d2950 151 DATA.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
gert_lauritsen 7:45511c3d2950 152 return 1;
gert_lauritsen 7:45511c3d2950 153 }
gert_lauritsen 7:45511c3d2950 154
gert_lauritsen 7:45511c3d2950 155 void zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
gert_lauritsen 7:45511c3d2950 156 {
gert_lauritsen 7:45511c3d2950 157 Serial DATA(_tx,_rx);
gert_lauritsen 7:45511c3d2950 158 wait_ms(5);
gert_lauritsen 7:45511c3d2950 159 DATA.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
gert_lauritsen 7:45511c3d2950 160 DATA.scanf ("%*s");
gert_lauritsen 7:45511c3d2950 161 return 1;
gert_lauritsen 7:45511c3d2950 162 }
gert_lauritsen 7:45511c3d2950 163
gert_lauritsen 7:45511c3d2950 164 void zigbee::Reset()
tristanjph 3:682615a0717e 165 {
tristanjph 6:6455a079bdb3 166 DigitalOut rst(_reset);
tristanjph 6:6455a079bdb3 167 rst = 0;
tristanjph 3:682615a0717e 168 wait_ms(10);
tristanjph 6:6455a079bdb3 169 rst = 1;
tristanjph 3:682615a0717e 170 wait_ms(1);
tristanjph 3:682615a0717e 171 }
tristanjph 3:682615a0717e 172
gert_lauritsen 7:45511c3d2950 173 //-----------------------Håndtere indkommende data------------------------------
tristanjph 3:682615a0717e 174
gert_lauritsen 7:45511c3d2950 175 unsigned long zigbee::hexToLong(const char *hex)
gert_lauritsen 7:45511c3d2950 176 {
gert_lauritsen 7:45511c3d2950 177 //return 32 bit
gert_lauritsen 7:45511c3d2950 178 unsigned long result = 0;
gert_lauritsen 7:45511c3d2950 179 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 180 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 181 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 182 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 183 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 184 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 185 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 186
gert_lauritsen 7:45511c3d2950 187 if (*++hex) //hvis den næstee ikke er null
gert_lauritsen 7:45511c3d2950 188 result <<= 4;
gert_lauritsen 7:45511c3d2950 189 }
gert_lauritsen 7:45511c3d2950 190 return result;
gert_lauritsen 7:45511c3d2950 191 }
gert_lauritsen 7:45511c3d2950 192
gert_lauritsen 7:45511c3d2950 193
gert_lauritsen 7:45511c3d2950 194 unsigned int zigbee::hexToInt(const char *hex)
gert_lauritsen 7:45511c3d2950 195 {
gert_lauritsen 7:45511c3d2950 196 //return 16 bit
gert_lauritsen 7:45511c3d2950 197 unsigned int result = 0;
gert_lauritsen 7:45511c3d2950 198 while (*hex) { //så længe det ikke er null
gert_lauritsen 7:45511c3d2950 199 if (*hex >= '0' && *hex <= '9')
gert_lauritsen 7:45511c3d2950 200 result += (*hex - '0');
gert_lauritsen 7:45511c3d2950 201 else if (*hex >= 'A' && *hex <= 'F')
gert_lauritsen 7:45511c3d2950 202 result += (*hex - 'A' +10);
gert_lauritsen 7:45511c3d2950 203 else if (*hex >= 'a' && *hex <= 'f')
gert_lauritsen 7:45511c3d2950 204 result += (*hex - 'a'+ 10);
gert_lauritsen 7:45511c3d2950 205
gert_lauritsen 7:45511c3d2950 206 if (*++hex) //hvis den næstee ikke er null
gert_lauritsen 7:45511c3d2950 207 result <<= 4;
gert_lauritsen 7:45511c3d2950 208 }
gert_lauritsen 7:45511c3d2950 209 return result;
gert_lauritsen 7:45511c3d2950 210 }
gert_lauritsen 7:45511c3d2950 211 //---------------------------------------------------------------
gert_lauritsen 7:45511c3d2950 212