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.

113 lines
3.5KB

  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 XBee Radios only
  23. Receives I/O samples from a remote radio with 16-bit addressing.
  24. The remote radio must have IR > 0, at least one digital or analog input enabled
  25. and DL set to the 16-bit address of the receiving XBee (the one connected to the Arduino).
  26. This example uses the SoftSerial library to view the XBee communication. I am using a
  27. Modern Device USB BUB board (http://moderndevice.com/connect) and viewing the output
  28. with the Arduino Serial Monitor.
  29. */
  30. // Define NewSoftSerial TX/RX pins
  31. // Connect Arduino pin 8 to TX of usb-serial device
  32. uint8_t ssRX = 8;
  33. // Connect Arduino pin 9 to RX of usb-serial device
  34. uint8_t ssTX = 9;
  35. // Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
  36. SoftwareSerial nss(ssRX, ssTX);
  37. XBee xbee = XBee();
  38. Rx16IoSampleResponse ioSample = Rx16IoSampleResponse();
  39. // 64-bit response is same except api id equals RX_64_IO_RESPONSE and returns a 64-bit address
  40. //Rx64IoSampleResponse ioSample = Rx64IoSampleResponse();
  41. void setup() {
  42. Serial.begin(9600);
  43. xbee.setSerial(Serial);
  44. // start soft serial
  45. nss.begin(9600);
  46. }
  47. void loop() {
  48. //attempt to read a packet
  49. xbee.readPacket();
  50. if (xbee.getResponse().isAvailable()) {
  51. // got something
  52. if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
  53. xbee.getResponse().getRx16IoSampleResponse(ioSample);
  54. nss.print("Received I/O Sample from: ");
  55. nss.println(ioSample.getRemoteAddress16(), HEX);
  56. nss.print("Sample size is ");
  57. nss.println(ioSample.getSampleSize(), DEC);
  58. if (ioSample.containsAnalog()) {
  59. nss.println("Sample contains analog data");
  60. }
  61. if (ioSample.containsDigital()) {
  62. nss.println("Sample contains digtal data");
  63. }
  64. for (int k = 0; k < ioSample.getSampleSize(); k++) {
  65. nss.print("Sample ");
  66. nss.print(k + 1, DEC);
  67. nss.println(":");
  68. for (int i = 0; i <= 5; i++) {
  69. if (ioSample.isAnalogEnabled(i)) {
  70. nss.print("Analog (AI");
  71. nss.print(i, DEC);
  72. nss.print(") is ");
  73. nss.println(ioSample.getAnalog(i, k));
  74. }
  75. }
  76. for (int i = 0; i <= 8; i++) {
  77. if (ioSample.isDigitalEnabled(i)) {
  78. nss.print("Digtal (DI");
  79. nss.print(i, DEC);
  80. nss.print(") is ");
  81. nss.println(ioSample.isDigitalOn(i, k));
  82. }
  83. }
  84. }
  85. }
  86. else {
  87. nss.print("Expected I/O Sample, but got ");
  88. nss.print(xbee.getResponse().getApiId(), HEX);
  89. }
  90. }
  91. else if (xbee.getResponse().isError()) {
  92. nss.print("Error reading packet. Error code: ");
  93. nss.println(xbee.getResponse().getErrorCode());
  94. }
  95. }