
Sample program for XBee remote API operation. This program get ADC sample data from remote XBee connected over the radio
main.cpp@1:3b74123e6d7a, 2010-12-12 (annotated)
- Committer:
- todotani
- Date:
- Sun Dec 12 07:38:03 2010 +0000
- Revision:
- 1:3b74123e6d7a
- Parent:
- 0:6e92a52f1a08
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
todotani | 0:6e92a52f1a08 | 1 | #include "mbed.h" |
todotani | 0:6e92a52f1a08 | 2 | #include "XBee.h" |
todotani | 0:6e92a52f1a08 | 3 | #include "TextLCD.h" |
todotani | 0:6e92a52f1a08 | 4 | |
todotani | 0:6e92a52f1a08 | 5 | TextLCD lcd(p25, p26, p24, p23, p22, p21); // RS, E, DB4, DB5, DB6, DB7 |
todotani | 0:6e92a52f1a08 | 6 | |
todotani | 0:6e92a52f1a08 | 7 | /*-- AT command and parameters --*/ |
todotani | 0:6e92a52f1a08 | 8 | uint8_t atISCmd[] = {'I', 'S'}; // Forces a read of all enabled digital and analog input lines |
todotani | 0:6e92a52f1a08 | 9 | uint8_t atDBCmd[] = {'D', 'B'}; // Received Signal Strength |
todotani | 0:6e92a52f1a08 | 10 | uint8_t cmdVal0[] = {0}; // Clear RSSI regisger |
todotani | 0:6e92a52f1a08 | 11 | |
todotani | 0:6e92a52f1a08 | 12 | /*-- Create instanse of Xbee object --*/ |
todotani | 0:6e92a52f1a08 | 13 | XBee xbee(p13, p14); |
todotani | 0:6e92a52f1a08 | 14 | XBeeAddress64 remoteAddress(0x0013A200, 0x406B7111); // Specify your XBee address |
todotani | 0:6e92a52f1a08 | 15 | |
todotani | 0:6e92a52f1a08 | 16 | /*-- Create instanse of Command and Response object --*/ |
todotani | 0:6e92a52f1a08 | 17 | // Remot ATIS command to read ADC value (ADC3 is enabled by X-CTU tool) |
todotani | 0:6e92a52f1a08 | 18 | RemoteAtCommandRequest remoteSampleRequest(remoteAddress, atISCmd); |
todotani | 0:6e92a52f1a08 | 19 | // Local ATDB command to read signal strength (RSSI) |
todotani | 0:6e92a52f1a08 | 20 | AtCommandRequest atDB(atDBCmd); |
todotani | 0:6e92a52f1a08 | 21 | // Local ATDB0 command to clear RSSI |
todotani | 0:6e92a52f1a08 | 22 | AtCommandRequest atDB0(atDBCmd, cmdVal0, sizeof(cmdVal0)); |
todotani | 0:6e92a52f1a08 | 23 | // Create instanse to handle command response |
todotani | 0:6e92a52f1a08 | 24 | AtCommandResponse response = AtCommandResponse(); |
todotani | 0:6e92a52f1a08 | 25 | RemoteAtCommandResponse remoteResp = RemoteAtCommandResponse(); |
todotani | 0:6e92a52f1a08 | 26 | |
todotani | 0:6e92a52f1a08 | 27 | |
todotani | 0:6e92a52f1a08 | 28 | /* Receive command response packet |
todotani | 0:6e92a52f1a08 | 29 | * If OK response recieved, return pointer to the Response Data Frame |
todotani | 0:6e92a52f1a08 | 30 | */ |
todotani | 0:6e92a52f1a08 | 31 | uint8_t* GetResponse() { |
todotani | 0:6e92a52f1a08 | 32 | // Read response |
todotani | 0:6e92a52f1a08 | 33 | if (xbee.readPacket(5000)) { |
todotani | 0:6e92a52f1a08 | 34 | // Got a response! Check if response is AT command respose |
todotani | 0:6e92a52f1a08 | 35 | if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) { |
todotani | 0:6e92a52f1a08 | 36 | xbee.getResponse().getAtCommandResponse(response); |
todotani | 0:6e92a52f1a08 | 37 | if ( response.getStatus() == AT_OK ) |
todotani | 0:6e92a52f1a08 | 38 | return response.getValue(); |
todotani | 0:6e92a52f1a08 | 39 | } else if (xbee.getResponse().getApiId() == REMOTE_AT_COMMAND_RESPONSE) { |
todotani | 0:6e92a52f1a08 | 40 | xbee.getResponse().getRemoteAtCommandResponse(remoteResp); |
todotani | 0:6e92a52f1a08 | 41 | if ( remoteResp.getStatus() == AT_OK ) { |
todotani | 0:6e92a52f1a08 | 42 | // Debug print |
todotani | 0:6e92a52f1a08 | 43 | printf("Response Data:"); |
todotani | 0:6e92a52f1a08 | 44 | for (int i = 0; i < remoteResp.getValueLength(); i++) |
todotani | 0:6e92a52f1a08 | 45 | printf("%02X ", remoteResp.getValue()[i]); |
todotani | 0:6e92a52f1a08 | 46 | printf("\n"); |
todotani | 0:6e92a52f1a08 | 47 | |
todotani | 0:6e92a52f1a08 | 48 | return remoteResp.getValue(); |
todotani | 0:6e92a52f1a08 | 49 | } else { |
todotani | 0:6e92a52f1a08 | 50 | printf("Remote Command Error:0x%X\n", response.getStatus()); |
todotani | 0:6e92a52f1a08 | 51 | } |
todotani | 0:6e92a52f1a08 | 52 | } |
todotani | 0:6e92a52f1a08 | 53 | } |
todotani | 0:6e92a52f1a08 | 54 | |
todotani | 0:6e92a52f1a08 | 55 | return 0; |
todotani | 0:6e92a52f1a08 | 56 | } |
todotani | 0:6e92a52f1a08 | 57 | |
todotani | 0:6e92a52f1a08 | 58 | |
todotani | 0:6e92a52f1a08 | 59 | /* Get ADC data |
todotani | 0:6e92a52f1a08 | 60 | * Data frame structure of ATIS |
todotani | 0:6e92a52f1a08 | 61 | * Offset |
todotani | 0:6e92a52f1a08 | 62 | * 0 : Number of Samples (Always 1) |
todotani | 0:6e92a52f1a08 | 63 | * 1-2 : Digital Channel Mask |
todotani | 0:6e92a52f1a08 | 64 | * 3 : Analog Channel Mask |
todotani | 0:6e92a52f1a08 | 65 | * 4-5 : Digital Samples (Omit if no DIO enabled) |
todotani | 0:6e92a52f1a08 | 66 | * 6-7 : First ADC Data |
todotani | 0:6e92a52f1a08 | 67 | */ |
todotani | 0:6e92a52f1a08 | 68 | uint16_t getAnalog(uint8_t *FrameData, int ADC) { |
todotani | 0:6e92a52f1a08 | 69 | // ADC data feild starts 4 bytes offest, if no DIO enabled |
todotani | 0:6e92a52f1a08 | 70 | uint8_t start = 4; |
todotani | 0:6e92a52f1a08 | 71 | |
todotani | 0:6e92a52f1a08 | 72 | // Contains Digital channel? |
todotani | 1:3b74123e6d7a | 73 | if (FrameData[1] > 0 || FrameData[2] > 0) { |
todotani | 0:6e92a52f1a08 | 74 | // make room for digital i/o |
todotani | 0:6e92a52f1a08 | 75 | start+=2; |
todotani | 0:6e92a52f1a08 | 76 | } |
todotani | 0:6e92a52f1a08 | 77 | |
todotani | 0:6e92a52f1a08 | 78 | // start depends on how many ADCs before this ADC are enabled |
todotani | 0:6e92a52f1a08 | 79 | for (int i = 0; i < ADC; i++) { |
todotani | 0:6e92a52f1a08 | 80 | // Is Analog channel Enabled ? |
todotani | 0:6e92a52f1a08 | 81 | if ( (FrameData[3] >> i) & 1 ) { |
todotani | 0:6e92a52f1a08 | 82 | start+=2; |
todotani | 0:6e92a52f1a08 | 83 | } |
todotani | 0:6e92a52f1a08 | 84 | } |
todotani | 0:6e92a52f1a08 | 85 | |
todotani | 0:6e92a52f1a08 | 86 | return (uint16_t)((FrameData[start] << 8) + FrameData[start + 1]); |
todotani | 0:6e92a52f1a08 | 87 | } |
todotani | 0:6e92a52f1a08 | 88 | |
todotani | 0:6e92a52f1a08 | 89 | |
todotani | 0:6e92a52f1a08 | 90 | int main() { |
todotani | 0:6e92a52f1a08 | 91 | unsigned int loop = 0; |
todotani | 0:6e92a52f1a08 | 92 | |
todotani | 0:6e92a52f1a08 | 93 | xbee.begin(9600); |
todotani | 0:6e92a52f1a08 | 94 | lcd.printf("RSSI:"); |
todotani | 0:6e92a52f1a08 | 95 | lcd.locate(0, 1); |
todotani | 0:6e92a52f1a08 | 96 | lcd.printf("ADC :"); |
todotani | 0:6e92a52f1a08 | 97 | printf("\nStart.\n"); |
todotani | 0:6e92a52f1a08 | 98 | |
todotani | 0:6e92a52f1a08 | 99 | while (true) { |
todotani | 0:6e92a52f1a08 | 100 | uint8_t *responseVal; |
todotani | 0:6e92a52f1a08 | 101 | uint8_t rssiVal = 0; |
todotani | 0:6e92a52f1a08 | 102 | uint16_t adcVal = 0; |
todotani | 0:6e92a52f1a08 | 103 | |
todotani | 0:6e92a52f1a08 | 104 | // Send ATDB command (Read RSSI register from local Xbee) |
todotani | 0:6e92a52f1a08 | 105 | xbee.send(atDB); |
todotani | 0:6e92a52f1a08 | 106 | responseVal = GetResponse(); |
todotani | 0:6e92a52f1a08 | 107 | if ( responseVal != 0 ) |
todotani | 0:6e92a52f1a08 | 108 | rssiVal = responseVal[0]; |
todotani | 0:6e92a52f1a08 | 109 | lcd.locate(5, 0); |
todotani | 0:6e92a52f1a08 | 110 | if (rssiVal == 0) |
todotani | 0:6e92a52f1a08 | 111 | lcd.printf("No Signal"); |
todotani | 0:6e92a52f1a08 | 112 | else |
todotani | 0:6e92a52f1a08 | 113 | lcd.printf("-%ddBm ", rssiVal); |
todotani | 0:6e92a52f1a08 | 114 | |
todotani | 0:6e92a52f1a08 | 115 | // Clear RSSI register, because Xbee hold RSSI value of last received packet even after radio disconneded |
todotani | 0:6e92a52f1a08 | 116 | xbee.send(atDB0); |
todotani | 0:6e92a52f1a08 | 117 | GetResponse(); |
todotani | 0:6e92a52f1a08 | 118 | |
todotani | 0:6e92a52f1a08 | 119 | // Read ADC3 value by sending ATIS command |
todotani | 0:6e92a52f1a08 | 120 | xbee.send(remoteSampleRequest); |
todotani | 0:6e92a52f1a08 | 121 | responseVal = GetResponse(); |
todotani | 0:6e92a52f1a08 | 122 | if ( responseVal != 0 ) { |
todotani | 0:6e92a52f1a08 | 123 | adcVal = getAnalog(responseVal, 3); // Assume ADC3 is enabled |
todotani | 0:6e92a52f1a08 | 124 | } |
todotani | 0:6e92a52f1a08 | 125 | lcd.locate(5, 1); |
todotani | 0:6e92a52f1a08 | 126 | if (adcVal == 0) |
todotani | 0:6e92a52f1a08 | 127 | lcd.printf("- "); |
todotani | 0:6e92a52f1a08 | 128 | else |
todotani | 0:6e92a52f1a08 | 129 | lcd.printf("%d", adcVal); |
todotani | 0:6e92a52f1a08 | 130 | |
todotani | 0:6e92a52f1a08 | 131 | printf("Loop:%d\n", loop++); |
todotani | 0:6e92a52f1a08 | 132 | wait(1.0); |
todotani | 0:6e92a52f1a08 | 133 | } |
todotani | 0:6e92a52f1a08 | 134 | } |