Hi, this is my first message here. I am doing a project on wireless sensor networks using Arduino (Arduino UNO to be more precise). Right now I have different sketches for different modules. One of the sketches receives information through an XBee radio and checks to see if it had already received it and if it hadn't it will send it (everything using broadcast). I am using the Arduino XBee API.
The problem I've got is that the sketch keeps restarting, so there is no way to know if I had received it already. I have read that too many Serial.print() might cause this because of RAM issues, but I have removed almost all of them, so I don't think it will be that. Here is the code, in case anyone can shed some light.
The variable names are in Spanish, so if anyone has any question about what anything does, please, don't hesitate to ask me.
Thank you!
PS: Please note that a lot of the code is commented (almost all of the Serial.print()).
#include <XBee.h>
XBee xbee;
XBeeAddress64 addr64;
ZBTxRequest zbTx;
ZBRxResponse rx = ZBRxResponse();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
uint8_t historial[10][9] = {0};
uint8_t payload[16] = { 0 };
int indiceHistorial;
const byte posContador = 8;
const byte posSaltos = 9;
const byte posTipo = 10;
const byte posLongitud = 11;
const byte posMensaje = 12;
void setup()
{
// Tell XBee to start Serial
xbee = XBee();
xbee.begin(9600);
addr64 = XBeeAddress64(0x0, 0xFFFF);
indiceHistorial = 0;
delay(10000);
Serial.flush();
Serial.println("INICIADO");
}
void loop()
{
recibirMensaje();
Serial.flush();
}
void recibirMensaje()
{
xbee.readPacket(100);
if (xbee.getResponse().isAvailable())
{
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
xbee.getResponse().getZBRxResponse(rx);
for(int i = 0; i < 16; i++)
{
payload[i] = rx.getData(i);
}
// Check to see if the message was already received
boolean distintos = true;
for(int i = 0; i < 10 && distintos; i++)
{
int iguales = 0;
for(int j = 0; j < 9; j++)
{
if(historial[i][j] == payload[j])
{
iguales++;
}
}
if(iguales > 8)
{
distintos = false;
}
}
// If it didn't exist already
if(distintos)
{
// Resend message
payload[posSaltos]++;
delay(100);
enviarMensaje();
}
else
{
Serial.println();
Serial.println("Ya se habia recibido este mensaje");
}
}
else
{
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
{
xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() != SUCCESS)
{
Serial.println();
Serial.println("REENVIANDO");
enviarMensaje();
}
}
}
}
/*else if (xbee.getResponse().isError())
{
Serial.println();
if(xbee.getResponse().getErrorCode() == CHECKSUM_FAILURE)
{
Serial.println("ERROR - CHECKSUM_FAILURE");
}
else if(xbee.getResponse().getErrorCode() == PACKET_EXCEEDS_BYTE_ARRAY_LENGTH)
{
Serial.println("ERROR - PACKET_EXCEEDS_BYTE_ARRAY_LENGTH");
}
else if(xbee.getResponse().getErrorCode() == UNEXPECTED_START_BYTE)
{
Serial.println("ERROR - UNEXPECTED_START_BYTE");
}
}*/
}
void enviarMensaje()
{
// First 9 positions indicate unique id
for(int i = 0; i < 9; i++)
{
historial[indiceHistorial][i] = payload[i];
}
indiceHistorial++;
if(indiceHistorial > 9)
{
indiceHistorial = 0;
}
zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
xbee.send(zbTx);
Serial.println();
Serial.print("Historial: ");
Serial.println(indiceHistorial);/*
Serial.println();
for(int i = 0; i < 10; i++)
{
Serial.flush();
Serial.print("Fila ");
Serial.print(i);
Serial.print(": ");
for(int j = 0; j < 9; j++)
{
Serial.print(historial[i][j], HEX);
}
Serial.println();
}*/
}