[Reference]Xbee baudrate restored

Using OTA functions with Waspmote
Post Reply
libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

[Reference]Xbee baudrate restored

Post by libelium-dev » Fri Apr 10, 2015 1:36 pm

Hello all,

There is a useful code to check and change the Xbee's baudrate using waspmote, if you have problems sending or receiving data maybe is due to a bad baudrate configuration of the Xbee, this code will solve this issue.
#include <WaspXBee802.h>

//AT: 7E 00 04 08 01 41 54 61
uint8_t AT[8] = {
  0x7E, 0x00, 0x04, 0x08, 0x01, 0x41, 0x54, 0x61};


//ATBD7: 7E 00 05 08 01 42 44 07 69
uint8_t ATBD7[9] = {
  0x7E, 0x00, 0x05, 0x08, 0x01, 0x42, 0x44, 0x07, 0x69};

//ATAP2: 7E 00 05 08 01 41 50 02 63
uint8_t ATAP2[9] = {
  0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x50, 0x02, 0x63};

//ATWR: 7E 00 04 08 01 57 52 4D
uint8_t ATWR[8] = {
  0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D};

//ATanswer: 7E 00 05 88 01 41 54 01 DF
uint8_t ATanswer[9] = {
  0x7e, 0x00, 0x05, 0x88, 0x01, 0x41, 0x54, 0x02, 0xdf};



void setup() {

  // put your setup code here, to run once:
  USB.println("Getting current BaudRate...");
  RTC.ON();


  checkBD(1200);
  checkBD(2400);
  checkBD(4800);
  checkBD(9600);
  checkBD(19200);
  checkBD(38400);
  checkBD(57600);
  checkBD(115200);

  USB.println("**************************************************************************");
  USB.println("**************************************************************************");


}


void loop() {
  // put your main code here, to run repeatedly:

}



// return 1 if BD introduced is the current one, 0 otherwise
uint8_t checkBD(uint32_t baudrate)
{
  uint8_t dummy;

  Utils.setMuxSocket0();
  delay(500);

  changeAPImode(baudrate);

  beginSerial(baudrate, 0);
  delay(200);
  serialFlush(0);
  delay(100);

  for (uint8_t i = 0; i < 9; i++)
  {
    printByte(AT, 0);
  }
  delay(150);

  // Now see if legible answer. Just the first byte is enought
  if (serialAvailable(0) > 0)
  {

    dummy = serialRead(0);
    if (dummy == ATanswer[0])
    { 
      closeSerial(0);
      delay(200);
      USB.print("current Baudrate is: ");
      USB.println(baudrate, DEC);
      changeBD(baudrate);


      if (baudrate == 115200)
      {
        USB.println("Baudrate ok");
        USB.println("XBee configuration OK");
      }
      else
      {
        USB.println("Baudrate changed to 115200");
        USB.println("XBee configuration OK");
      }

      return 1;
    }       
  }

  closeSerial(0);
  delay(200);

  USB.print("error at: ");
  USB.println(baudrate, DEC);
  return 0;
}

void changeAPImode(uint32_t BD)

  xbee802.ON();
  Utils.setMuxSocket0();
  delay(500);
  beginSerial(BD, 0);
  printString("+++", 0);
  delay(2000);
  printString("ATAP2,WR,CN\r\n", 0);
  delay(200);
  closeSerial(0);
  delay(200);

  //ZigBee
  beginSerial(BD, 0);
  for (uint8_t i = 0; i < 9; i++)
  {
    printByte(ATAP2, 0);
  }
  delay(150);
  for (uint8_t i = 0; i < 8; i++)
  {
    printByte(ATWR, 0);
  }
  delay(150);
  closeSerial(0); 

}


void changeBD(uint32_t BD)

  xbee802.ON();
  Utils.setMuxSocket0();
  delay(500);
  beginSerial(BD, 0);
  printString("+++", 0);
  delay(2000);
  printString("ATBD7,WR,CN\r\n", 0);
  delay(200);

  // 4.1 In case of Zigbee modules:

  for (uint8_t i = 0; i < 9; i++)
  {
    printByte(ATBD7, 0);
  }
  delay(150);
  closeSerial(0);
  delay(200);
  beginSerial(115200, 0);

  for (uint8_t i = 0; i < 8; i++)
  {
    printByte(ATWR, 0);
  }
  delay(150);
  closeSerial(0); 


}



Regards.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests