Hardware Reference
In-Depth Information
Continued from previous page.
void draw() {
// if you have new data and it's valid (>0), graph it:
if ((rssi > 0 ) && (rssi != lastReading)) {
// set the background:
background(0);
// set the bar height and width:
int rectHeight = rssi;
int rectWidth = 50;
// draw the rect:
stroke(23, 127, 255);
fill (23, 127, 255);
rect(width/2 - rectWidth, height-rectHeight, rectWidth, height);
// write the number:
text("XBee Radio Signal Strength test", 10, 20);
text("Received from XBee with address: " + hex(address), 10, 40);
text ("Received signal strength: -" + rssi + " dBm", 10, 60);
// save the current byte for next read:
lastReading = rssi;
}
}
void serialEvent(Serial XBee ) {
// read a byte from the port:
int thisByte = XBee.read();
// if the byte = 0x7E, the value of a start byte, you have
// a new packet:
if (thisByte == 0x7E) { // start byte
// parse the previous packet if there's data:
if (packet[2] > 0) {
rssi = parseData(packet);
}
// reset the byte counter:
byteCounter = 0;
}
// put the current byte into the packet at the current position:
packet[byteCounter] = thisByte;
// increment the byte counter:
byteCounter++;
}
/*
Once you've got a packet, you need to extract the useful data.
This method gets the address of the sender and RSSI.
*/
int parseData(int[] thisPacket) {
int result = -1; // if you get no result, -1 will indicate that.
// make sure you've got enough of a packet to read the data:
if (thisPacket.length > 6) {
ยป
 
Search WWH ::




Custom Search