Receive XBee frames using Arduino UNO

Hello, I’ve been trying for a couple of days to read the frames sent from an API with escapes mode router to an API with escapes mode coordinator, when using XCTU I can see everything being sent and received as expected (xctu.png attached below). However, I’ve tried multiple ways of reading the received frames to no avail. Serial.available() is never anything other than 0.

My physical connection is shown in the attached image. I’m using a sainsmart shield for the arduino with the coordinator xbee and a sainsmart usb adapter for the router xbee.

Any help would be greatly appreciated. Thank you!

EDIT: added connection.jpg

You do know that that ESD bag is conductive don't you?

I had no idea, I'm way more on the software side of things haha, thanks for that

You need to post your program otherwise we have no idea what you are trying.

AFAIK an XBee uses serial comms so have a look at Serial Input Basics

...R

The last code I tried is this one:

#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX

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

void loop()
{
  // If data comes in from serial monitor, send it out to XBee
  if(Serial.available()){
    XBee.write("IM IN");
  }
  // If data comes in from XBee, send it out to serial monitor
  if(XBee.available()){
    Serial.println(XBee.read());
  }
}

But I’ve tried many

This is the code I use on the Rx Xbee to parse out the data coming in as Serial from another Xbee

// Beehive reciever with XBee V1 Wireless Module

#include "RunningAverage.h"
#include <XBee.h>

// Uno Only
//#include <SoftwareSerial.h>
//SoftwareSerial xbeeserial(10, 11); // RX, TX
#define DEBUG false

// Setup Running Average Arrays
RunningAverage BatteryRA(10);
RunningAverage SolarRA(25);

int BatterySamples = 0;
int SolarSamples = 0;

// WiFi Stuff Deleted from public view

int nodeID;

// Xbee Variables
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();

uint8_t data = 0;
uint8_t rssi;
String id;
char instring[100];

// initialize data variables
String hiveID; //Common to all
// Solar Variables
String load, load_current, battery, battery_max, full, battery_temp, pv, charging, charge_current, LiPo_V, Ambient_t, Ambient_h, solar_pwr, load_pwr;
//Hive Variables
String tempC, temp1, temp2, temp3, temp4, humidity1, h_temp1, inTotal, outTotal;

void setup() {
  // start serial
  Serial.begin(115200);
  // Uno Only
  //xbeeserial.begin(9600);
  //xbee.setSerial(xbeeserial);
  Serial.print("Reciever Ready \r\n");

  // Mega Only
  Serial2.begin(57600);  //Xbee
  xbee.setSerial(Serial2);
  Serial.print("Xbee Ready \r\n");

  Serial3.begin(115200);  //ESP8266
  Serial3.println("AT");
  delay(1000);
  if (Serial3.find("OK")) {
    Serial.println("WiFi module ready!");
    connectWiFi();
  } else {
    Serial.print("No Communication to WiFi Module \r\n");
  }



  BatteryRA.clear();
  SolarRA.clear();

  Serial3.println("AT+RST");
}

// continuously reads packets, looking for RX16 or RX64
void loop() {
  xbee.readPacket(100);

  if (xbee.getResponse().isAvailable()) {
    // got something

    if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
      // got a rx packet
      //Serial.println("Packet Recieved");

      if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
        xbee.getResponse().getRx16Response(rx16);
        rssi = rx16.getRssi();
        data = rx16.getData(0);
        id =  String(rx16.getRemoteAddress16(), HEX);


        for (int i = 0; i < rx16.getDataLength(); i++) {
          //Serial.write(rx16.getData(i));
          instring[i] = rx16.getData(i);
        }
        parsedata();
        CalculateAverage();
        printData();
      } else {
        xbee.getResponse().getRx64Response(rx64);
        rssi = rx64.getRssi();
        data = rx64.getData(0);
        //Serial.print("64 RSSI - ");
        //Serial.println(rssi);
      }
    } else {
      // not something we were expecting

    }
  } else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");
    //nss.println(xbee.getResponse().getErrorCode());
    // or flash error led
  }
}

void CalculateAverage()
{
  BatteryRA.addValue(battery.toFloat());
  BatterySamples++;
  SolarRA.addValue(pv.toFloat());
  SolarSamples++;
}

parsedata() code below as limited to posting no more than 9000 characters and a 5 minute wait between posts!! Grrrr!!!

void parsedata() {
  //Serial.println(instring);
  char delimiters[] = "!:,";
  char* dataPosition;
  dataPosition = strtok(instring, delimiters);
  String data[15];

  for (int k = 0; k < 15; k++) {
    data[k] = dataPosition;
    dataPosition = strtok(NULL, delimiters);
  }

  // load variable with recieved and parsed data
  switch (id.toInt()) {
    case 1100: // Hive #1
      hiveID = data[0];
      tempC = data[1];
      temp1 = data[2];
      temp2 = data[3];
      temp3 = data[4];
      temp4 = data[5];
      humidity1 = data[6];
      h_temp1 = data[7];
      inTotal = data[8];
      outTotal = data[9];
      break;
    case 1200: // Solar Power
      hiveID = data[0];
      load = data[1];
      load_current = data[2];
      battery = data[3];
      battery_max = data[4];
      full = data[5];
      battery_temp = data[6];
      pv = data[7];
      charging = data[8];
      charge_current = data[9];
      LiPo_V = data[10];
      Ambient_t = data[11];
      Ambient_h = data[12];
      solar_pwr = charge_current.toFloat() * pv.toFloat();
      load_pwr = load_current.toFloat() * battery.toFloat();
      break;
    case 1300: //Hive #2
      hiveID = data[0];
      tempC = data[1];
      temp1 = data[2];
      temp2 = data[3];
      temp3 = data[4];
      temp4 = data[5];
      humidity1 = data[6];
      h_temp1 = data[7];
      inTotal = data[8];
      outTotal = data[9];
      break;
    case 1400: //Test Hive
      hiveID = data[0];
      tempC = data[1];
      temp1 = data[2];
      temp2 = data[3];
      temp3 = data[4];
      temp4 = data[5];
      humidity1 = data[6];
      h_temp1 = data[7];
      inTotal = data[8];
      outTotal = data[9];
      break;
  }
  //Serial.println(id);
}


void printData() {
  switch (id.toInt()) {
    case 1100:
#if DEBUG
      Serial.println("---- Recieved New Data From Hive #1 ----");
      Serial.print("Remote ID - ");
      Serial.println(id);
      Serial.print("Signal Strength - ");
      Serial.println(rssi);
      //Serial.print("Payload Length - ");
      //Serial.println(rx16.getDataLength());
      Serial.println("---Payload Data --- \r\n");
      Serial.print("Monitor 1 Temperature: ");
      Serial.println(tempC);
      Serial.print("Temperature 1: ");
      Serial.println(temp1);
      Serial.print("Temperature 2: ");
      Serial.println(temp2);
      Serial.print("Temperature 3: ");
      Serial.println(temp3);
      Serial.print("Temperature 4: ");
      Serial.println(temp4);
      Serial.print("Hive Humidity: ");
      Serial.println(humidity1);
      Serial.print("Hive Temperature: ");
      Serial.println(h_temp1);
      Serial.print("Bees Coming In in last 15 Secs: ");
      Serial.println(inTotal);
      Serial.print("Bees Going Out in last 15 Secs: ");
      Serial.println(outTotal);
#endif
      updateData();
      break;
    case 1200:
#if DEBUG
      Serial.println("---- Recieved New Data From Solar Power ----");
      Serial.print("Remote ID - ");
      Serial.println(id);
      Serial.print("Signal Strength - ");
      Serial.println(rssi);
      //Serial.print("Payload Length - ");
      //Serial.println(rx16.getDataLength());
      Serial.println("---Payload Data --- \r\n");
      Serial.print("Load Connected: ");
      Serial.println(load);
      Serial.print("Load Current: ");
      Serial.println(load_current);
      Serial.print("Battery Voltage: ");
      Serial.println(battery);

      Serial.print("Battery Average: ");
      Serial.println(BatteryRA.getAverage(), 2);

      Serial.print("Battery Max Volts: ");
      Serial.println(battery_max);
      Serial.print("Battery Full: ");
      Serial.println(full);
      Serial.print("Charger Temperature: ");
      Serial.println(battery_temp);
      Serial.print("Solar Voltage: ");
      Serial.println(pv);

      Serial.print("Solar Average: ");
      Serial.println(SolarRA.getAverage(), 2);

      Serial.print("Battery Charging: ");
      Serial.println(charging);
      Serial.print("Charge Current: ");
      Serial.println(charge_current);
      Serial.print("LiPo Voltage: ");
      Serial.println(LiPo_V);
      Serial.print("Ambient Temperature: ");
      Serial.println(Ambient_t);
      Serial.print("Ambient Humidity: ");
      Serial.println(Ambient_h);
      //Serial.println(); // Required to make look pretty as /n not being transmmited - remove when finalised
#endif
      updateData();
      break;
    case 1300:
      Serial.println("---- Recieved New Data From Hive #2 ----");
      Serial.print("Remote ID - ");
      Serial.println(id);
      Serial.print("Signal Strength - ");
      Serial.println(rssi);
      //Serial.print("Payload Length - ");
      //Serial.println(rx16.getDataLength());
      Serial.println("---Payload Data --- \r\n");
      Serial.print("Monitor 2 Temperature: ");
      Serial.println(tempC);
      Serial.print("Temperature 1: ");
      Serial.println(temp1);
      Serial.print("Temperature 2: ");
      Serial.println(temp2);
      Serial.print("Temperature 3: ");
      Serial.println(temp3);
      Serial.print("Temperature 4: ");
      Serial.println(temp4);
      Serial.print("Hive Humidity: ");
      Serial.println(humidity1);
      Serial.print("Hive Temperature: ");
      Serial.println(h_temp1);
      Serial.print("Bees Coming In in last 15 Secs: ");
      Serial.println(inTotal);
      Serial.print("Bees Going Out in last 15 Secs: ");
      Serial.println(outTotal);
      break;
    case 1400:
#if DEBUG
      Serial.println("---- Recieved New Data From Test Hive ----");
      Serial.print("Remote ID - ");
      Serial.println(id);
      Serial.print("Signal Strength - ");
      Serial.println(rssi);
      //Serial.print("Payload Length - ");
      //Serial.println(rx16.getDataLength());
      Serial.println("---Payload Data --- \r\n");
      Serial.print("Monitor 1 Temperature: ");
      Serial.println(tempC);
      Serial.print("Temperature 1: ");
      Serial.println(temp1);
      Serial.print("Temperature 2: ");
      Serial.println(temp2);
      Serial.print("Temperature 3: ");
      Serial.println(temp3);
      Serial.print("Temperature 4: ");
      Serial.println(temp4);
      Serial.print("Hive Humidity: ");
      Serial.println(humidity1);
      Serial.print("Hive Temperature: ");
      Serial.println(h_temp1);
      Serial.print("Bees Coming In in last 15 Secs: ");
      Serial.println(inTotal);
      Serial.print("Bees Going Out in last 15 Secs: ");
      Serial.println(outTotal);
#endif
      updateData();
      break;
    default: // No hives have sent data
      Serial.print("Unknown Data!! \r\n");
      break;
  }
  memset(instring, NULL, sizeof(instring)); // Null out string array for new data
}

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. Just use cstrings - char arrays terminated with 0.

...R

I’ve managed to make a LED light up whenever I send a packet form the router XBee to the coordinator using the following simple code:

const int ledPin = 13;
void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop(){
  if(Serial.available() > 0){
    digitalWrite(ledPin, HIGH);
  }
}

However this works when the jumpers are set to the XBee position only (as expected), now I want to be able to read whatever was sent to my coordinator from the Serial monitor, as I understand it I would need to use a software serial as such:

#include <SoftwareSerial.h>
SoftwareSerial XBee(2, 3);
const int ledPin = 13;
void setup()
{
  XBee.begin(9600);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  // If data comes in from XBee, send it out to serial monitor
  if(XBee.available()){
    digitalWrite(ledPin, HIGH);
  }
}

But I am unsure on how to connect this physically, is the middle pin where the jumpers usually go the DIN/DOUT of the XBee? I already tried connecting those to my digital pins 2 and 3 but I still can’t get the LED to light up.

EDIT
I was finally able to make it work! I wasn’t understanding serials correctly, I still haven’t managed to be able to upload AND see the xbee serial at the same time but I can work with just switching the jumpers around.

Thank you all for your help, honestly!