Xbee connection

Hi,

I’m using 2 Xbee Series 1 radios to test basic communication. They are on the Arduino wireless shield (https://store.arduino.cc/usa/arduino-wirelss-sd-shield), and used with the UNO.

I’ve written some code for a basic setup of a master-slave communication, where a button is pushed on the master side, which then sends a character to the slave side to light an led.

The master side works, but the light on the slave side doesn’t. I can’t tell if there’s a setup with the board which is wrong, or if the xbees aren’t actually connected.

I went through a tutorial to connect them in Putty, which I found here (up to the 6min mark):

I’ve attached both the master and slave’s code here.

Any ideas as to what I do first for trouble shooting?

master.ino (1.05 KB)

slave.ino (1.24 KB)

Hmm...the Arduino Wireless SD Shield's pdf schematics don't seem to be downloadable. In any case, I think your problem is that on the transmitter, you're printing to the main Serial port on the Uno. This goes to the USB, not the XBee. What you most likely need to do (can't tell without checking the shield's schematics) is set up a software serial connection with the wireless shield on whatever pins the XBee is connected to. Then do the print on that. An example would be something like:

SoftwareSerial xbeeTx(2, 3); // RX, TX

void setup()
{

// ..init stuff for buttons, LEDs
  xBeeTx.begin(9600);

}

void loop()
{
    value = digitalRead(button);   // read input value and store it in val
  
  if (value == HIGH) 
    {               // check if the button is pressed
      digitalWrite(ledPin, HIGH);   // turn LED on
      xBeeTx.print('H');  //this sends whatever is between the (' ') to the partner xbee
      delay(1);
    }
// ... rest of stuff
}

or something to that extent. You basically need to have a software serial instance that transmits to the XBee, rather than the main Serial which is just sending data out the USB port.
Hope that helps.

One more thing. You might want to run stock XBee sample code to make sure the link is working between your two devices before writing the custom code. It's more of a sanity check.

I tried your suggestions, and edited the code for both the master and the slave. I wasn’t sure if I used it correclty, but here are the files for both. It did not work.

Do you have an suggestions to basic xbee code to test? I’ve looked around on the internet for it, and actually I see people utilizing the xBee commands differently, so I’m a bit lost.

Master Code:

/*
 *  Master Xbee
 *  When button is pressed on master's circuit, master's LED will be lit
 *  When button is pressed, it will also send data to slave Xbee to light it's LED
 */

#include <SoftwareSerial.h>
SoftwareSerial xBeeTx(2,3);  // RX, TX

int ledPin = 12;             // LED is connected to pin 12
int button = 7;              // button is connected to pin 7
int value;                    // variable for reading the pin status

void setup() {
  
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);    // Set the LED pin as output
  pinMode(button, INPUT);    // Set the button as input

   xBeeTx.begin(9600);
   
}

void loop(){
    
  value = digitalRead(button);   // read input value and store it in val
  
  if (value == HIGH) 
    {               // check if the button is pressed
      digitalWrite(ledPin, HIGH);   // turn LED on
      Serial.print('H');  //this sends whatever is between the (' ') to the partner xbee
      xBeeTx.print('H');
      
      delay(1);
    }
    
    else // (value == LOW)
    {     
      digitalWrite(ledPin, LOW);    // turn LED off
      //Serial.print('L');  //this sends whatever is between the (' ') to the partner xbee
     delay(1);
    }
  
    
 }

Slave Code:

/*
 *  Light LED only if button is pressed
 *  Xbee 1 (receiver)
 */

#include <SoftwareSerial.h>
SoftwareSerial xBeeTx(2, 3); // RX, TX
 
int ledPin = 12;             // LED is connected to pin 12
int incomingByte;      // a variable to read incoming serial data into
int ping_once = 0;  /* done so that robot will not always ping on manual motors to stop,
                       because next time around, x > 1, and is reset
                       each time switch from autonomous mode
                     */
                     
void setup() 
{
  Serial.begin(9600);  // xbees are communicating over sieral data, except it is occuring wirelessly
  pinMode(ledPin, OUTPUT);      // Set the LED pin as output

  xBeeTx.begin(9600);
}

void loop()
{
  //  if (Serial.available() > 0)   // see if there's incoming serial data:
  if (xBeeTx.available())
      {
      Serial.println(xBeeTx.read());
      
      incomingByte = xBeeTx.read(); // read the oldest byte in the serial buffer
      ping_once = 0;  /* done so that robot will not always ping on manual,
                 because next time around, x > 1, and is reset
                 each time switch from autonomous mode
              */
      
          // light led
        if (incomingByte == 'H')  
          {
            digitalWrite(ledPin, HIGH);
            delay(1000);
            digitalWrite(ledPin, LOW);
            delay(1000);
          }
       }
    
 }