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.

Series1_Tx.pde 3.6KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 1 XBee
  22. Sends a TX16 or TX64 request with the value of analogRead(pin5) and checks the status response for success
  23. Note: In my testing it took about 15 seconds for the XBee to start reporting success, so I've added a startup delay
  24. */
  25. XBee xbee = XBee();
  26. unsigned long start = millis();
  27. // allocate two bytes for to hold a 10-bit analog reading
  28. uint8_t payload[] = { 0, 0 };
  29. // with Series 1 you can use either 16-bit or 64-bit addressing
  30. // 16-bit addressing: Enter address of remote XBee, typically the coordinator
  31. Tx16Request tx = Tx16Request(0x1874, payload, sizeof(payload));
  32. // 64-bit addressing: This is the SH + SL address of remote XBee
  33. //XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4008b490);
  34. // unless you have MY on the receiving radio set to FFFF, this will be received as a RX16 packet
  35. //Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload));
  36. TxStatusResponse txStatus = TxStatusResponse();
  37. int pin5 = 0;
  38. int statusLed = 11;
  39. int errorLed = 12;
  40. void flashLed(int pin, int times, int wait) {
  41. for (int i = 0; i < times; i++) {
  42. digitalWrite(pin, HIGH);
  43. delay(wait);
  44. digitalWrite(pin, LOW);
  45. if (i + 1 < times) {
  46. delay(wait);
  47. }
  48. }
  49. }
  50. void setup() {
  51. pinMode(statusLed, OUTPUT);
  52. pinMode(errorLed, OUTPUT);
  53. Serial.begin(9600);
  54. xbee.setSerial(Serial);
  55. }
  56. void loop() {
  57. // start transmitting after a startup delay. Note: this will rollover to 0 eventually so not best way to handle
  58. if (millis() - start > 15000) {
  59. // break down 10-bit reading into two bytes and place in payload
  60. pin5 = analogRead(5);
  61. payload[0] = pin5 >> 8 & 0xff;
  62. payload[1] = pin5 & 0xff;
  63. xbee.send(tx);
  64. // flash TX indicator
  65. flashLed(statusLed, 1, 100);
  66. }
  67. // after sending a tx request, we expect a status response
  68. // wait up to 5 seconds for the status response
  69. if (xbee.readPacket(5000)) {
  70. // got a response!
  71. // should be a znet tx status
  72. if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
  73. xbee.getResponse().getZBTxStatusResponse(txStatus);
  74. // get the delivery status, the fifth byte
  75. if (txStatus.getStatus() == SUCCESS) {
  76. // success. time to celebrate
  77. flashLed(statusLed, 5, 50);
  78. } else {
  79. // the remote XBee did not receive our packet. is it powered on?
  80. flashLed(errorLed, 3, 500);
  81. }
  82. }
  83. } else if (xbee.getResponse().isError()) {
  84. //nss.print("Error reading packet. Error code: ");
  85. //nss.println(xbee.getResponse().getErrorCode());
  86. // or flash error led
  87. } else {
  88. // local XBee did not provide a timely TX Status Response. Radio is not configured properly or connected
  89. flashLed(errorLed, 2, 50);
  90. }
  91. delay(1000);
  92. }