XBee Pro S3B SPI works intermittently

Hi everyone,

I’m currently trying to prototype with a XBee Pro 900HP S3B board attached to my Arduino Due. I’m running some SPI transmits, and the interesting thing that is happening to me is that the board only responds some of the time (maybe 5% of the time I get the correct response).

When I do get a response, the response is correct for the input command, but still I’m concerned that the Xbee is only working occasionally.

My code is below:


#include 

#define ATTN_PIN  8
#define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i)

void setup() {
  Serial.begin(9600);
  pinMode(ATTN_PIN, INPUT);
  SPI.begin();
  digitalWrite(SS, HIGH);
  delay(1000);
  Serial.println("Starting...");
}

char v[17] = "7E00040801485658";
char hexString[2];
uint8_t val;
int i = 0;
char outputChar[2];

void loop() {
 SPI.beginTransaction(SPISettings (1000000, MSBFIRST, SPI_MODE0));
 delay(10);
 digitalWrite(SS, LOW);
 delay(100);
 for(int i = 0; i < strlen(v); i = i + 2) {
    hexString[0] = v [i];
    hexString[1] = v [i + 1];
    hexString[2] = '\0';
    val = (uint8_t)strtol(hexString, NULL, 16);
    SPI.transfer(val);
 }
 Serial.println();
 digitalWrite(SS, HIGH);
 delay(100);
 Serial.print("ATTN: ");
 Serial.println(digitalRead(ATTN_PIN));
  if(digitalRead(ATTN_PIN) == 0) {
    int num = 0;
    Serial.println("Found something");
    digitalWrite(SS, LOW);
    delay(10);
    while(digitalRead(ATTN_PIN) == 0) {
      uint8_t val = SPI.transfer(0);
      if (val <= 0xFF) {
        outputChar[0] = TO_HEX(((val & 0xF0) >> 4));
        outputChar[1] = TO_HEX((val & 0x0F));
        outputChar[2] = '\0';
      }
      Serial.print(outputChar);
      num = num + 1;
    }
    Serial.println();
    digitalWrite(SS, HIGH);
    delay(10);
    Serial.print("Num Received: ");
    Serial.println(num);
  }
  SPI.endTransaction();
  i = i + 1;
  if(i == 20) {
    blackHole();
  }
  Serial.print("COUNT: ");
  Serial.println(i);
  delay(1000);
}

void blackHole() {
  SPI.end();
  Serial.println("Ending...");
  while(1);
}

Essentially it writes the command v via SPI, and then reads the ATTN pin to see if the Xbee has created a response.

Let me know if anything seems wrong in my code or if there is any timing problems that I could be running into!

Thanks!

1 Like