Final Processing Code
/* Xbee Signal Strength Reader
Language: Processing
Reads a packet from an Xbee radio and parses it. The packet
should be 22 bytes long. It should be made up of the following:
byte 1: 0x7E, the start byte value
byte 2-3: packet size, a 2-byte value (not used here)
byte 4: API identifier value, a code that says what this response is (not used here)
byte 5-6: Sender's address
byte 7: RSSI, Received Signal Strength Indicator (not used here)
byte 8: Broadcast options (not used here)
byte 9: Number of samples to follow
byte 10-11: Active channels indicator (not used here)
byte 12-21: 5 10-bit values, each ADC samples from the sender
Created 3 Mar. 2007
by Tom Igoe
- modified for 4 XBees by David Steele Overholt on 5 May, 2008
*/
import processing.serial.*;
PImage logo;
PImage sensors;
PImage bag1;
PImage bag2;
PImage bag3;
Serial xbee; // input serial port from the Xbee Radio
int[] packet = new int[40]; // with 5 samples, the Xbee packet is 22 bytes long
int byteCounter; // keeps track of where you are in the packet
int rssi1 = 0; // received signal strength
int rssi2 = 0; // received signal strength
int rssi3 = 0; // received signal strength
int address = 0; // the sending Xbee’s address
Serial myPort; // The serial port
// int lastReading = 0; // value of the previous incoming byte
void setup () {
size(1440, 800, P3D); // window size
// higher framerate helps get more.
frameRate(200);
// get fullscreen exclusive mode
//setFullScreen( true );
logo = loadImage(”logo.gif”); // 493 x 148 px
sensors = loadImage(”sensors.gif”); // 493 x 148 px
bag1 = loadImage(”bag1.jpg”); // 700 x 662 px
bag2 = loadImage(”bag2.jpg”); // 700 x 569 px
bag3 = loadImage(”bag3.jpg”); // 494 x 700 px
// create a font with the second font available to the system:
PFont myFont = loadFont(”Monaco-36.vlw”);
textFont(myFont, 36);
// get a list of the serial ports:
println(Serial.list());
// open the serial port attached to your Xbee radio:
xbee = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
// set the background:
background(#121012);
//place the logo
image(logo, 400, 0);
image(sensors, 1100, 20);
// set the bar height and width:
// rssi should range from 92 to 0:
int rectHeight1 = 430 - rssi1;
int rectHeight2 = 430 - rssi2;
int rectHeight3 = 430 - rssi3;
int rectWidth = 230;
//place the bags
//image(bag1, 130, height-rectHeight1 - 330);
//image(bag2, 530, height-rectHeight2 - 300);
//image(bag3, 970, height-rectHeight3 - 330);
// draw the rect:
// stroke(23, 127, 255);
fill (142, 42, 20);
rect(200, height-rectHeight1, rectWidth, height);
rect(600, height-rectHeight2, rectWidth, height);
rect(1000, height-rectHeight3, rectWidth, height);
//write the names:
fill (192, 173, 153);
//text(”Xbee Radio Signal Strength test”, 10, 20);
text(”The Dame”, 225, height-rectHeight1 - 40);
text(”The Hussy”, 615, height-rectHeight2 - 40);
text(”The Punk”, 1020, height-rectHeight3 - 40);
/*
text (rssi1, 45, 70);
text (rssi2, 145, 70);
text (rssi3, 245, 70);
*/
}
void serialEvent(Serial xbee) {
// read a byte from the port:
int thisByte = xbee.read();
// if the byte = 0×7E, the value of a start byte, you have a new packet:
if (thisByte == 0×7E) { // start byte
// parse the previous packet if there’s data:
if (packet[2] > 0) {
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:
println(”byteCounter: ” + byteCounter + ” thisByte: ” + thisByte);
byteCounter++;
}
/*
Once you’ve got a packet, you need to extract the useful data.
This method gets the address of the sender and RSSI.
*/
void parseData(int[] thisPacket) {
// read the address. It’s a two-byte value, so you
// add the two bytes as follows:
address = thisPacket[5] + thisPacket[4] * 256;
if(address==1) {
// get RSSI:
rssi1 = thisPacket[6];
}
else if (address==2) {
rssi2 = thisPacket[6];
}
else if (address==3) {
rssi3 = thisPacket[6];
}
}
Final AT Commands
ATID
your-own-number-goes-hereATDH
0ATDL
FFFFATMY
1ATIT
1ATIR
3E8ATNT
19ATSC
1FFEATSD
4ATCA
2CATST
1388ATDP
3E8ATBD
3ATRO
3ATPR
FFATD0
3ATD1
0ATD7
1ATD5
1ATD4
0
5-8-2008
Things are working well now! I had a presentation for another class during the beginning of the week and have had to put this project on hold for a couple days, but I spent a bit of time yesterday on the code, got fed up and revisited it this morning. I’ve discovered the best way to make sure I’m receiving everything fluidly I need to connect to the XBee’s command mode, obtain the RSSI (it only gives out one at a time), then disconnect so it can start broadcasting to the other XBees again. I’m receiving some stray variables (131, 126 and 0) so I’ve done some simple error checking (if value != 131, 126, or 0). I’m not sure where the values are coming from (I’m assuming start and end bytes mixed in), but this works for now. Hopefully I can compile something at the end that’s more usable for other applications. It should be really helpful for myself and others down the road. I’ve also added a toggle switch so the bag can be turned on/off so it’s not clogging up the lines or playing audio when not being used, as well as a reset button on the outside of my project case for the Arduino.
The XBees were taking a while to connect to my serial commands, so I re-wrote some of the code I got from Daniel Liss to clean up how often it sends packets.
On the Processing front, I’ve taken TIgoe’s XBee sensor meter and extended it to be used with multiple (4) XBees. I plan on showing this at the show on a monitor so people can have a direct view of how they’re effecting the network.
5-5-2008
While writing my code I’ve run in to a bit of a problem of knowing how to get all of the XBee RSSI signals with their addresses all at once. I also just realized for the current version I have the option of not reading the sending XBee’s address and only playing one at a time - this way it cuts down tremendously on the amount of parsing I have to do. Again, not what I was wanting to do, but in the show environment all it has to do is bitch about a bag based on proximity. Sometimes it will be the same bag, sometimes another.
