PIC to MAX232

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@jayanthd I am going to have a field day figuring out your code:eek: but thanks though:)
I just don't use the code, I try to figure it out and write. Without that I won't learn anything.
I had to ask why "asm clrwdt" so much ? I am not using WDT. :confused:

I just needed a parsing routine my friend. I appreciate the effort but I have no idea how to implement your code.

Is it possible to read the modem response shown in realterm images and set flags accordingly. I can use the flag bits to do what I want.
So far there is no SMS reading. The images show what I will be using for now. Just sending.
Oh crap ! I just remembered I would need to parse ">" too to send SMS :oops:

Modem Init() Just sends basic commands as this modem does not save any settings I believe.
Needs them on power cycle. That is what I figured.

By the way there no GSM reset.
The modem resets itself during network lost.
And there no connection to modem besides TX and RX.


in line 25 it seems that the code break out of the infinite while loop.
I don't think it does .


CSS:
   if(Get_Response() == ERROR)              // AT+CPIN?
     Lcd_Out(3,9,Const2Ram(String,MSLD10)); // "Detecting!";
      Send2Modem(ATC4);
      Wait();
   if(Get_Response() == GSM_ERROR)   // +COPS: 0
       Lcd_Out(4,9,Const2Ram(String,MSLD11)); //"Registering!";
  
   if(Get_Response() == GSM_DhiMobile)   // +COPS: 0,2,47201
       Lcd_Out(4,9,Const2Ram(String,MSLD08)); //"DhiMobile";
   if(Get_Response() == GSM_Ooredoo)   // +COPS: 0,2,47202
       Lcd_Out(4,9,Const2Ram(String,MSLD09)); //"Ooredoo";
You see The lcd shows "Registering!" when I issue " AT+COP: 0?"
and it shows ""Detecting!" when I issue " AT+CPIN?"

So it parsing up to the " AT+COP: 0?".
I think the parsing stops at ","
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Is it possible to read the modem response shown in realterm images and set flags accordingly. I can use the flag bits to do what I want.
So far there is no SMS reading. The images show what I will be using for now. Just sending.
That is what I have done.

In my Send_Const_AT_Cmd() function the last parameter can be 0 or 1. If it is 1 then when function returns the buffer will be cleared and if it is 0 the buffer will not be cleared and you can use strstr() or strcmp() to check the buffer with the response for that AT command which you issued.

The gsmAttempt value by default in the Send_Const_AT_Cmd() is 3 that is if it doesn't get any response or error respone then it tries to send that AT command three times. If it still gets no / error response then it clears the buffer and repeats the process and hence I have used the

Code:
while(gsmAttempt < 3) {

}
If attempt is more than 3 then it comes out of the loop and based on it the gsmStatus value is set.

The outer strstr() functions is for checking the responses that is in the buffer after issuing the particular AT command.

I have made the parsing function of +COPS: response.

I need to know what you will do after extracting the singal strenght value to write the code.

There was a bug in the code. I am fixing it. I will post the new code in 10 minutes.
 

jayanthd

Joined Jul 4, 2015
945
why so much "asm clrwdt" ?
Have not yet fine tuned the WDT code. For that exact hardware is needed. I have PIC16F887 but I don't have your modem to test and tune it and hence lots of WDT so that WDT doesn't cause a reset because WDT is enabled. Later after code works fine then you can remove the unnecessary

C:
asm clrwdt
from the code after testing time taken for executing each block of code.

Here is the new code. Have fixed all errors. Now I think it will work great.

If you want to send a AT command and check for Y response related to that command then you can just use

Send_Const_AT_Command() with that AT command and its response with last parameter of the function as 1. This will clear the buffer when the function returns but if it doesn't receive the response required then it will keep trying 3 times and then if no response then it sends GSM reset command and then keeps trying the AT command.


Just test my code. It will beautifully work and show all required messages on the LCD. Just change LCD connections in the code according to yours.

Have to add one more command that is AT+CMGD for deleting SMS. This is done only if new SMS notification is received. For that you have to use AT+CNMI command once after modem responds for AT.

C:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

#define ON   1
#define OFF  0

#define HIGH 1
#define LOW  0

#define TRUE 1
#define FALSE 0

// GSM module connections
sbit gsmReset at RD7_bit;
sbit gsmReset_Direction at TRISD7_bit;
// End of GSM module connections

sbit LED at RD0_bit;

char gsmBuffer[80];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 0;

sbit sendSmsFlag at myFlags.B0;

//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS=\"0000000000\"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
const char atCommandB[] = "AT+COPS?\r";
const char atCommandC[] = "AT+CPIN?\r";
const char atCommandD[] = "AT+CSQ?\r";
const char atCommandE[] = "ATH\r";

const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: \"SM\",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING\r\n\r\n";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
const char atResponseA[] = "+COPS: 0";
const char atResponseB[] = "+COPS: 0,2,47201";
const char atResponseC[] = "+COPS: 0,2,47202";
const char atResponseD[] = "+CSQ: ";
const char atResponseE[] = "AT\r\n\r\nOK\r\n";
const char atResponseF[] = "AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n";
const char atResponseG[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n";
const char atResponseH[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n";
const char atResponseI[] = "AT+CPIN?\r\n\r\nERROR\r\n";
const char atResponseJ[] = "AT+CPIN?\r\n\r\n+CPIN: READY\r\n";
const char atResponseK[] = "AT+CSQ\r\n\r\n+CSQ: ";
const char atResponseL[] = "AT+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";

const char msg1[] = "No Network ";
const char msg2[] = "No SIM     ";
const char msg3[] = "No Network And No SIM";
const char msg4[] = "Network and SIM Ready";
const char msg5[] = "Signal Strength = ";
const char msg6[] = "Not Registered On Network";
const char msg7[] = "SIM Removed";
const char msg8[] = "OK         ";
const char msg9[] = "ERROR      ";
const char msgA[] = "SIM Ready  ";
const char msgB[] = "Dhi Mobile ";
const char msgC[] = "Ooredoo    ";
const char msgD[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

const char sms1[] = "LED is ON";
const char sms2[] = "LED is OFF";
const char sms3[] = "Invalid Command Received";

//Function Prototypes
void DelayXSec(unsigned long int sec);
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);

void interrupt() {
  
    if((RCIE_bit) && (RCIF_bit)) {
        if(OERR_bit) {
              OERR_bit = 0;
              CREN_bit = 0;
              CREN_bit = 1;
        }

        gsmBuffer[gsmBufferIndex++] = UART1_Read();
        gsmBuffer[gsmBufferIndex] = '\0';

        RCIF_bit = 0;
    }   
}

char *CopyConst2Ram(char *dest, const char *src){
    char *d;
  
    asm clrwdt
    d = dest;
  
    for(;*dest++ = *src++;)asm clrwdt;

    return d;
}

void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}

void GSM_RESET() {
    asm clrwdt
    gsmReset = 1;   
    Delay_ms(20);
    gsmReset = 0;   
}

void Extract_Signal_Strength(char *s1, char *s2) {
    unsigned int i = 0;
  
    asm clrwdt
  
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
  
    asm clrwdt
  
    while(*s1 != ' ') {
           *s1++;
           asm clrwdt
    }

    *s1++;
  
    while(*s1 != ',') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';   
    asm clrwdt
}

void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;
  
    asm clrwdt
  
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
  
    asm clrwdt
  
    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            asm clrwdt
    }

    *s2 = '\0';                       
    asm clrwdt
}

void Get_SMS_Index(char *s1, char *s2) {

    while(*s1 != 'C') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'M') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'T') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'I') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != ',') {
           *s1++;
           asm clrwdt
    }

    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}

void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
  
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));       
        DelayXSec(2);       
        if((*gsmAttempt)++ == 3) {
            GSM_RESET();           
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
        }
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));   
    (*gsmAttempt) = 0;
    (*idx) = 0;
}

void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    asm clrwdt
  
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        DelayXSec(2);
      
        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;           
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }

    (*idx) = 0;

    Extract_SMS(s1, s6);
  
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    asm clrwdt
}

void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
  
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        asm clrwdt
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);
      
        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);
      
        DelayXSec(6);
    }

    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    asm clrwdt
}

void main() {

    asm clrwdt
    //OPTION_REG = 0x87;
  
    CMCON = 0x07;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x80;
    TRISD = 0x00;
    TRISE = 0x00;
  
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
  
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
      
    UART1_Init(9600);
    Delay_ms(100);
  
    RCIF_bit = 0;
    RCIE_bit = 1;
  
    PEIE_bit = 1;
    GIE_bit = 1;   
          
    while(1) {

        asm clrwdt
      
        switch(gsmState) {
            case 0:
                
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //If new SMS  received
                      DelayXSec(1);                                                 
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));
                      gsmState = 1;                           
                  }                     
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                      gsmState = 0;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                    
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                     
                
                  gsmBufferIndex = 0; 
                  gsmAttempt = 0;
                  break;           
            case 1:
                
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseJ, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //If new SMS  received
                      DelayXSec(1);                                                     
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                      gsmState = 2;                           
                  }                     
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));
                      gsmState = 1;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                    
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                                         
                    
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 2:
                
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseF, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                    
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {   //If new SMS  received
                      DelayXSec(1);                         
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                      gsmState = 2;                           
                  }                     
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) {
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));
                      gsmState = 3;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) {
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));
                      gsmState = 3;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                    
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }                     
                    
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
          
            case 3:           
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) {   //If new SMS  received
                      DelayXSec(1);
                      Extract_Signal_Strength(gsmBuffer, signalStrength);
                    
                      signal_strength = atoi(signalStrength);
                    
                      if(signal_strength == 99) {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));
                          gsmState = 3;
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;
                      }                                     
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                           
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                    
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                 
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                           
                      }                     
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                           
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }             
                  }
                              
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:     
                  break;       
        };     
    }
}
 

jayanthd

Joined Jul 4, 2015
945
If the above code doesn't work but repeateadly keeps sending the AT commands then use this code

C:
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
  
  
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));       
        DelayXSec(2);       
        if((*gsmAttempt)++ == 3) {  
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;           
        }
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));   
    (*gsmAttempt) = 0;
    (*idx) = 0;
}

I had mentioned

C:
GSM_Send_Const_Command()
as

C:
Send_Const_AT_Cmd()
in previous posts.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I really do not understand how you understand these o_O
CSS:
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
s1, s2, s3, idx and so on ....:eek:

What do you have in your brain.
I just go blank when I see those things :oops:

I would need some time to figure out your code.

In the mean time is it possible to write the way I wrote. :(
 

jayanthd

Joined Jul 4, 2015
945
I have written the code in a similar way as you have written but just used one function to send Const AT command and check for a particular response. It is just simple. I wrote this code 4 years back and I was using a PIC with less RAM and I could not pass a buffer of size 300 bytes to the function and hence needed to use pointers to all buffers.

I will explain in detail how the

C:
GSM_Send_Const_Command()
works. Then it will be easier to understand how the code works.

If you want to send AT command AT\r and check for response \r\nOK\r\n then you would do like this

Code:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
or

Code:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 1);
The only difference between these 2 calls is the last paramenter 0 or 1.

1 means when function returns then gsmBuffer and gsmBufferIndex will be cleared.

0 means they are not cleared on function return and you can then use the gsmBuffer for testing for other atResponses like error or any other thing.

*s1, *s2, *s3... are pointers to particular arrays.

buffer1 is used as CopyConst2Ram() is used inside the above function because AT Commands and Responses are stored in ROM and these are passed to the above function abd hence inside the function we have to bring this ROM data to RAM for testing.

Inside the function you can change the value here

C:
if((*gsmAttempt)++ == 3) {

}
if 3 is changed to 5 then when you use the above function, it sends that AT command and checks for its response for 5 times and if it doesn't receive the required response then it will reset the GSM modem and then tries again sending the AT command till it gets the proper response.
 

jayanthd

Joined Jul 4, 2015
945
Use this code. It will work fine as you need.

C:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

#define ON   1
#define OFF  0

#define HIGH 1
#define LOW  0

#define TRUE 1
#define FALSE 0

// GSM module connections
sbit gsmReset at RD7_bit;
sbit gsmReset_Direction at TRISD7_bit;
// End of GSM module connections

sbit LED at RD0_bit;

char gsmBuffer[80];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 0;

sbit sendSmsFlag at myFlags.B0;

//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS=\"0000000000\"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
const char atCommandB[] = "AT+COPS?\r";
const char atCommandC[] = "AT+CPIN?\r";
const char atCommandD[] = "AT+CSQ?\r";
const char atCommandE[] = "ATH\r";

const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: \"SM\",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING\r\n\r\n";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
const char atResponseA[] = "+COPS: 0";
const char atResponseB[] = "+COPS: 0,2,47201";
const char atResponseC[] = "+COPS: 0,2,47202";
const char atResponseD[] = "+CSQ: ";
const char atResponseE[] = "AT\r\n\r\nOK\r\n";
const char atResponseF[] = "AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n";
const char atResponseG[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n";
const char atResponseH[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n";
const char atResponseI[] = "AT+CPIN?\r\n\r\nERROR\r\n";
const char atResponseJ[] = "AT+CPIN?\r\n\r\n+CPIN: READY\r\n";
const char atResponseK[] = "AT+CSQ\r\n\r\n+CSQ: ";
const char atResponseL[] = "AT+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";

const char msg1[] = "No Network ";
const char msg2[] = "No SIM     ";
const char msg3[] = "No Network And No SIM";
const char msg4[] = "Network and SIM Ready";
const char msg5[] = "Signal Strength = ";
const char msg6[] = "Not Registered On Network";
const char msg7[] = "SIM Removed";
const char msg8[] = "OK         ";
const char msg9[] = "ERROR      ";
const char msgA[] = "SIM Ready  ";
const char msgB[] = "Dhi Mobile ";
const char msgC[] = "Ooredoo    ";
const char msgD[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

const char sms1[] = "LED is ON";
const char sms2[] = "LED is OFF";
const char sms3[] = "Invalid Command Received";

//Function Prototypes
void DelayXSec(unsigned long int sec);
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);

void interrupt() {
   
    if((RCIE_bit) && (RCIF_bit)) {
        if(OERR_bit) {
              OERR_bit = 0;
              CREN_bit = 0;
              CREN_bit = 1;
        }

        gsmBuffer[gsmBufferIndex++] = UART1_Read();
        gsmBuffer[gsmBufferIndex] = '\0';

        RCIF_bit = 0;
    }    
}

char *CopyConst2Ram(char *dest, const char *src){
    char *d;
   
    asm clrwdt
    d = dest;
   
    for(;*dest++ = *src++;)asm clrwdt;

    return d;
}

void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}

void GSM_RESET() {
    asm clrwdt
    gsmReset = 1;    
    Delay_ms(20);
    gsmReset = 0;    
}

void Extract_Signal_Strength(char *s1, char *s2) {
    unsigned int i = 0;
   
    asm clrwdt
   
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
   
    asm clrwdt
   
    while(*s1 != ' ') {
           *s1++;
           asm clrwdt
    }

    *s1++;
   
    while(*s1 != ',') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';    
    asm clrwdt
}

void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;
   
    asm clrwdt
   
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }
   
    asm clrwdt
   
    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            asm clrwdt
    }

    *s2 = '\0';                        
    asm clrwdt
}

void Get_SMS_Index(char *s1, char *s2) {

    while(*s1 != 'C') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'M') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'T') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'I') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != ',') {
           *s1++;
           asm clrwdt
    }

    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}

void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
   
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));        
        DelayXSec(2);        
        if((*gsmAttempt)++ == 3) {
            //GSM_RESET();            
            //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;            
        }
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));    
    (*gsmAttempt) = 0;
    (*idx) = 0;
}

void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    asm clrwdt
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        DelayXSec(2);
       
        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;            
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }

    (*idx) = 0;

    Extract_SMS(s1, s6);
   
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    asm clrwdt
}

void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt
   
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        asm clrwdt
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);
       
        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);
       
        DelayXSec(6);
    }

    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    asm clrwdt
}

void main() {

    asm clrwdt
    //OPTION_REG = 0x87;
   
    CMCON = 0x07;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x80;
    TRISD = 0x00;
    TRISE = 0x00;
   
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
   
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
       
    UART1_Init(9600);
    Delay_ms(100);
   
    RCIF_bit = 0;
    RCIE_bit = 1;
   
    PEIE_bit = 1;
    GIE_bit = 1;    
           
    while(1) {

        asm clrwdt
       
        switch(gsmState) {
            case 0:
                 
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //If new SMS  received
                      DelayXSec(1);                                                  
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));
                      gsmState = 1;                            
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                      gsmState = 0;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }              
                  }                      
                 
                  gsmBufferIndex = 0;  
                  gsmAttempt = 0;
                  break;            
            case 1:
                 
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseJ, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //If new SMS  received
                      DelayXSec(1);                                                      
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                      gsmState = 2;                            
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));
                      gsmState = 1;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }              
                  }                                          
                     
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 2:
                 
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseF, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                     
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {   //If new SMS  received
                      DelayXSec(1);                          
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                      gsmState = 2;                            
                  }                      
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) { 
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));
                      gsmState = 3;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) { 
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));
                      gsmState = 3;
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }              
                  }                      
                     
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
           
            case 3:            
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                 
                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) {   //If new SMS  received
                      DelayXSec(1);
                      Extract_Signal_Strength(gsmBuffer, signalStrength);
                     
                      signal_strength = atoi(signalStrength);
                     
                      if(signal_strength == 99) {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));
                          gsmState = 3;
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;
                      }                                      
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {                            
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseO, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                     
                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {   //If new SMS  received
                          DelayXSec(1);                                                  
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));
                          gsmState = 0;                            
                      }                      
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {                            
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));
                          gsmState = 0;
                          break;
                      }              
                  }
                               
                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:      
                  break;        
        };      
    }
}
 

jayanthd

Joined Jul 4, 2015
945
With my one single function you have option to send any AT command and check for any AT response inside the function and come out of the function with either buffer and index cleared or not and if buffer is not cleared then you can check for the received response outside of the function for other responses and extract required data from the response and take dicisions.

My code does what you require. The response will be inside the gsmBuffer after issuing the particular AT Command and I am using strstr() to check which response is received for that AT Command in if()...else if()... conditions and based on that displaying required messages on LCD.
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Too many words..trying to get my head around this.

Why SMS Received. I am not using it.
And WDT is disabled.
 

jayanthd

Joined Jul 4, 2015
945
@dannyf

Thank you for pointing it out.

It should be

C:
char num[] = "18";
You can reduce the memory usage by reducing the size of gamBuffer[80] to gsmBuffer[40]

But also make changed here

C:
[LIST=1]
[*]if((*gsmAttempt)++ == 1) {
[*]           //GSM_RESET();            
[*]           //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
[*]           (*gsmAttempt) = 0;
[*]           (*idx) = 0;
[*]           break;           
[*]       }
[/LIST]
because if the attempt is > 2 and if response length is 30 bytes then gsmBuffer will overflow on 2nd attempt.
 

bwack

Joined Nov 15, 2011
113
I don't think it does .


CSS:
   if(Get_Response() == ERROR)              // AT+CPIN?
     Lcd_Out(3,9,Const2Ram(String,MSLD10)); // "Detecting!";
      Send2Modem(ATC4);
      Wait();
   if(Get_Response() == GSM_ERROR)   // +COPS: 0
       Lcd_Out(4,9,Const2Ram(String,MSLD11)); //"Registering!";

   if(Get_Response() == GSM_DhiMobile)   // +COPS: 0,2,47201
       Lcd_Out(4,9,Const2Ram(String,MSLD08)); //"DhiMobile";
   if(Get_Response() == GSM_Ooredoo)   // +COPS: 0,2,47202
       Lcd_Out(4,9,Const2Ram(String,MSLD09)); //"Ooredoo";
You see The lcd shows "Registering!" when I issue " AT+COP: 0?"
and it shows ""Detecting!" when I issue " AT+CPIN?"

So it parsing up to the " AT+COP: 0?".
I think the parsing stops at ","
You say you don't think it does, but you are leaving out the part of what I was refering to !

I was refering to a post above mine on line 25. It shouldn't be necessary for me to quote it but it is really hard to help when you keep leaving out code. This is the code you had there:

C:
while(1){
   while(Init_Modem_Flag){
     Modem_Init();
   }
   if(Get_Response() == GSM_OK)break; // OK this was line 25
I was stuck at "+COPS: 0" before :oops:
Now It is parsing to "+COPS: 0" fine and LCD shows that but it now it is not going beyond that...o_O
if this doesn't break out of this infinte while loop i don't know what. Is it gone in your new code or did you not leave it in? To me it looks like the switch statements were replaced with if sentenced and a break was forgotten to be removed. Because breaking out of the main (and i don't even know it is in main !) will stop the program. You asked why it is not going beyond that, I see a while loop that ends with this code.


and what does the modem send after COPS: 0 ? It sends "OK". ! and if you get OK, your GSM_OK will break the main loop. I can look at the statemachine but I think it is better to do it like @jayanthd did with a statemachine gsm_states and string matching techniques. :) If you still want to debug the large statemachine for say learning, then use the debugger in mikroC. Trust me on this you will spend some time learning the debugger but you will save tons of time after that.
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
I was able to parse "+COPS: 0" once I looked at the Realterm images. Before the CR+LF cannot be seen my earlier terminal programs. :confused:

Now I get it, ....I am getting "OK" and it is breaking the while(1). Silly me...Why did I not see that. :(

thanks @bwack

I spent 3 hrs on that last night. I will see to it again.

I like to use @jayanthd code but hardly understand it.

other point it his code is still way too big for the space I have left in my project.
It needs to be smaller.

My parsing is like 5 times smaller as far as the statistics are showing.
 

jayanthd

Joined Jul 4, 2015
945
Yes, you
And where the heck is o_O
char num[] = "18";
I just cannot find it...!!! :(

That was where atoi() and other method was mentioned in this thread not in the code..

Yes, you can safely remove those unused functions. That was my GSM library and hence included function for sending SMS.
 
Top