I have created the following code and it is not doing what I expected it to do >:(
Header file:
#ifndef test_H
#define test_H
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <XBee.h>
int *processData(XBee *myXBee, ZBRxResponse *myRx, SoftwareSerial *myNSS);
#endif
CPP file:
#include "test.h"
int *processData(XBee *myXBee, ZBRxResponse *myRx, SoftwareSerial *myNSS){
int *dataVar = (int*)malloc(sizeof(int)*6);
if (dataVar == 0)
{
myNSS->println("ERROR: Out of memory");
}
int casenum = -1;
int incomingByte;
char xbv[5];
int jj = 0;
int cnt = 0;
myXBee->readPacket();
if (myXBee->getResponse().isAvailable()) {
// got something
if (myXBee->getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
myXBee->getResponse().getZBRxResponse(*myRx);
if (myRx->getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
myNSS->println("packet acknowledged:");
} else {
myNSS->println("packet not acknowledged");
}
for(cnt = 0; cnt < myRx->getDataLength(); cnt++) {
incomingByte = myRx->getData()[cnt];
if(incomingByte == ','){ // Go to the next array slot
xbv[jj] = '\0';
casenum++; // Increment for next letter
dataVar[casenum] = atoi(xbv);
jj = -1;
} else {
xbv[jj] = incomingByte;
}
jj++;
}
}
} else if (myXBee->getResponse().isError()) {
myNSS->print("oh no!!! error code:");
myNSS->println(myXBee->getResponse().getErrorCode());
}
return dataVar;
}
Sketch File:
#include "test.h"
#include <SoftwareSerial.h>
#include <XBee.h>
XBee xbee = XBee();
ZBRxResponse rx = ZBRxResponse();
// Define NewSoftSerial TX/RX pins
// Connect Arduino pin 8 to TX of usb-serial device
uint8_t ssRX = 8;
// Connect Arduino pin 9 to RX of usb-serial device
uint8_t ssTX = 9;
SoftwareSerial nss(ssRX, ssTX);
void setup() {
Serial.begin(57600);
xbee.setSerial(Serial);
nss.begin(57600);
nss.println("Starting Up!");
}
void loop() {
if(Serial.available()) {
xBeeReport = processData(&xbee, &rx, &nss);
for (int ii=0; ii<6; ii++) {
nss.print(xBeeReport[ii]);
nss.print(", ");
}
free(xBeeReport);
xBeeReport = NULL;
nss.println();
}
}
I am sending the following message three time to my XBee in API2 mode:
1,-12,2,
31 2C 2D 31 32 2C 32 2C in hex
After receiving the message three times, it looks like this on my terminal:
Starting Up!
0, 0, 0, 0, 0, 0,
packet acknowledged:
1, -12, 2, 0, 0, 0,
0, -12, 2, 0, 0, 0,
packet acknowledged:
1, -12, 2, 0, 0, 0,
0, -12, 2, 0, 0, 0,
packet acknowledged:
1, -12, 2, 0, 0, 0,
But I expected to see the following message:
Starting Up!
packet acknowledged:
1, -12, 2, 0, 0, 0,
packet acknowledged:
1, -12, 2, 0, 0, 0,
packet acknowledged:
1, -12, 2, 0, 0, 0,
I can't figure out what I am doing wrong!
Can somebody point out my mistake?
Thanks in advance