RN-XV - how do I block while waiting for a specific string?

I’m trying to talk to the RN-XV adapter via the UART. To send data over wifi from an arduino, I need to send the command sequence, “$$$” wait for “CMD”, then open the connection with “open www.pachube.com 80” and wait for OPEN before sending data. Often the adapter will spit out <2.13> first then OPEN after a few moments. How can I write the code to block while waiting for the OPEN string?

This is the adapter: http://dlnmh9ip6v2uc.cloudfront.net/dat … -RN-UM.pdf

Thanks in advance…

I could really use some help with this. Is anyone else trying to talk to the RN-XV via UART?

I’m using the following code to issue a string of commands - the same commands can be entered via teraterm and they seem to work. When you issue the commands in teraterm it responds with specific strings but I seem to be getting garbage with the arduino. For example, if I enter $$$ in teraterm it responds with CMD immediately. I’m supposed to enter open nimbits1.appspot.com after this then wait for it to connect. Teraterm returns a OPEN after a couple of command lines - and on the same line as the second command line (<2.31>) At this point the green light goes steady and I’m supposed to send the URL string (GET…) It should respond with page content and CLOS at the end to indicate the connection is closed. My code never makes it past issuing the domain name. If I use the peek function I see garbage and some fragments of the strings I’m expecting.

Without the peek code I get the following:

Initializing WIFI…

Hopefully the green light is flashing slowly by now…

$$$

Matched CMD: CMD

open nimbits1.appspot.com 80

With the peek code I get:

Initializing WIFI…

Hopefully the green light is flashing slowly by now…

W$$$

D

Matched CMD: CMD

open nimbits1.appspot.com 80

¹ÿ¹ÿµÿ¥ÿÅþ.ÁÿÍÿ½ÿ³þcþkÖLøCt .17.

<2.31*O

This is actually a finite state machine. I’ve stripped out all the switching to focus on just the problem functionality.

#include <SoftwareSerial.h>
#include <avr/pgmspace.h>


prog_char init_0[] PROGMEM = "$$";
prog_char init_1[] PROGMEM = "set wlan ssid temp\r"; //using an ad-hoc network so we can run wireshark
prog_char init_2[] PROGMEM = "set wlan phrase rascal\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 0\r";
prog_char init_9[] PROGMEM = "set com remote 0";
prog_char init_10[] PROGMEM = "set com size 1420";
prog_char init_11[] PROGMEM = "save\r";
prog_char init_12[] PROGMEM = "exit\r";

prog_char poll_0[] PROGMEM = "$$";
prog_char poll_1[] PROGMEM = "open nimbits1.appspot.com 80\r";
prog_char poll_2[] PROGMEM = "GET /service/batch?email=sdkljlkje@gmail.com&format=double&secret=dd34ae90-62e4-401d-5466-14126024de64&p1=test&v1=1234 HTTP/1.0\r";

PROGMEM const char *init_table[] =
{   
  init_0,
  init_1,
  init_2,
  init_3,
  init_4,
  init_5,
  init_6,
  init_7,
  init_8,
  init_9,
  init_10,
  init_11,
  init_12 };

PROGMEM const char *poll_table[] = 
{   
  poll_0,
  poll_1,
  poll_2 };

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
#define ASSOC 4
#define SPIN 5

#define MAX_LINE 90

#define DEBUG_PRINT


char buffer[64];
char lastLine[81];

int state = POLL;  //create state variable and initialize it to START  state
int lastState = state;
int taskStep = 0;
int lastTaskStep = taskStep;
int resetPin = 4;



void setup(){
  pinMode(resetPin, OUTPUT);
  digitalWrite(resetPin, LOW);
  digitalWrite(resetPin, HIGH);

  Serial.begin(57600);
  mySerial.begin(9600);

  Serial.println("\nInitializing WIFI...");

  delay(15000);
  Serial.println("Hopefully the green light is flashing slowly by now...");
}


void loop(){
  char line[MAX_LINE + 1]; // must be at least 1 char > MAXLINE

  lastState = state;
//  if (mySerial.available() > 0)
//  {
//    static char c;
//    c = mySerial.peek();
//    Serial.print(c);
//  }

  if (taskStep == lastTaskStep){
    strcpy_P(buffer, (char*)pgm_read_word(&(poll_table[taskStep])));

    Serial.println(buffer);

    mySerial.flush();
    mySerial.print(buffer);
    
    if (lastTaskStep >=2){
      lastTaskStep=0;
      taskStep=0;
    } else {
      lastTaskStep++;
    }
    
  }

  if (lineAvailable(MAX_LINE,line))
  {
    String temp = line;
    if (temp.endsWith("CMD")){
      Serial.print("Matched CMD: "); 
      Serial.println(temp);
      taskStep = 1;
    }
    else if (temp.endsWith("*OPEN*")){
      Serial.print("Matched *OPEN*: "); 
      Serial.println(temp);
      taskStep = 2;
    }
    else if (temp.endsWith("*CLOS*")){
      Serial.print("Matched *CLOS*: "); 
      Serial.println(temp);
      taskStep = 0;
    } 

  } 

}

boolean lineAvailable(int max_line,char *line)
{
  int c;
  static int line_idx = 0;
  boolean eol = false;
  if (max_line <= 0)    // handle bad values for max_line
  {
    eol = true;
    if (max_line == 0)
      line[0] = '\0';
  }
  else		    // valid max_line
  {
    if (mySerial.available() > 0)
    {
      c = mySerial.read();
      if (c != -1)  // got a char -- should always be true
      {
        if (c == '\r')
          eol = true;
        else
          line[line_idx++] = c;
        if (line_idx >= max_line)
          eol = true;
        line[line_idx] = '\0';     // always terminate line, even if unfinished
        if (eol)
          line_idx = 0;	     // reset for next line
      }
    }
  }
  return eol;
}

I don’t use Arduino, but could your problem be due to interrupts fouling up the received bit timing on the serial port? If you have two independent sets of serial routines, one relying on software timing loops and the other on interrupts, they will conflict.

I don’t think that is the problem here, but it will be a concern in the final device. I do have another device that will be generating interrupts. Thanks for bringing it up.