Go Down

Topic: Serial Communication between XBee using Arduino (Read 1 time) previous topic - next topic

Priyanshu27

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.

Robin2

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

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

Priyanshu27

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?

Robin2

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

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

Priyanshu27

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

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);
}


Robin2

#7
Aug 17, 2018, 10:19 pm Last Edit: Aug 17, 2018, 10:20 pm by Robin2
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

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

Priyanshu27

#8
Aug 17, 2018, 10:58 pm Last Edit: Aug 17, 2018, 11:15 pm by Priyanshu27
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.

Robin2

#9
Aug 18, 2018, 10:01 am Last Edit: Aug 18, 2018, 10:02 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

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

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

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.

Go Up