Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
weatherbox:team_nova:xbee_code [2016/03/18 01:10] dtokita created |
weatherbox:team_nova:xbee_code [2021/09/19 21:59] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | /** | ||
+ | * Copyright (c) 2009 Andrew Rapp. All rights reserved. | ||
+ | * | ||
+ | * This file is part of XBee-Arduino. | ||
+ | * | ||
+ | * XBee-Arduino is free software: you can redistribute it and/or modify | ||
+ | * it under the terms of the GNU General Public License as published by | ||
+ | * the Free Software Foundation, either version 3 of the License, or | ||
+ | * (at your option) any later version. | ||
+ | * | ||
+ | * XBee-Arduino is distributed in the hope that it will be useful, | ||
+ | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
+ | * GNU General Public License for more details. | ||
+ | * | ||
+ | * You should have received a copy of the GNU General Public License | ||
+ | * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>. | ||
+ | */ | ||
- | #include <SoftwareSerial.h> //Using SoftwareSerial library | + | #include <XBee.h> |
- | SoftwareSerial XBee(2, 3); // RX (Receiving), TX (Transmitting) | + | //#include <SoftwareSerial.h> |
+ | /* | ||
+ | This example is for Series 2 XBee | ||
+ | Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success | ||
+ | */ | ||
- | //Set PAN ID to: DAD on both Xbees using XCTU | + | //SoftwareSerial Xbee(2,3); |
- | //Set Destination Address Low to: FFFF on both Xbees using XCTU | + | |
- | void setup() | + | // create the XBee object |
- | { | + | XBee xbee = XBee(); |
- | XBee.begin(9600); //Start the Xbee at 9600 baud | + | |
- | Serial.begin(9600); //Start the serial port at 9600 | + | uint8_t payload[] = { 0, 0 }; |
- | Serial.println("Initializing Xbee"); //Debug to show initialization | + | |
+ | // SH + SL Address of receiving XBee | ||
+ | XBeeAddress64 addr64 = XBeeAddress64(0x00, 0x00); | ||
+ | ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload)); | ||
+ | ZBTxStatusResponse txStatus = ZBTxStatusResponse(); | ||
+ | |||
+ | int pin5 = 0; | ||
+ | |||
+ | int statusLed = 13; | ||
+ | int errorLed = 13; | ||
+ | |||
+ | void flashLed(int pin, int times, int wait) { | ||
+ | |||
+ | for (int i = 0; i < times; i++) { | ||
+ | digitalWrite(pin, HIGH); | ||
+ | delay(wait); | ||
+ | digitalWrite(pin, LOW); | ||
+ | |||
+ | if (i + 1 < times) { | ||
+ | delay(wait); | ||
+ | } | ||
+ | } | ||
} | } | ||
- | void loop() | + | void setup() { |
- | { | + | pinMode(statusLed, OUTPUT); |
- | if (Serial.available()) //If Data is inputted into the serial monitor | + | pinMode(errorLed, OUTPUT); |
- | { | + | |
- | Serial.print("Sending to Xbee..."); | + | Serial.begin(9600); |
- | while(Serial.available()){ | + | xbee.setSerial(Serial); |
- | XBee.write(Serial.read()); //Send it to the Xbee to transmit out | + | } |
- | //Serial.println("Sending to XBee"); //Debug | + | |
+ | void loop() { | ||
+ | // break down 10-bit reading into two bytes and place in payload | ||
+ | pin5 = analogRead(5); | ||
+ | payload[0] = '2'; | ||
+ | payload[1] = '5'; | ||
+ | |||
+ | xbee.send(zbTx); | ||
+ | |||
+ | // flash TX indicator | ||
+ | flashLed(statusLed, 1, 100); | ||
+ | |||
+ | // after sending a tx request, we expect a status response | ||
+ | // wait up to half second for the status response | ||
+ | if (xbee.readPacket(500)) { | ||
+ | // got a response! | ||
+ | |||
+ | // should be a znet tx status | ||
+ | if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) { | ||
+ | xbee.getResponse().getZBTxStatusResponse(txStatus); | ||
+ | |||
+ | // get the delivery status, the fifth byte | ||
+ | if (txStatus.getDeliveryStatus() == SUCCESS) { | ||
+ | // success. time to celebrate | ||
+ | flashLed(statusLed, 10, 50); | ||
+ | } else { | ||
+ | // the remote XBee did not receive our packet. is it powered on? | ||
+ | flashLed(errorLed, 3, 500); | ||
+ | } | ||
} | } | ||
+ | } else if (xbee.getResponse().isError()) { | ||
+ | //nss.print("Error reading packet. Error code: "); | ||
+ | //nss.println(xbee.getResponse().getErrorCode()); | ||
+ | } else { | ||
+ | // local XBee did not provide a timely TX Status Response -- should not happen | ||
+ | flashLed(errorLed, 2, 50); | ||
} | } | ||
- | if (XBee.available()) //If Data is inputted into the Xbee | + | |
- | { | + | delay(1000); |
- | Serial.write(XBee.read()); //Recieve it from the Xbee to print on serial monitor | + | |
- | //Serial.println("Reading from Xbee"); //Debug | + | |
- | } | + | |
} | } | ||