Pages: [1]   Go Down
Author Topic: XBee transparent mode with Alternate pins.  (Read 1245 times)
0 Members and 1 Guest are viewing this topic.
Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I have an XBee Series 01 - XB24-AWI-001 and would like to run it in "Transparent Mode" so I can send simple serial commands to my Arduino Uno wirelessly.

Only there is a catch, I need hook the XBee to pins 2 and 13 because I would like to still be able to use the USB interface as a "fail safe" to upload code without having to move a jumper. Another hurdle is that I am using a motorshield with this project and I only have pins 2,13 left to play with.

I do not have the project with me, but will be working with it today after work. I believe that the following would be similar to the code I would need to perform the communication between my XBee USB dongle and my project:

Code:
#include <SoftwareSerial.h>
 
SoftwareSerial mySerial =  SoftwareSerial(2, 13);

Has anybody tried something similar?

Thoughts?

Thank you,
Ec7


--EDIT-- (More Info)

The current code for the USB interface is:

Code:
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 

I am also guessing I would leave this in here to keep the USB enabled, but where to add the above code to read Pins 2,13? Right after it?

This is the XBee Adapter I am using:
https://www.adafruit.com/products/126

Thanks =)

« Last Edit: May 08, 2012, 01:25:19 pm by EnigmaCypher7 » Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Has anybody tried something similar?
Yes. It works fine.

Quote
I am also guessing I would leave this in here to keep the USB enabled, but where to add the above code to read Pins 2,13? Right after it?
If you are not using the Serial object, then you don't have to leave that in there. If you are, you do.

The code to read the mySerial (lousy name; what is wrong with xbee?) instance can go before or after that BLOCK of code (that you don't show all of).
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally got a chance to play with the XBees, I reprogrammed them with custom channels and gave them new ID's etc. Very straight forward with the X-CTU software, very happy so far  smiley-lol.

I decided to start from square one and run a few simple experiments to get a better handle on how this all works. the first thing I did was load a code to make a LED turn on with the press of the 1 and 2 keys on a keyboard.

This code works while tethered to a USB cable (COM5)

I then hooked up one of the XBees to the Arduino using pins 0 & 1 (RX & TX) and the code worked flawlessly (COM6) [ smiley-small Really cool by the way  smiley-small ]

But I can't for the life of me figure out how to get the RX and TX to move over to pins 2 & 13 where I need them to be, much less both serials to work in the same code (The end goal).

Here's what I have been playing with, only I have tried several variations all without too much in the way of progress:

Code:
#include <SoftwareSerial.h>

void setup() {
Serial.begin(9600); // Setup Serial library at 9600 bps

SoftwareSerial mySerial = SoftwareSerial(2, 13);
mySerial.begin(9600); // Setup Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
    for (int thisPin = 9; thisPin < 11; thisPin++) {
    pinMode(thisPin, OUTPUT);
  
         switch (inByte) {
  
//Pin 9 - Lights
 
    case '1':    
      digitalWrite(9, HIGH); // Turns on Lights
      break;

    case '2':    
      digitalWrite(9, LOW); // Turns off Lights
      break;
 
    default:
      // turn all the connections off:
      for (int thisPin = 9; thisPin < 11; thisPin++) {
        digitalWrite(thisPin, LOW);
        }
      }
    }
  }
}

Thanks,
Ec7
« Last Edit: May 09, 2012, 09:49:04 pm by EnigmaCypher7 » Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial mySerial = SoftwareSerial(2, 13);
mySerial.begin(9600); // Setup Serial library at 9600 bps
Do you really have a mySerial connected to these two pins? Use a meaningful name!

The SoftwareSerial instance is local to setup(). When setup() ends, the variable goes out of scope. You won't be able to use it in loop(). Sort of makes it useless, don't you think? A global instance would be a lot more useful.

Quote
I then hooked up one of the XBees to the Arduino using pins 0 & 1 (RX & TX) and the code worked flawlessly
Good.

Quote
But I can't for the life of me figure out how to get the RX and TX to move over to pins 2 & 13
You have to move some wires. Unless you are using a shield to connect the XBee, in which case you probably can't use pins 2 and 13.

Quote
much less both serials to work in the same code (The end goal).
Well, first you need to get the XBee connected to different pins...

If the XBee is not connected to the 0 and 1 pins, that code is going to do nothing. You need to read from the serial class instance that owns the pins that the XBee is connected to.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial mySerial = SoftwareSerial(2, 13);
mySerial.begin(9600); // Setup Serial library at 9600 bps
I thought this is what allowed me to use pins 2,13 for the RX & TX?

Quote
You have to move some wires. Unless you are using a shield to connect the XBee, in which case you probably can't use pins 2 and 13.
I am physically moving the wires from headers 0,1 to the headers marked 2,13. The shield I am using does not have a need foe these two pins, therefore I am going to wire the XBee directly through these headers by way of the shields headers. (It was a motor shield that I have slightly customized to suit this project)

Quote
Use a meaningful name!
Can I name it anything I want? I was thinking this name was referenced somewhere in the library... No I haven't opened it and looked...

Quote
The SoftwareSerial instance is local to setup(). When setup() ends, the variable goes out of scope. You won't be able to use it in loop(). Sort of makes it useless, don't you think? A global instance would be a lot more useful.
Ah, a global instance sounds good but I am unsure how to declare it, so I declared the scope the same way that Serial is normally declared. I thought you said;
Quote
instance can go before or after that BLOCK of code

I was kind of using this old code from Adafruit as an example to set new pins to read RX / TX, it still uses the "NewSoftSerial".
Code:
#include <NewSoftSerial.h>

NewSoftSerial mySerial =  NewSoftSerial(2, 3);


void setup()  {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}



void loop()                     // run over and over again
{

  if (mySerial.available()) {
      Serial.print((char)mySerial.read());
  }
  if (Serial.available()) {
      mySerial.print((char)Serial.read());
  }
  delay(100);
}
(http://www.ladyada.net/make/xbee/point2point.html)

Thanks =)
Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought this is what allowed me to use pins 2,13 for the RX & TX?
It would, except that mySerial is local to setup(). As soon as setup() ends, mySerial ceases to exist. If you make it a global variable, then it would continue to exist, and be usable.

Quote
Can I name it anything I want?
Yes, you can.

Quote
I was thinking this name was referenced somewhere in the library... No I haven't opened it and looked...
What library? The SoftwareSerial class has no idea what instances of the class are named, nor does it care. Names are not part of the hex file uploaded to the Arduino. They are for people to use.

Quote
Ah, a global instance sounds good but I am unsure how to declare it
Move it before setup().

Quote
I thought you said;
I did. I was referring to where you use, not declare, the instance. The block I was referring to was from code you posted, where only the opening { was posted.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, so with the following I get this error:

Quote
sketch_may10a:3: error: expected constructor, destructor, or type conversion before '.' token

Code:
#include <SoftwareSerial.h>

SoftwareSerial WirelessSerial = SoftwareSerial(2, 13);
WirelessSerial.begin(9600); // Setup Serial library at 9600 bps

void setup() {
Serial.begin(9600); // Setup Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
    for (int thisPin = 9; thisPin < 11; thisPin++) {
    pinMode(thisPin, OUTPUT);
 
         switch (inByte) {
   
//Pin 9 - Lights
 
    case '1':   
      digitalWrite(9, HIGH); // Turns on Lights
      break;

    case '2':   
      digitalWrite(9, LOW); // Turns off Lights
      break;
 
    default:
      // turn all the connections off:
      for (int thisPin = 9; thisPin < 11; thisPin++) {
        digitalWrite(thisPin, LOW);
        }
      }
    }
  }
}
Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Okay, so with the following I get this error:
Sure, because you can't call the begin() method there. You need to call it in a function. The setup() function makes the most sense, because you only want to call begin() once.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am looking through:
http://arduino.cc/en/Reference/HomePage
and I don't see a whole lot in the way of setup structure for global variables.

I did see (Loop) [http://arduino.cc/en/Reference/Loop]:

Code:
int buttonPin = 3;

// setup initializes serial and the button pin
void setup()
{
  beginSerial(9600);
  pinMode(buttonPin, INPUT);
}

// loop checks the button pin each time,
// and will send serial if it is pressed
void loop()
{
  if (digitalRead(buttonPin) == HIGH)
    serialWrite('H');
  else
    serialWrite('L');

  delay(1000);
}

When it calls the serial, it uses:
Quote
beginSerial(9600);
Instead of:
Quote
Serial.begin(9600)

So how does that work?

And I couldn't go from:
Code:
WirelessSerial.begin(9600);
To:
Code:
beginwirelessserial(9600);
or something similar could I? Is this sort of on track with what you were explaining to me?

I usually know what I want the code to do, but getting it into the correct context, now there's the trick... Is there some other place that references different "setups"?

Thanks,
Ec7
Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am looking through:
http://arduino.cc/en/Reference/HomePage
and I don't see a whole lot in the way of setup structure for global variables.
The HomePage site talks about stuff that is Arduino-specific. Variable scope is not specific to the Arduino, so it is not mentioned.

Quote
So how does that work?
It doesn't. There should be a beginSerial() function that calls Serial.begin().

Quote
And I couldn't go from:
or something similar could I?
You could, but there is no advantage to having another function.

Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, Still not getting to the bottom of the global variable thing..

I am thinking that
Code:
SoftwareSerial WirelessSerial = SoftwareSerial(2, 13);
is being declared globally because it is outside of the instance of:
Code:
void setup() {
Serial.begin(9600); // Setup Serial library at 9600 bps
}
or rather the "{}"
etc.

But where do I have to place the:
Code:
WirelessSerial.begin(9600); // Setup Serial library at 9600 bps
?
 
Or am I missing something else?
« Last Edit: May 11, 2012, 12:55:20 pm by EnigmaCypher7 » Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But where do I have to place the:
Inside the { and } that define the start and end of setup().
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This feels like where I started out:

I had already tried:
Code:
#include <SoftwareSerial.h>

SoftwareSerial WirelessSerial = SoftwareSerial(2, 13);

void setup() {
Serial.begin(9600); // Setup Serial library at 9600 bps
WirelessSerial.begin(9600); // Setup Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
    for (int thisPin = 9; thisPin < 11; thisPin++) {
    pinMode(thisPin, OUTPUT);
 
         switch (inByte) {
   
//Pin 9 - Lights
 
    case '1':   
      digitalWrite(9, HIGH); // Turns on Lights
      break;

    case '2':   
      digitalWrite(9, LOW); // Turns off Lights
      break;
 
    default:
      // turn all the connections off:
      for (int thisPin = 9; thisPin < 11; thisPin++) {
        digitalWrite(thisPin, LOW);
        }
      }
    }
  }
}

When I posted:

Code:
#include <SoftwareSerial.h>

SoftwareSerial WirelessSerial = SoftwareSerial(2, 13);
WirelessSerial.begin(9600); // Setup Serial library at 9600 bps

void setup() {
Serial.begin(9600); // Setup Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
    for (int thisPin = 9; thisPin < 11; thisPin++) {
    pinMode(thisPin, OUTPUT);
 
         switch (inByte) {
   
//Pin 9 - Lights
 
    case '1':   
      digitalWrite(9, HIGH); // Turns on Lights
      break;

    case '2':   
      digitalWrite(9, LOW); // Turns off Lights
      break;
 
    default:
      // turn all the connections off:
      for (int thisPin = 9; thisPin < 11; thisPin++) {
        digitalWrite(thisPin, LOW);
        }
      }
    }
  }
}

It never worked =\ (tried it again, doesn't work)

Ideas?
Logged

He who dares, Wins.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ideas?
Yes. Read your own code. Look at the code in the first box in your last post. You define a SoftwareSerial instance to communicate with the XBee on pins 2 and 13. In setup(), you call the begin() method for that instance. That is the last time the instance is referenced in the code.

If Serial is to talk to the Serial Monitor (or other PC application), and WirelessSerial is to talk to the XBee, then somewhere you need to call some WirelessSerial methods, just like you call Serial methods.

Why are you setting pin modes in loop()? Is that something that needs to be done over and over?
Logged

Pages: [1]   Go Up
Jump to: