GSM SIM800C sms receive in symbolic form

General discussion on Libstock website & codes posted on this website.
Post Reply
Author
Message
Ulfat
Posts: 8
Joined: 26 Dec 2017 13:04

GSM SIM800C sms receive in symbolic form

#1 Post by Ulfat » 17 Feb 2019 08:31

Hi All,

I have written the following code to receive sms using GSM SIM800c and Mikroc Pro, when message received, message is shown in the form of symbols, if anyone help me as I want to receive specific text like "ON" or "OFF" and also want to display on LCD 16x2.

I appreciate for your help

Code: Select all

sbit Lcd_Rs at RC4_bit;
sbit lcd_en at Rc5_bit;
sbit lcd_d4 at rc0_bit;
sbit lcd_d5 at rc1_bit;
sbit lcd_d6 at rc2_bit;
sbit lcd_d7 at rc3_bit;

sbit Lcd_Rs_direction at TRISC4_bit;
sbit Lcd_EN_direction at TRISC5_BIT;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;

char text;
char data[80];
            // command string search result
int i = 0;

void main() {
     //  TRISB = 0;
      // portb.f0=0;
      // portb.f1=0;
    TRISD = 0;
 TRISC = 0;
 PORTC=0 ;
 LCD_Init();
 Lcd_Cmd(_LCD_CLEAR);
              

  UART1_Init(9600);               // Initialize UART module at 9600 bps
                   // Wait for UART module to stabilize
  Delay_ms(2000);
  UART1_Write_Text("ATE0\r\n");   
  Delay_ms(1000);
  UART1_Write_Text("AT\r\n");       
  Delay_ms(1000);
  UART1_Write_Text("AT+CMGF = 1\r\n");  
  Delay_ms(1000);
  UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n"); 
  Delay_ms(1000);


while(1)
 {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
    for (i= 0; i < 80; i++)   {
     text = UART1_Read();     // read the received data,
      data[i]  = text;
         Lcd_Out(1,1,data[i]);  //display on LCD
      }
        }
    }  
    
 
    
    }

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: GSM SIM800C sms receive in symbolic form

#2 Post by stefan.filipovic » 26 Feb 2019 12:07

Hi,

For receiving messages with this module, the best way is to use a UART RX interrupt, and compare character per character to detect the expecting message.
For example, If you send the AT+CMGL="ALL" command, you will get a response in the below format:
+CMGL: 1,"REC READ","+85291234567",,"07/05/01,08:00:15+32",145,37
It is easy to list SMS text messages.
So, you need to ignore all these characters +CMGL: 1,"REC READ","+85291234567",,"07/05/01,08:00:15+32",145,37 and store the message "It is easy to list SMS text messages." in some string variable.
After that, you can display it on the LCD, but of course, with the length limit of 16 characters per row.

Please find an example for GSM with PIC18F452 in the attachment.
On top of the main file, there is a note with instructions on how to test it properly.
You can modify this example for messages "ON" and "OFF".

Kind regards,
Attachments
PIC18F452_GSM_LCD.ZIP
(112.02 KiB) Downloaded 235 times
Stefan Filipović

Ulfat
Posts: 8
Joined: 26 Dec 2017 13:04

Re: GSM SIM800C sms receive in symbolic form

#3 Post by Ulfat » 14 Apr 2019 10:26

Hi Stefan,

Thanks for you reply!

Is there any simple code to be fit for project starting, like if I send sms any one character eg: "t" from phone then I would be able to receive it and display on LCD
Microcontroller is :PIC18f452
GSM : SIM800c

Thanks in advance
Ulfat

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: GSM SIM800C sms receive in symbolic form

#4 Post by stefan.filipovic » 30 Apr 2019 13:48

Hi Ulfat,

I'm afraid there is no example you are looking for, but for your needs, you can try to modify the project I've sent you, specifically the switch statement of the UART RX interrupt routine.

Kind regards,
Stefan Filipović

Haven
Posts: 40
Joined: 18 Mar 2017 21:20
Location: South Africa
Contact:

Re: GSM SIM800C sms receive in symbolic form

#5 Post by Haven » 14 Dec 2020 16:47

Hi Guys,

I found some of the code on the internet and then discovered some of it in this forum posting, so I figure I'll ask here.

Basically, the code below does the following

1 - Start Up,
2 - Send test sms
3 - Check for "Button Pressed" event and send SMS
4 - loop through ReadSmsTwo to check for incoming SMS
5 - goto 3

So the problems with the code below are as follows

1 - The incoming SMS is on the SIM card or is delivered (verified), but for some reason, it's never read or the PIC does not read the text in the SMS - see function: void ReadSmsTwo(). void ReadSmsTwo(), should read the incoming SMS which has either ON or OFF as the value or content in the SMS. A NULL as a text word is sent as a reply if nothing is read from the SMS. If the word "ON" is read, the reply to the SMS should be "On" to verify it was recieved. While many SMSes with the word ON are sent, all that is ever sent in reply is the word NULL - by design and the diagnostic result of the SMS not being read. This means that the ReadSmsTwo function does not seem to be reading the incoming SMS out of the receive buffer and converting. This does not seem to make sense as by syntax it's correct . . . at least from my perspective.

2 - While the code "returns' to the main function, it only ever detects that the "Button Pressed" or PIN PORTB.BO pulled to ground before it calls the ReadSmsTwo() function, and seldom or never after when it returns which makes me wonder if something changes on the port settings.

The code sample was found here https://www.electro-tech-online.com/thr ... 87.153661/, and was allegedly working on a PIC16F887 which from what I can see should be no problem for the PIC16F1847 to run the same code . . . I am open to correction. I would appreciate any assistance because I am either not seeing what I am doing wrong or there is something else not working here.

Code: Select all


sbit TestSms at RB0_bit;

void SetupDevice();
void SendSms();
void ReadSms();
void ReadSmsTwo();
void DeleteSMS(int Command);
void debounce();

unsigned char test,got_something;
char *TST = "Test SMS";
char *MSG = "Button Pressed";
char *SMS;
char *result;
int i=0;
char incchar;
char incoming[80];
bit RecFlag;

void SetupDevice(){

     test = 1;

     // Configure Analogue Ports
     ANSELA = 0;
     ANSELB = 0;
     
     // Confgiure Ports and TRIS Registers
     TRISB = 0b00000001;                   // SendSms Input Button
     
     // Configure Uart
     UART1_Init(9600);               // Initialize UART module at 9600 bps
     Delay_ms(100);                  // Wait for UART module to stabilize
}

void SendSms(char Action){
     /*
     0 - External input
     1 - Button Press
     2 - Mirror Message
     3 - Null
     */
     
     UART1_Write_Text("AT+CMGF=1\r\n");                 // Set the GSM module into text mode
     Delay_ms(1000);

     UART1_Write_Text("AT+CMGS=\"[CELL NUMBER]\"\r\n");
     Delay_ms(1000);
     
     if(test ==1){
             UART1_Write_Text(TST);
             Delay_ms(1000);
             
             test = 0;
             
             UART1_Write(26);
             Delay_ms(1000);
             
             return;
     }
     
     if(Action == 1){

             UART1_Write_Text(MSG);
             Delay_ms(1000);

             UART1_Write(26);
             Delay_ms(1000);

             return;
     }
     
     if(Action == 2){

             UART1_Write_Text(SMS);
             Delay_ms(1000);

             UART1_Write(26);
             Delay_ms(1000);

             RecFlag = 0;

             return;
     }
     
     if(Action == 3){

             UART1_Write_Text("NULL");
             Delay_ms(1000);

             UART1_Write(26);
             Delay_ms(1000);

             RecFlag=0;

             return;
     }
}

void ReadSms(){

     int count =0,comp=1;
     got_something = 0;

     UART1_Write_Text("ATE0\r\n");                      // Echo OFF
     Delay_ms(1000);

     UART1_Write_Text("AT+CMGF = 1\r\n");             // Set to text mode
     Delay_ms(1000);
     
     UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n"); // setup receiving (now important at this stage)
     Delay_ms(1000);

                 if (UART1_Data_Ready() == 1) {          // if data is received
                 UART1_Read_Text(SMS, "OK", 254);          // reads text until 'OK' is found
                 }
return;
}

void ReadSmsTwo(){
  
  UART1_Write_Text("ATE0\r\n");   // AT command for Echo OFF
  Delay_ms(1000);
  
  UART1_Write_Text("AT\r\n");       // test GSM Modem
  Delay_ms(1000);

  UART1_Write_Text("AT+CMGF = 1\r\n");  //convert Sim800 module to text mode
  Delay_ms(1000);
  
  UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n"); // setup receiving (now important at this stage)
  Delay_ms(1000);

  if (UART1_Data_Ready()==1) {              // If data is received,
       for (i= 0; i < 80; i++)   {
             
             incchar = UART1_Read();       // read the received data,
             incoming[i]  = incchar;
      }
  }
  
  if (result = strstr (incoming, "ON"));          // search for string "ON" in incoming and save it in result
    if (memcmp("ON", result, 2) == 0)

               {
                         SMS = "On";
                         RecFlag =1;
               }
  if (result = strstr (incoming, "OFF"));          // search for string "OFF" in incoming and save it in result
    if (memcmp("OFF", result, 3) == 0)
             {
                         SMS = "Off";
                         RecFlag =1;
              }
}

void DeleteSMS(int Command){

     switch(Command){
     case 1:break;
     case 2:break;
     case 3:break;
     case 4: Uart1_Write_Text("AT+CMGD=0,4\r");Delay_ms(500);break;
     default:break;
     }
}

void debounce(){
  Delay_ms(300);
 }

void main() {

     Delay_ms(500);         // Gives modem time to power up

     SetupDevice();        // Device peripherals

     SendSms(0);
     
     DeleteSMS(4);

     while(1){

              if(!PORTB.B0){
                
                debounce();
                
                SendSms(1);
                
                Delay_ms(3000);
              }
              
              // Continual Test for incoming SMSes
              ReadSmsTwo();
              
              if(RecFlag ==1){
              SendSms(2);
              }
              
              else{SendSms(3);}
              
     }
Michael A Havenga
HavenTechnik | SP
WEB: http://www.haventechnik.com
twitter: @HavenTechnik

Post Reply

Return to “Libstock Discussion”