Re: XBee Series 2, Arduino codes

Greetings all,

I have a question to ask about the XBee codes pertaining to reading 2 XBees Series 2. 1 of which is the Coordinator API and the other the Router AT.

Both are plugged onto a wireless proto shield and the shield onto a MEGA for the Coordinator API, UNO for the Router AT.
The wireless proto shields have their tx and rx pins bent so that they will not be connected tx to tx and rx to rx on the Arduino MEGA and UNO. I use wires instead to manually connect the tx to rx and rx to tx.

Now, my plan is to send Hello World every 1 second from my Router and my Coordinator will receive it. The codes are below,

Router AT:

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(“Hello World”);
delay(1000);
}

Coordinator API:
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
if(Serial3.available() > 21) {
if(Serial3.read() == 0x7E){
for(int i=0; i<33; i++) {
Serial.print(Serial3.read(), HEX);
Serial.print(",");
}
Serial.println();
}
}

So the above codes work perfectly. I do read the API Hex frames from the Router so on my serial log it appears like this:
0 12 90… checksum byte. (7E does not appear due to the if statement check).

So heres my question, when I change to the Arduino library which is:
https://github.com/andrewrapp/xbee-ardu … Bee.h#L413

and all my codes include arduino library, I am unable to receive the “Hello World” anymore.

My router stays the same, only my Coordinator API changes its codes which are below:
//Some demo codes I extracted.
#include <EEPROM.h> //I am not sure about this library yet
#include <SoftwareSerial.h> //ditto on this library but from my understanding, something to do with the serial begin?
#include <XBee.h> //XBee library from the above link
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/

XBee xbee = XBee(); //XBee object
ZBRxResponse rx = ZBRxResponse(); //XBee receive response
//XBeeResponse rx = XBeeResponse();

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

void loop(void)
{
xbee.readPacket(); //unsure what this function does
Serial << “Hello” << endl; //this is printed infinitely
if (xbee.getResponse().isAvailable()) {
Serial << “Hello1” << endl; //does not print this line
switch (xbee.getResponse().getApiId()) { //what kind of packet did we get?
Serial << “Hello2” << endl; // does not print this line
case ZB_RX_RESPONSE: //rx data packet
xbee.getResponse().getZBRxResponse(rx); //get the received data
switch (rx.getOption()) {
case ZB_PACKET_ACKNOWLEDGED:
Serial.print(“Packet received”); // does not print this line
break;
}
}
}
}

As this above codes mentioned, I am only able to print Hello from the 2nd line in the void loop, but am unable to do anything else. BUT, if I were to change
if (xbee.getResponse().isAvailable()) {
to
if(Serial3.available() > 21) {
it continues to run down the chain and Hello1 will be printed.

So my questions are:

  1. Is that xbee library only for series 1?
  2. Must my router be in API mode as well?
  3. I tried wiring the Din and Dout to tx and rx but is this also one of the case?
  4. Is there anything wrong with the codes?

Any guidance would be appreciated. Thanks!

The wireless proto shields have their tx and rx pins bent so that they will not be connected tx to tx and rx to rx on the Arduino MEGA and UNO. I use wires instead to manually connect the tx to rx and rx to tx.

The direction of the pin depends on the shield. The Arduino sends data out the TX pin, and gets data in from the RX pin. The shield gets data in on the TX pin and sends data out on the RX pin. The pins should be connected straight, not bent and twisted.

It really does not make sense to have one XBee using AT mode and the other using API mode. They should all use the same mode.

  1. Is that xbee library only for series 1?

No.

  1. Must my router be in API mode as well?

Both should be in the same mode. If the sender is not sending API packets, it doesn't make sense to have the receiveer expecting API packets.

Since the XBee is not sending data of its own, API mode doesn't really make sense.

  1. I tried wiring the Din and Dout to tx and rx but is this also one of the case?

I don't understand this question.

  1. Is there anything wrong with the codes?

Yes.

PaulS: The direction of the pin depends on the shield. The Arduino sends data out the TX pin, and gets data in from the RX pin. The shield gets data in on the TX pin and sends data out on the RX pin. The pins should be connected straight, not bent and twisted.

Yes, from my understanding, the Tx pin of the shield when mounted onto the Arduino MEGA/UNO, it will connect the Tx to Tx and Rx to Rx. So I had to bend the pins from the shield and manually use wires to connect so that it will be Tx to Rx and Rx to Tx.

PaulS: It really does not make sense to have one XBee using AT mode and the other using API mode. They should all use the same mode. Both should be in the same mode. If the sender is not sending API packets, it doesn't make sense to have the receiveer expecting API packets.

Ok I have tested them both in API modes but am still not getting the packet or even printing Hello1. I dont actually understand but my Coordinator do get the API frame from the Router XBee as it sends from: 7E, 0, .... "Router's address", ... "Hello World in hex",.... checksum byte

PaulS: No.

Thanks. That is very reassuring as now I know Im not programming something that dosent even works with Series 2.

PaulS: Since the XBee is not sending data of its own, API mode doesn't really make sense.

Someone taught me that when I serial print something, the arduino board will send this message to the shield pins (wired manually so its from tx to rx) and then the shield will send this message to the XBee (Router AT) and then the XBee will transmit the message to the Coordinator (API) XBee (I set the DH: 0 and DL: 0) and Coordinator XBee will receive it wirelessly, thus sending this message to the shield and then the shield sends it to the MEGA (Serial 3, manually wired tx to rx) and then serial print the serial3 read.

This is from my understanding. Do guide me if I am wrong somewhere.

PaulS: I don't understand this question.

I saw that you can also connect the XBee Din and Dout to the Arduino MEGA/UNO tx and rx without the shield too. Maybe like using a breadboard?

And theres a line of code which I saw that goes something like: XBee.print("Hello1");

Not sure if they are related in any way.

PaulS: Yes.

Please do enlighten me where it is wrong so I can go and take a look.

Yes, from my understanding, the Tx pin of the shield when mounted onto the Arduino MEGA/UNO, it will connect the Tx to Tx and Rx to Rx. So I had to bend the pins from the shield and manually use wires to connect so that it will be Tx to Rx and Rx to Tx.

The shield connected the Serial pins properly. YOU did not. Unless the pin bending was to connect to the Serial3 pins on the Mega. It is not clear which shield you bent the pins on.

Ok I have tested them both in API modes but am still not getting the packet or even printing Hello1.

Test them in AT mode. API mode is needed only if you have multiple senders, one or more of the sending XBees has pins defined as input or output, or it is the ID of the sending XBee (not the Arduino that it is attached to) that is important.

I set the DH: 0 and DL: 0)

Bad idea.

This is from my understanding. Do guide me if I am wrong somewhere.

Except for using broadcast mode, your understanding is correct.

I saw that you can also connect the XBee Din and Dout to the Arduino MEGA/UNO tx and rx without the shield too. Maybe like using a breadboard?

Yes. The only problem with that is that the XBee doesn't fit a standard breadboard.

Please do enlighten me where it is wrong so I can go and take a look.

Unless one of the reasons for using API mode applies (I can't see that any of them do), use AT mode. Send using Serial.print() (or Serial3.print()). Receive using Serial.available() and Serial.read() (or the Serial3 equivalent).

PaulS: The shield connected the Serial pins properly. YOU did not. Unless the pin bending was to connect to the Serial3 pins on the Mega. It is not clear which shield you bent the pins on.

I bent the pins on both the MEGA and the UNO. Yes on the MEGA I am using Serial 3. Is it wrong to do so? Because I read that I have to connect tx to rx and rx to tx and thats one of the ways that I can do by bending the pins and then manually using wires to connect tx to rx and rx to tx.

PaulS: Test them in AT mode. API mode is needed only if you have multiple senders, one or more of the sending XBees has pins defined as input or output, or it is the ID of the sending XBee (not the Arduino that it is attached to) that is important.

Ok, maybe I should test them in AT mode first. But yes actually I do intend on having not only multiple Routers but may also include End Devices as well after learning the basics of this 2 XBees communication. Some Routers may also be receiving and sending the data to the Coordinator.

PaulS: Bad idea.

Should I put set it to DH: 0 DL: FFFF to enter broadcast mode? I will be using many XBees in the end. But for 1 to 1 now do I test the broadcast first or enter the Coordinators address instead?

PaulS: Except for using broadcast mode, your understanding is correct.

Thank you. I hope to learn more from you.

PaulS: Yes. The only problem with that is that the XBee doesn't fit a standard breadboard.

I do have the explorer dongle that can fit the breadboard but I dont have that many. The shield looks quite nice to have to set everything in place (like in one cube size) haha. Just wondering if it is ok to just use the shields instead?

PaulS: Unless one of the reasons for using API mode applies (I can't see that any of them do), use AT mode. Send using Serial.print() (or Serial3.print()). Receive using Serial.available() and Serial.read() (or the Serial3 equivalent).

Yes, I used Serial.print(Serial3.read()) etc. on my previous codes. And it works fine. But when I change to XBee library, it dosent work like if i were to change:

if(Serial.available() > 21) // this if statement works and it receives the Hello World from the Router

to this:

if (xbee.getResponse().isAvailable()) // this if statement does not work anymore

So, I was just wondering if this library is applicable as I do see many functions that I can use. Is this the right way to do it?

Yes on the MEGA I am using Serial 3. Is it wrong to do so?

On the Mega, no? On the Uno? Yes.

Because I read that I have to connect tx to rx and rx to tx and thats one of the ways that I can do by bending the pins and then manually using wires to connect tx to rx and rx to tx.

THE SHIELD CONNECTS THE PINS CORRECTLY. I'm not going to repeat that. Read is 1000 times if you need to!

But yes actually I do intend on having not only multiple Routers but may also include End Devices as well after learning the basics of this 2 XBees communication.

Routers are the only thing you need. An end device can not act as a router. Otherwise, there is no difference between a router and an end device. So, make everything except the coordinator a router.

Unless it is the ID of the XBee, as opposed to the ID of the Arduino that the XBee is attached to, that is important, skip the hassles of using API mode.

And it works fine. But when I change to XBee library, it dosent work like if i were to change:

If you aren't using API mode, the XBee library gains you nothing. Nada, zip, zilch.

So, I was just wondering if this library is applicable as I do see many functions that I can use. Is this the right way to do it?

If you are using API mode, yes. BUT, if you are using API mode, you MUST put your data in a packet correctly. Serial.print("Hello world") is NOT putting the data in a packet correctly.

PaulS: On the Mega, no? On the Uno? Yes.

Ok roger on that.

PaulS: THE SHIELD CONNECTS THE PINS CORRECTLY. I'm not going to repeat that. Read is 1000 times if you need to!

Ok I think I understand it. I will connect it correctly. But if I am using Serial3 on the MEGA I can dont connect the Shield pins to the MEGA?

PaulS: Routers are the only thing you need. An end device can not act as a router. Otherwise, there is no difference between a router and an end device. So, make everything except the coordinator a router.

Thats 1 plan on my mind, thanks for clarifying. Although now I have troubles with 2 XBees so I cant even play with more haha

PaulS: Unless it is the ID of the XBee, as opposed to the ID of the Arduino that the XBee is attached to, that is important, skip the hassles of using API mode.

But is it possible to do mesh networking in AT mode? And technically I may need the XBee ID more. But if mesh networking is possible in AT mode which from my understanding I dont think it works, am I right?

PaulS: If you aren't using API mode, the XBee library gains you nothing. Nada, zip, zilch.

Ok, thanks for that info.

PaulS: If you are using API mode, yes. BUT, if you are using API mode, you MUST put your data in a packet correctly. Serial.print("Hello world") is NOT putting the data in a packet correctly.

Sorry, does this mean my XBee must XBee.print out the entire api frame? How do I put my data correctly?

But if I am using Serial3 on the MEGA I can dont connect the Shield pins to the MEGA?

You don't connect the TX and RX pins. Those you jumper to the appropriate TXn and RXn pins - the TX0 pin connects to the TXn pin and the RX0 pin connects to the RXn pin, where n is the Serialn instance you want to use instead.

But is it possible to do mesh networking in AT mode?

How the XBees connect has nothing to do with what data they transmit. AT mode can be used or API mode can be used.

Sorry, does this mean my XBee must XBee.print out the entire api frame?

Repeat after me: I DO NOT NEED TO USE API MODE at least until I know what I am doing!

PaulS: You don't connect the TX and RX pins. Those you jumper to the appropriate TXn and RXn pins - the TX0 pin connects to the TXn pin and the RX0 pin connects to the RXn pin, where n is the Serialn instance you want to use instead.

Ok. I understand this now thanks!

PaulS: How the XBees connect has nothing to do with what data they transmit. AT mode can be used or API mode can be used.

But the best is still if I need AT modes then all XBees will be in AT and if I need API, then all XBees will be in API mode. I dont need to have 1 in API and 1 in AT yes?

PaulS: Repeat after me: I DO NOT NEED TO USE API MODE at least until I know what I am doing!

Thanks! I will continue to work on AT mode for now until I further understand. Please continue to guide me along the way.

But the best is still if I need AT modes then all XBees will be in AT and if I need API, then all XBees will be in API mode.

Yes.

I dont need to have 1 in API and 1 in AT yes?

Don't need, don't want, and can't have.

PaulS: Yes.

Don't need, don't want, and can't have.

Hahaha! Thanks Paul! I will try it out now the AT modes first. Please continue to guide me along.

Ok, I did an Router AT -> Coordinator AT experiment. Both Arduino boards are connected to my Laptop via serial cable.

Basically, Router AT Serial printing "Hello World" and then the Coordinator AT receives it and prints it on its own Serial log "Hello World".

And then (sorry I just had to try this out), I did R AT -> C API, same thing as above, the Coordinator receives the "Hello World".

But when I change to R API -> C API, the Coordinator Serial log is not printing out the "Hello World" anymore. Router API Serial log is still printing Hello World though.

So how would I go about printing the "Hello World" with both of them in API modes? Is it something to do with printing out the bytes itself (the huge hassle)?

So how would I go about printing the "Hello World" with both of them in API modes?

You have to determine where in the API packet the string "Hello World" is.

Is it something to do with printing out the bytes itself (the huge hassle)?

Of course. And, yes. That's why there is AT mode for sending just text.

PaulS: You have to determine where in the API packet the string "Hello World" is. Of course. And, yes. That's why there is AT mode for sending just text.

Sorry but to do this, do I change the codes on the Router or the Coordinator? Could you provide an example?

Sorry but to do this, do I change the codes on the Router or the Coordinator?

Yes.

Could you provide an example?

No. I have never felt it necessary to abuse API mode by sending packets containing just text.

PaulS: Yes.

Yes to both? Oh wow interesting.

PaulS: No. I have never felt it necessary to abuse API mode by sending packets containing just text.

Ok, I wanna do mesh networking though, with many routers and 1 coordinator. Is there an example from the internet that you could provide?

Ok, I wanna do mesh networking though, with many routers and 1 coordinator.

Suppose you wanted to have 6 of your friends send you notes, and you want to reply to them. How would you arrange to know who the notes were from?

PaulS: Suppose you wanted to have 6 of your friends send you notes, and you want to reply to them. How would you arrange to know who the notes were from?

Their names or addresses?

JonathanYSG: Their names or addresses?

Each person that sent you a note would need to include their name. Now, substitute XBee for friend, and you should be able to figure out that to do on the sending friend nodes.

PaulS: Each person that sent you a note would need to include their name. Now, substitute XBee for friend, and you should be able to figure out that to do on the sending friend nodes.

Ok, I understand that part now. But to literally send their name, the code logic would be to Serial Print it?