Go Down

Topic: Serial Communication between XBee using Arduino (Read 3451 times) previous topic - next topic

Robin2

I just noticed this thing and got the point you are referring to. I changed it to 4 both on the transmitter as well as on the receiver side and surprisingly for me, it was working.
I'm glad you have it working, though there is nothing surprising about errors arising when you write beyond an array.

Quote
I'm attaching my output so that you can see what output I'm getting.
I have asked you more than once NOT to post pictures of text. If you do so again I will just ignore this Thread. Copy and paste the text because then I can also copy and paste it.


Quote
Also, after switching from broadcasting to taking input from the user, it is not accepting input from the Serial Monitor of Arduino. And that is the main thing which I want to do for my project. If you want I can attach the code and output of that too.
I don't understand and so far it has been very difficult to establish meaningful communication between us. Maybe that is my fault for wanting stuff in the way that my brain can absorb.

I think you mean this ... (please correct me if I am wrong)
  • There is a program AA than can send data to program B
  • There is another program AB that attempts to get input from a user and send it to the same program B
  • The program AB is a small evolution of program AA
  • The program AB is not working

If I have summarised the problem correctly then please post each of the programs AA, B and AB - and examples of their output in text form. And please do NOT refer me to programs you posted earlier. I need to start with a clean sheet of paper.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

#46
Aug 26, 2018, 11:29 pm Last Edit: Aug 26, 2018, 11:33 pm by Priyanshu27
Quote
I have asked you more than once NOT to post pictures of text. If you do so again I will just ignore this Thread. Copy and paste the text because then I can also copy and paste it.
Sorry to say but the output I attached is from the Serial Monitor of the Arduino UNO and it doesn't allow me to copy paste the text. However, I tried doing that as well and for the receiver, I'm able to copy paste, but for the transmitter, I'm not. As at the time of transmitting messages, there are random symbols at the output because of which copy paste is not working. If you can suggest something, I would be happy to do so.

Quote
I think you mean this ... (please correct me if I am wrong)
There is a program AA than can send data to program B
There is another program AB that attempts to get input from a user and send it to the same program B
The program AB is a small evolution of program AA
The program AB is not working
Yes, you are almost correct, I can explain adding examples to it

  • AA is the program for broadcasting and B is the program of Receiver
  • Now, AB is the program for unicasting which was a modified version of the program AA
  • I tried making program AA run first so that program AB can run after that
  • The program AA is running but when I modify it to AB, it is not working

Quote
please post each of the programs AA, B and AB
I can attach the code for AA, B and AB for reference again, and for the output of it I'm not able to attach the output of AA and AB, but I'm able to attach the output of the program B in text form.

NOTE: The program AA and the program AB are a transmitter code and the program B is a receiver code which is same for both the transmitter codes.

Code for program AA: Broadcasting

Code: [Select]

//Transmission from Router 6 to Router 5

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t input = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
}

void loop()
{
  uint8_t i = 0;
  uint8_t data[4];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  
  delay(500);
  Serial.println("Start Transmitting");
  Serial.print("Data Sent:\t");
  Serial.print(data[0]);
  Serial.print(data[1]);
  Serial.print(data[2]);
  Serial.println(data[3]);
  delay(500);
  xbee.send(zbTx5);
  Serial.println("To Router 5");
  delay(500);
}


Code for the program AB: Unicasting by taking input from user from Serial Monitor

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t input = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
}

void loop()
{
  uint8_t i = 0;
  uint8_t data[4];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  
  delay(500);
  Serial.println("Start Transmitting");
  Serial.print("Data Sent:\t");
  Serial.print(data[0]);
  Serial.print(data[1]);
  Serial.print(data[2]);
  Serial.println(data[3]);
  delay(500);
  if(Serial.available() > 0)
  {
    receivedChar = Serial.read();
    newData = true;
  }

  if(newData == true)
  {
    Serial.print("Input: ");
    Serial.println(receivedChar);
    newData = false;
    if(receivedChar == '5')
    {
      xbee.send(zbTx5);
      Serial.println("To Router 5");
      delay(500);
    }
    else
    {
      Serial.println("Wrong Input Entered");
      delay(500);
    }
}


Code for the program B: Receiver Code (Same for both the transmitted programs)

Code: [Select]

#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
uint8_t count = 0;
String sample = "";
void setup()
{
  Serial.begin(9600);
  xbee.begin(Serial);
}
void loop()
{
  uint8_t receivedData[4];
  byte* dataPointer;
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  delay(500);
  if(xbee.getResponse().isAvailable())
  {
    Serial.print("Packet Available: ");
    Serial.println(count);
    delay(500);
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      delay(300);
      dataPointer = reinterpret_cast<byte *>(receivedData);
      for(int n =0; n < rx.getDataLength(); n++)
      {
        *dataPointer = rx.getData(n);
        dataPointer += 1;
      }
      Serial.print("Packet Received: ");
      for (byte n = 0; n < 4; n++)
      {
        Serial.println(receivedData[n]);
      }
      Serial.print("count: ");
      count++;
      Serial.println(count);
      delay(1500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
  else
  {
    Serial.println("Packet Not Found");
    delay(500);
  }
}


Output of the program B after the program AA transmitted:

Reading for Packet
Packet Available: 0
Packet Received: 54
14
44
92
count: 1
Reading for Packet
Packet Available: 1
Packet Received: 54
78
44
180
count: 2
Reading for Packet
Packet Available: 2
Packet Received: 54
218
44
236
count: 3

Output of the program B after the program AB transmitted:

Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found
Reading for Packet
Packet Not Found

Robin2

Sorry to say but the output I attached is from the Serial Monitor of the Arduino UNO and it doesn't allow me to copy paste the text.
I don't understand. This is a sample of the output from the Serial Monitor on my PC
Quote
========  For Slave 1  ========
  Data Sent ToSlv1  0  Tx failed

  ========  For Slave 2  ========
  Data Sent ToSlv2  0  Tx failed
Note that the output has nothing to do with your programs - it was just what happens to be on an Uno on my desk.

I will look at your programs.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

Having looked at your programs it is clear that the output of the program AB is essential if we are to make progress. As I said earlier I can't understand why you can't copy and paste it just like for the B program. But if you really cannot then do post the picture.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

#49
Aug 27, 2018, 06:55 pm Last Edit: Aug 27, 2018, 07:13 pm by Priyanshu27
Quote
Having looked at your programs it is clear that the output of the program AB is essential if we are to make progress. As I said earlier I can't understand why you can't copy and paste it just like for the B program.
NOTE: I'm attaching the output as an image only because you asked me to do so...

Output of the program AA transmitter: Transmitter_USB



Output of the program AB transmitter: Transmitter_XBEE



These highlighted boxes are the reason why I was not able to copy paste the output.

Also, the thing I told about jumpers for which I posted the link to understand, the program AA is when the jumpers are in USB Mode (when we want to upload the sketch in our Arduino) and the program AB is when the jumpers are in XBEE Mode (when we want to perform wireless communication between the XBee. I'm uploading the datasheet link again for you: Datasheet for XBee Shield

Robin2

#50
Aug 27, 2018, 07:24 pm Last Edit: Aug 27, 2018, 07:27 pm by Robin2
What line(s) of your programs are producing the funny characters? Can't you stop that?

The output in your first picture in Reply #49 is NOT the output from the program AA because the program AA does not contain any line that prints To Router 1. Please post the correct output.

How do I know that output that is supposed to come from program AB actually did so?

Take out the random numbers - just get it to send the same numbers every time - or maybe get it to send numbers that increment by 1 within a small range - say 100 to 110. And put some space between the numbers when you print the values that are being sent.

Does this
Quote
Also, the thing I told about jumpers for which I posted the link to understand, the program AA is when the jumpers are in USB Mode (when we want to upload the sketch in our Arduino) and the program AB is when the jumpers are in XBEE Mode
mean that there is some different setting for the two programs? Why are they not the same?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

#51
Aug 27, 2018, 08:43 pm Last Edit: Aug 27, 2018, 08:50 pm by Priyanshu27
Quote
What line(s) of your programs are producing the funny characters? Can't you stop that?
I genuinely don't know what lines are producing these characters, in starting I tried figuring it out that what exactly it symbolizes to but later on I gave up assuming that as a representation for me to understand that once I received these characters, it means it is transmitting.

Quote
The output in your first picture in Reply #49 is NOT the output from the program AA because the program AA does not contain any line that prints To Router 1
I just provided you this output for the understanding that why I was not able to copy paste the matter I was getting at the Serial Monitor because of those characters.

Quote
How do I know that output that is supposed to come from program AB actually did so?
If you run the program for the Receiver part (i.e., program B) for the AB transmitter program, you'll receive this output. And I've copy-pasted the output of both the programs AA and AB receiver part in Reply #46.

Quote
Take out the random numbers - just get it to send the same numbers every time - or maybe get it to send numbers that increment by 1 within a small range - say 100 to 110. And put some space between the numbers when you print the values that are being sent.
I've taken out the random numbers and replaced them with the numbers ranged from 100 to 105. Here's the code:

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t input = 0;
uint8_t i = 100;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
}

void loop()
{
  
  uint8_t data[4];
  uint8_t receivedChar = '0';
  boolean newData = false;
  while(i < 105)
  {
  data[0] = '6';
  data[1] = i;
  data[2] = ',';
  data[3] = ++i;
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  
  delay(500);
  Serial.println("Start Transmitting");
  Serial.print("Data Sent:\t");
  Serial.print(data[0]);
  Serial.print(data[1]);
  Serial.print(data[2]);
  Serial.println(data[3]);
  delay(500);
  xbee.send(zbTx5);
  Serial.println(": To Router 5");
  delay(500);
  }
}


Here instead of space, I've given a comma whose ASCII value is 44. So, when data is sent it is like this: 5410044101, which means data is sent from Router 6 (ASCII value: 54), 100 is the x-coordinate and 101 is the y-coordinate separated by a comma (ASCII Value: 44).

The output is like this:

Transmitted Output:



Received Output:



Here, you can the highlighted text I've done to see the data sent and the data received.

Quote
Does this mean that there is some different setting for the two programs? Why are they not the same?
No, there are two settings to get the output. Let say, I'm talking about the program AA which is working fine. So, when we want to upload the sketch or code we wrote in the Arduino UNO, we have to keep the setting in USB Mode and when we want to see the output at the receiver end that the data is wirelessly getting transmitted or not, we switch the setting to XBEE Mode of the SainSmart XBee-Arduino Shield.
You can see the details present on the Arduino Forum as well: ArduinoXbeeShield

Robin2

#52
Aug 27, 2018, 10:13 pm Last Edit: Aug 27, 2018, 10:13 pm by Robin2
Here, you can the highlighted text I've done to see the data sent and the data received.
That looks as if things are now working properly?


Quote
No, there are two settings to get the output. Let say, I'm talking about the program AA which is working fine. So, when we want to upload the sketch or code we wrote in the Arduino UNO, we have to keep the setting in USB Mode and when we want to see the output at the receiver end that the data is wirelessly getting transmitted or not, we switch the setting to XBEE Mode of the SainSmart XBee-Arduino Shield.
You can see the details present on the Arduino Forum as well: ArduinoXbeeShield
I don't know enough about XBees to make sense of this. It almost sounds as if you can't use the Xbee and talk to the PC at the same time - which seems ridiculous.

Have you found any example program that takes a message from the Serial Monitor and sends it to another XBee on another Arduino? If so, please post a link to it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Quote
That looks as if things are now working properly?
No, this thing is working when we are not taking input from the user. Means, it is broadcasting the message right now and the actual thing we want to do is unicasting in which we can select to which Router we want to send. For our common understanding, this is the output of the program AA, which was working. Now, we want to get the output of the program AB which I'm not getting.

Quote
I don't know enough about XBees to make sense of this. It almost sounds as if you can't use the Xbee and talk to the PC at the same time - which seems ridiculous.

Have you found any example program that takes a message from the Serial Monitor and sends it to another XBee on another Arduino? If so, please post a link to it.
No, I tried searching almost everywhere, but couldn't find any example of the kind I want. Have even search regarding the transmission in XBEE Mode from the input taken from the Serial Monitor. But, I was not able to find enough outputs to claim that I can resolve this problem.

Robin2

I reckon I have exhausted all the ideas I have thought of. Sorry.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

I know, even I tried searching almost everywhere that why it is not working by searching for a keyword "How to get input from Serial Monitor of Arduino in XBee Mode". But, I couldn't find any relevant answer for getting that problem sorted.

Anyways, if you can help me by looking for it on the web and provide me a link to see into that. I know the inner details of it just that two brains are better than applying one brain in one direction. It will be a great help as maybe I missed something while searching which just in case you find out.

Robin2

As far as I can see Googling arduino xbee example throws up lots of examples on the first page.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Yeah, those examples are suggesting how can we transmit and receive using XBee. But, my question is how can transmit and receive using XBee with the help of Serial Monitor for which also I found examples to it but not that relevant which can resolve the doubt.

Robin2

But, my question is how can transmit and receive using XBee with the help of Serial Monitor for which also I found examples to it
Post a link to the example that seems most promising.

Better still, try the example before posting and tell us how it works.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

After a very thorough search, I finally asked my professor about it. To which she said that we don't want input from the user, we want to make Robots search and send autonomously.

It means, suppose we've two six XBee and there are moving in some range from one another, now whenever any Robot comes in a particular range of the sensor (in the actual project, currently I don't have any location which we can get from the sensor, so have to take a range on own) it should transmit location to that Robot.

Can you suggest me some idea of how to send autonomously as I didn't get properly as how to select the Router on its own to which the message to be sent?

Go Up