Xbee series 1, Arduino, serial data transmit and receive.

Hello,

I want to get sensor data from a remote xbee by using an IS (forced sample) api command. The rcvr (xbee1) is connected to an arduino mega. The xmtr is xbee2 (sensors) Try as I might, and reading Jon Titus’ book on xbees, I’ve had no luck. My goal is to force a sample transmission with the IS command. Take this data and parse it out for the sensor bytes. My ultimate goal is to have 4 independent xbees sending sensor data to the receiving xbee attached to the arduino. Can you please help me? Here is my code.

/* If there is no data in the reciever buffer an "H" prints out.  I can't seem to get data into
the buffer as the result of this program is a printing of the "H". 
   
   Materials:  2 xbee series 1 in API mode, factory settings.  Do--D3 are activated on xbee2 (sensors are here)
                   1 arduino mega.  Mega Serial 0 Tx connected to xbee1 Din
                                                                 Rx connected to xbee1 Dout
                   1 logic level converter bi directional 3.3 to 5 volts between xbee1 and mega
*/   

byte IScmd[] = {0x7E,0x00,0x0F,0x17,0x53,0x00,0x13,0xA2,0x00,        //API command byte, from the mega to Xbee1 then transmitted to xbee2, I S (49,53) equals forced sample
                0x40,0x81,0x82,0x0D,0xFF,0xFE,0x02,0x49,0x53,0xF5};
                
byte Rcvd[27];  // array; data received from xbee2.  Using D0,D1,D2,D3 on xbee2

void setup()
{
  Serial.begin(9600);  //using arduino mega to avoid any serial write/read conflicts
  Serial1.begin(9600);
}

void loop()
{
     for(int x = 0; x < 19; x++)   //sends the command byte from mega to xbee#1 which transmits to xbee#2
       {
         Serial.write(IScmd[x]);
         delay(1);
       }
      
  while(Serial.available() == 0) //program stops here if nothing in buffer.
    {
      Serial.print("H"); //lets me know nothing is there yet.
      Serial.println();
      delay(100);
    }
    
    
  if(Serial.available() >= 20) //data is in the buffer and  waits until a mouthful of data in the buffer to read
    {
      if(Serial.read() == 0x7E)
        {
          for(int n=1; n< 27; n++)  // reads the bytes from Xbee#2 into the array Rcvd
             {
               Rcvd[n] = Serial.read();
               delay(1);
             }
        }
    }
    for(int i = 1; i<27; i++)
      {
         Serial1.print(Rcvd [i],HEX);   //prints the bytes from Rcvd array
         Serial1.println(); 
         delay(50);
      }
      delay(500);
}

Which API mode are the two Xbees set? There are 2 (AP=1 or 2) If it is the mode taking into account escaped bytes (AP=2) then the API packet might be misinterpreted because it contains the value 0x13 in the adress. (And the packet in your code does not seem to be escaped) Then it will be ignored because the checksum fails.

Hello Valen,

Thank you for responding. I am using API mode 1. Do you think my arduino program is written incorrectly and could be the problem?

Scott

jedihonor1:
Hello Valen,

Thank you for responding. I am using API mode 1.

Ok, then that can’t be the issue then.

Do you think my arduino program is written incorrectly and could be the problem?

Scott

Can you assemble a API packet with an AT command for the local XBee? If that gets a response then your program should be ok. (your use of time delays may be a bit excessive/unnecessary, though nothing should escape the buffer)

Also, do you have the remote Xbee Dout and Din connected to an additional serial connection to your computer (or any other way to record serial communication). I have a feeling your desired sample report is coming out of the transmitter UART, instead of being sent over as an API packet. Or you can just connect a led and current limiting resistor to Dout to show if there is something coming out. Iirc sending sample data to remote receivers needs special registers to be set (IU to disable I/O out of Uart, and DL/DH set to receiver Xbee address). Try it, I’m not a 100% sure.

HI Valen,

Thank you for helping. I solved the problem. I think it was a couple of things. First I am using the mega and I think that RXo and TXo are the ports to print to the screen. Tx1 and RX1 don’t print to the screen (unless I am wrong.) Also I changed to the FFFF bytes to the IA command on both sbees. It was FFFFFFFFFFFFFFFF on both. However I thought this would not cause a problem because I am addressing by SH and SL.

My plan is to have 4 motion sensors each connected to an xbee (4 xbees total). If all 4 xbees transmit data there will be problems with collisions so I thought to have the arduino force a sample of each xbee, and read each sample via the arduino.

Can I also do this with a coordinator and end modules? without an IS command? If so, each xbee would send information constantly likely causing collisions in data received? What do you think?

Thank you,

Scott

Register IA is more for line-passing, and set on the receiving Xbee. And indicates the address of the Xbee that is allowed to change the outputs of the receiving Xbee. That is different from what you want. But maybe this is a undocumented feature of this register. As I said, I’m not 100% sure which register is needed to be set with the destination address, but my bets are on DL and DH.

Sl and SH are the 64 bit serial number unique to each xbee, so yes you can use them to uniquely identify them as an address.

4 xbees with 4 sensors each is going to be a lot of data to handle. Then again, you may only need a low sample rate. But I’m not sure if that is useful for motion sensors. What kind of things are they anyway? You better not say accelerometers, the bandwidth available and irregular lag will be totally inappropriate for them.

I never messed with the xbees in coordinator and end point mode. I only used them in pairs in their default peer-to-peer mode. So I can’t help really help here from experience.

HI Valen,

I will be using 4 pir motion sensors so only a high or low will be transmitted in the api packet so a narrow bandwitdth. Jon Titus has an excellent book on xbees, series1. It is very detailed. Chapters 19 and 20 address the coordinator and end module but it is a struggle for me to understand it all. I will keep trying.

I thought it would be fun to have 4 motion sensors located around a tent when camping. If an animal (bear…) gets close I will know which xbee was triggered by a colored led and a buzz. I could do a forced sample of each xbee every .1 second, so all would get sampled in .4 seconds. I could have an arduino with each xbee and a nothing would be sent unless the pir was triggered. This would make things software simpler but add quite a bit more cost and hardware. A fun project.

Scott