Pages: [1]   Go Down
Author Topic: SM5100B trouble finding <CR>  (Read 1770 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I'm having an issue parsing a response from the SM5100B and finding a carriage return.  Currently, I set the module to notify me of a new SMS (AT+CNMI=3,3,0,0).  According to the documentation:

http://www.sparkfun.com/datasheets/CellularShield/SM5100B%20AT%20Command%20Set.pdf (page 72 of 141, PDF)

there is a carriage return right before the text of the SMS.  When my ParseResp() function is run, it can't find a carriage return.

I know my ParseResp() code works because I tested it by looking for a different character.  For instance, I told it to look for a "&", and then a put a "&" at the beginning of my SMS to the module.  Obviously this is a quick fix for the problem, and I could just start all my SMSs with a "&", but I would rather fix the issue than skirt around it.

Here is all my code:
Code:
/*  There is an issue here with digitalRead and digitalWrite.
    When I send pin 8 HIGH, pin 7 reads as HIGH for some reason.
*/

#include <NewSoftSerial.h>  
#include <string.h>        
#define BUFFSIZE 512

char ATBuff[BUFFSIZE];
String SMSBuff = "";
String Mess = "";
int CELL_REG = 0;
int CELL_AT = 0;
//int CELL_SMSMODE = 0;
int nBufPos;
int dataReady = 0;
int noCR = 0;
NewSoftSerial cell(2,3);

void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
  delay(1500);
  Serial.println("Starting Serial");
  pinMode(7,INPUT); //get rid of this
    
  while(CELL_REG == 0 || CELL_AT == 0){
    ATGetResp();
    ATParse();
  }
  
  Serial.println("Found Network");
  SetText();
  SetNotify();
  
  /*   Sends SMS
  cell.print("AT+CMGS=");
  cell.print(byte(34));
  cell.print("PHONENUMBER");
  cell.print(byte(34));
  cell.print(byte(13));
  cell.print("'IT WORKS!!!!!'");
  cell.println(byte(26));
  Serial.println("done");
  */
  
  
}

void loop() {
  
  //This loop exists to delete SMSs while the code is running
  if (digitalRead(7)==HIGH){
    cell.println("AT+CMGD=1,4");
    Serial.println("SMSs deleted.");
  }
  //////////////////////////////////////////////////////////
  
  //This loop exists to look for incoming data from the sm5100b
  //   If it finds data, it parses it and decides what to do
  dataReady = cell.available();
  if (dataReady > 0){
    //Serial.println("data available");
    ATGetResp();
    Serial.print(ATBuff);
    ParseResp();
    dataReady = 0;
  }
  ///////////////////////////////////////////////////////////

}

// This function gets commands from the sm5100b
void ATGetResp(void) {
    char c;
    memset(ATBuff, '\0', BUFFSIZE);                // clear array
    
    nBufPos = 0;                                   // Reset array counter
    int nBytes = 0;                                
    nBytes = cell.available();                     // Store number of bytes in serial queue
    
    if (nBytes > 0) {
      for (int i = 1; i <= nBytes; i++) {          // Keep running loop till no more bytes in queue
        if (nBufPos == BUFFSIZE - 3) {             // Stop if array is full
          nBufPos = 0;
        }  
        c = cell.read();                           // Set c to current serial character
        if (c == '\r') {                           // If c is carriage return
            ATBuff[nBufPos] = c;                   //   add it to the array
            c = cell.read();                       // Doesnt work without this line, can anyone explain why?
            if (c == '\n') {                       // If c is newline
              ATBuff[nBufPos] = c;                 //   add to array
              ATBuff[nBufPos+1] = '\0';            //   and terminate
              return;                              //   then return to program
            }
        }
        ATBuff[nBufPos] = c;
        nBufPos++;
      }
    
    }
    ATBuff[nBufPos] = '\0';
    return;
}
///////////////////////////////////////////////////////

//This function parses the intial responses from the sm5100b
//   to make sure everything is operating correctly
void ATParse(void){
  if (strstr(ATBuff, "+SIND: 11") != 0) {
    CELL_REG = 1;
    return;
    }
  if (strstr(ATBuff, "+SIND: 4") != 0) {
    CELL_AT = 1;
    return;
    }
}
///////////////////////////////////////////////////////

//This function sets the sm5100b to TEXT mode and waits
//  for error free response
void SetText(void){
  int isText = 0;
  cell.println("AT+CMGF=1");
  while(!isText){
    ATGetResp();
    if (strstr(ATBuff,"OK") !=0){
      isText = 1;
      Serial.println("Text is go");
      return;
    }
  }
}
////////////////////////////////////////////////////

//This function sets the sm5100b to notify when it
//   receives an SMS and waits for an error free response
void SetNotify(void){
  int isNotify = 0;
  cell.println("AT+CNMI=3,3,0,0");
  while(!isNotify){
    ATGetResp();
    if (strstr(ATBuff,"OK") !=0){
      isNotify = 1;
      Serial.println("Notify is go");
      return;
    }
  }
}
//////////////////////////////////////////////////////

//This function parses the data that is received after
//   the sm5100b is set up correctly.  Right now it just
//   looks for SMS and attempts to parse the response to
//   pull out the actual message
//   NOTE to me:  issue with noCR, should be resolved
//   with <CR> issue
void ParseResp()
{
  if (strstr(ATBuff,"+CMT:") !=0 || noCR == 1){
    String SMSBuff = ATBuff;
    if (SMSBuff.indexOf('\r') != -1){   //found CR '\r'  tried 13...no dice
      for(int j = SMSBuff.indexOf('\r')+1; j<=SMSBuff.length(); j++){
        Mess += SMSBuff[j];
        noCR = 0;
      }
    Serial.print("The message is: ");
    Serial.print(Mess);  
    }
    else{      //no <CR>
      noCR = 1;
      Serial.println("No <CR> found :(");
      return;
    }
    
  }
  else{
    return;
  }
}
//////////////////////////////////////////////////////

NOTE:  I took the ATGetResp() code from user Philherup.

My question for anyone out there is:  Why can't I find the carriage return?  What am I missing and/or doing wrong?

Thanks!

Logged

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

In the ATGetResp() function, I would recommend putting each { on a new line, and properly indenting the code.

Put the useful comments BEFORE the code. Get rid of the useless comments.
Code:
       c = cell.read();                           // Set c to current serial character
Well, duh?

When you have long comments at the end of the code, and more code than fits in the window, I have to scroll up and down and left and right to read it. Scrolling windows inside of windows is difficult. Make it easy by making the lines short, so the vertical scroll bars are not necessary.

Comments after code are almost always added to explain what a statement is doing, for the benefit of new users. The people that will be able to help you are NOT new users.

Read each character, and decide what to do with THAT character. Nested reads are very difficult to keep straight.
Logged

Pages: [1]   Go Up
Jump to: