OK, now I am completely confused.
I made a slightly simpler example that includes the FSM code and I can make it work/not work by just changing 1 if statement in a block of code that shouldn't even be being executed yet.
Here is the code:
#include <FiniteStateMachine.h>
#include <XBee.h>
XBee xbee;
XBeeAddress64 coord_addr = XBeeAddress64(0,0);
const byte NUMBER_OF_STATES = 2;
State state1 = State(f_enter, f_update, f_exit);
State state2 = State(f2_enter, f2_update, f2_exit);
FSM fsm = FSM(state1);
// send a message and return the frame ID of the sent message
uint8_t send_msg(uint8_t * msg, int msg_len, XBeeAddress64 dst_addr) {
randomSeed(analogRead(0));
uint8_t frame_id = random(1,254);
ZBTxRequest req = ZBTxRequest(dst_addr, ZB_BROADCAST_ADDRESS, ZB_BROADCAST_RADIUS_MAX_HOPS, ZB_TX_UNICAST, msg, msg_len, frame_id);
Serial.println("Sending Message");
Serial.print(req.getFrameId(), HEX);
Serial.println(" <- FrameID");
xbee.send(req);
return frame_id;
}
// Check if a specific frame ID message sent
boolean did_msg_send(uint8_t frame_id) {
Serial.println("Waiting for TX confirmation");
int max_loops = 10;
int loop_count = 0;
while(loop_count < max_loops) {
if(xbee.readPacket(100)) {
if(xbee.getResponse().isAvailable()) {
Serial.println("Something available");
if(xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
ZBTxStatusResponse tx_resp;
xbee.getResponse().getTxStatusResponse(tx_resp);
if(tx_resp.getFrameId() == frame_id) {
if(tx_resp.isSuccess() == true)
{
Serial.println("Message delivered");
return true;
} else {
Serial.println("Message TX failed...maybe");
Serial.print(tx_resp.getDeliveryStatus(), HEX);
Serial.println(" <- Delivery status");
Serial.print(tx_resp.getRemoteAddress(), HEX);
Serial.println(" <- Remote Address");
Serial.print(tx_resp.getTxRetryCount(), HEX);
Serial.println(" <- TX Retries");
Serial.print(tx_resp.getDiscoveryStatus(), HEX);
Serial.println(" <- Discovery status");
return false;
}
} else {
Serial.println("Received TX status for incorrect frame ID");
Serial.print(frame_id, HEX);
Serial.println(" <- Expected");
Serial.print(tx_resp.getFrameId(), HEX);
Serial.println(" <- Received");
}
} else {
Serial.println("Received something else");
Serial.print(xbee.getResponse().getApiId(), HEX);
Serial.println(" <- API ID");
}
}
}
else{
Serial.println("Timed out reading packet");
}
delay(500);
Serial.println("looping");
loop_count += 1;
}
Serial.println("Looped too many times");
return false;
}
void setup() {
pinMode(13, OUTPUT);
xbee = XBee();
Serial1.begin(9600);
xbee.setSerial(Serial1);
Serial.begin(9600);
while(!Serial) {}
digitalWrite(13, HIGH);
}
void loop() {
delay(2000);
fsm.update();
}
void f_enter() { Serial.println("enter"); }
void f_update() {
Serial.println("update");
uint8_t registration_msg[] = {'R','E','G','I','S','T','R','A','T','I','O','N','1'};
uint8_t msg_id = send_msg(registration_msg, sizeof(registration_msg), coord_addr);
if (did_msg_send(msg_id)) {
Serial.println("Sent");
while(true) {
xbee.readPacket();
if(xbee.getResponse().isAvailable()) {
Serial.println("Response available");
if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
ZBRxResponse rx_resp = ZBRxResponse();
xbee.getResponse().getZBRxResponse(rx_resp);
uint8_t rx123_data[4];
rx123_data[0] = rx_resp.getData(0);
rx123_data[1] = rx_resp.getData(1);
rx123_data[2] = rx_resp.getData(2);
rx123_data[3] = rx_resp.getData(3);
if((rx123_data[0] == 0x30) && (rx123_data[1] == 0x31) && (rx123_data[2] == 0x30) && (rx123_data[3] == 0x31)){
// if(1 == 1) {
Serial.println("Anything");
fsm.transitionTo(state2);
break;
}
else {
Serial.println("Got something other than registration success");
}
}
}
}
}
}
void f_exit() { Serial.println("exit"); }
void f2_enter() { Serial.println("enter2"); }
void f2_update() { Serial.println("update2"); }
void f2_exit() { Serial.println("exit2"); }
If I change the line
if((rx123_data[0] == 0x30) && (rx123_data[1] == 0x31) && (rx123_data[2] == 0x30) && (rx123_data[3] == 0x31)){
to something like if(1 == 1){ then it magically starts working!
It shouldn't have even got to evaluating this yet though as did_msg_send needs to return true and then it needs to receive a ZB_RX_RESPONSE, which it isn't.
Any insight would be very helpful.