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.

103 lines
2.7KB

  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. /*
  21. This example is for Series 2 XBee
  22. Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
  23. */
  24. // create the XBee object
  25. XBee xbee = XBee();
  26. uint8_t payload[] = { 0, 0 };
  27. // SH + SL Address of receiving XBee
  28. XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
  29. ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  30. ZBTxStatusResponse txStatus = ZBTxStatusResponse();
  31. int pin5 = 0;
  32. int statusLed = 13;
  33. int errorLed = 13;
  34. void flashLed(int pin, int times, int wait) {
  35. for (int i = 0; i < times; i++) {
  36. digitalWrite(pin, HIGH);
  37. delay(wait);
  38. digitalWrite(pin, LOW);
  39. if (i + 1 < times) {
  40. delay(wait);
  41. }
  42. }
  43. }
  44. void setup() {
  45. pinMode(statusLed, OUTPUT);
  46. pinMode(errorLed, OUTPUT);
  47. Serial.begin(9600);
  48. xbee.setSerial(Serial);
  49. }
  50. void loop() {
  51. // break down 10-bit reading into two bytes and place in payload
  52. pin5 = analogRead(5);
  53. payload[0] = pin5 >> 8 & 0xff;
  54. payload[1] = pin5 & 0xff;
  55. xbee.send(zbTx);
  56. // flash TX indicator
  57. flashLed(statusLed, 1, 100);
  58. // after sending a tx request, we expect a status response
  59. // wait up to half second for the status response
  60. if (xbee.readPacket(500)) {
  61. // got a response!
  62. // should be a znet tx status
  63. if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
  64. xbee.getResponse().getZBTxStatusResponse(txStatus);
  65. // get the delivery status, the fifth byte
  66. if (txStatus.getDeliveryStatus() == SUCCESS) {
  67. // success. time to celebrate
  68. flashLed(statusLed, 5, 50);
  69. } else {
  70. // the remote XBee did not receive our packet. is it powered on?
  71. flashLed(errorLed, 3, 500);
  72. }
  73. }
  74. } else if (xbee.getResponse().isError()) {
  75. //nss.print("Error reading packet. Error code: ");
  76. //nss.println(xbee.getResponse().getErrorCode());
  77. } else {
  78. // local XBee did not provide a timely TX Status Response -- should not happen
  79. flashLed(errorLed, 2, 50);
  80. }
  81. delay(1000);
  82. }