PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AtCommand.pde 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2009 Andrew Rapp. All rights reserved.
  3. *
  4. * This file is part of XBee-Arduino.
  5. *
  6. * XBee-Arduino is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * XBee-Arduino is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <XBee.h>
  20. #include <SoftwareSerial.h>
  21. /*
  22. This example is for Series 1 (10C8 or later firmware) or Series 2 XBee radios
  23. Sends a few AT command queries to the radio and checks the status response for success
  24. This example uses the SoftSerial library to view the XBee communication. I am using a
  25. Modern Device USB BUB board (http://moderndevice.com/connect) and viewing the output
  26. with the Arduino Serial Monitor.
  27. */
  28. // Define SoftSerial TX/RX pins
  29. // Connect Arduino pin 8 to TX of usb-serial device
  30. uint8_t ssRX = 8;
  31. // Connect Arduino pin 9 to RX of usb-serial device
  32. uint8_t ssTX = 9;
  33. // Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
  34. SoftwareSerial nss(ssRX, ssTX);
  35. XBee xbee = XBee();
  36. // serial high
  37. uint8_t shCmd[] = {'S','H'};
  38. // serial low
  39. uint8_t slCmd[] = {'S','L'};
  40. // association status
  41. uint8_t assocCmd[] = {'A','I'};
  42. AtCommandRequest atRequest = AtCommandRequest(shCmd);
  43. AtCommandResponse atResponse = AtCommandResponse();
  44. void setup() {
  45. Serial.begin(9600);
  46. xbee.begin(Serial);
  47. // start soft serial
  48. nss.begin(9600);
  49. // Startup delay to wait for XBee radio to initialize.
  50. // you may need to increase this value if you are not getting a response
  51. delay(5000);
  52. }
  53. void loop() {
  54. // get SH
  55. sendAtCommand();
  56. // set command to SL
  57. atRequest.setCommand(slCmd);
  58. sendAtCommand();
  59. // set command to AI
  60. atRequest.setCommand(assocCmd);
  61. sendAtCommand();
  62. // we're done. Hit the Arduino reset button to start the sketch over
  63. while (1) {};
  64. }
  65. void sendAtCommand() {
  66. nss.println("Sending command to the XBee");
  67. // send the command
  68. xbee.send(atRequest);
  69. // wait up to 5 seconds for the status response
  70. if (xbee.readPacket(5000)) {
  71. // got a response!
  72. // should be an AT command response
  73. if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
  74. xbee.getResponse().getAtCommandResponse(atResponse);
  75. if (atResponse.isOk()) {
  76. nss.print("Command [");
  77. nss.print(atResponse.getCommand()[0]);
  78. nss.print(atResponse.getCommand()[1]);
  79. nss.println("] was successful!");
  80. if (atResponse.getValueLength() > 0) {
  81. nss.print("Command value length is ");
  82. nss.println(atResponse.getValueLength(), DEC);
  83. nss.print("Command value: ");
  84. for (int i = 0; i < atResponse.getValueLength(); i++) {
  85. nss.print(atResponse.getValue()[i], HEX);
  86. nss.print(" ");
  87. }
  88. nss.println("");
  89. }
  90. }
  91. else {
  92. nss.print("Command return error code: ");
  93. nss.println(atResponse.getStatus(), HEX);
  94. }
  95. } else {
  96. nss.print("Expected AT response but got ");
  97. nss.print(xbee.getResponse().getApiId(), HEX);
  98. }
  99. } else {
  100. // at command failed
  101. if (xbee.getResponse().isError()) {
  102. nss.print("Error reading packet. Error code: ");
  103. nss.println(xbee.getResponse().getErrorCode());
  104. }
  105. else {
  106. nss.print("No response from radio");
  107. }
  108. }
  109. }