Hardware Reference
In-Depth Information
• Byte 5-6: XBee sender's address.
• Byte 7: Received Signal Strength Indicator (RSSI).
• Byte 8: Broadcast options (not used here).
• Byte 9: Number of samples in the packet (you set it to 5
using the IT command shown earlier).
• Byte 10-11: Which I/O channels are currently being
used. This example assumes only one analog channel,
AD0, and no digital channels are in use.
• Byte 12-21: 10-bit values, each ADC sample from the
sender. Each 10-bit sample represents a voltage from 0
to the XBee's maximum of 3.3V, so a value of 0 means 0
volts, and a value of 1023 means 3.3 volts. Each point of
the sensor's value is then 3.3/1024, or 0.003 volts.
Because every packet starts with a constant value, 0x7E
(that's decimal value 126), you can start by looking for that
value.
X
Read It
The following
sketch reads
bytes and puts them in an array until it
sees the value 0x7E. Then, it parses the
array to find the sensor's value. To start,
you need the message length (or packet
length ), an array, and a counter to know
where you are in the array. Then, you
need to set up serial communications.
/*
XBee message reader
Context: Arduino
*/
const int packetLength = 22; // XBee data messages will be 22 bytes long
int dataPacket[packetLength]; // array to hold the XBee data
int byteCounter = 0; // counter for bytes received from the XBee
void setup() {
// start serial communications:
Serial.begin(9600);
}
8
The main loop doesn't do much
in this sketch; it just calls a method,
listenToSerial() , that goes through the
bytes and does the work.
void loop() {
// listen for incoming serial data:
if (Serial.available() > 0) {
listenToSerial();
}
}
8
To start with, just read the bytes in
and look for the value 0x7E. When you
see it, print a linefeed. No matter what,
print the byte values separated by a
space.
void listenToSerial() {
// read incoming byte:
int inByte = Serial.read();
// beginning of a new packet is 0x7E:
if (inByte == 0x7E ) {
Serial.println();
}
// print the bytes:
Serial.print(inByte, DEC);
Serial.print(" ");
}
NOTE: In this circuit, you didn't connect the
Arduino's transmit pin to the XBee's receive
pin on purpose. This way, you can use the
serial connection both to receive from the
XBee and to send debugging information to
the Serial Monitor.
 
Search WWH ::




Custom Search