For a point to point data transmission S1 modules are handy as they are capable of Direct I/O transmission.You can connect a sensor node like a temperature sensor or door switch to an I/O pin of S1 module and program the other module to follow the changes happening to that I/O pin.The changes reflect at the I/O pin of the distant radio.No external Microcontroller is required for a S1 Direct I/O.
This I/O functionality is not possible with S2 modules.But this can be achieved by reading the API frames transmitted by the sensor node .The XBee Radio at the sensor node should be configured as a ROUTER or END device & the data collecting XBee should be configured with COORDINATOR API Firmware.For parsing the data received & decision making , a Microcontroller is required at the Coordinator end.Arduino is the best choice as it has access to powerful open source libraries.
In this post we shall see how to collect data from a Digital input like a Door switch & how to parse the API frame data received from that sensor node .
Materials required :
1. XBEE S2 type Radio – 2 nos. 2. Door switch – Reed switch type 3.Arduino UNO
4.XBEE USB Breakout board for Xbee configuration & to 5.XBEE breakout board to host connect the Door switch Coordinator Radio
6. Jumper wires, LED, USB cable
A Door switch is used as Digital input.The switch has 2 rectangle parts.The one with wires has a Reed switch molded inside. When a magnet is brought near the reed switch , the normally open switch gets closed.The part without wire has a small piece of magnet inside , which makes the reed switch on.
The Door switch is connected to pin 20 (DIO_0) of distant xbee which is configured with Router AT firmware.
Configuration of XBEE Radios.
One of the Radios to be configured as Router.The Router radio is programmed with AT Firmware & the Destination address DH, DL is given as the Source address (Serial Number SH,SL) of Coordinator.
The PAN ID & Baud Rate should be same for both the Radios.
Pin 20 – DIO 0 of Router is to be connected to the Door switch.This pin is declared as DIGITAL INPUT.
Plug one radio on to the USB breakout & connect it to PC.Open the new XCTU program & click on the Search icon on the top.The radio is identified & 64 bit Address is displayed along with the Firmware loaded.
Click on the icon & update the Radio firmware as XB24-ZB / ZigBee Router AT.
The Destination address is the Source address of other XBee.Enter the address under DH , DL as shown.
Scroll down further & under I/O settings change the D0 AD0/DIO0 configuration to 3 which represents Digital Input.
Click on the Write button to record the changes done.
Scroll down further to set the IR Sampling Rate.Set this to hex value 1388 which equals 5000 in Decimal.i.e a digital sample is sent every 5000ms or 5 secs.
Plug the other Radio on the USB explorer.Click on the update firmware icon & load the
XB24-ZB / ZigBee Coordinator API firmware.
Ensure that the PANID (you can feed your own value ) & Baud Rate are the same for both the Radios.
The Destination address is the Source address of Router as shown below .
Scroll down further to set the API mode Enable (1).
Click on Write to record the changes.
Configuration of both Radios is completed now.
Connecting Coordinator Radio to Arduino
Remove the Coordinator Radio (you can make an ink mark as C to identify) from the USB board & plug it to the other break out board with LEDs.This has to be connected to Arduino UNO board as per connections shown below :
Pin 1 of breakout board is connected to 5v of Arduino & pin 10 to GND. As the breakout board has built in 3.3v regulator , its safe to provide 5v supply.Pin 2 (TX) of XBee breakout goes to pin 0 (Rx) of Arduino. Pin3 (RX) of XBee goes to pin 1 (Tx) of Arduino.
XBEE Breakout Arduino
PIN 1 ———> +5V
PIN 10 ———> GND
PIN 2 (TX) ———> PIN 0 (Rx)
PIN 3 (RX) ———> PIN 1 (Tx)
The Arduino board is connected to PC using USB cable.
The Router Radio is plugged on to the USB breakout. PIN 20 (ADIO 0) is connected to the door switch
While connecting the Cordinator radio to Arduino it is advisable to use a Resistor network between Tx of Arduino & Rx of XBEE.This ensures that the voltage level ( which is TTL (5V) at Arduino Tx ) is acceptable around 3.3v at Rx of XBEE.
The Arduino considers 3.3v level from Tx of XBEE as HIGH.So there is no need of Resistors between Tx of Xbee & Rx of Arduino.
Now open the Arduino IDE & select the port where Arduino is connected.Upload the following Sketch .
Remember to disconnect the wires at Rx/Tx pins of Arduino while uploading the Sketch.
——————————–
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()>=21){
for(int i=0;i<22;i++){
Serial.print(Serial.read(),HEX);
Serial.print(",");
}
Serial.println();
}
}
————————————–
Now power up the Router by connecting the USB cable to the XBee USB breakout.Remember to switch on the Coordinator first & then the Router.
As soon as the Router joins the Coordinator , it starts sending Digital samples collected from pin 20 every 5 secs.
Open the Serial monitor of Arduino to view the API Frames received.
Let us analyze the API Frame data received from the Router where the Door switch is connected :
7E 0 12 92 0 13 A2 0 40 A9 C5 92 79 9 1 1 0 1 0 0 0 F3
The API Frame consists of total 22 bytes (counted from 0 to 21) starting with a Delimiter 7E.
The next 2 bytes represent the length of frame starting from Frame type & ending before Checksum.
In our sample here its 0 12 .The second byte 0x12 in HEX is equal to 18 in Decimal.You can see total 18 bytes after this value 0x12 & before the checksum F3.
The next byte 92 is the API Frame type , Hex value 92 represents “ RX I/O Data Received “
The other Frame types are as seen below.
Byte no. 4 to 11 represent the 64 bit address from where the data sample is received.
Byte 12 (MSB) & 13 (LSB) is the 16 bit address of sender.
Byte 14 represents Receive options. 0x01 is for packet acknowledged & 0x02 means the packet is a Broadcast one.
Byte 15 represents Number of Sample sets included in the payload which is always set to 1.
Bytes 16 & 17 represent the Digital channel mask.Indicates which Digital I/O lines on the remote radio have sampling enabled.In our case we’ve enabled D0 as Digital IN.
The first byte of channel mask is read as per following table
n/a n/a n/a D12 D11 D10 n/a n/a
n/a means not available or not defined.
The second byte is read as :
D7 D6 D5 D4 D3 D2 D1 D0
In our sample the data reads 0 1 which in binary is 0000 0000 0000 0001
i.e the bit D0 only is enabled.
Byte 18 is the Bit mask field that indicates which analog I/O lines on the remote radio have analog sampling.In our sample it is 0 , i.e No analog samples.
Bytes 19 & 20 deliver the actual Data samples. The data is mapped same as in the Digital channel mask field . x x x D12 D11 D10 x x D7 D6 D5 D4 D3 D2 D1 D0
If you receive 0x0 in the first digital sample byte & 0x1 in the second one, that indicates high voltage (level 1) is being received only on pin D0.
The last one is the checksum which is the 8 bit sum of bytes from offset 3 to this byte.
Note that if Analog lines are enabled you receive another single byte before this checksum representing the Analog data.
————————–
You can watch the byte no. 20 which is the last but one in the Frame data.This changes to 1 when the Door switch is closed & changes to 0 when the door switch is open.
Now we shall upload a little modified code on to Arduino.
————————————————
//xbee digital receive
int readValue=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() >=21) {
if (Serial.read() == 0x7E){
//skip over the API frame bytes we don’t want
for (int i =0; i<19; i++) {
byte skip = Serial.read();
}
readValue = Serial.read();
Serial.print("Door is : ");
if(readValue == 0 ){
Serial.println("open");
}else if (readValue == 1) {
Serial.println("closed");
}
}
}
}
————————————
If you monitor the serial port of Arduino you can get the data whether Door is closed or open
Watch this support video ;
Thank you for this excellent application of XBEEs and digital sampling!
It is very suitable for a Lab session in our “Advanced Microcontroller class.
I am a professor of Electronics at Fairmont State University in WV.