For getting RSSI value from xbee
Dependents: XBeeGetRSSISampleProgram Xbee_s2_RX_versao2
XBeeGetRssi.cpp@8:b1313aaddf17, 2013-08-27 (annotated)
- Committer:
- ATKINZ117
- Date:
- Tue Aug 27 09:37:52 2013 +0000
- Revision:
- 8:b1313aaddf17
- Parent:
- 6:2e725705db90
when I execute this main program, this shows "OKKKK".
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ATKINZ117 | 6:2e725705db90 | 1 | /*---------------------------------------------------------- |
ATKINZ117 | 6:2e725705db90 | 2 | 20130824:Todos:improve the performance of this program. |
ATKINZ117 | 6:2e725705db90 | 3 | get only the rssi value. |
ATKINZ117 | 6:2e725705db90 | 4 | |
ATKINZ117 | 6:2e725705db90 | 5 | |
ATKINZ117 | 6:2e725705db90 | 6 | ----------------------------------------------------------*/ |
ATKINZ117 | 6:2e725705db90 | 7 | |
ATKINZ117 | 0:d1ff6ea68ab2 | 8 | #include "XBeeGetRssi.h" |
ATKINZ117 | 0:d1ff6ea68ab2 | 9 | #include "mbed.h" |
ATKINZ117 | 4:16396d9ec459 | 10 | //#include <string.h> |
ATKINZ117 | 0:d1ff6ea68ab2 | 11 | |
ATKINZ117 | 0:d1ff6ea68ab2 | 12 | |
ATKINZ117 | 0:d1ff6ea68ab2 | 13 | XBeeGetRssi::XBeeGetRssi(PinName tx,PinName rx){ |
ATKINZ117 | 0:d1ff6ea68ab2 | 14 | _tx=tx; |
ATKINZ117 | 0:d1ff6ea68ab2 | 15 | _rx=rx; |
ATKINZ117 | 0:d1ff6ea68ab2 | 16 | } |
ATKINZ117 | 0:d1ff6ea68ab2 | 17 | |
ATKINZ117 | 6:2e725705db90 | 18 | char XBeeGetRssi::getRssi(int n){ |
ATKINZ117 | 4:16396d9ec459 | 19 | //int judge_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 20 | int judge_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 21 | int rssi_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 22 | int xbee_message_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 23 | |
ATKINZ117 | 0:d1ff6ea68ab2 | 24 | Serial pc(USBTX,USBRX); //for debugging. |
ATKINZ117 | 0:d1ff6ea68ab2 | 25 | Serial _MyXbee(_tx,_rx); |
ATKINZ117 | 0:d1ff6ea68ab2 | 26 | |
ATKINZ117 | 6:2e725705db90 | 27 | for(int i=0;i<20;i++){ |
ATKINZ117 | 6:2e725705db90 | 28 | _rssi[i]='^'; //initial value |
ATKINZ117 | 6:2e725705db90 | 29 | _xbee_message[i]='^';//initial value; |
ATKINZ117 | 6:2e725705db90 | 30 | } |
ATKINZ117 | 1:8cb27d9eec8e | 31 | |
ATKINZ117 | 4:16396d9ec459 | 32 | // pc.printf("At the first of xbee class.\r\n"); |
ATKINZ117 | 2:befae843036e | 33 | _MyXbee.putc('T'); |
ATKINZ117 | 4:16396d9ec459 | 34 | while(_MyXbee.readable()){ |
ATKINZ117 | 4:16396d9ec459 | 35 | _xbee_message[0]=_MyXbee.getc(); |
ATKINZ117 | 0:d1ff6ea68ab2 | 36 | } |
ATKINZ117 | 0:d1ff6ea68ab2 | 37 | |
ATKINZ117 | 4:16396d9ec459 | 38 | // pc.printf("first message from xbee;%s\r\n",_xbee_message[0]); |
ATKINZ117 | 4:16396d9ec459 | 39 | wait(1.0); |
ATKINZ117 | 4:16396d9ec459 | 40 | //_MyXbee.printf("+++"); //entering AT command mode. |
ATKINZ117 | 4:16396d9ec459 | 41 | _MyXbee.putc('+'); |
ATKINZ117 | 6:2e725705db90 | 42 | wait_ms(1); |
ATKINZ117 | 4:16396d9ec459 | 43 | _MyXbee.putc('+'); |
ATKINZ117 | 6:2e725705db90 | 44 | wait_ms(1); |
ATKINZ117 | 4:16396d9ec459 | 45 | _MyXbee.putc('+'); |
ATKINZ117 | 6:2e725705db90 | 46 | wait_ms(1); |
ATKINZ117 | 4:16396d9ec459 | 47 | // pc.printf("sent +++\r\n"); |
ATKINZ117 | 4:16396d9ec459 | 48 | while(_xbee_message[judge_counter++]!='\r'){ |
ATKINZ117 | 4:16396d9ec459 | 49 | while(_MyXbee.readable()){ |
ATKINZ117 | 4:16396d9ec459 | 50 | _xbee_message[xbee_message_counter++]=_MyXbee.getc(); |
ATKINZ117 | 4:16396d9ec459 | 51 | } |
ATKINZ117 | 4:16396d9ec459 | 52 | }_xbee_message[judge_counter]='\0'; |
ATKINZ117 | 4:16396d9ec459 | 53 | judge_counter=0; //reset counter for later use |
ATKINZ117 | 4:16396d9ec459 | 54 | xbee_message_counter=0; //reset counter for later use |
ATKINZ117 | 4:16396d9ec459 | 55 | |
ATKINZ117 | 4:16396d9ec459 | 56 | // while(_xbee_message[judge_counter++]!='\0'){ |
ATKINZ117 | 4:16396d9ec459 | 57 | // pc.printf("got xbee message;%s\r\n",_xbee_message[xbee_message_counter++]); |
ATKINZ117 | 4:16396d9ec459 | 58 | //} |
ATKINZ117 | 4:16396d9ec459 | 59 | wait(1.0); |
ATKINZ117 | 4:16396d9ec459 | 60 | judge_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 61 | xbee_message_counter=0; |
ATKINZ117 | 4:16396d9ec459 | 62 | |
ATKINZ117 | 4:16396d9ec459 | 63 | _MyXbee.putc('A'); |
ATKINZ117 | 6:2e725705db90 | 64 | wait_ms(5); |
ATKINZ117 | 4:16396d9ec459 | 65 | _MyXbee.putc('T'); |
ATKINZ117 | 6:2e725705db90 | 66 | wait_ms(10); |
ATKINZ117 | 4:16396d9ec459 | 67 | _MyXbee.putc('D'); |
ATKINZ117 | 6:2e725705db90 | 68 | wait_ms(10); |
ATKINZ117 | 4:16396d9ec459 | 69 | _MyXbee.putc('B'); |
ATKINZ117 | 6:2e725705db90 | 70 | wait_ms(10); |
ATKINZ117 | 4:16396d9ec459 | 71 | _MyXbee.putc('\r'); |
ATKINZ117 | 6:2e725705db90 | 72 | wait_ms(10); |
ATKINZ117 | 4:16396d9ec459 | 73 | // pc.printf("sent ATDB!\r\n"); |
ATKINZ117 | 4:16396d9ec459 | 74 | |
ATKINZ117 | 8:b1313aaddf17 | 75 | /*while(_rssi[judge_counter++]!='\0'){ |
ATKINZ117 | 4:16396d9ec459 | 76 | while(_MyXbee.readable()){ |
ATKINZ117 | 4:16396d9ec459 | 77 | _rssi[rssi_counter++]=_MyXbee.getc(); |
ATKINZ117 | 4:16396d9ec459 | 78 | //rssi_counter++; |
ATKINZ117 | 4:16396d9ec459 | 79 | } |
ATKINZ117 | 8:b1313aaddf17 | 80 | }*/ |
ATKINZ117 | 8:b1313aaddf17 | 81 | do{ |
ATKINZ117 | 8:b1313aaddf17 | 82 | while(_MyXbee.readable()){ |
ATKINZ117 | 8:b1313aaddf17 | 83 | _rssi[rssi_counter++]=_MyXbee.getc(); |
ATKINZ117 | 8:b1313aaddf17 | 84 | } |
ATKINZ117 | 8:b1313aaddf17 | 85 | }while(_rssi[judge_counter++]!='^'); |
ATKINZ117 | 4:16396d9ec459 | 86 | |
ATKINZ117 | 4:16396d9ec459 | 87 | _MyXbee.putc('A'); |
ATKINZ117 | 6:2e725705db90 | 88 | wait_ms(5); |
ATKINZ117 | 4:16396d9ec459 | 89 | _MyXbee.putc('T'); |
ATKINZ117 | 6:2e725705db90 | 90 | wait_ms(5); |
ATKINZ117 | 4:16396d9ec459 | 91 | _MyXbee.putc('C'); |
ATKINZ117 | 6:2e725705db90 | 92 | wait_ms(5); |
ATKINZ117 | 4:16396d9ec459 | 93 | _MyXbee.putc('N'); |
ATKINZ117 | 6:2e725705db90 | 94 | wait_ms(5); |
ATKINZ117 | 4:16396d9ec459 | 95 | _MyXbee.putc('\r'); |
ATKINZ117 | 4:16396d9ec459 | 96 | |
ATKINZ117 | 4:16396d9ec459 | 97 | return _rssi[n]; |
ATKINZ117 | 4:16396d9ec459 | 98 | |
ATKINZ117 | 4:16396d9ec459 | 99 | } |