API Mode XBee Series 1 Analog Sensor

Hello! I have 2 XBee S1's with one as the coordinator hooked up to an Arduino Uno and one as the end device connected to a pressure sensitive resistor. I am trying to send the analog signal from the one XBee to the other using API mode. I am currently using the following code:

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

void loop() {
if (Serial.available()>21){
for (int i=0; i<22; i++){
Serial.print(Serial.read(),HEX);
Serial.print(",");
}
Serial.println();
}

The issue, however, is that the data that is printing is not consistent with what I would expect.

The first couple lines I receive are as predicted:
7E , 0 , A , 83 , 0 , 0 , 27 , 0 , 1 , 2 , 0 , 0 , F , 43
In this case from what I understand, F is my analog value from the sensor.

However, after a short period of time, the packets become much shorter:
7E , 0 , A , 83 , 0 , 24 , 2 , 55

I am not sure what is causing this issue. Possibly something with my code? Thank you!

You are receiving and printing to the same serial port.
What pins are connected to the Xbee?

You probably need to have the Xbee connected to a softserial port so that you can use the hardware serial port for your prints.

I have my receiving XBee going from Dout to Rx on the Arduino and then from Din to Tx on the Arduino. I also have power and ground supplied.

On the transmitting XBee I have the analog signal applied to pin 20 (AD0) as well as power, ground, and Vref.

With you current arrangement every time you print something it will also send a copy of the text to the Xbee which will probably confuse it.

Are you using an adaptor or shield for the Xbee?

I am only using a Sparkfun XBee breakout board for both XBee's

Not very familiar with stuff like this so I'm not sure exactly what I did wrong here:

#include <SoftwareSerial.h>
#include <XBee.h>
XBee xbee = XBee();
float value;
int readValue=0;
SoftwareSerial soft_serial(0, 1);

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

void loop() {
if (soft_serial.available()>21){
for (int i=0; i<22; i++){
Serial.print(soft_serial.read(),HEX);
Serial.print(",");
}
Serial.println();
}

Your softserial needs to use pins other than 0 and 1, for example 2 and 3. You'll need to change the physical wiring to the Xbee to match the Arduino pins you are using.

I changed it to pins 2 and 3 and now run the Dout pin on the XBee to pin 2 on the Arduino and the Din pin to pin 3.

However, I am still getting the same issue where the packets get smaller. Not sure what is happening.

Tried using altSoftSerial library as well with no luck

Try something like this

#include <SoftwareSerial.h>
SoftwareSerial soft_serial(2, 3);

void setup() {
  Serial.begin(9600);
  soft_serial.begin(9600);
}

void loop() {
  uint8_t counter = 0;
  if (soft_serial.available() > 21) {
    for (uint8_t i = 0; i < 22; i++) {
      
      counter++;
      Serial.print("Array location= ");
      Serial.println(counter);

      Serial.print("data= ");
      Serial.println(soft_serial.read(), BIN);
    }
  }
}

Still getting the same issue for some reason

Show the exact output that you see.

Array location= 1
data= 7E
Array location= 2
data= 0
Array location= 3
data= A
Array location= 4
data= 83
Array location= 5
data= 0
Array location= 6
data= 0
Array location= 7
data= 24
Array location= 8
data= 0
Array location= 9
data= 1
Array location= 10
data= 2
Array location= 11
data= 0
Array location= 12
data= 0
Array location= 13
data= 0
Array location= 14
data= 55
Array location= 15
data= 7E
Array location= 16
data= 0
Array location= 17
data= A
Array location= 18
data= 83
Array location= 19
data= 0
Array location= 20
data= 0
Array location= 21
data= 27
Array location= 22
data= 0
Array location= 1
data= 1
Array location= 2
data= 2
Array location= 3
data= 0
Array location= 4
data= 0
Array location= 5
data= 0
Array location= 6
data= 52
Array location= 7
data= 7E

It starts with 7E as expected but the packets steadily get smaller. Is it something with my counter? I found somewhere it should be 22 but my first one is only 14 so not sure.

All I need are the analog values so if there is an easy way to do that, that is all I need

I switched some things up and am able to see the analog values using the following line of code:

xbee.onResponse(printRawResponseCb, (uintptr_t)(Print*)&DebugSerial);

Does anyone know how to pull the actual value out of the packet though? I can see the value and see it change as I change the signal but I'm not sure how to get the single analog value from the packet. Thanks!

Do I need to use different baud rates for the serial and xbee communication?

Not necessarily. I typically set the serial to 115200. Software serial is reliable at 9600, but may also work fine at slightly higher baud rates. You'll need to reconfigure the Xbees if you start messing with the softserial baud rate.

Series 1 Xbees are fundamentally slow at moving data so there isn't much to be gained by running at high baud rates.

For some reason, I am still having the same issue. Whenever I run the code, the first few packets are delivered fine. After that however, something is causing a problem because the 7E start delimiter will come much too frequently.

Using the other code I referenced earlier, I am able to see a good response each time but I am not sure how to actually access the values from the packet. I feel like there has to be an easy fix I'm just not familiar with it.

Run the exact code shown in reply #9. Do not modify it or use you own version.
Show the output.

I think I actually got it working but with a different method:

#include <XBee.h>
#include <SoftwareSerial.h>
//import the xbee and software serial libraries

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16IoSampleResponse rx16 = Rx16IoSampleResponse();

//define the two pins used for data transmission
//Dout on the XBee goes to pin 2 and Din goes to pin 3
#define ssRX 2
#define ssTX 3
SoftwareSerial nss(ssRX, ssTX);

//variables for each of the four sensor values
uint8_t data = 0;
uint8_t data2 = 0;
uint8_t data3=0;
uint8_t data4=0;

void setup() {
// start serial
Serial.begin(9600);
// and the software serial port
nss.begin(9600);
// now that they are started, hook the XBee into
// Software Serial
xbee.setSerial(nss);
Serial.println("starting up!");
}

void loop() {

xbee.readPacket();

if (xbee.getResponse().isAvailable()) {
// got something
Serial.println();
Serial.print("Frame Type is ");
Serial.println(xbee.getResponse().getApiId(), HEX);

if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
// got a rx packet

if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
xbee.getResponse().getRx16IoSampleResponse(rx16);
//prints the analog values from each of the sensors
data = rx16.getData(4);
data2=rx16.getData(6);
data3=rx16.getData(8);
data4=rx16.getData(10);
Serial.println(data);
Serial.println(data2);
Serial.println(data3);
Serial.println(data4);
}

} else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
}
}

I really appreciate all the help! So relieved to finally get it working