Pages: [1]   Go Down
Author Topic: Arduino for AT commands  (Read 958 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All,
 This is my first post and very new to Arduino.

I'm trying to  Communicate with serial device using Arduino.
Which usually replies "OK" for every AT commands.

I have to send next command only after i receive OK.

Example.

Code:
OK

N1 = TEST\n
OK

N0 \n
OK

T5  = 1 \n
OK

I tried this code..

Code:
char Sword[10];
char inchar;

void setup()
{
  Serial.begin(9600);
  //Sword.reserve(200);
  Serial.println("Hello World");
}

void loop()
{
  if(Serial.available() >0)
  {
    inchar=Serial.read();
    if (inchar=='O')
    {
      delay(10);
      inchar=Serial.read();
      if (inchar=='K')
      {       
        delay(10);
        Serial.print("N1 = TEST \n");
        if(Serial.available())
        {
          inchar=Serial.read();
          if (inchar=='O')
          {
            delay(10);
            inchar=Serial.read();
            if (inchar=='K')
            { 
              Serial.print("N2 = TEST \n");
            }
         }
       }
    }
  }
}
}



But didnt work.. Can anyone please provide me the example code..
Thanks in advance..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
  if(Serial.available() >0)
  {
    inchar=Serial.read();
    if (inchar=='O')
    {
      delay(10);
      inchar=Serial.read();
That band-aid (delay()) may not be big enough. Maybe you need
Code:
delay(10000000UL);

The response is probably NOT just the two characters O and K. There are likely other characters send, too, like carriage return and line feed. You need to determine EXACTLY what is returned, and read ALL the data returned.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In your code you don't send "AT" commands. Is your thread title wrong or your code?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The response is probably NOT just the two characters O and K. There are likely other characters send, too, like carriage return and line feed. You need to determine EXACTLY what is returned, and read ALL the data returned.

Hmmmm. I'm still trying with serial monitor, not into actual hardware yet.. I just wanted to know how to read and confirm that second OK  is received before sending 2nd command.. As manual said we should send 2nd command only after OK is received..

I'm trying with string here..

Code:

String Sword = "";

void setup()
{
  Serial.begin(9600);
  //Sword.reserve(200);
  Serial.println("Power On");
}

void loop()
{
  while(Serial.available())
  {
     char Data = (char)Serial.read();
     Sword += Data; 
     //Serial.print(Data);
     //Serial.println(Sword);       
  }
  if(Sword != "")
  {
    //Serial.println(Sword);
  }
  if(Sword == "OK") //First Response
  {
    Serial.print("N1 = TEST\n");// I got OK So Printed. First command
    Sword ="";
   
    char Data = (char)Serial.read();
    Sword += Data;
    //How to read OK Again ? For sending next command ?
    if(Sword == "OK") //Second Response
    {
   
       Serial.println("N2 = TEST\n"); //Second Response
    }
  }
    if(Sword == "NOTOK") //Just for Debugging
    {
      Serial.println("Rcvd Not OK!");
      Sword ="";
    }
}


And Also I'm not getting where the control get lost after printing 1st command
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are sending the OK from the Serial Monitor? What option is selected, at the bottom? Are you sending "OK" or are you sending "OK<cr><lf>"? If you are sending "OK<cr><lf>", the "OK" will match, and will be removed from the String. Then the "<cr><lf>" will arrive, followed by another "OK<cr><lf>", so the String will be "<cr><lf>OK<cr><lf>", which is, of course, not the same as "OK".
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hooray for a fellow AT command guy.

I absolutely hate the OK responses and waiting for them but sure this is possible.

I have had mixed success using string.trim() to remove the control characters. Most of the time im actually using substrings to find parameters within a response.

Something that might help us a bit more is the overall end goal of your sketch. I see that you are having trouble waiting for OKs to appear which is a constant problem you will face and as such the end goal might help us to provide something that will work later down the line when you are sending any number of commands.

If you search for waitFor and SM5100B you will find numerous implementations of a function that should allow for a better implementation.

Logged

Pages: [1]   Go Up
Jump to: