PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

144 lines
4.6KB

  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
  23. Sends two Remote AT commands to configure the remote radio for I/O line monitoring
  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. // Turn on I/O sampling
  37. uint8_t irCmd[] = {'I','R'};
  38. // Set sample rate to 65 seconds (0xffff/1000)
  39. uint8_t irValue[] = { 0xff, 0xff };
  40. // Set DIO0 (pin 20) to Analog Input
  41. uint8_t d0Cmd[] = { 'D', '0' };
  42. uint8_t d0Value[] = { 0x2 };
  43. // SH + SL of your remote radio
  44. XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x400a3e02);
  45. // Create a remote AT request with the IR command
  46. RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(remoteAddress, irCmd, irValue, sizeof(irValue));
  47. // Create a Remote AT response object
  48. RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();
  49. void setup() {
  50. Serial.begin(9600);
  51. xbee.begin(Serial);
  52. // start soft serial
  53. nss.begin(9600);
  54. // When powered on, XBee radios require a few seconds to start up
  55. // and join the network.
  56. // During this time, any packets sent to the radio are ignored.
  57. // Series 2 radios send a modem status packet on startup.
  58. // it took about 4 seconds for mine to return modem status.
  59. // In my experience, series 1 radios take a bit longer to associate.
  60. // Of course if the radio has been powered on for some time before the sketch runs,
  61. // you can safely remove this delay.
  62. // Or if you both commands are not successful, try increasing the delay.
  63. delay(5000);
  64. }
  65. void loop() {
  66. sendRemoteAtCommand();
  67. // now reuse same object for DIO0 command
  68. remoteAtRequest.setCommand(d0Cmd);
  69. remoteAtRequest.setCommandValue(d0Value);
  70. remoteAtRequest.setCommandValueLength(sizeof(d0Value));
  71. sendRemoteAtCommand();
  72. // it's a good idea to clear the set value so that the object can be reused for a query
  73. remoteAtRequest.clearCommandValue();
  74. // we're done
  75. while (1) {};
  76. }
  77. void sendRemoteAtCommand() {
  78. nss.println("Sending command to the XBee");
  79. // send the command
  80. xbee.send(remoteAtRequest);
  81. // wait up to 5 seconds for the status response
  82. if (xbee.readPacket(5000)) {
  83. // got a response!
  84. // should be an AT command response
  85. if (xbee.getResponse().getApiId() == REMOTE_AT_COMMAND_RESPONSE) {
  86. xbee.getResponse().getRemoteAtCommandResponse(remoteAtResponse);
  87. if (remoteAtResponse.isOk()) {
  88. nss.print("Command [");
  89. nss.print(remoteAtResponse.getCommand()[0]);
  90. nss.print(remoteAtResponse.getCommand()[1]);
  91. nss.println("] was successful!");
  92. if (remoteAtResponse.getValueLength() > 0) {
  93. nss.print("Command value length is ");
  94. nss.println(remoteAtResponse.getValueLength(), DEC);
  95. nss.print("Command value: ");
  96. for (int i = 0; i < remoteAtResponse.getValueLength(); i++) {
  97. nss.print(remoteAtResponse.getValue()[i], HEX);
  98. nss.print(" ");
  99. }
  100. nss.println("");
  101. }
  102. } else {
  103. nss.print("Command returned error code: ");
  104. nss.println(remoteAtResponse.getStatus(), HEX);
  105. }
  106. } else {
  107. nss.print("Expected Remote AT response but got ");
  108. nss.print(xbee.getResponse().getApiId(), HEX);
  109. }
  110. } else if (xbee.getResponse().isError()) {
  111. nss.print("Error reading packet. Error code: ");
  112. nss.println(xbee.getResponse().getErrorCode());
  113. } else {
  114. nss.print("No response from radio");
  115. }
  116. }