How to Transmit and Recieve Analog Values using the Xbee (S2) Library

Hello All,

I’ve been going crazy for the past 5 days trying to solve a problem and I’m losing my mind I need help. I’m trying to Transmit analog values from a photodiode sensor (pin A0 on Arduino UNO) through an (Router API) Xbee S2 and Receive those values to another (Coordinator API) Xbee S2 and view them on the Arduino Serial monitor but I’m not getting any results on the Serial Monitor from the Coordinator Side. I have configured both Xbees’ with the XCTU software.

One Xbee is set as function set XB24-ZB Zigbee Router API with the following parameters: API enabled = 2; PAN ID = 885; JV channel enabled = 1; IO Sampling Rate (1F4*1ms).
And I’m running the following code on this Xbee /Xbee Shield/Arduino UNO (See the schematic):

#include <XBee.h>

XBee xbee = XBee();

uint8_t payload[] = {0,0};

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int analogpin0 = 0;
int value1;

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

void loop() 
{
  value1 = analogRead(analogpin0);
  //break down 10-bit reading into two bytes and place in payload
  payload[0] = value1 >> 8 & 0xff;
  payload[1] = value1 & 0xff;
  Serial.print(" Value ");
  Serial.println(value1);
  xbee.send(zbTx);
  // after sending a tx request, we expect a status response
  // wait up to half second for the status response
  if (xbee.readPacket(500)) 
  {
    // got a response!
    // should be a znet tx status             
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) 
    {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
    }
  }
  delay(500);
 }

While the other Xbee S2 is set as function set XB24-ZB Zigbee Coordinator API with the following parameters: API enabled = 2; PAN ID = 885;
And I’m running the following code on this Xbee /Xbee Adapter/Arduino UNO (See the schematic):

#include <XBee.h>

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

void setup() 
{
  // start serial
  Serial.begin(9600);
  xbee.begin(Serial);
}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() 
{
  xbee.readPacket();
  if (xbee.getResponse().isAvailable()) 
  {
    // got something
    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) 
    {
      // got a zb rx packet
      // now fill our zb rx class
      xbee.getResponse().getZBRxResponse(rx);
      // I check both bytes (or)
      uint8_t analogMSB = rx.getData(0);
      uint8_t analogLSB = rx.getData(1);
      int value1 = analogLSB + (analogMSB * 256);
      float pvolt = value1 / 1024.0 * 5.0;
      Serial.print(pvolt);
      Serial.println(" V from Photodiode ");
    }
  }
}

But I’m not getting any readings from the Coordinator. It’s just a blank Serial monitor.
Please anyone can help me. At the moment I only have ONE sensor connected eventually I’d like to connect all 4 to get all four readings out of the Router API Xbee. Help Me Please!!!

I have a feeling the code to Transmit and Receive with the Xbee Library for Xbee S2 isn't working correctly. I have viewed a couple of sites and codes and they're people who are having similar issues. I will try and transmit Arduino/Xbee(Router API) to Python/Xbee(Coordinator API) because that is the end goal for me. I will post back if I get any results that way.

Have you gotten your setup first working as desired just using a wired connection between the two arduinos?

Yes I have, I began with this tutorial XBee Basics - Lesson 4 - API mode: Reading analog data from remote XBee - YouTube and it worked perfectly. Wiring & Connections worked fine on the two Arduino themselves connected with two Xbee S2 [Router AT to Coordinator API] on D3 pin 17 on Xbee Router AT. So I know the connection on the Arudino are fine.

But when I try using the Library and connecting the sensor to the Analog pins on Arudino's Analog pin A0, things get difficult. I get a reading from the Serial Monitor on the Router API side but it doesn't seem to Send the values on to the Coordinator API side.

I have tried all sort of things too by switching the firmware, the MODES from AT to API, and Baud Rate, and well as setting both Xbees to API = 1. NOTHING!!!!

Eventually I want to try and read 30 analog sensors from an Arduino through an Xbee but I know it can't be done with the Xbee analog pins themselves because they can only handle 4 analog inputs. So I'm working with trying to get one sensor to transmit and receive data through the analog pins on the Arduino but it seems I'm either doing some wrong with the code or maybe the library has a flaw. But I'm tired so I will go on with trying to just receive the data through Python Serial library.

You might try just sending basic text like "hello world" from one arduino thru the xbees to the other arduino to verify you can establish simple communication via the xbees.

Hi Jpena21,

I don't think it's a good idea to make Serial.print to the serial where the Xbee is connected. Aren't you messing the communication between the arduino and the Xbee?

So are you saying I take out 'Serial.print' and leave the rest of the code as is? I've tried that still nothing.

OK so an update to this post…
I have wrecked my brain trying to transmit and receive data through Xbee S2 modules and I have found out the solution. The solution is configuring the Xbee modules first. Upload the code to the Arduino. Finally!!! (This does not pertain to everyone who is not using this shield: https://www.arduino.cc/en/uploads/Main/Arduino_WirelessSDShield_Front3_450px.jpg) flip the switch from USB to Serial. Once you have done so, Voila!!! like magic see your data transmit wirelessly through the air. For this type of shield there is no need of a Xbee library. All you need is a simple code that stores the data onto serial. This is only for the my case of transmitting analog data through pins A0-A5 on the Arduino board.

See this for more info: http://www.instructables.com/id/Arduino-Wireless-SD-Shield-Tutorial/?ALLSTEPS

Bro, could you tell me how to do it?? I got same problem as you..

I know that this post is old, but I imagine that might someone need to know It. I’ve started working with xbee recently and this same problem happened to me. Then, I read the documentation of the library of Andrew Rapp, the same library that you were probably using. There he says that xbee requires 2 serials for the communication and recommends using arduino leonardo, because it has 2 serials. What I did to solve my problem? I’ve used a display LCD 16x2 to visualize the data. Another way is to instead of using arduino, you visualize all your data through your computer connecting you coordenator xbee directly with an usb adapter.

seth07:
There he says that xbee requires 2 serials for the communication

'Requiring 2 serials for the communication' is quite vague. What does it actually mean?