Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: Priyanshu27 on Aug 16, 2018, 09:09 pm

Title: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 16, 2018, 09:09 pm
Hi,

I'm using XBee Pro 900 HP DigiMesh kit for having serial communication between them using Arduino UNO. I want to update my data everytime loop initiates, i.e., the data I'm passing in ZBTxRequest() is to be updated as shown below:

uint8_t data[] = "This is Router 1!";

ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));


Now, since the data type of data is uint8_t and I want to update its value. So, what shall I do for that?

Note: One option I thought is of storing some value in a String and then typecast it into uint8_t and then pass it to data. If we can do something simpler, please suggest me.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 16, 2018, 09:47 pm
Note: One option I thought is of storing some value in a String and then typecast it into uint8_t and then pass it to data. If we can do something simpler, please suggest me.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings (http://www.cplusplus.com/reference/cstring/) - char arrays terminated with '\0' (NULL).

Quote
Code: [Select]
uint8_t data[] = "This is Router 1!";
Now, since the data type of data is uint8_t and I want to update its value. So, what shall I do for that?
You have not told us what change you want to make.

If, for example. you just want to change the '1' to a '2' you can do that with
Code: [Select]
data[15] = '2';
(hope I have counted correctly).
If you want to make a bigger change you could use some the cstring functions (http://www.cplusplus.com/reference/cstring/)

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 16, 2018, 10:34 pm
Hello Robin,

Thanks for your suggestion. I'll look into the cstring functions you suggested.

For the actual project, I need to update a location of the Lego Robot on which Arduino and XBee are mounted using a color sensor with the help of a Pixel Map.

However, for now, I just need to update data on a random basis, you can say I have to create a counter so that in every iteration it updates the value of data and transmit it to other XBees.

Did I mention everything you need or more information is required?
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 17, 2018, 10:38 am
However, for now, I just need to update data on a random basis,
You start with a value of This is Router 1!

Can you give some examples of what you want to change that to?

Be very very careful not to try to save more characters in the array as that will screw things up by overwriting other parts of memory. Or, put another way, make sure the array is defined to be be a bit bigger than the biggest text you want it to hold.

...R

Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 17, 2018, 05:28 pm
You start with a value of This is Router 1!

Can you give some examples of what you want to change that to?

For example, if I want to change from "This is Router 1!" to some different value let say some numeric value. Now, I want to update it by either incrementing it or changing into some random value using rand() function. So, what is the correct way of doing it?

Quote
Be very very careful not to try to save more characters in the array as that will screw things up by overwriting other parts of memory. Or, put another way, make sure the array is defined to be be a bit bigger than the biggest text you want it to hold.

I tried one thing by using atoi() function, I created a new string which was typecasted into int, but since my data is of the datatype uint8_t, the value I got from the string is of int datatype. So, I'm facing issues stating that:

incompatible types in assignment of 'int' to 'uint8_t [18]'

The code for which I got the error is:

uint8_t data[] = "This is Router 1!";
char buf[myString.length()+1];
myString.toCharArray(buf, myString.length()+1);
data = ((uint8_t*)myString, strlen(myString)); -------------> In this line I was getting the error.

An alternative to that, I tried one more thing:

String myString = "By Priyanshu";
data = atoi(myString.c_str()); -------------> In this line also I got the same error.


If anyone can provide a solution to this, my problem of creating a counter in the next step will be resolved. As the preliminary stage of creating a counter is the string I want to change is to store in the data of datatype uint8_t.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 17, 2018, 06:00 pm
For example, if I want to change from "This is Router 1!" to some different value let say some numeric value. Now, I want to update it by either incrementing it or changing into some random value using rand() function. So, what is the correct way of doing it?
You are still being too vague.

Give one or two examples of what the data might look like after you have changed it from This is Router 1!



And if you want help with a program problem please post the complete program.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 17, 2018, 06:20 pm
You are still being too vague.

Give one or two examples of what the data might look like after you have changed it from This is Router 1!



And if you want help with a program problem please post the complete program.

...R
Sorry for being vague. I post my code so that you can look into it.

Quote
#include <XBee.h>
#include <stdlib.h>
#include <string.h>
XBee xbee = XBee();
XBee xbee1 = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0X4147FE2A);
//XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
//XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
//XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
String sample;
uint8_t count = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  Serial.println("Start Transmitting");
}

void loop()
{
  uint8_t i = 0;
  uint8_t data[] = "HI, I'M Router 5!";
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  sample = "";
  while(i<1)
  {
    xbee.send(zbTx6);
    Serial.println("To Router 6");
    delay(1000);
    i++;
  }

  //Convert a string into uint8_t
  //String myString = "By Priyanshu";
  //data = atoi(myString.c_str());
  //char buf[myString.length()+1];
  //myString.toCharArray(buf, myString.length()+1);
  //data = ((uint8_t*)myString, strlen(myString));


  xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      for(int i=0; i<rx.getDataLength(); i++)
      {
        sample+= (char)rx.getData(i);
      }
      Serial.print("From Router 5: ");
      Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);
}

Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 17, 2018, 10:19 pm
Sorry for being vague. I post my code so that you can look into it.

You have still not answered my simple request for some examples of the changes you want to make. The examples have nothing to do with the code to cause those changes. Until I know what you want to achieve I can't help.

And to make it easy for people to help you please modify your post and use the code button </>
Code: [Select]
so your code looks like this
 and is easy to copy to a text editor. See How to use the Forum (http://forum.arduino.cc/index.php?topic=149014.0)

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 17, 2018, 10:58 pm
As you see in the following code, I've passed "HI, I'M Router 5!" in the data field, which is an unsigned character array.

Code: [Select]

#include <XBee.h>
#include <stdlib.h>
#include <string.h>
XBee xbee = XBee();
XBee xbee1 = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0X4147FE2A);
//XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
//XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
//XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
String sample;
uint8_t count = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  Serial.println("Start Transmitting");
}

void loop()
{
  uint8_t i = 0;
  uint8_t data[] = "HI, I'M Router 5!";
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  sample = "";
  while(i<1)
  {
    xbee.send(zbTx6);
    Serial.println("To Router 6");
    delay(1000);
    i++;
  }

  //Convert a string into uint8_t
  //String myString = "By Priyanshu";
  //data = atoi(myString.c_str());
  //char buf[myString.length()+1];
  //myString.toCharArray(buf, myString.length()+1);
  //data = ((uint8_t*)buf, strlen(buf));

  xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      for(int i=0; i<rx.getDataLength(); i++)
      {
        sample+= (char)rx.getData(i);
      }
      Serial.print("From Router 5: ");
      Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);
}

I need help exactly at this point:

Code: [Select]

  //Convert a string into uint8_t
  //String myString = "By Priyanshu";
  //data = atoi(myString.c_str());
  //char buf[myString.length()+1];
  //myString.toCharArray(buf, myString.length()+1);
  //data = ((uint8_t*)buf, strlen(buf));

The error I was getting when I uncommented this above piece of code is:

incompatible types in assignment of 'size_t {aka unsigned int}' to 'uint8_t [18] {aka unsigned char [18]}'


Now, my question is I've to update my location at every point where on the color map where I'll be getting the sensor value in x and y coordinates.

Now, suppose if you replace the message "HI, I'M Router 5!" to some integer but it should be a character array. And, the new data which will be getting after getting values from the sensor needs to be updated in the data[] also known as payload[] which is transmitted from one XBee to another.

For example: At present, the Robot is present at (24, 50) coordinates :

uint8_t data[] = "24, 50"; --------->   //The coordinate axis at which the LEGO Robot is standing

now when the loop initiates again or Robot moves from its place,

the new coordinates are 36, 80 at which the LEGO Robot is present.

So I want my data to be updated to:

uint8_t data[] = "36, 80"; --------->   //New value of the LEGO Robot.

Similarly, every time the loop initiates the LEGO Robot should update its location and transmit it to the other LEGO Robots.



I hope I'm clear this time. Sorry, since not having much experience so, don't know how to ask exactly on the forum.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 18, 2018, 10:01 am
I hope I'm clear this time.
Sorry, No. You seem to be making heavy weather of my simple question.

You start with data[] = "HI, I'M Router 5!";  

All I want to know is what the HI, I'M Router 5! should be changed to?

For example, should it change to "Elephants" or to "GoodBye" or to "123 cats" or what?


(and thanks for posting the code in code tags).

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 18, 2018, 08:28 pm
Quote
You start with data[] = "HI, I'M Router 5!"; 
For instance, I started with the data containing a location of let say some coordinates instead of "HI, I'M Router 5!" and now I want to update its location next time loop initiates.

Quote
All I want to know is what the HI, I'M Router 5! should be changed to?

For example, should it change to "Elephants" or to "GoodBye" or to "123 cats" or what?
Yeah, you can say if next time loop initiates, we change it to "Elephant". Then again next time it changes to "123 cats", and after that "Ice-cream". However, they are so random changes which I know not possible to be done, but can we update the data already stored into something different, next time the loop initiates. This is my question.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 18, 2018, 09:37 pm
Yeah, you can say if next time loop initiates, we change it to "Elephant". Then again next time it changes to "123 cats", and after that "Ice-cream". However, they are so random changes which I know not possible to be done, but can we update the data already stored into something different, next time the loop initiates. This is my question.
I suspect you don't really want to change it to "Elephant" or Ice-cream" and I am beginning to think that there is some specific reason why you are not prepared to say what you do want to change it to. Perhaps it is for an illegal or anti-social purpose and if so I don't intend to help.

This is the last time of asking - give some actual examples of what you want to put into the array in place of HI, I'M Router 5!

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 18, 2018, 11:28 pm
Quote
I suspect you don't really want to change it to "Elephant" or Ice-cream" and I am beginning to think that there is some specific reason why you are not prepared to say what you do want to change it to. Perhaps it is for an illegal or anti-social purpose and if so I don't intend to help.
Yeah, I don't want to change data to "Elephant or Ice-cream". As I just want a location in terms of coordinates to get updated. I tell you my actual project. There are 6 LEGO EV3 Mindstorm Robots on which Arduino UNO is mounted. Now, there is color sensor used on a pixel map made from either two colors of the RGB colors so that we can use every pixel as a coordinate. Also, there is an ultrasonic sensor to detect the presence of other sensor and by calculating the distance between two robots, we use Smooth Turn Mobility model for collision avoidance and path navigation. XBees are used to transfer the data wirelessly.

Quote
This is the last time of asking - give some actual examples of what you want to put into the array in place of HI, I'M Router 5!
For once, please don't misunderstand that I want to send the data as "HI, I'M Router 5!". I gave it in the code for understanding that at exactly which place we want to transfer. In the actual project, I shall be reading the sensor data of a gyro sensor or an ultrasonic sensor and get the coordinates.

For example, as I mentioned of sending coordinates in data[] = "37, 19". Next time loop initiates, the robot position changed to (57,78) so I want data to be updated as data[] = "57, 78".
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 19, 2018, 10:26 am
For example, as I mentioned of sending coordinates in data[] = "37, 19". Next time loop initiates, the robot position changed to (57,78) so I want data to be updated as data[] = "57, 78".
Hooray --- Finally ---

And, as I suspected when I asked back in Reply #1, there is a much easier way to send the data you actually want to send

Try something like this (I don't have an Xbee so I may have not got the syntax perfectly correct
Code: [Select]
xVal = 37;
yVal = 19;
myXbee.print(xVal);
myXbee.print(','); // comma
myXbee.print(yVal);


...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 20, 2018, 05:51 pm
Quote
Try something like this
By this way we can't print values, however, I find out a solution to it that I can generate random values between 1 and 100 to get a coordinates value. But, the place I got stuck with this is there are 6 XBee connected to each other and all are sending data to each other. Then how do we get to know that from which XBee we are getting our values?

Code: [Select]
#include <stdio.h>
#include <time.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Initializing the program");
  srand((unsigned)time(NULL));
}

void loop() {
  // put your main code here, to run repeatedly:
  uint8_t i = 0;
  //srand((unsigned)time(NULL));
  while(i<5)
  {
    uint8_t x = rand()%100;
    uint8_t y = rand()%100;
    uint8_t xy[] = {x,y};
    Serial.print(xy[0],xy[1]);
    Serial.println();
    i++;
  }
}

Can you suggest some way for it.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 20, 2018, 06:09 pm
By this way we can't print values
I don't understand that - what can you do? I have not used XBees myself. Do you mean that xbee.print() is not valid? If so, what is the correct syntax? My general understanding is that XBees just send serial data - but maybe I am wrong.

On the other hand maybe you can put your values into an array of ints and send that?

Can you post a link to the documentation for the Xbee library that you are using?


Quote
But, the place I got stuck with this is there are 6 XBee connected to each other and all are sending data to each other. Then how do we get to know that from which XBee we are getting our values?
Include in each message an ID number for the sender.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 20, 2018, 07:14 pm
Quote
Can you post a link to the documentation for the Xbee library that you are using?
Link for the documentation:

https://github.com/andrewrapp/xbee-arduino (https://github.com/andrewrapp/xbee-arduino)

Quote
Do you mean that xbee.print() is not valid?
Yes, because there is no print function defined in xbee library.

Quote
nclude in each message an ID number for the sender
Can you provide an example of it because I was not able to understand it?
I tried passing a message along with the random values to which I got an error message.

no matching function for call to 'HardwareSerial::print(uint8_t&, uint8_t&, uint8_t&)'
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 20, 2018, 07:53 pm
Thanks for the link. It's very irritating that there is no online documentation so I had to download the library. And the documentation is crap - it does not explain "send" at all. But there are some examples and it seems I have been barking up the wrong tree (not for the first time).

If you want to send (say) three numbers then create an array, make an XBee object with it, put the values you want into the array, and send the array. Like this, I think

Code: [Select]
int myArray[3];
ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, myArray, sizeof(myArray));
myArray[0] = thisXBeeID; //   I assume you will give each one a different number
myArray[1] = xVal;
myArray[2] = yVal;
xbee.send(zbTx5);


...R

PS ... where did you get the idea for such cryptic names for your variables :)
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 20, 2018, 08:19 pm
Quote
But there are some examples and it seems I have been barking up the wrong tree (not for the first time).
Sorry Man, I'm not an expert at coding. But, I'm trying to ask in the easiest manner I can.

Quote
If you want to send (say) three numbers then create an array, make an XBee object with it, put the values you want into the array, and send the array.
I tried it earlier when I got an error.

Code: [Select]
uint8_t xy[3];
xy[0] = "XBeeId 1:";
xy[1]= rand()%100;
xy[2] = rand()%100;
Serial.print(xy[0],xy[1],xy[2]);
//Serial.print(xy[0]);
//Serial.print(",");
//Serial.print(xy[1]);
//Serial.println(xy[2]);
ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, xy, sizeof(xy));
xbee.send(zbTx5);

Error Message:

no matching function for call to 'HardwareSerial::print(uint8_t&, uint8_t&, uint8_t&)'

The output I want in return should be like in this format:
    XBeeId 1: 28,34

And same way I want to get at the receiver side.

Is it possible to print? Sorry for asking such silly questions.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 20, 2018, 09:05 pm
Error Message:

no matching function for call to 'HardwareSerial::print(uint8_t&, uint8_t&, uint8_t&)'
You need to post the complete program so we can see how all the variables are defined.

Quote
The output I want in return should be like in this format:
    XBeeId 1: 28,34

And same way I want to get at the receiver side.
I don't believe there is any need for the style  XBeeId 1: 28,34. All that needs to be sent is 3 numbers in an array with no colons or commas or text. The receiving program will be written to expect that and everything will be much simpler.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 20, 2018, 09:38 pm
Quote
You need to post the complete program so we can see how all the variables are defined.
Code: [Select]
#include <XBee.h>
#include <stdlib.h>
#include <time.h>
XBee xbee = XBee();
XBee xbee1 = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0X4147FE2A);
//XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
//XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
//XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
//uint8_t sample;
uint8_t count = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  Serial.println("Start Transmitting");
  srand((unsigned)time(NULL));
}

void loop()
{
  uint8_t i = 0;
  uint8_t xy[3];
  //Serial.print("Data Sent: ");
  xy[0] = "XBeeId:";
  xy[1] = rand()%10;
  xy[2] = rand()%10;
  Serial.print(xy[0]);
  Serial.print(xy[1]);
  Serial.print(",");
  Serial.println(xy[2]);
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, xy, sizeof(xy));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, xy, sizeof(xy));
  //sample = "";
  while(i<1)
  {
    xbee.send(zbTx5);
    Serial.println("... To Router 5 ...");
    delay(1000);
    i++;
  }
  xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      Serial.print("Data Received: ");
      for(int i=0; i<rx.getDataLength(); i++)
      {
        //sample+= (char)rx.getData(i);
        Serial.print(rx.getData(i),DEC);
      }
      //Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);
}


Quote
I don't believe there is any need for the style  XBeeId 1: 28,34
Yeah, we don't need to style like this but by just sending three numbers there may be a possibility of reading wrong coordinates.

For example, suppose we sending data from Router 1 to Router 5 with a location of (2,34). So, the data received at Router 5 will be 1234. Here, we can say 1 stands for Router 1 but for the coordinates, we may read as (23,4) or (2,34). So we can read an incorrect location as well. How to get rid of that?
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 20, 2018, 10:45 pm
Assuming the code in Reply #20 is what gave rise to the error message in Reply #18, I can't figure out what the problem might be without knowing the line number reported along with the error message. Post the complete error message.

Quote
For example, suppose we sending data from Router 1 to Router 5 with a location of (2,34). So, the data received at Router 5 will be 1234.
There will be no ambiguity. On the receiving side there will also be an array with 3 elements. receivedData[0] will have the ID number; receivedData[1] will have the xValue and receivedData[2] will have the yValue. (Obviously you can choose your own array name)

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 20, 2018, 11:02 pm
Quote
Post the complete error message.
This was the complete error message:

error: no matching function for call to 'HardwareSerial::print(uint8_t&, uint8_t&, uint8_t&)'

     Serial.print(xy[0],xy[1],xy[2]);


Code: [Select]

xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      Serial.print("Data Received: ");
      for(int i=0; i<rx.getDataLength(); i++)
      {
        //sample+= (char)rx.getData(i);
        Serial.print(rx.getData(i),DEC);
      }
      //Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);


Quote
On the receiving side there will also be an array with 3 elements
As you see in the code, we are getting the received data by using  (rx.getData(i), DEC). I guess, but not sure if it is getting data bit by bit or it taking data as a packet. So, can you suggest a way of getting a data packet in form of an array with three elements?
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 21, 2018, 10:06 am
This was the complete error message:

error: no matching function for call to 'HardwareSerial::print(uint8_t&, uint8_t&, uint8_t&)'

     Serial.print(xy[0],xy[1],xy[2]);


Somehow I think there is more. Never mind the fancy orange colour - just copy and paste the error message from the Arduino IDE.



Quote
As you see in the code,
I can't see because you have not posted the complete program.

And I presume you have realized that if you change the way you send data you also have to change the way it is received so that the receiving code matches the sending code.

Post the latest versions of your sending and receiving programs. There is no value looking at only one of them. That's like one-handed clapping.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 22, 2018, 12:02 am
Quote
just copy and paste the error message from the Arduino IDE.
That was the entire error I was getting. I've copied and pasted it as it is. Just highlighted it with a fancy orange color so that you can see it.

Quote
I can't see because you have not posted the complete program.
If you see Reply #20, I've posted my complete updated code. However, in Reply #22 I posted on receiver code again because I wanted to ask problem from that part of the code.

Quote
And I presume you have realized that if you change the way you send data you also have to change the way it is received so that the receiving code matches the sending code
If you see my entire code, as said by you to assign an ID no. to every router so that we can know from which router data is coming. But the issue I was facing on the receiver side is that we are getting data bit by bit and I'm not able to collect the data coming from the respective array into different arrays. If you can see the code and tell me how to create arrays at the receiving end and how to store the incoming data into it, it will be a huge huge help.

I'm attaching my complete code again just in case you face problem in going back and forth for different replies.

Code: [Select]

#include <XBee.h>
#include <stdlib.h>
#include <time.h>
XBee xbee = XBee();
XBee xbee1 = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0X4147FE2A);
//XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
//XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
//XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
//uint8_t sample;
uint8_t count = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  Serial.println("Start Transmitting");
  srand((unsigned)time(NULL));
}

void loop()
{
  uint8_t i = 0;
  uint8_t xy[3];
  //Serial.print("Data Sent: ");
  xy[0] = "XBeeId:";
  xy[1] = rand()%10;
  xy[2] = rand()%10;
  Serial.print(xy[0]);
  Serial.print(xy[1]);
  Serial.print(",");
  Serial.println(xy[2]);
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, xy, sizeof(xy));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, xy, sizeof(xy));
  //sample = "";
  while(i<1)
  {
    xbee.send(zbTx5);
    Serial.println("... To Router 5 ...");
    delay(1000);
    i++;
  }
  xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      Serial.print("Data Received: ");
      for(int i=0; i<rx.getDataLength(); i++)
      {
        //sample+= (char)rx.getData(i);
        Serial.print(rx.getData(i),DEC);
      }
      //Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);
}


The data which we are taking at the receiver end is in this manner:

Code: [Select]

 if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      Serial.print("Data Received: ");
     for(int i=0; i<rx.getDataLength(); i++)          //This the loop for taking the receiving values.
      {
        //sample+= (char)rx.getData(i);
        Serial.print(rx.getData(i),DEC);              //Here, we are printing the received value
      }
      //Serial.println(sample);
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 22, 2018, 12:21 am
I'm attaching my complete code again just in case you face problem in going back and forth for different replies.
Thank you for that.

However you seem to have posted the complete transmitting program but only part of the receiving program. Please post two complete programs.

And I remain sceptical about the completeness of your error message. I deliberately caused an error in a simple program and this is the message I got
Code: [Select]
ArduinoTemp.ino: In function 'void loop()':
ArduinoTemp.ino:22:20: error: 'low' was not declared in this scope
Error compiling.

You can see that it has identified the name of the program, the function where the error was and the line and character number where it was detected.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 22, 2018, 05:57 pm
Quote
However you seem to have posted the complete transmitting program but only part of the receiving program. Please post two complete programs.
There are no two codes as I'm using this code as the complete code. However, you may be finding the receiver code incomplete because I might have written an incomplete code for receiving the data. As my coding is not strong, I'm seeking help from the forum if I can get the completeness in my code.

Quote
And I remain skeptical about the completeness of your error message
Code: [Select]

#include <XBee.h>
#include <stdlib.h>
#include <time.h>
XBee xbee = XBee();
XBee xbee1 = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0X4147FE2A);
//XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
//XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
//XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
String sample;
uint8_t count = 0;
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  Serial.println("Start Transmitting");
  srand((unsigned)time(NULL));
}

void loop()
{
  uint8_t i = 0;
  uint8_t x = rand()%10;
  uint8_t y = rand()%10;
  uint8_t c = "XBeeId 1:";
  uint8_t data[] = {x,y};
  Serial.print(c,data[0],data[1]);
  //uint8_t data[] = "HI, I'M Router 5!";
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  sample = "";
  while(i<1)
  {
    xbee.send(zbTx6);
    Serial.println("To Router 6");
    delay(1000);
    i++;
  }
  //String myString = "By Priyanshu";
  //data = atoi(myString.c_str());
  //char buf[myString.length()+1];
  //myString.toCharArray(buf, myString.length()+1);
  //data = ((uint8_t*)buf, strlen(buf));
  xbee.readPacket(500);
  Serial.println("Packet Detected");
  Serial.print("count: ");
  Serial.println(count);
  if(xbee.getResponse().isAvailable())
  {
    Serial.println("Packet Arrived");
    Serial.print("count: ");
    Serial.println(count);
    //Serial.println(xbee.getResponse().getApiId());
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      Serial.print("From Router 6: ");
      xbee.getResponse().getZBRxResponse(rx);
      for(int i=0; i<rx.getDataLength(); i++)
      {
        //sample+= (char)rx.getData(i);
        Serial.println(rx.getData(i),DEC);
      }
      count++;
      Serial.print("count: ");
      Serial.println(count);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  }
  delay(1000);
}



Try this code as I run on my machine and I'm getting this error. However, my objective from this error message is that at the transmitting end I'm able to send an ID no. but cannot differentiate that the coordinates I'm sending, how to figure it out.

As the random values, I'm generating as of now is between 0 to 9 just for the understanding purpose. But, in future, I shall be sending between 0 to 255 as there are in total 256 pixels of any color. So at that time how do we segregate the coordinates. Like the example, I gave earlier we are sending ID no. (say) 5 so we know it's ASCII value is 53 and the coordinates are (12,206). The output I'm getting is 5312206. So, how do we know if it is (12,206) or (122,06)? That's the problem I'm facing.

I know I've made it a big hype out of it, but as I mentioned my coding is not strong and this is an entirely new project for me so I'm facing a lot of challenges with it. You people being expert and thoroughly active on the forum to help the rookie coders might help me with it.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 22, 2018, 07:09 pm
There are no two codes as I'm using this code as the complete code.
You will make life a great deal easier for yourself if you create two separate programs - one for transmit and one for receive. At least do that until you are know how to get the communication working. Just make the simplest programs possible that will send and receive the data in the format you require.

It will also make it a great deal easier to help you.

When you have it working then you can combine the code into a single program if you wish.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 23, 2018, 06:01 pm
Hi Robin, I've resolved all the previous problems and now the data was transmitting the way I wanted. There was a change in the requirement of the project as earlier I was sending a broadcasted message, now I need to send as a unicast message. Means that now we have to take value from the user to send the message to the desired router. This thing should be done autonomously which I shall be seeing when an actual sensor is attached instead of generating a random number.

I used a help from your topic on "Serial Input Basics - updated (https://forum.arduino.cc/index.php?topic=396450.0)" which helped me in taking input from the serial monitor and I must say it was a great help.

Now, the problem I'm facing is that at the time of taking input in the XBee mode of the Sainsmart Arduino-XBee Shield which is used to communicate between XBee and Arduino. Whatever, input I'm taking is not getting displayed on the serial monitor. So, my question is, do we have to apply a similar code at the receiving end just like we did at the transmitting end or that code works fine and I'm doing mistake at somewhere else.

I'm attaching my code for your reference.

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t count = 0;
uint8_t input = 0;
String sample = "";
void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
}

void loop()
{
  delay(1000);
  transmitXBee();
  delay(1000);
  receiveXBee();
  delay(1000);
}

void transmitXBee()
{
  uint8_t i = 0;
  uint8_t data[3];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  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;
    switch(receivedChar)
    {
      case '1':
        Serial.println("To Router 1");
        xbee.send(zbTx1);
        Serial.println();
        delay(500);
        break;
      case '2':
        Serial.println("To Router 2");
        xbee.send(zbTx2);
        Serial.println();
        delay(500);
        break;
      case '3':
        Serial.println("To Router 3");
        xbee.send(zbTx3);
        Serial.println();
        delay(500);
        break;
      case '4':
        Serial.println("To Router 4");
        xbee.send(zbTx4);
        Serial.println();
        delay(500);
        break;
      case '5':
        Serial.println("To Router 5");
        xbee.send(zbTx5);
        Serial.println();
        delay(500);
        break;
      default:
        Serial.println("Wrong input entered");
        break;
    }
  }
}

void receiveXBee()
{
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  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);
     
      for(int i=0; i<rx.getDataLength(); i++)
      {
        sample += (char)rx.getData(i);
        //Serial.print(rx.getData(i),DEC);
      }
      Serial.print("count: ");
      count++;
      Serial.println(count);
      Serial.print("Packet Received: ");
      Serial.println(sample);
      delay(1500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
}
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 23, 2018, 06:14 pm
So, my question is, do we have to apply a similar code at the receiving end just like we did at the transmitting end or that code works fine and I'm doing mistake at somewhere else.

I'm attaching my code for your reference.
I have several times asked you to post BOTH the sending and the receiving code. Without seeing both how can I answer your question.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 23, 2018, 06:32 pm
If you see the code in Reply #28, in the loop there are two functions called, one for the transmitXBee and other is for receiveXBee. I'm using only and only this code for exchanging messages as the Arduino UNO has to be mounted on the top of the LEGO Robot and I've to merge my code for taking values from the sensor as well as have to transmit and receive the data to other XBee.

However, as asked by you to make two programs till the time it is sure if both the codes are working fine and merge them after that. I'm attaching my transmitter and receiver code separately.

Transmitter Code:

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
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 data[3];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  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;
    switch(receivedChar)
    {
      case '1':
        Serial.println("To Router 1");
        xbee.send(zbTx1);
        Serial.println();
        delay(500);
        break;
      case '2':
        Serial.println("To Router 2");
        xbee.send(zbTx2);
        Serial.println();
        delay(500);
        break;
      case '3':
        Serial.println("To Router 3");
        xbee.send(zbTx3);
        Serial.println();
        delay(500);
        break;
      case '4':
        Serial.println("To Router 4");
        xbee.send(zbTx4);
        Serial.println();
        delay(500);
        break;
      case '5':
        Serial.println("To Router 5");
        xbee.send(zbTx5);
        Serial.println();
        delay(500);
        break;
      default:
        Serial.println("Wrong input entered");
        break;
    }
  }
}


Receiver Code:

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()
{
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  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);
     
      for(int i=0; i<rx.getDataLength(); i++)
      {
        sample += (char)rx.getData(i);
        //Serial.print(rx.getData(i),DEC);
      }
      Serial.print("count: ");
      count++;
      Serial.println(count);
      Serial.print("Packet Received: ");
      Serial.println(sample);
      delay(1500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
}


Hope this will help your query which I might missed.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 23, 2018, 08:09 pm
Thanks, that's a big help. However you have not told us exactly what happens when you try that pair of programs.

Also, if this was my problem I would take everything out of the Tx program except the minimum needed to send a message to a single Rx. Simplify, Simplify, Simplify.

And you seem to be sending the contents of the array data[] but I don't see any corresponding array in the Rx program.

I have been having another look at the XBee library documentation and examples. IMHO it is all a bit of dog's breakfast.

It looks like you can send an array[] easily but there is no easy way to receive it - so I can't even be sure that it will be sent :)

Assuming your array data[]is sent correctly then what is received in the payload are the bytes that make up the array. This is a wild guess, but it may work.

... I'll need a little time to figure this out - will reply again asap

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 23, 2018, 08:42 pm
Quote
However you have not told us exactly what happens when you try that pair of programs
I mentioned my question in Reply #28 but, I'll state again for your easiness. I'm using a Sainsmart XBee-Arduino Shield which is used to make communication between XBee and Arduino. So there are two jumpers on that shield stating USB/XBEE. So when you have to upload an Arduino Code place the jumpers in USB Mode and when you want to make communication wirelessly, switch them to XBEE Mode.

So, when the jumpers are in USB Mode, the transmitted code is working fine and I'm able to take input from the user. But, when I'm switching the jumpers to XBEE Mode, neither it accepting any serial input nor it is displaying anything at the receiver end (which I understand since it didn't take any input at transmitter end so how it will show anything at the receiver end). So, do I have to apply some similar code I applied at the transmitter end on the receiver side as well or I'm missing any function or library because of which communication will be possible (I have no idea if I would be missing as I'm having zero experience with XBee)?

Quote
Assuming your array data[]is sent correctly then what is received in the payload are the bytes that make up the array. This is a wild guess, but it may work.
Yeah, kind of you are correct. As on the transmitter end if I'm sending from the router (say) 6, and the coordinates are (123,230). So the data which is sent is visible as 5412344230, which means, 54 is the ASCII value of symbol '6', 123 and 230 are the coordinates of the random location and 44 is the ASCII value of ','. So we can understand the coordinates with the help of seeing the ASCII value as 44. So we are sending a data array but at the receiver, we are taking it bitwise and because of lack of knowledge, I'm not able to figure out how to receive the data as sent.

Quote
... I'll need a little time to figure this out - will reply again asap
Sure, you can have your time and meanwhile, I also search for some option by searching through some pages and groups available online. I'll update immediately if I get anything.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 23, 2018, 09:40 pm
The following contains a simple technique that I have used previously but had forgotten the details. And I have been meaning for some time to refresh my knowldege.

At the top of your RX program add these two definitions
Code: [Select]
int receivedData[3];
byte* dataPointer;


and then replace this piece of code
Code: [Select]
      for(int i=0; i<rx.getDataLength(); i++)
      {
        sample += (char)rx.getData(i);
        //Serial.print(rx.getData(i),DEC);
      }
      Serial.print("count: ");
      count++;
      Serial.println(count);
      Serial.print("Packet Received: ");
      Serial.println(sample);


with the following
Code: [Select]
    dataPointer = reinterpret_cast<byte *>(receivedData);
    for(int n =0; n < rx.getDataLength(); n++)
    {
        *dataPointer = rx.getData(n);
        dataPointer += 1;
    }
    for (byte n = 0; n < 3; n++) {
        Serial.println(receivedData[n]);
    }


This may look complicated (thanks to C++'s cryptic language) but what it does is actually very simple. It figures out the memory address of the start of the array (in the variable dataPointer) and then writes each successive received byte into the memory addresses of the array. Hopefully that will replicate exactly the array that was sent.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 24, 2018, 06:54 pm
Hi Robin, I appreciate you gave me a solution for the receiver mode to store the data transmitted in an array. But for checking that there was some problem at the transmitting end as well which I'm not able to figure out.

Quote
'm using a Sainsmart XBee-Arduino Shield which is used to make communication between XBee and Arduino. So there are two jumpers on that shield stating USB/XBEE. So when you have to upload an Arduino Code place the jumpers in USB Mode and when you want to make communication wirelessly, switch them to XBEE Mode.

So, when the jumpers are in USB Mode, the transmitted code is working fine and I'm able to take input from the user. But, when I'm switching the jumpers to XBEE Mode, neither it accepting any serial input nor it is displaying anything at the receiver end (which I understand since it didn't take any input at transmitter end so how it will show anything at the receiver end)
I explained the whole procedure of how to send a message wirelessly through an XBee it in my previous Reply #32. So, when I type anything in USB Mode it is taking the input entered by me, however, when I type in XBee Mode when the XBee has to transmit wirelessly it is not taking any input.

I tried searching in the documentation of the XBee provided by DIGI "Datasheet for XBee 900HP DigiMesh Kit" (https://www.digi.com/resources/documentation/digidocs/pdfs/90002173.pdf) and few other places on the internet like StackExchange, and the Arduino Forum itself, but couldn't find any relevant information on that. There may be a possibility that I have missed something while searching, however, almost every place it states that I need to configure my XBee through XCTU to make communication possible which I already did.

Can you look something for me, it will be a great help. So far, you helped me a lot because of which I can see some progress in my project.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 24, 2018, 07:33 pm
Hi Robin, I appreciate you gave me a solution for the receiver mode to store the data transmitted in an array. But for checking that there was some problem at the transmitting end as well which I'm not able to figure out.
I am not an XBee expert but I will try to help. Please post the latest version of your Tx and Rx programs that take account of my earlier advice. And tell us in detail what happens when you run them (or post error messages if they won't compile).

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 24, 2018, 08:32 pm
Quote
Please post the latest version of your Tx and Rx programs that take account of my earlier advice
Transmitter Code:
Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
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 data[3];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  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;
    switch(receivedChar)
    {
      case '1':
        Serial.println("To Router 1");
        xbee.send(zbTx1);
        Serial.println();
        delay(500);
        break;
      case '2':
        Serial.println("To Router 2");
        xbee.send(zbTx2);
        Serial.println();
        delay(500);
        break;
      case '3':
        Serial.println("To Router 3");
        xbee.send(zbTx3);
        Serial.println();
        delay(500);
        break;
      case '4':
        Serial.println("To Router 4");
        xbee.send(zbTx4);
        Serial.println();
        delay(500);
        break;
      case '5':
        Serial.println("To Router 5");
        xbee.send(zbTx5);
        Serial.println();
        delay(500);
        break;
      default:
        Serial.println("Wrong input entered");
        break;
    }
  }
}


Receiver Code:
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()
{
  int receivedData[3];
  byte* dataPointer;
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  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 < 3; 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);
  }
}


I'm attaching my output on the serial monitor. You can find them in the attachments.

Also, the code was working there was no compiling error.

Quote
And tell us in detail what happens when you run them
In the attachment, when we transmit a message in USB Mode (@Figure: Transmitter_USB), the input can be seen on the Monitor. However, when we switch it to the XBEE Mode (@Figure: Transmitter_XBEE),  on the Monitor, no input is accepted by the Arduino. On the receiver side, the output remains constant in both the USB and the XBEE modes.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 24, 2018, 09:25 pm
I'm attaching my output on the serial monitor. You can find them in the attachments.

Please don't post pictures of text - just copy and paste your text.

And if posting images (for other reasons) please display them in your post so we can see them without downloading them. See this  Simple Image Guide (http://forum.arduino.cc/index.php?topic=519037)


I looked at your programs, but I need to see the output.

I think I asked before and you did not say why there are all these lines
Code: [Select]
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);



...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 24, 2018, 10:02 pm
Quote
And if posting images (for other reasons) please display them in your post so we can see them without downloading them. See this  Simple Image Guide
The outputs I've attached in Reply #36, I'm attaching them again as directed in the link you provided.

Quote
I looked at your programs, but I need to see the output.
Transmitter_USB: When the jumpers of the shield are in USB Mode.

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271593)


Transmitter_XBEE: When the jumpers are switched in XBee Mode.

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271595)


Receiver: The output on the receiving side.

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271597)

Quote
I think I asked before and you did not say why there are all these lines
I'm afraid you didn't ask that, but, I'll tell you what it tells.

These lines are defining the respective Router's 64-bit MAC Address. Now, when you want to transmit a message from Router 6 to Router 5 (say), you can see zbTx5 in the Transmitter Code. This zbTx5 contains the MAC Address of Router 5, the data we are transmitting and the size of the data which is predefined variables in ZBTxRequest() function of the XBee library.

I hope I answered your queries, but I think that you shall be asking me for the data I'm sending to explain.
So as you can see in the Transmitter_USB image data is 5410244161 when sent to Router 5, it means, 54 is representing '6' which the Router from which message is transmitted, 102 is the x-coordinate, 44 is the ASCII value of "," (comma) and 161 is the y-coordinate. So when we receive it at the receiver end after storing in an array which you suggested, we can separate the coordinates by seeing 54 and 44 position in the data.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 24, 2018, 11:57 pm
I'm afraid you didn't ask that, but, I'll tell you what it tells
Until you get something working remove ALL but one of them of the destinations. Make the program as simple as it can possibly be.

The output from your RX program "Reading for Packet" (which I can't copy and past from your picture) suggests that it never receives anything.

Perhaps you have the wrong MAC address? - hence the need to simplify.

I have no idea what the jumpers mean (I prefer a cardigan myself).

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 25, 2018, 12:46 am
Quote
Until you get something working remove ALL but one of them of the destinations. Make the program as simple as it can possibly be
I tried that as well but still, it was not working. What I figured out from my code is that till the time I was sending to the Routers directly without having an input from the Serial Monitor, the code was working fine and I was able to receive it as well.

But, after I added the switch-case statements for performing unicasting operations the code is not working. If you see in the Reply #36, the code I used for transmitting, the code is working till the point we are printing the data we are sending. After that, I've applied an if-statement: if (Serial.available()>0), from that point onwards, the code is not working.

Quote
The output from your RX program "Reading for Packet" (which I can't copy and past from your picture) suggests that it never receives anything.
Yeah in the receiver code, it is just reading the first line stating "Reading for Packet".

Quote
Perhaps you have the wrong MAC address?
No, MAC address is correct. If MAC Address would have been wrong then it won't be able to broadcast any message so far. The problem of transmission begun after I tried unicasting messages by taking input from the user.

Quote
I have no idea what the jumpers mean
LOL!!! I'm attaching a link of Documentation for XBee Shield (http://wiki.sainsmart.com/index.php/Chapter21_XBee_shield), it might help you understanding the jumpers (Btw jumpers are the Blue colored blocks in the image shown in the documentation).
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 25, 2018, 10:00 am
I tried that as well but still, it was not working. What I figured out from my code is that till the time I was sending to the Routers directly without having an input from the Serial Monitor, the code was working fine and I was able to receive it as well.
Post the Tx and the Rx programs that work.

When you run into problems it is often a good idea to go back to a working version and start over.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 25, 2018, 09:08 pm
Quote
Post the Tx and the Rx programs that work.
Transmitter Code (Simplified):

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
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[3];
  uint8_t receivedChar = '0';
  boolean newData = false;
  data[0] = '6';
  data[1] = rand()%256;
  data[2] = ',';
  data[3] = rand()%256;
  //ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  //ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  //ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  //ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  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);
  while(i<1)
  {
    /*
    delay(500);
    xbee.send(zbTx1);
    Serial.println("To Router 1");
    delay(500);
    xbee.send(zbTx2);
    Serial.println("To Router 2");
    delay(500);
    xbee.send(zbTx3);
    Serial.println("To Router 3");
    delay(500);
    xbee.send(zbTx4);
    Serial.println("To Router 4");
    delay(500);*/
    xbee.send(zbTx5);
    Serial.println("To Router 5");
    delay(500);
    i++;
  }
}


Receiver Code (Suggested by you):

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()
{
  int receivedData[3];
  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 < 3; 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);
  }
}


Quote
When you run into problems it is often a good idea to go back to a working version and start over.
I tried running the above programs and attaching my output for both transmitter and receiver.

Output Transmitter:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271714)

Output Receiver:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271716)

The question I wanted to ask after running the programs is regarding the suggestion you gave by creating a pointer, we are storing values in an array. So after we typecasted the output I'm able to get in return is like this:

Packet Received: -26058
-25300
-25597

Here, "-26058" value is changing in every iteration, but "-25300" and "-25597" is repeating in every iteration as soon as any packet is received. So, firstly, what are these values, i.e., what it is symbolizing. And, secondly, why are they repeating in every iteration which you clearly saw in the attached image.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 25, 2018, 10:11 pm
Let's slow down a bit and get back to basics.

In Reply #40 you said
Quote
till the time I was sending to the Routers directly without having an input from the Serial Monitor, the code was working fine and I was able to receive it as well.
and in Reply #41 I asked you to post those working programs.

Instead you have posted some other programs, you have posted pictures of the output even though I asked you not to, and you have not said whether the programs you did post are working properly.

How can you claim the transmitter code is simplified when it still has addresses for 6 XBees?

Generating random numbers when you are trying to debug a problem is just madness.

When you create an array with 3 elements you CAN NOT write to the 4th element.

In the circumstances it is very very hard to help.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 26, 2018, 12:11 am
Quote
Instead you have posted some other programs, you have posted pictures of the output even though I asked you not to, and you have not said whether the programs you did post are working properly.
I've posted the programs which are not taking any input from the user, the one you asked me to. Also, I run them just to show that it is working without taking input from the user. If you see in the code I sent, I've not mentioned any Serial.read() function to take input from the user.

Quote
How can you claim the transmitter code is simplified when it still has addresses for 6 XBees?
If you want I can remove them, as they are just mentioned them but not actually sending them. If you look in the while loop, I've commented all the other transmissions and have considered only sending data to Router 5 (zbTx5)

Quote
When you create an array with 3 elements you CAN NOT write to the 4th element.
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 attaching my output so that you can see what output I'm getting.

The outputs I'm getting after correcting the array size as you instructed are:

Transmitter:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271725)

Receiver:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271727)

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.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 26, 2018, 10:43 am
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)

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
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 26, 2018, 11:29 pm
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


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
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 27, 2018, 11:03 am
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
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 27, 2018, 11:12 am
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
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 27, 2018, 06:55 pm
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

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271919)

Output of the program AB transmitter: Transmitter_XBEE

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271921)

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 (http://wiki.sainsmart.com/index.php/Chapter21_XBee_shield)
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 27, 2018, 07:24 pm
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
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 27, 2018, 08:43 pm
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:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271934)

Received Output:

(https://forum.arduino.cc/index.php?action=dlattach;topic=563990.0;attach=271936)

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 (https://www.arduino.cc/en/Guide/ArduinoXbeeShield)
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 27, 2018, 10:13 pm
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 (https://www.arduino.cc/en/Guide/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
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 27, 2018, 10:25 pm
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.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 28, 2018, 08:37 am
I reckon I have exhausted all the ideas I have thought of. Sorry.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 28, 2018, 05:49 pm
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.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 28, 2018, 07:36 pm
As far as I can see Googling arduino xbee example throws up lots of examples on the first page.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 28, 2018, 07:54 pm
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.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 28, 2018, 08:36 pm
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
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 30, 2018, 06:07 pm
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?
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 30, 2018, 06:24 pm
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?
If this was my problem and I was not familiar with XBees I would first figure out how to send data that comes from the PC - as you have already been considering.

When you can do that then communication between the XBees should be straightforward.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 30, 2018, 06:50 pm
Actually, data is not to be given by the PC, it is the sensor data but since right now the sensor is not attached, we are self-defining the range.

I explain you with an example of what I'm looking for, suppose a sensor has 5cm of range. Now, one Router has located at a point and it will calculate the distance between the other XBee(s) if present in 5 cm of range. If found, it will send the message which is its own location. So, how should we get to know that which XBee is present among the other 5 XBee(s)?

Please tell me if you got my question, I know it's a little bit tricky to understand in one go.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 30, 2018, 08:14 pm
Actually, data is not to be given by the PC, it is the sensor data
Never mind that for now ....

We are at Reply #62 almost all of which time has been spent trying to figure out how to get data from a PC and send it to another XBee. And it seemed to me you were nearly at the successful end of that exploration in Reply #57.

Finish that to the point where you know how to do it and then you will almost certainly know how to use the Xbee for other purposes.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Aug 30, 2018, 08:48 pm
Quote
We are at Reply #62 almost all of which time has been spent trying to figure out how to get data from a PC and send it to another XBee. And it seemed to me you were nearly at the successful end of that exploration in Reply #57.
Actually, after asking my professor, she said that we don't have to do the process explained in Reply #57.

The thing we need to do is to make them work autonomously, rather than providing input manually.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Aug 30, 2018, 10:42 pm
Actually, after asking my professor, she said that we don't have to do the process explained in Reply #57.
I know that. You said so in Reply #59.

Everything I have written since Reply #59 has taken that into account. This is getting very tiresome - especially as I think the essence of the solution was in Reply #13.

Why don't you just ask your Professor to help you? That's what s/he is paid for.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 19, 2018, 02:05 am
Hi Robin,

Sorry for the delay in replying, but I figured out a way to make them work autonomously and it was working as well. Now, I want to make the data in real time rather than provided with a certain value.

As I explained my project earlier, I'll explain again for your reference but I'll make it short so that you can understand the exactness of my need. I've done the part of transmission and reception from an Arduino to another Arduino using XBee(s). Now I have to take data in real time using a Color Sensor provided by LEGO in EV3 Mindstorm Robots. For getting data from the sensor I'm using a Pixel Map made from Red and Blue colors making green (third primary color) completely zero.

Now, as the sensor scans the pixels in a range of 0 to 255 for each of the two colors, we need to convert them into a coordinate system (x-axis, y-axis). For converting I find a way by using MATLAB function "pix2map" by which pixels can be converted into a coordinate system, but since I'm not having experience of making them work together, I thought of seeking your help.

I hope you get my problem, if not please feel free to ask any single detail left by me just in case.

For your help, I'm attaching a link of how to make LEGO Robots communicate with Arduino. I know it won't make much difference for my problem but it will give you an idea of the product I'm using. Link: Connecting EV3 with Arduino (https://www.dexterindustries.com/howto/connecting-ev3-arduino/)
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 19, 2018, 10:52 am
Sorry for the delay in replying, but I figured out a way to make them work autonomously and it was working as well.
I reckon that by now I should not have to waste time asking you to post the program.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 19, 2018, 05:28 pm
Actually, I asked the Professor directly that what exactly she means by autonomously, to which she told me the exact requirement of the project. And after realizing the basicness of the requirement, I didn't want to waste your time by asking a silly question. Moreover, I can provide the working code I've done for communication between six XBee(s).

Here's my code for one Router communicating with other five Routers:

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t count = 0;
uint8_t data[4];
uint8_t receivedData[4];
byte* dataPointer;
uint8_t dist = 0;

void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
  data[0] = '6';
  data[1] = '9';
  data[2] = ',';
  data[3] = '8';
}

void loop()
{
  delay(1000);
  transmitXBee();
  delay(1000);
  receiveXBee();
  delay(1000);
}

void transmitXBee()
{
  dist = sqrt((receivedData[1]-data[1])^2 + (receivedData[3]-data[3])^2);
  ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  delay(500);
  Serial.print("Distance: ");
  Serial.println(dist);
  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);
  data[1]++;
  data[3]++;
 
  if ((dist > 0) && (dist <= 1))
  {
    Serial.println("To Router 1");
    xbee.send(zbTx1);
    Serial.println();
    delay(500);   
  }
  else if((dist>1) && (dist<=2))
  {
    Serial.println("To Router 5");
    xbee.send(zbTx5);
    Serial.println();
    delay(500);
  }
  else if((dist>2) && (dist<=3))
  {
    Serial.println("To Router 3");
    xbee.send(zbTx3);
    Serial.println();
    delay(500);
  }
  else if((dist>3) && (dist<=4))
  {
    Serial.println("To Router 4");
    xbee.send(zbTx4);
    Serial.println();
    delay(500);
  }
  else if((dist>4) && (dist<=5))
  {
    Serial.println("To Router 2");
    xbee.send(zbTx2);
    Serial.println();
    delay(500);
  }
  else
    Serial.println("No XBee Found");
}

void receiveXBee()
{
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  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(500);
      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.print(receivedData[n]);
        delay(500);
      }
      Serial.println();
      Serial.print("count: ");
      count++;
      Serial.println(count);
      delay(500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
  else
    Serial.println("No data received");
}

Sorry for stretching the topic so long. Could you help me with the part I asked regarding the MATLAB and Arduino thing to convert pixels into coordinates?
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 19, 2018, 07:57 pm
Actually, I asked the Professor directly that what exactly she means by autonomously, to which she told me the exact requirement of the project.
It would be a great help if you would write down what your Professor said and get her agreement that your written description is correct and then post the description for us to read.

Is the code you posted in Reply #67 the program that you were referring to in Reply #65 when you said "I figured out a way to make them work autonomously and it was working as well". That is not clear from your wording in Reply #67.

...R

PS ... I presume you have made your Professor aware of this Thread?
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 19, 2018, 08:18 pm
Quote
It would be a great help if you would write down what your Professor said and get her agreement that your written description is correct and then post the description for us to read.
She said that right now since you don't have any real time value, give the coordinates of the robots in starting only and in every iteration update that distance by incrementing the coordinates so that you distance, for now, remains same and you can decide that to which Router you need to give the priority.

So, as you see in the code, I've provided the coordinates of x and y as (9,8) for Router 6 and it is (4,8) for Router 1. So, in the next iteration, it will be (10,9) for Router 6. Like this, the distance will remain constant for now and it will be changed when the data will be collected in real time on which I'm seeking help.

Quote
Is the code you posted in Reply #67 the program that you were referring to in Reply #65 when you said "I figured out a way to make them work autonomously and it was working as well". That is not clear from your wording in Reply #67.
Yes, the code I provided in Reply# 67 is the final and updated code of the program I was referring in Reply# 65 in which they are sending and receiving data autonomously.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 19, 2018, 08:42 pm
This line
Code: [Select]
  dist = sqrt((receivedData[1]-data[1])^2 + (receivedData[3]-data[3])^2);
seems to be what changes the data to be sent.

Put that into its own function - perhaps called updateDataToSend() and change the code in loop() to this
Code: [Select]
void loop()
{
  delay(1000);
  updateDataToSend();
  transmitXBee();
  delay(1000);
  receiveXBee();
  delay(1000);
}


Then, when you want to change the data to be sent just change the code in the function updateDataToSend()

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 19, 2018, 09:57 pm
Quote
Then, when you want to change the data to be sent just change the code in the function updateDataToSend()
Yeah, it is a better way to write. Thank you for it.

Can you help me how to convert the pixel value which is being taken from Serial.read() function to convert into coordinates so that the data which is updated can be done in real time. The details of it are mentioned in Reply# 65 for your reference. Because I don't know how to combine the code of MATLAB and Arduino together.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 19, 2018, 11:32 pm
Can you help me how to convert the pixel value which is being taken from Serial.read() function to convert into coordinates so that the data which is updated can be done in real time.
FIRST -- you have not said that your Professor knows about this Thread and approves of it. I don't think I should help further until you confirm that.

Assuming you do confirm that, then


Please provide a typical example of the pixel data that is received and the corresponding output that you want to create.

Be aware that I know nothing about Matlab.

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 21, 2018, 05:48 pm
Quote
FIRST -- you have not said that your Professor knows about this Thread and approves of it. I don't think I should help further until you confirm that.
Yes, she knows that I'm seeking help from the Forum, and she approves the description I gave is said by her.

Quote
Please provide a typical example of the pixel data that is received and the corresponding output that you want to create.
I too, don't have any idea of the example I should suggest to you, but after reading a little about the color sensor, I can tell you some estimates of the project requirement. But how to implement them is the problem here.

Two colors are used as the coordinate system, where blue color represents the x-axis and red color represents the y-axis making it a grid of 255*255, thus resulting in the total number of unique grids are 65025. Each unique grid has a resolution of 1.2cm2 resulting in the total area of the map as 7.8m2 in size.

So, how to get the coordinates of a point where a robot is standing currently so that once the coordinates are known we can calculate the distance if we consider a reference point on the table, and set priorities to the Routers it should encounter first and use the collision avoidance algorithm.
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 21, 2018, 06:34 pm
I too, don't have any idea of the example I should suggest to you, but after reading a little about the color sensor, I can tell you some estimates of the project requirement. But how to implement them is the problem here.

Two colors are used as the coordinate system, where blue color represents the x-axis and red color represents the y-axis making it a grid of 255*255, thus resulting in the total number of unique grids are 65025. Each unique grid has a resolution of 1.2cm2 resulting in the total area of the map as 7.8m2 in size.

So, how to get the coordinates of a point where a robot is standing currently so that once the coordinates are known we can calculate the distance if we consider a reference point on the table, and set priorities to the Routers it should encounter first and use the collision avoidance algorithm.
You are making this far too complicated at this stage. Computer programs need to be developed in very small steps.

Do you have a simple program that gets the pixel data from the sensor and prints it on the Serial Monitor?

...R
Title: Re: Serial Communication between XBee using Arduino
Post by: Priyanshu27 on Sep 21, 2018, 07:32 pm
Quote
Do you have a simple program that gets the pixel data from the sensor and prints it on the Serial Monitor?
Unfortunately, I don't have it. Neither I'm getting any idea of how to write it. :smiley-sad:
Title: Re: Serial Communication between XBee using Arduino
Post by: Robin2 on Sep 22, 2018, 09:36 am
Quote
Do you have a simple program that gets the pixel data from the sensor and prints it on the Serial Monitor?
Unfortunately, I don't have it. Neither I'm getting any idea of how to write it. :smiley-sad:
That leaves me completely at a loss.

I have no idea how you think you can implement the project without such basic capability.

...R