How to Send Integers via XBee?

Hello,

Like in the title, I'm trying to figure out how to send integers--preferably as ints--via XBee to essentially be displayed on XTCU, which is the program I'm using to read what the other-end XBee receives. The idea is to send unmodified sensor data from a not-USB connected robot to a PC.

Currently, I've set it up like this:

byte rx_byte = 0; // stores received byte

void adjust(){
int tooclose = 10;
int justright = 20;
int goaway = 5;

if(bm.dist() > justright)
{
  rx_byte = '3';
}
else if (bm.dist() > tooclose)
{rx_byte = '2';
}
else if (bm.dist() > goaway)
{rx_byte = '1';
}
else
{rx_byte = '0';
}

void setup() {
 Serial.begin(9600);
  Serial3.begin(9600); // serial port 3
}

void loop() {
 adjust();
 if (Serial3.available()) {
 Serial3.write(rx_byte);
 }
}

It's essentially predetermining what range the value falls in and then sending the respective character (which in this case is a number). I'd like for it to send the actual integer value of the sensor reading so all the other calculations can be done later, on the actual PC.

Does anybody have any suggestions on how to send 'int's (or even these integers as strings) over XBee?

Thanks!

rx implies received. You don’t send received data.

Why are you using byte to hold characters? ‘1’ is a character, not an int.

and then sending the respective character (which in this case is a number).

The character is a digit. It is not a number.

You don’t seem to know what you want to send.

You didn’t way what the code actually does, or what your problem is.

I’d like for it to send the actual integer value of the sensor reading so all the other calculations can be done later, on the actual PC.

Then, why don’t you?

It would behoove you to learn what write() does vs. what print() does.

Hi @PaulS thanks for your time.

You’re right I am a mess lol. A quick explanation for this is that I’ve been changing code around from sending to receiving and just kept it to byte because I saw it used in another thread that I’d referenced for a different part, and what I send seems to display correctly using the byte and Serial3.write() (I’m using a MEGA, Serial3 is where the XBee is connected).

In response to your first point: I realize that ‘1’ is a char. This method of char to byte correction seems to work, so I’ve just run with it. The issue is that char’s are single characters.

I would like to send an integer (say 234) over XBee, but so far I only know how to send these individual chars. Is there a way to either a) send integers over XBee that I am unaware of, or b) way to convert an integer to a variable-length char array?

Also not to fret, I do understand the difference between write() and print(); I am using write() on purpose.

EDIT: wait... are you suggesting that I can get the int correctly on the other side if I use print()? Would this work for like multiple-digit ints?

Thanks!

Is there a way to either a) send integers over XBee that I am unaware of

No.

Also not to fret, I do understand the difference between write() and print(); I am using write() on purpose.

I'd like for you to explain the difference, to convince me that you know the difference. The fact that you asked the first question above suggests to me that you don't really understand the difference.

are you suggesting that I can get the int correctly on the other side if I use print()?

You can use either print() or write(), but print () will send the int as a string (239 = "239"), so you'll need to read multiple characters, store them in an array, and convert the array back to an int (atoi()).

Of course, the rub is how many characters to read. Well, a perusal of this thread should be required before anyone is allowed to unbox an Arduino.

Serial Input Basics - updated

Would this work for like multiple-digit ints?

But of course.

PaulS:
Well, a perusal of this thread should be required before anyone is allowed to unbox an Arduino.

Wow that thread is awesome, thanks! I've been using Arduino for quite a few years now and never come upon it... somebody should pin/sticky it

PaulS:
I'd like for you to explain the difference, to convince me that you know the difference.

Fair enough. :slight_smile:
So .write() is like the more basic, faster way to send things. It sends like bytes, so on the other end you would see the ASCII representation of a character. .print()/.println() is the marginally slower way, which converts the ASCII to a readable format, which is why (I) it's commonly used with the Serial montior... so you can actually read what you're trying to debug lol.

The trouble is that when I have tried to use .print() with XBees, it sends the actual character and then that gets interpreted as an ASCII value, so I end up with a totally different character on the other end. (I'm currently reading through XTCU, but I will be reading in from MATLAB eventually...)

PaulS:
You can use either print() or write(), but print() will send the int as a string (239 = "239"), so you'll need to read multiple characters, store them in an array, and convert the array back to an int (atoi()).

So what I'm trying to do is take a sensor value (read in as a variable) and then send it out via XBee, preferably all in one piece. If I'm reading them into XTCU, it's like reading them into the Serial monitor and I'd prefer not to leave any calculations to be done (eventually in MATLAB). If I use .write(), do you know that there's a way I can send multiple digits at the same time? (Or is the reconstructing of the number on the other side inevitable?)

Thanks!

Fair enough. :slight_smile:
So .write() is like the more basic, faster way to send things. It sends like bytes, so on the other end you would see the ASCII representation of a character. .print()/.println() is the marginally slower way, which converts the ASCII to a readable format, which is why (I) it's commonly used with the Serial montior... so you can actually read what you're trying to debug lol.

Not exactly. write() sends a binary value. print() converts the binary value to a string, and then write()s each character.

If you have a byte, containing 215, write() will send one byte (215). print() will send 3 bytes (50, 48, and 53).

Depending on what type is used on the receiving end, the result may be intelligent, or not. Use a char, and the 215 will be interpreted incorrectly. Use a byte, and 50, 48, and 53 will not print as '2', '1', and '5'.

The trouble is that when I have tried to use .print() with XBees, it sends the actual character and then that gets interpreted as an ASCII value, so I end up with a totally different character on the other end. (I'm currently reading through XTCU, but I will be reading in from MATLAB eventually...)

Examples are far more useful than hand-waving or hand-wringing.

If I use .write(), do you know that there's a way I can send multiple digits at the same time?

There are no digits in binary data. The question is meaningless. The value is what it is. The value may be printed using more than one digit, depending on the base used to print the value (binary, octal, hexadecimal, or some odd base like 7).

Keep in mind that an int is a two byte type, so if you are sending an int, you need to send the high order byte and the low order byte, in the order that the other end expects.

If I'm reading them into XTCU, it's like reading them into the Serial monitor

The Serial Monitor doesn't expect binary data, so using write() to send data to the Serial Monitor is pointless. XCTU might be different. I haven't used it in quite a while.