How to send data (from the terminal) using xbee?

Hi there.
I am trying (for hours now) to send one letter from the terminal to the arduino, using two xbee's modules (one conntected to my pc using ftdi cable) and the other connected to the remotely arduino.

Is there a SIMPLE way of doing it?

Thank you!

Which xbee radios do you have - series 1 or series 2?

How do you have them configured? If they are series 1, the MY address on one needs to be the AL address on the other.

What code do yout have running on the Arduino?

Hi,
I have series 2 xbee radio. (Xbee Adapter

I am actually building a follow-line robot (using ir sensors).

This robot can turn left/right on junctions and I currently I can control the robot turns using input letters through the terminal.

I'm attaching the code:

// analog sensors
int FrontSensorR = 0;    
int FrontSensorL = 0;  
int BackSensorR = 0;   
int BackSensorL = 0;
//motors
int RMotorDir = 12; // Direction pin for motor B is Digital 12
int LMotorDir =12;// Direction pin for motor A is Digital 12
int VelMotorR = 9; // Speed pin for motor B is Digital 9 (PWM)
int VelMotorL = 10; // Speed pin for motor B is Digital 9 (PWM)
int speed=50,S=50;//Speed;
int OutLine=50,OnLine=100;
int sf=0,delay1=300,timeflag=0,timeflag2=0,code=0,checking=0,k1=0,sum=0,Asum=0;
int flag1=1,AR=1,ActiveSensor=0,L;
int q=0,q1=0;// Junction Number

void setup() {
  Serial.begin(9600);
  
  pinMode(RMotorDir, OUTPUT);
  pinMode(LMotorDir, OUTPUT);
  
  pinMode(0, INPUT);   // digital sensor is on digital pin 0
  pinMode(1, INPUT);   // digital sensor is on digital pin 1
  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  pinMode(3, INPUT);   // digital sensor is on digital pin 3
   
}

void loop()
 {
   if (Serial.available()) 
   {
    L = Serial.read();
    for(int i=1;i<=31;i+=1)
    {
    if(L==64+i || L==64+32+i)//big or small, ASCI letter
 {
      q=i;
 }
   }
   }
   ///////////////////////////////////////
   if(q!=0)
   {
     
    checking=q1;
    FrontSensorL  = analogRead(0);
    FrontSensorR = analogRead(1);
    BackSensorL = analogRead(2);
    BackSensorR= analogRead(3);
     // send sensor values for printig
     Serial.print(FrontSensorL, DEC);
     Serial.print(",");
     Serial.print(FrontSensorR, DEC);
     Serial.print(",");
     Serial.print( BackSensorL, DEC); 
     Serial.print(",");
     Serial.print( BackSensorR, DEC); 
     Serial.print(",");
     Serial.print(",AR=");
     Serial.print( AR, DEC); 
     Serial.print(",checking=");
     Serial.println( checking );
     
     speed=S;
   if( q1==q+1||q1==q+2)  //wait around your letter
    { 
      if(q1==q+1)
      {
      extern volatile unsigned long timer0_millis;
      timer0_millis = 0;
      timeflag2=millis();
      q1++;
      }
      speed=0;
      if(millis()>=timeflag2+5000)
      {
      q1++;
      speed=S;
      }
      //delay(5000);
    }  
    
   
  if (AR==1) //Active Regime 1,Follow Line 
  {
  code=0;
  ActiveSensor=BackSensorR;
    if (FrontSensorR < OutLine && FrontSensorL < OutLine )// Go Straight
    {
  digitalWrite(RMotorDir, 12); // set direction
  analogWrite(VelMotorR, speed); // set speed (PWM)
  digitalWrite(LMotorDir, 12); // set direction
  analogWrite(VelMotorL, speed); // set speed (PWM)
  sf=0;
     }
       
  if(FrontSensorL < OutLine  && FrontSensorR > OnLine)// Correction to Right
     {
  digitalWrite(RMotorDir, 12); // set direction
  analogWrite(VelMotorR, speed-speed); // set speed (PWM)
  digitalWrite(LMotorDir, 12); // set direction
  analogWrite(VelMotorL, speed+sf); // set speed (PWM)
  sf=+10; 
     }
     
     if(FrontSensorR < OutLine  && FrontSensorL > OnLine)//Correction to Left
     {
  digitalWrite(RMotorDir, 12); // set direction
  analogWrite(VelMotorR, speed+sf); // set speed (PWM)
  digitalWrite(LMotorDir, 12); // set direction
  analogWrite(VelMotorL, speed-speed); // set speed (PWM)
  sf=+10; 
     }
 
   }
  //////////////////////////////////
  if(FrontSensorL > OnLine && FrontSensorR > OnLine && (q1==q ||q1==q+3)  ) 
  {
    AR=2;//go to new Active Regime
  } 
  
  
  //////////////////////////////////
  if(FrontSensorL > OnLine && FrontSensorR > OnLine && q1==q+4 ) 
  {
    AR=3;//go to new Active Regime
    q1=0;
    
  } 

  //////////////////////////////////
    if(AR==2)// Active Regime 2: Turn Left on Junction
  { 
    ActiveSensor=FrontSensorR;
    if(code==1)
    {
      AR=1;
      ActiveSensor=BackSensorR;
    }
  digitalWrite(RMotorDir, 12); // set direction
  analogWrite(VelMotorR,speed-speed); // set speed (PWM)
  digitalWrite(LMotorDir, 12); // set direction
  analogWrite(VelMotorL, speed); // set speed (PWM)
  //sf=+10;
    
  }
  ////////////////////////////////////
  
  if(AR==3)// Active Regime 2: Turn Left on Junction
  { 
    ActiveSensor=FrontSensorL;
    if(code==1)
    {
      AR=1;
      ActiveSensor=BackSensorR;
    }
  digitalWrite(RMotorDir, 12); // set direction
  analogWrite(VelMotorR, speed); // set speed (PWM)
  digitalWrite(LMotorDir, 12); // set direction
  analogWrite(VelMotorL,speed-speed); // set speed (PWM)
  
    
  }

 /////////////////////////////
 
   
     //GetCode
     if ( ActiveSensor > OnLine  && k1==0  ) // 
  {
    
    extern volatile unsigned long timer0_millis;
    timer0_millis = 0;
    timeflag=millis();
    k1=1;
     Asum=0;
    // code=0;
      
  }
  if (millis()<timeflag+delay1)// Scanning Time
  {
  flag1++; // Scanning Number kamut srikot
  sum=sum+ActiveSensor;//
    }
  
    
  if (millis()>timeflag+delay1)// Scanning Result
  {
    Asum=sum/flag1;
    flag1=1;
    sum=0;
   //code=0;
    if(ActiveSensor < OutLine)
    {
    k1=0;
    }
    
      
  }
  if (Asum > OnLine)
  { 

    if(millis()>timeflag+delay1 && ActiveSensor==BackSensorR && BackSensorL < OutLine)
    {
      q1++;// placing code
    }
     
    code++; // service code
    delay(100);//go out from junction
   
    }
 }


    }

I got lost with the all information on how to connect the xbee. I just need to send a data (one letter) to the arduino.

I got lost with the all information on how to connect the xbee. I just need to send a data (one letter) to the arduino.

So, how did you hook it up?

One Xbee is connected like this, to my pc:

The other xbee is connected to the arduino, like this:
(just without the capacitor)..

now, all i want to do is, when pressing L in the terminal, to "deliver" it to the arduino..

Do the green and red lights come on and/or flash at appropriate times - when power is applied, and when transmitting data?

No, just a solid green light on both xbee's....

The shield that is attached to the computer should flash the red light as it's transmitting data. For one character, it may flash too fast to see. What happens if you enter more than a single letter - say 30 or 40 letters?

How did you program the XBees? Using X-CTU?

Hi.
Now the two green leds flash!

I had used this link: http://blog.kevinhoyt.org/wp-content/xbee-setup.pdf.

my simple question is - how to send a char to the arduino?

btw - is it possible to upload a sketch? and if so - how?
Thanks

I'd add some Serial.print(ln) statements in the loop function, to print out the number of bytes available, the byte read, in, etc. to see if the Arduino is getting the data from the xbee. The flashing lights indicate that the data is being sent and received by the xbees.

The next hurdle is getting the data from the xbee to the Arduino. More precisely, seeing whether the data is getting from the xbee to the Arduino.

btw - is it possible to upload a sketch? and if so - how?

From the computer to the remote Arduino? I've read that it is possible, but I have no idea how to do it.

thank, but -
I'm trying to find some specific answer and just can't..

how to send a data from pc to arduino using xbee.. I thought it should be simple, boy I was wrong!

So, did you add the Serial.print statements? Did they reveal anything?

I don't know what it means to add "serial.print statments"...

could you please be more specific, i'm a newbie :slight_smile:
thanks

   int bytesAvail = Serial.available();
   Serial.print("There are ");
   Serial.print(bytesAvail);
   Serial.println(" bytes available to read");

   if(bytesAvail > 0)
   {
      L = Serial.read();
      Serial.print("Read byte: ");
      Serial.println(L, DEC);

      for(int i=1;i<=31;i+=1)
      {
         if(L==64+i || L==64+32+i)//big or small, ASCI letter
         {
           q=i;
         }
      }
   }

Upload the sketch, and then press the Serial Monitor button (the one to the right of the Upload button). A new window opens, where the serial data appears.

Do I need any special library?

I tried to upload this sketch but got an error:
error: expected constructor, destructor, or type conversion before '.' token (the second line).

I followed this page:http://blog.kevinhoyt.org/wp-content/xbee-setup.pdf, currently the Router led (green) blink twice a second, but the coordinator blinks one's a second.

Anyway, I'm starting to feel I will never be able to send data to the arduino through xbee :cry:

Please help me figure what is the problem.

You have other Serial.print statements, in a sketch that used to compile, so, no additional libraries are needed.

Would you post your code again? I don't see anything wrong in what I posted. I'm wondering if you pasted it in the right place.

Blinking lights are a good sign. I think you are very close to getting it to work. Don't give up yet.

I paste your code into arduino enviorment and tried to upload it (got the error).

what else can I do?

I pasted it into a sketch:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int q, L;
  int bytesAvail = Serial.available();
  Serial.print("There are ");
  Serial.print(bytesAvail);
  Serial.println(" bytes available to read");

  if(bytesAvail > 0)
  {
    L = Serial.read();
    Serial.print("Read byte: ");
    Serial.println(L, DEC);

    for(int i=1;i<=31;i+=1)
    {
      if(L==64+i || L==64+32+i)//big or small, ASCI letter
      {
        q=i;
      }
    }
  }
}

This compiled just fine, for me.

Now It's compiling (I forgot to add the void setup).

It's said that there are 0 bytes to read..

what does this sketch meant to do/check?

I still don't see how to connect my pc to the arduino using xbee's...

You have the Arduino attached to the PC, via the sable you use to upload the sketch to it.

You have one xbee attached to the PC.

When you send data from the PC to the xbee, the xbee sends it out, as a radio transmission.

The other xbee (the one on the Arduno) receives it, and sends it to the Arduino. The Arduino also sends data to the PC, via the serial port that it's cable is connected to.

The sketch is supposed to determine if any data is being received by the xbee on the Arduino.

The 0 bytes available message suggests that it is not.

Try swapping the wires from the RX and TX pins on the xbee, so that RX on the xbee connects to RX on the Arduino, and TX connects to TX.