Basic API demo
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "XBee.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 00006 RawSerial xbeeUart(p28, p27, 9600); 00007 XBee xbee(xbeeUart); 00008 00009 // serial high 00010 uint8_t shCmd[] = {'S','H'}; 00011 // serial low 00012 uint8_t slCmd[] = {'S','L'}; 00013 // association status 00014 uint8_t assocCmd[] = {'A','I'}; 00015 00016 uint8_t disableApiCmd[] = {'A', 'P'}; 00017 uint8_t disableApiData = 0; 00018 00019 AtCommandRequest atRequest = AtCommandRequest(shCmd); 00020 AtCommandResponse atResponse = AtCommandResponse(); 00021 00022 uint8_t messageData[] = {'H', 'e', 'l', 'l', 'o'}; 00023 Tx16Request txReq(0x0003, messageData, 5); 00024 TxStatusResponse txStatus; 00025 00026 void sendAtCommand(); 00027 00028 void loop() { 00029 // get SH 00030 sendAtCommand(); 00031 00032 // set command to SL 00033 atRequest.setCommand(slCmd); 00034 sendAtCommand(); 00035 00036 // Say hello! 00037 xbee.send(txReq); 00038 if (xbee.readPacket(5000)) { 00039 if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) { 00040 xbee.getResponse().getTxStatusResponse(txStatus); 00041 00042 // get the delivery status, the fifth byte 00043 if (txStatus.getStatus() == SUCCESS) { 00044 // success. time to celebrate 00045 pc.printf("Message was sent and received!\r\n"); 00046 } else { 00047 // the remote XBee did not receive our packet. is it powered on? 00048 pc.printf("Message was not acknowledged\r\n"); 00049 } 00050 } 00051 } else if (xbee.getResponse().isError()) { 00052 pc.printf("Error reading packet. Error code: %d\r\n", xbee.getResponse().getErrorCode()); 00053 } else { 00054 // local XBee did not provide a timely TX Status Response. Radio is not configured properly or connected 00055 pc.printf("Transmit timeout\r\n"); 00056 } 00057 00058 // Disable API mode 00059 // atRequest.setCommand(disableApiCmd); 00060 // atRequest.setCommandValue(&disableApiData); 00061 // atRequest.setCommandValueLength(1); 00062 // sendAtCommand(); 00063 00064 // Demo complete 00065 while (1) {}; 00066 } 00067 00068 int main() 00069 { 00070 pc.printf("Waiting for Xbee to initialize...\r\n"); 00071 wait(5); 00072 pc.printf("Xbee should be ready\r\n"); 00073 00074 while(1) { 00075 loop(); 00076 } 00077 } 00078 00079 void sendAtCommand() { 00080 pc.printf("Sending command to the XBee\r\n"); 00081 00082 // send the command 00083 xbee.send(atRequest); 00084 00085 // wait up to 5 seconds for the status response 00086 if (xbee.readPacket(5000)) { 00087 // got a response! 00088 00089 // should be an AT command response 00090 if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) { 00091 xbee.getResponse().getAtCommandResponse(atResponse); 00092 00093 if (atResponse.isOk()) { 00094 pc.printf("Command [%c%c] was successful!\r\n", atResponse.getCommand()[0], atResponse.getCommand()[1]); 00095 00096 if (atResponse.getValueLength() > 0) { 00097 pc.printf("Command value length is "); 00098 pc.printf("%d\r\n", atResponse.getValueLength()); 00099 00100 pc.printf("Command value: "); 00101 00102 for (int i = 0; i < atResponse.getValueLength(); i++) { 00103 pc.printf("%x ", atResponse.getValue()[i]); 00104 } 00105 00106 pc.printf("\r\n"); 00107 } 00108 } 00109 else { 00110 pc.printf("Command return error code: %x\r\n", atResponse.getStatus()); 00111 } 00112 } else { 00113 pc.printf("Expected AT response but got %x\r\n", xbee.getResponse().getApiId()); 00114 } 00115 } else { 00116 // at command failed 00117 if (xbee.getResponse().isError()) { 00118 pc.printf("Error reading packet. Error code: %x\r\n", xbee.getResponse().getErrorCode()); 00119 } 00120 else { 00121 pc.printf("No response from radio\r\n"); 00122 } 00123 } 00124 } 00125
Generated on Mon Jul 18 2022 02:03:46 by
1.7.2