I’ve been trying to use a finite state machine as a base to set up an RN-XV from an arduino using the UART interface. I would really like some feedback while debugging, so I’m currently trying to issue a command then wait for a response before issuing the next command. I can see commands being issued but they are interspersed with a lot of garbage so I’m not sure what’s going on. it ends up locking up the rn-xv - I have to reset the unit before initializing now.
I’m using the adafruit xbee adapter, btw…
My code is below. The important sections at the moment are INIT and WFR (wait for response). At the moment I’m just trying to dump the data back to the serial monitor but eventually I would like to read the responses into arrays and ensure I’m getting AOK and not ERR. (I could use some advice on how to do that too, actually…)
#include <SoftwareSerial.h>
#include <avr/pgmspace.h>
prog_char init_0[] PROGMEM = "$$";
prog_char init_1[] PROGMEM = "set wlan ssid CalypsonAP\r";
prog_char init_2[] PROGMEM = "set wlan phrase spork\r";
prog_char init_3[] PROGMEM = "set wlan auth 2\r";
prog_char init_4[] PROGMEM = "set wlan join 2\r";
prog_char init_5[] PROGMEM = "set ip proto 18\r";
prog_char init_6[] PROGMEM = "set iphost 0\r";
prog_char init_7[] PROGMEM = "set ip remote 80\r";
prog_char init_8[] PROGMEM = "set dns name www.pachube.com\r";
prog_char init_9[] PROGMEM = "set com remote POST$/api/feeds/43798.csv?_method=put&key=0E9E-s6ZvEBZHEbgiQ_Xd2qaqPh-uxxsa4VoGqOOO5ST_IAP0SxFqitQDz6Dnxv0PTHNYpogohvPG33_NkcUEkv0UTpqWzZ4pMAfwWvhcllyll-0NRAQQ&1=";
prog_char init_10[] PROGMEM = "save\r";
// Then set up a table to refer to your strings.
PROGMEM const char *init_table[] = // change "string_table" name to suit
{
init_0,
init_1,
init_2,
init_3,
init_4,
init_5,
init_6,
init_7,
init_8,
init_9,
init_10 };
SoftwareSerial mySerial(2, 3);
//first define states with obvious names and different values
#define START 0
#define INIT 1
#define POLL 2
#define WFR 3
char buffer[200];
int state = START;
int lastState = START;
int initStep = 0;
int resetPin = 4;
int delayLen = 750;
void setup(){
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
Serial.begin(57600);
mySerial.begin(9600);
}
void loop(){
//FSM time
switch(state){
case START:
Serial.println("Initializing wifi..."); //More to come later
state = INIT;
break; //end of START case
case INIT:
lastState = INIT;
if (initStep==0){ // reset the RN-XV
digitalWrite(resetPin, LOW);
delay(100);
digitalWrite(resetPin, HIGH);
}
strcpy_P(buffer, (char*)pgm_read_word(&(init_table[initStep])));
Serial.println(buffer); //see command
mySerial.print(buffer); //send command
delay(100);
initStep++;
if (initStep == 11){
state = POLL; //we're done here - move on
}
else {
state = WFR; //wait for a response
}
break;
case POLL:
lastState = POLL;
Serial.println("Polling...");
delay(5000);
break;
case WFR:
while (!mySerial.available()); // block until we get a response. Eventually I'll add a timeout.
while (mySerial.available()){
Serial.print((char)mySerial.read()); // Dump the response to the serial monitor.
}
state = lastState;
break;
}
}