interfacing SIM800 and read sms

Post your requests and ideas on the future development of mikroC PRO for PIC.
Post Reply
Author
Message
pravin727@gmail.com
Posts: 18
Joined: 30 Jan 2017 12:12

interfacing SIM800 and read sms

#1 Post by pravin727@gmail.com » 02 Apr 2017 20:36

Hello,

I AM able to receive sms and read also. I can read only message ending with '$'. if message received with out '$' then i am not able to read it and am not able to read next sms with my logic also. Cos i am reading message on index=1, so when ever message come's without '$' it occupies index=1 and i am not able to read my next message.
Please give me your important suggestions. I have to submit this project in college in next 2-3 days.

code:

unsigned cnt, cnt1;
int end_flag=1,i = 0,delete_flag=0, flag = 0;

char index, rxchar ; // Variable for storing the data from UART and array counter
unsigned char output[110]; // array to store the received charaters
unsigned short j;
int k=0;
unsigned char buffer1[13];
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

void interrupt()
{

if (PIR1.RCIF)
{ // test the interrupt for uart rx
rxchar = Uart1_Read(); //
output = rxchar;
// ******************************************************
// Only select one of the following statements
if (rxchar == 36) { // select this if looking for a terminating character
flag = 1;
i=0;
}

if (rxchar == 'O') { // select this if looking for 'O'
flag = 2;

}
if (flag ==2 && rxchar=='K') { // select this if looking for a 'K'
flag = 3;

}
if ( rxchar=='S') { // select this if looking for 's'
flag = 4;
}
if (flag ==4 && rxchar=='M') { // select this if looking for 'm'
flag = 5;
i=0;
}
i++;
PIR1.RCIF = 0; // clear RCIF
} // end if (PIR1.RCIF)

}
int sms_read()
{

int l=0,flag1=0,count=0,j=0,k=10,mob=0;
// Lcd_Out(2,1,"SMS");
end_flag=0;
// delay_ms(500) ;

// delay_ms(50);
// UART1_Write_Text("AT+CMGR=1\n\r");
// delay_ms(50);
// UART1_Write_Text(output);
memset(buffer1,'\0',strlen(buffer1));
while(output[l]!='$')
{
//delay_ms(400) ;
// Lcd_Cmd(_LCD_CLEAR); // Clear display
// Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,output);
//UART1_Write_Text(output);
delay_ms(10);
if(output[l]=='"')
{
count++ ;
}
// UART1_Write(count+48);
// UART1_Write(output);
if( output[l]=='+' && output[l+1]=='9')
{
mob=1;
}

if( mob==1) //mobile number
{
// UART1_Write(output[i+1]);
buffer1[j]=output[l];
delay_ms(10);
j++;
if(j==13)
mob=0;
}

if(count==1 )
{
if(output[l+4]=='R') //check SMS readed or not
{
// UART1_Write(output[i+4]);
Lcd_Out(1,1,"SMS 1Received");
delay_ms(10);
}
}
if(output[l]=='F' && output[l+1]=='1' )
{
// UART1_Write_Text("FanOn");
Lcd_Out(2,1,"F1");
// delay_ms(10);
}
if(output[l]=='F' && output[l+1]=='0')
{
// UART1_Write_Text("FanOff");
Lcd_Out(2,1,"F0");
// delay_ms(10);
}
if(output[l]=='L' && output[l+1]=='0')
{
// UART1_Write_Text("LightOff");
Lcd_Out(2,5,"L0");
//delay_ms(10);
}
if(output[l]=='L' && output[l+1]=='1' )
{
// UART1_Write_Text("LightOn");
Lcd_Out(2,5,"L1");
// delay_ms(10);
}
if(output[l]=='T' && output[l+1]=='0')
{
// UART1_Write_Text("TVOff");
Lcd_Out(2,10,"T0");
// delay_ms(10);
}
if(output[l]=='T' && output[l+1]=='1' )
{
// UART1_Write_Text("TVOn");
Lcd_Out(2,10,"T1");
//delay_ms(10);
}
if(j==13)
{
k=strcmp(buffer1,"+918237319741") ;
Lcd_Out(1,1,buffer1);
//delay_ms(10);
j=0;

}
if(k==0)
{
Lcd_Out(1,1,"Mobile Valid");
// delay_ms(10);
}

// delay_ms(50);
//UART1_Write_Text(output);

//UART1_Write_Text(buffer1);
//
l++ ;
}


//delay_ms(1000);
// delay_ms(1000);
delay_ms(100);

Lcd_Out(1,1,buffer1);
memset(output,'\0',strlen(output));
i=0;

UART1_Write_Text("AT+CMGD=1\n\r");
end_flag=1;
return 0;
}



void main()
{ int temp=0;
lcd_init();
delay_ms(400) ;
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Hello");

UART1_Init(9600);
Delay_ms(500); // Wait for UART module to stabilize
PIE1.RCIE = 1; //enable USART receiver interrupt bit
INTCON.PEIE = 1; //enable peripheral interrupt bit
INTCON.GIE = 1; //enable global interrupt bit
// UART1_Write_Text("AT+CNMI=1,2,0,0,0\n\r");
delay_ms(200) ;
UART1_Write_Text("ATE0\n\r");
delay_ms(200) ;
UART1_Write_Text("AT+CMGF=1\n\r");
delay_ms(200) ;
UART1_Write_Text("AT+CMGD=1\n\r");
delay_ms(500) ;
memset(output,'\0',strlen(output));
while (1)
{
if(flag==3)
{
Lcd_Out(1,8,"OK");
// memset(output,'\0',strlen(output));
// i=0;
//
}

if(flag==5)
{
Lcd_Out(1,1,"SMS Received");
memset(output,'\0',strlen(output));
UART1_Write_Text("AT+CMGR=1\n\r");
}

if (flag ==1 && end_flag==1 )
{
temp=sms_read();
flag=0;
} // end if (flag)
}
}

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: interfacing SIM800 and read sms

#2 Post by dusan.poluga » 05 Apr 2017 19:42

Hi,

Well a stop condition is needed so your micro controller knows when to stop reading the UART.
You are doing it right. Maybe you can add 2 white spaces at the end of your SMS and check in your code for 2 white spaces instead.

Regards,
Dusan Poluga.

Post Reply

Return to “mikroC PRO for PIC Wish List”