Go Down

Topic: XBee transparent mode with Alternate pins. (Read 1 time) previous topic - next topic

EnigmaCypher7

May 08, 2012, 08:05 pm Last Edit: May 08, 2012, 08:25 pm by EnigmaCypher7 Reason: 1
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: [Select]
#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: [Select]
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 =)

He who dares, Wins.

PaulS

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).

EnigmaCypher7

#2
May 10, 2012, 04:44 am Last Edit: May 10, 2012, 04:49 am by EnigmaCypher7 Reason: 1
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  XD.

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) [ :. Really cool by the way  :. ]

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: [Select]

#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
He who dares, Wins.

PaulS

Code: [Select]
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.

EnigmaCypher7

Code: [Select]
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: [Select]
#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 =)
He who dares, Wins.

Go Up