PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 line
5.9KB

  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 2 (ZigBee) XBee Radios only, though Series 1 also support sleep mode.
  23. This example demonstrates the XBee pin sleep setting, by allowing the Arduino
  24. to sleep/wake the XBee. In this example and end device is attached to the Arduino.
  25. The end device sleep mode must be set to 1 (SM=1), to enable pin sleep.
  26. Set SP=AF0 (28 seconds) on the coordinator. This will instruct the coordinator to buffer any packets, for up to 28 seconds,
  27. while the end device is sleeping. When the end device wakes, it will poll the coordinator and receive the packet.
  28. Note: I'm using the SoftSerial library to communicate with the Arduino since the Arduino's Serial is being used by the XBee
  29. How it works:
  30. When you send a "1", the Arduino will sleep the XBee.
  31. Sending "2" wakes the XBee and "3" will send an arbitrary TX packet.
  32. Of course if the XBee is sleeping, the TX packet will not be delivered.
  33. Connect the Arduino Serial Monitor to the usb-serial device to send the commands.
  34. Connect an LED to the XBee Module Status (pin 13). This will turn on when the XBee is awake and off when it's sleeping
  35. Attach the coordinator to your computer and send a TX packet ever 28 seconds. You should be able to verify the
  36. end device receives the packet when it wakes from sleep.
  37. Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
  38. */
  39. // create the XBee object
  40. XBee xbee = XBee();
  41. // create an arbitrary payload -- what we're sending is not relevant
  42. uint8_t payload[] = { 0, 0 };
  43. // SH + SL Address of a remote XBee
  44. XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
  45. ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  46. ZBTxStatusResponse txStatus = ZBTxStatusResponse();
  47. ZBRxResponse rx = ZBRxResponse();
  48. // note: xbee sleep pin doesn't need 3.3. to sleep -- open circuit also will sleep it, but of course it needs 0V to wake!
  49. // connect Arduino digital 7 to XBee sleep pin (9) through a voltage divider. I'm using a 10K resistor.
  50. uint8_t sleepPin = 7;
  51. // SoftSerial RX: connect Arduino digitial 8 to the TX of of usb-serial device. note: I'm using Modern Device's USB BUB (set to 5V). You can use a 3.3V usb-serial with a voltage divider on RX (TX does not require since Arduino is 3.3V tolerant)
  52. uint8_t ssRX = 8;
  53. // SoftSerial TX: connect Arduino digital 9 to RX of usb-serial device
  54. uint8_t ssTX = 9;
  55. SoftwareSerial nss(ssRX, ssTX);
  56. void setup() {
  57. pinMode(sleepPin, OUTPUT);
  58. // set to LOW (wake)
  59. digitalWrite(sleepPin, LOW);
  60. // start XBee communication
  61. Serial.begin(9600);
  62. xbee.setSerial(Serial);
  63. // start soft serial
  64. nss.begin(9600);
  65. }
  66. void sendPacket() {
  67. nss.println("Sending a packet");
  68. xbee.send(zbTx);
  69. // after sending a tx request, we expect a status response
  70. // wait up to 5 seconds for the status response
  71. if (xbee.readPacket(5000)) {
  72. // got a response!
  73. // should be a znet tx status
  74. if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
  75. xbee.getResponse().getZBTxStatusResponse(txStatus);
  76. // get the delivery status, the fifth byte
  77. if (txStatus.getDeliveryStatus() == SUCCESS) {
  78. // success. time to celebrate
  79. nss.println("packet was delivered");
  80. } else {
  81. // the remote XBee did not receive our packet. is it powered on?
  82. nss.println("packet delivery failed");
  83. }
  84. } else if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
  85. // we received a packet something!
  86. xbee.getResponse().getZBRxResponse(rx);
  87. // print the payload. lets assume it's text.
  88. for (uint8_t i = 0; i < rx.getDataLength(); i++) {
  89. nss.println(rx.getData(i));
  90. }
  91. }
  92. } else {
  93. // local xbee is sleeping
  94. nss.println("no response -- is local xbee sleeping?");
  95. }
  96. }
  97. void loop() {
  98. if (nss.available()) {
  99. int cmd = nss.read();
  100. // ascii 1 == 49
  101. if (cmd == 49) { // 1 (ASCII)
  102. // sleep XBee
  103. digitalWrite(sleepPin, HIGH);
  104. nss.println("sleeping xbee");
  105. } else if (cmd == 50) { // 2 (ASCII)
  106. digitalWrite(sleepPin, LOW);
  107. nss.println("waking xbee");
  108. } else if (cmd == 51) { // 3 (ASCII)
  109. // send packet
  110. sendPacket();
  111. } else {
  112. nss.println("I didn't understand");
  113. }
  114. }
  115. readPacket();
  116. }
  117. // when XBee powers up it sends a modem status 0 (hardware reset), followed by a 2 (Joined Network), assumming it's configured correctly
  118. // when XBee is woken up it sends a modem status 2 (Joined Network)
  119. void readPacket() {
  120. xbee.readPacket();
  121. if (xbee.getResponse().isAvailable()) {
  122. // got something.. print packet to nss
  123. nss.print("API=");
  124. nss.print(xbee.getResponse().getApiId(), HEX);
  125. nss.print(",frame=");
  126. // print frame data
  127. for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
  128. nss.print(xbee.getResponse().getFrameData()[i], HEX);
  129. nss.print(" ");
  130. }
  131. nss.println("");
  132. } else if (xbee.getResponse().isError()) {
  133. nss.print("XBee error. error code is");
  134. nss.println(xbee.getResponse().getErrorCode(), DEC);
  135. }
  136. }