Xbee Remote Blink Example
Hello all. After spending countless hours reading over Xbee material, untold examples, including the Digi examples at:
http://examples.digi.com/category/get-started/
and all the Youtube vids by tunnelsup plus his Xbee S2 Quick Reference Guide, I finally have something to
contribute to help others who may be struggling like me.
I was suucessful at using the Digi 'Getting Started' examples, but I could not get any of the examples by tunnelsup to work because I did not understand what I was doing with the tools I had. Since I have a lot of the Xbee S1's I own, I needed to get these to function right for me no matter what. The race was on.
Also, I own both the Arduino Uno and the YUN. This example is based strictly for the Uno, but should be easy to convert to the YUN. I just haven't done it yet.
What do you need for this project?
1 ea Uno, plus USB connector cable for loading the program sketch
2 ea Xbee S1's (be sure to have antennas of some sort if you want range)
2 ea breakout boards (one for each Xbee-I am using the regulated ones from Sparkfun)
1 ea USB programming controller (mine is a cheap Filopino knockoff bought off Ebay)
http://www.ebay.com/itm/XBee-Explorer-Xbee-USB-Mini-Adapter-Module-Board-Base-Shield-Multifunction-New-/231326657750?pt=LH_DefaultDomain_0&hash=item35dc24acd6
X-CTU programming software-download free from Digi-
1 breadboard with power source for the remote Xbee
A handful of breadboard wires
1 led -any color
1 resistor for the led-220-ohm resistor to 330 ohm resistor
Wires and soldering tool if you want to make life easier for yourself-more on this later
I think it would be wise to see the videos from tunnelsup on Youtube to see how he sets up the breadboards and
solder wires onto the Xbees and Uses the X-CTU software. I won't go over all that. Start here at the first video
http://www.youtube.com/watch?v=odekkumB3WQ.
Please do not program your Xbee S1's according to his video or the code below will not work for you. His are S2 anyways and the X-CTU programming is slightly different, but be forewarned.
On to the Project
Setting up my Xbees-
- One is to be a Coordinator
- One is to be an Endpoint or Remote
- Each one has to be programmed accordingly
Programming using the newest X-CTU software and the USB programming controller is simple and almost painless.
Caveat- be sure the direction of the Xbee matches both the breakout board outline and the programming board outline.
I will not go over every single command available as it is way over my head and not necessary for what I am
showing, but again tunnelsup does a very good job showing some choices and the Digi
http://examples.digi.com/category/get-started/ is so helpful.
First program the Coordinator Xbee in API mode. I prefer to start in an unpowered state, then plug in to my USB port
PanID--choose any numbers appropriate to your example up to 0xFFFF-each Xbee should have the same number though
DH = 0
DL = 2
MY = 1
API = enabled
Next the Endpoint or Remote Xbee in AP mode
PanID - set same as above
DH = 0
DL = 1
MY = 2
IA = FFFF need this to be able to have another Xbee change pin settings
D5 = 4 -set to digital output low initially- will be controlled by our Arduino sketch thereafter
Be sure to use the Write function in X-CTU to get all these changes set on each of the Xbee S1's
The Sketch- modify as you see fit
//beginning of remote xbee blink
int led = 13; //this is merely used as a condition reached indicator on the Uno board only
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(led,HIGH); //turn on Uno LED
setRemoteState(0x5); //turning on the remote LED by Xbee for 5 seconds
delay(5000);
digitalWrite(led, LOW); //turn off Uno LED
setRemoteState(0x4); //turning off the remote LED by Xbee for 5 seconds
delay(5000);
}
//this function is used directly from tunnelsup example on Youtube-only modified with my code needs
void setRemoteState(char value) {
//what you are doing here is creating an API frame according to Digi standards and manipulating the remote Xbee
//based on what you desire--in this case, to change the HIGH/LOW state of the digital output pin D5
//Any number of functions like this could be created to deal with different conditions that may need to be
//addressed by your desires within your sketch.
Serial.write(0x7E); //most commenting will be done below-but you can also read the XBee S2 Reference Guide by tunnelsup
Serial.write((byte)0x0);
Serial.write(0x10);
Serial.write(0x17);
Serial.write((byte)0x0); //Frame ID-not used
Serial.write((byte)0x0);//start of first 4 bytes of 64 bit address =DH
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0); //start of 2nd 4 bytes of 64 bit address = DL
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write(0x01); //set to the DL of the preprogrammed Endpoint or Remote Xbee
Serial.write((byte)0x0); //start of 16 bit address
Serial.write(0x02); //MY set to 2 of the preprogrammed Endpoint or Remote Xbee
Serial.write(0x02); //write AT commands
Serial.write(0x44); //0x44 == 'D'-pin type
Serial.write(0x35); //0x35 == '5' -pin number
Serial.write(value);
//value is passed through the function when called
long sum = 0x17 + 0x01 + 0x02 + b 0x02 + 0x44 + 0x35 + value;
//calculates checksum--the sum of the above addition is And'ed with 0xFF, then subtracted from 0xFF
Serial.write( 0xFF - (sum & 0xFF) );
}
//end of remote xbee blink
have to post in 2 pieces r/t length requirements
houdinihar