PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Series2_IoSamples.pde 3.5KB

3 lat temu
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. #include <SoftwareSerial.h>
  21. /*
  22. This example is for Series 2 (ZigBee) XBee Radios only
  23. Receives I/O samples from a remote radio.
  24. The remote radio must have IR > 0 and at least one digital or analog input enabled.
  25. The XBee coordinator should be 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. ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();
  39. XBeeAddress64 test = XBeeAddress64();
  40. void setup() {
  41. Serial.begin(9600);
  42. xbee.setSerial(Serial);
  43. // start soft serial
  44. nss.begin(9600);
  45. }
  46. void loop() {
  47. //attempt to read a packet
  48. xbee.readPacket();
  49. if (xbee.getResponse().isAvailable()) {
  50. // got something
  51. if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) {
  52. xbee.getResponse().getZBRxIoSampleResponse(ioSample);
  53. nss.print("Received I/O Sample from: ");
  54. nss.print(ioSample.getRemoteAddress64().getMsb(), HEX);
  55. nss.print(ioSample.getRemoteAddress64().getLsb(), HEX);
  56. nss.println("");
  57. if (ioSample.containsAnalog()) {
  58. nss.println("Sample contains analog data");
  59. }
  60. if (ioSample.containsDigital()) {
  61. nss.println("Sample contains digtal data");
  62. }
  63. // read analog inputs
  64. for (int i = 0; i <= 4; i++) {
  65. if (ioSample.isAnalogEnabled(i)) {
  66. nss.print("Analog (AI");
  67. nss.print(i, DEC);
  68. nss.print(") is ");
  69. nss.println(ioSample.getAnalog(i), DEC);
  70. }
  71. }
  72. // check digital inputs
  73. for (int i = 0; i <= 12; i++) {
  74. if (ioSample.isDigitalEnabled(i)) {
  75. nss.print("Digital (DI");
  76. nss.print(i, DEC);
  77. nss.print(") is ");
  78. nss.println(ioSample.isDigitalOn(i), DEC);
  79. }
  80. }
  81. // method for printing the entire frame data
  82. //for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
  83. // nss.print("byte [");
  84. // nss.print(i, DEC);
  85. // nss.print("] is ");
  86. // nss.println(xbee.getResponse().getFrameData()[i], HEX);
  87. //}
  88. }
  89. else {
  90. nss.print("Expected I/O Sample, but got ");
  91. nss.print(xbee.getResponse().getApiId(), HEX);
  92. }
  93. } else if (xbee.getResponse().isError()) {
  94. nss.print("Error reading packet. Error code: ");
  95. nss.println(xbee.getResponse().getErrorCode());
  96. }
  97. }