Hi,
Just gonna round up the last bit and thank everyone for helping out!
This is my transmitter/router code to send data. I've made it such that it's easy to do debugging on Serial by changing some of the codes.
It basically send 'aa' over. Thanks to Paul's SoftwareSerial codes, I can line the Xbee on pin8 and 9. =)
#include <XBee.h>
#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
*/
char data = 'a';
// Connect Arduino pin 8 to TX of usb-serial device
uint8_t ssRX = 8;
// Connect Arduino pin 9 to RX of usb-serial device
uint8_t ssTX = 9;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);
// create the XBee object
XBee xbee = XBee();
uint8_t payload[] = { 0, 0 };
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4079a7f9);
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 setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
nss.begin(9600);
Serial.begin(9600);
xbee.setSerial(nss);
}
void loop() {
// break down 10-bit reading into two bytes and place in payload
payload[0] = data & 0xff;
payload[1] = data & 0xff;
//pin5 = analogRead(5);
//payload[0] = pin5 >> 8 & 0xff;
//payload[1] = pin5 & 0xff;
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, 5, 50);
} else {
// the remote XBee did not receive our packet. is it powered on?
flashLed(errorLed, 3, 500);
}
}
} else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
} else {
// local XBee did not provide a timely TX Status Response -- should not happen
flashLed(errorLed, 2, 50);
}
delay(1000);
}
And this is for the receiver. I notice Dray did something similar in the past when I was surfing the web. Great minds think alike!
#include <XBee.h>
#include <SoftwareSerial.h>
/*
This example is for Series 2 XBee
Receives a ZB RX packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
// Define NewSoftSerial TX/RX pins
// Connect Arduino pin 8 to TX of usb-serial device
uint8_t ssRX = 8;
// Connect Arduino pin 9 to RX of usb-serial device
uint8_t ssTX = 9;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);
void setup() {
// start serial
Serial.begin(9600);
nss.begin(9600);
xbee.setSerial(nss);
Serial.println("starting up here!");
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
Serial.println("Got an rx packet!");
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
Serial.println("packet acknowledged");
} else {
Serial.println("packet not acknowledged");
}
Serial.print("checksum is ");
Serial.println(rx.getChecksum(), HEX);
Serial.print("packet length is ");
Serial.println(rx.getPacketLength(), DEC);
for (int i = 0; i < rx.getDataLength(); i++) {
Serial.print("payload [");
Serial.print(i, DEC);
Serial.print("] is ");
Serial.println(rx.getData()[i], HEX);
}
for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
Serial.print("frame data [");
Serial.print(i, DEC);
Serial.print("] is ");
Serial.println(xbee.getResponse().getFrameData()[i], HEX);
}
}
} else if (xbee.getResponse().isError()) {
Serial.print("oh no!!! error code:");
Serial.println(xbee.getResponse().getErrorCode());
}
}
And I will add that I use xbee-python library to solve the api problem on PC. It was really good and easy to use and I love PYTHON! =)