Hi, I am trying to get acknowledgement packets from my XBee End-device on whether my sent frame was received successfully, if not, the same frame will be retransmitted. However, when the XBees are not on the same network, frames will fail to transmit, and the End-device will not continue sending the frames it has missed. Also, when the reset button is pressed, a few frames are being skipped after it is rebooted.
For example, frames 1, 2, 3 are sent successfully, then on frame 4, the devices lose connection (nwk ack failure, not joined to network) / reset button is pressed, the code continues to run (frame 5, 6...), and when the devices are back on the same network / reboot completed, frames will continue to be sent from lets say frame 8.
How can I make the device send the frame (say frame 4, according to above example) from when frames fail to transmit and when the reset button is pressed?
I am reading the reset button state, but the XBee3 document (page 24) states that the reset pin is an input pin. I am getting correct button states, but after the button is pressed, it will toggle between 0 and 1 randomly, so now I am not sure if it is correct. Any help is greatly appreciated
This is my code:
byte basicFrame[273] = {0x7E, 0x01, 0x0D, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte rfData[48] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48};
byte lastFrame[184] = {0x7E, 0x00, 0xB4, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,
0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x98};
int wakePin = 2; //xbee pin 10 (sleep) to arduino pin 2
int resetPin = 4; //xbee pin 6 (reset)
int resetState; //state of reset button (active low reset)
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(resetPin, INPUT);
pinMode(wakePin, OUTPUT);
delay(300);
Serial.println("Setup Complete"); //debug
}
void loop()
{
digitalWrite(wakePin, LOW);
for (int i = 0 ; i < 48 ; i ++) //frames 1-48
{
basicFrame[17] = rfData[i]; //increment header from 1-48
//Serial.print(basicFrame[17], HEX);
for (int x = 19 ; x < 272 ; x ++) //rf data [19]-[272]
{
basicFrame[x] = rfData[i]; //Serial.println(basicFrame[x], HEX);
}
byte s = 0; //checksum frame 1(A7)-frame 48(35)
s += rfData[i];
long chexsum = 0x10 + 0x01 + 0x49 + (s * 254);
byte checksum = 0xFF - (chexsum & 0xFF); //keep last 8 bits, subtract from 0xFF
basicFrame[272] = checksum; //replace checksums from frame 1-48
//Serial.println(checksum, HEX);
//for (int y = 0 ; y < 273 ; y ++)
//{
// Serial.print(basicFrame[y], HEX);
//}
//Serial.println();
resetState = digitalRead(resetPin); //read state of reset button
Serial.println(resetState);
delay(50);
Serial1.write(basicFrame, sizeof(basicFrame)); //send frames 1-48
delay(50); //for stability
//acknowledgement coding:
if (resetState == 1) //if button not pressed (11 bytes)
{
if (Serial1.available() >= 11)
{
for (int d = 0 ; d < 8 ; d ++) //skip to read 9th byte
{
byte discard = Serial1.read();
}
byte deliveryStat = Serial1.read(); //9th byte
byte junk1 = Serial1.read(); //read remaining 2 bytes to prevent errors
byte junk2 = Serial1.read();
if (deliveryStat == 0x00) //success frame: 7E 00 07 8B 01 00 00 00 00 00 73
{
Serial.println("Success, Sending next frame... "); //move on to send next frame
delay(500);
}
else if (deliveryStat == 0x21) //failure frame: 7E 00 07 8B 01 00 00 00 21 00 52
{
Serial.println("Failure, Retransmitting...");
Serial1.write(basicFrame, sizeof(basicFrame)); //re-transmit same frame
delay(500);
}
else if (deliveryStat == 0x22) //wait for end device to be in nwk
{
Serial.println("Not joined to network");
delay(300);
}
}
}
else if (resetState == 0) //button pressed (6 bytes)
{ //reset frame: 7E 00 02 8A 00 75
Serial.println("Hardware reset");
Serial.println("Device associated, sending frames"); //approx 1-5 secs to send packets
Serial.println("Please wait...");
delay(300);
Serial1.write(basicFrame, sizeof(basicFrame)); //send frame again
delay(500);
}
delay(20);
}
Serial1.write(lastFrame, sizeof(lastFrame)); //send frame 49
while(1)
{}
}