Saturday, June 18, 2016

Sending from coordinator, receiving by end device, and sending back to coordinator working!

Same simple 01 TX message as previous post.  Setting are the same and given in the code below.

Here's the Arduino code.  New code to send back a message is at the bottom.

/*****************************************************************
XBEE_receive_ex.ino

Set up a software serial port to pass data between an XBee Shield
and the serial monitor.

Hardware Hookup:
  The XBee Shield makes all of the connections you'll need
  between Arduino and XBee. If you have the shield make
  sure the SWITCH IS IN THE "DLINE" POSITION. That will connect
  the XBee's DOUT and DIN pins to Arduino pins 2 and 3.
 
 
  End Device + arduino
  DL = 1234
  MY = 5678
  API = 2
  CE = 0
 
  Receives this example rx signal from the transmission signal sent by coordinator
 
  Coordinator + explorer shield
  DL = 5678
  MY = 1234
  API = 2
  CE = 1
 
*****************************************************************/
// We'll use SoftwareSerial to communicate with the XBee:
#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
int incomingByte = 0;
int PacketArray[11] = {1,1,1,1,1,1,1,1,1,1,1}; // to store the validated xbee packet
int data = 0;
byte packetRX[] = {0x7E, 0x00, 0x07, 0x01, 0x01, 0x12, 0x34, 0x00, 0x03, 0xff, 0xB5};

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
Serial.println(XBee.available()); //prints # bytes in transmitted message
if (XBee.available()>10) //needs 11 bytes to proceed - this is the xbee soft serial port
  {
    incomingByte = XBee.read(); //read the first byte
    Serial.println(incomingByte);  //print the first byte to the serial monitor
    if (incomingByte == 0x7E){
      PacketArray[0]=0x7E; //this is the start of the frame
      for(int i=1; i<11; i++) {  // for the next 10 bytes, do the following
       PacketArray[i] = XBee.read(); //set each byte to a spot in the vector
       Serial.print(i); //print the array location
       Serial.print(" "); //print a space
       Serial.print(PacketArray[i]); // print the value of the byte
       Serial.print("  "); //print a space
       delay(100); // try different values
        }
        Serial.println(" "); //add a line break
        if (PacketArray[4] == 0x12 & PacketArray[5] == 0x34 & PacketArray[9] ==255 ){ // there are two byte of data, this is the second one
          Serial.println("ON"); // if the second byte is 0xFF or 255, print this.
          data = PacketArray[9] + PacketArray[8]*256
  //NEW PART
          if (data == 1023)
          {
           for (int counter = 0;counter<11;counter++)
           {
             Serial.print(packetRX[counter]);
             Serial.print("  ");
             XBee.write(packetRX[counter]);
           }
           Serial.println();
          }
        }  
      }
  }
  /*The coordinator sends a Tx status reply that is 7 bytes long.
  7e 00 03 89 01 00 75
  7E is the start of the packet
  00 03 is the # of bytes after the first 3 and before the checksum (last byte)
  89 is the frame type which is 89 (TX (Transmit) Status)
  01 is the frame ID (could be anything - doesn't mean anything
  00 is the status of the message - 00 means success
  75 is the checksum
  The following code clears this message from the XBee serial buffer  */
  if (XBee.available()>0)
  {
    while(XBee.available()>0)
    {
      Serial.println(XBee.read());
    }
  }
    delay(2000);//delay of 2 seconds between each iteration of void loop
   
} //end void loop

The code checks to see that the data is HIGH (1023), and if so, it sends a packet back.
I created a hard-coded packet
{0x7E, 0x00, 0x07, 0x01, 0x01, 0x12, 0x34, 0x00, 0x03, 0xff, 0xB5};
The code loops through this packet and prints to the serial monitor for debugging and the xbee.

I put each packet into XCTU's frame interpreter to illustrate.
    transmitted frame from end device             received frame coordinator
                






No comments:

Post a Comment