display SMS at GLCD, received from UART originally from GSM

General discussion on mikroC.
Post Reply
Author
Message
saharul
Posts: 489
Joined: 08 Jul 2010 08:11

display SMS at GLCD, received from UART originally from GSM

#1 Post by saharul » 06 Oct 2015 03:55

Hi, i use (PIC18F45K22, GSM, EASYPIC7)

I need to receive message from GSM via UART. I need to display this message at GLCD.

The problem were:
1. first i have to prepapare the empty string without character e.gchar *someText ( someText=" "))
2. when the received SMS message is short ( Less than 15 character the value display contains garbage character(unknown character).

How can i remove the garbage character.

Part of the codes that i have modified from the orinial "GSM SMS" example, as below;

Code: Select all

char *someText; 

void main:

---------------
 someText = "               ";
while (1)
---------

 Glcd_Fill(0x00);
 Glcd_Set_Font(font5x7, 5, 7, 32);       // Choose font, see __Lib_GLCDFonts.c in Uses folder
 Glcd_Write_Text(someText, 0, 0, 2); 

---------
}

void interrupt() {
------
------
switch (gsm_state) {
      case  0: {-----------
                   ----------
                    if (tmp == 'Z')                 
                    gsm_state = 40; i=0;               
                       ----------

case 40: {
                    someText[i] = tmp;   i ++;
                  if (i < 15) {  gsm_state = 40; }
                    if (i>=15){gsm_state=0;i=0;check=~check;}
                   if(tmp=='/'){    for (i = i; i < 15; i ++){
                   someText[i] = '0';}             // clear display
                   if (i >= 15) {gsm_state=0;i=0;check=~check; } }
                }; break;

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: display SMS at GLCD, received from UART originally from

#2 Post by Aleksandar.Mitrovic » 06 Oct 2015 13:43

HI Saharul,

When you try to debug your code and watch string someText, do you receive your message correct and after that you have garbage or your message is also displayed incorrect?
Did you try observe your string in watch window?

Best regards,
Aleksandar

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: display SMS at GLCD, received from UART originally from

#3 Post by saharul » 07 Oct 2015 02:18

Thanks Aleksandar,

lets say i send SMS " Zworks"

the display at the glcd display "works+++++++++++"
where +++++++++++++= garbage character(unknown character).

:D :D :D :D :D

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: display SMS at GLCD, received from UART originally from

#4 Post by Aleksandar.Mitrovic » 07 Oct 2015 15:05

Hi,

Did you try to clear your buffer before you write your message in to him and display message to the LCD?

Please try to clear someText before you start writing to it.

Best regards,
Aleksandar

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: display SMS at GLCD, received from UART originally from

#5 Post by saharul » 15 Oct 2015 07:36

Thank you,

when i clear it works fine, but when the message is longer (require 2 rows and more of GLCD display) it appears again at second or third row.

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: display SMS at GLCD, received from UART originally from

#6 Post by Aleksandar.Mitrovic » 20 Oct 2015 10:57

Hi saharul,

Can you please send me your project in small zipped form, so how I could be able to take a look at your code?

Kind regards,
Aleksandar

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: display SMS at GLCD, received from UART originally from

#7 Post by saharul » 22 Oct 2015 13:38

Thanks Alex,

Code: Select all

 #include <built_in.h>
   #define BOOL unsigned char
#define TRUE 1
#define FALSE 0
   sbit blink         at    RA3_bit;
sbit blink_Direction at TRISA3_bit;
  sbit check          at    RA1_bit;
sbit check_Direction at TRISA1_bit;
   int i=0;   int i1=0;
// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at LATB0_bit;
sbit GLCD_CS2 at LATB1_bit;
sbit GLCD_RS  at LATB2_bit;
sbit GLCD_RW  at LATB3_bit;
sbit GLCD_EN  at LATB4_bit;
sbit GLCD_RST at LATB5_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction  at TRISB2_bit;
sbit GLCD_RW_Direction  at TRISB3_bit;
sbit GLCD_EN_Direction  at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections


// Set of basic AT commands
const char atc0[] = "AT";                        // Every AT command starts with "AT"
const char atc1[] = "ATE0";                      // Disable command echo
const char atc2[] = "AT+CMGF=1";                 // TXT messages
      char atc3[] = "AT+CMGS=\"";                // sends SMS to desired number
const char atc4[] = "AT+CMGR=1";                 // Command for reading message from location 1 from inbox
const char atc5[] = "AT+CMGD=1,4";               // Erasing all messages from inbox
const char atc6[] = "AT+CMGL=\"ALL\"";           // Check status of received SMS
//

// Responses to parse
const GSM_OK                       = 0;
const GSM_Ready_To_Receive_Message = 1;
const GSM_ERROR                    = 2;
const GSM_UNREAD                   = 3;
//

sbit RTS at LATE0_bit;
sbit RTS_Direction at TRISE0_bit;



sbit GM862_ON_OFF at LATA2_bit;
sbit GM862_ON_OFF_Direction at TRISA2_bit;
//

// SMS Message string
char SMS_Message[300];
    char temp_txt[15];

    char txt1[100];
char txt2[21];
char txt3[21] ;
char txt4[21] ;

         int val=0;

// ADC data string
char ADC1_data[6];

// phone number string
//char phone_number[20]="+60122209912";   //ju
char phone_number[20]="+60124209364";
//char phone_number[20]="+60195762584";  //nazmi

// State machine control variables
char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1, rsp;
char set_stat = 0, clr_stat = 0;
char PORT_flag = 0;
char Digital_O = 0;
char gsm_number = 0;
char Unread_flag;
//
char status_req = 0; // Status request variable

// Send command or data to the Telit GM862 Module - (const)
void GM862_Send(const char *s)
{
// Send command or data string
   while(*s) {
    UART1_Write(*s++);
   }
// Terminatation by CR
   UART_Wr_Ptr(0x0D);
}

// Send command or data to the Telit GM862 Module - (RAM)
void GM862_Send_Ram(char *s1)   //
{
// Send command or data string
   while(*s1) {
    UART_Wr_Ptr(*s1++);
   }
// Terminatation by CR
   UART_Wr_Ptr(0x0D);
}

// Get GSM response, if there is any
short Get_response() {
    if (response_rcvd) {
      response_rcvd = 0;
      return responseID;
    }
    else
      return -1;
}

// Wait for GSM response (infinite loop)
void Wait_response(char rspns) {
char test = 1;

  while (test){
  test = Get_response();
  if ((test == rspns) || (test == GSM_ERROR))
    test = 0;
  else
    test = 1;
  }
}

// Compose Status SMS
unsigned ComposeMessage(char* Message);

// Send Status SMS
void Send_Msg(char* Msg){
  char atc[33];

  atc[0] = 0;                        // clear atc string
  strcat(atc, atc3);                 // atc3 command for sending messages
  strcat(atc, phone_number);         // add phone number
  strcat(atc, "\"");                 // complete AT command
  GM862_Send_Ram(atc);               // send AT command for SMS sending
  Wait_response(GSM_Ready_To_Receive_Message); // Wait for appropriate ready signal

  GM862_Send_Ram(Msg);               // Send message content
  UART_Wr_Ptr(0x1A);                 // Send CTRL + Z as end character
  UART_Wr_Ptr(0x0D);                 // Send CR
  Wait_response(GSM_OK);             // Wait OK as confirmation that the message was sent
}

// Send status SMS to the cell phone number defined by the atc3 const string
void Send_Status(){
 ComposeMessage(SMS_Message);
 Send_Msg(SMS_Message);
}

// 3sec pause
void Wait(){
   Delay_ms(3000);
}

// Main
void main(){
  PLLEN_bit = 1;
  ANSELA = 0;             // Set all ports as digital
  ANSELB = 0;
  ANSELC = 0;
  ANSELD = 0;
  ANSELE = 0;

  SLRCON = 0;             // Set output slew rate on all ports at standard rate

  ANSELA |= 0x01;         // Set A0 as Analog input
  //TRISA0_bit = 1;
 LATA=0;
 TRISA= 0x03;
  check=0;
            check_direction=0;


// Setup interrupts
  RC1IE_bit = 1;          // Enable Rx1 intterupts
  PEIE_bit = 1;           // Enable peripheral interrupts
  GIE_bit  = 1;           // Enable global interrupts
//
blink = 0;
  blink_Direction = 0;

  GM862_ON_OFF = 0;
  GM862_ON_OFF_Direction = 0;

//  Set RTS pin to zero (we will use only RX i TX)
  RTS_Direction  = 0;     // RTS pin
  RTS = 0;

// Turn on the GM862 module
  GM862_ON_OFF = 1;       // hardware reset
  Delay_ms(2500);         // hold it at least for two seconds
  GM862_ON_OFF = 0;
//

  UART1_Init(9600);
  //ADC_Init();
  Delay_ms(200);
   Glcd_Init();                                   // Initialize GLCD
  Glcd_Fill(0x00);
  Wait();                 // Wait a while till the GSM network is configured

  for (i1=0;i1<=100;i1++){txt1[i1]=' ';}

// Negotiate baud rate
  while(1) {
    GM862_Send(atc0);                 // Send "AT" string until GSM862 sets up its baud rade
    Delay_ms(100);                    // and gets it correctly
    if (Get_response() == GSM_OK)     // If GSM862 says "OK" on our baud rate we program can continue
      break;
  }

 GM862_Send(atc1);        // Disable command echo
 Wait_response(GSM_OK);

 GM862_Send(atc2);        // Set message type as TXT
 Wait_response(GSM_OK);

 while(1){
   GM862_Send(atc5);      // Delete all messages (if any)
   if (get_response() == GSM_OK) // If messages are deleted
     break; // break from while
   Delay_ms(500);
 }
 // blink as a sign that initialization is successfully completed

  Glcd_Fill(0x00);
       Glcd_Set_Font(font5x7, 5, 7, 32);
  // infinite loop
  while(1) {
    GM862_Send(atc6);        // Read status of the messages and read message it self
    Delay_ms(100);           // Wait until the message is read
           // Choose font, see __Lib_GLCDFonts.c in Uses folder

    while(1) {
      GM862_Send(atc0);      // Wait until the module is ready
      Delay_ms(50);
      if (Get_response() == GSM_OK)
        break;
    }   
    
    blink=~blink;

         if (val>20){   for (i1 = 0; i1 <=20; i1 ++)  {
         txt2[i]=txt1[i1];}}
         if (val>0&&val<=20){   for (i1 = 0; i1 <=val-1; i1 ++)  {
         txt2[i]=txt1[i1];}} ;
          if (val>40 ){ for (i1 = 21; i1 <=40; i1 ++)  {txt3[i1-20]=txt1[i1];} }
       if (val>20 && val <=40 ){ for (i1 = 21; i1 <=val-1; i1 ++)  {txt3[i1-20]=txt1[i];} }
           Glcd_Fill(0x00);
           Glcd_Write_Text(txt2, 3, 0, 2);
           Delay_ms(300);
             Glcd_Write_Text(txt3, 3, 1, 2);
            Delay_ms(300);
            for (i1 = 0; i1 <=21; i1 ++)  {txt2[i1]=' ';}
            for (i1 = 0; i1 <=21; i1 ++)  {txt3[i1]=' ';}
    if (Unread_flag){

      while(1) {
        GM862_Send(atc0);    // Wait until the module is ready
        Delay_ms(50);
        if (Get_response() == GSM_OK)
          break;
      }
      if (status_req){       // Send status SMS if it's been requested
        status_req = 0;
        Send_Status();
      }
      Unread_flag = 0;
    }

    while(1){
      GM862_Send(atc5);  // Delete all messages (if any)
      Delay_ms(50);
      if (get_response() == GSM_OK) // If messages are deleted
        break;           // break from while
      Delay_ms(50);
      if (Unread_flag){  // if we have received message in mean time
        Unread_flag = 0;
        break;           // break from while
      }
    }
    Wait();
  }}



/******************************************************************************/

// Compose Status SMS
unsigned ComposeMessage(char* Message){
  unsigned adc_value1, adc_value2;
  char temp_txt[15];
  char PORTB_temp,  PORTD_temp;

  RC1IE_bit = 0;                // Disable Rx1 intterupts

  Message[0] = '\0';

  // SMS header
  strcat(Message, "message sent:");
  strcat(Message, "\r\n");      // Add new line (CR + LF)
  //


  // Add the status of Digital Inputs to the SMS Message
  //for (i = 0; i < 8; i ++){
   // temp_txt[13 - i] = (PORTD_temp % 2) + 48;
   // PORTD_temp = PORTD_temp / 2;
  //}
  //temp_txt[14] = 0;
 // strcat(Message, temp_txt);
  //strcat(Message, "\r\n");      // Add new line (CR + LF)*/
  //



  // SMS footer
  strcat(Message, "End.");
  strcat(Message, "\r\n");      // Add new line (CR + LF)
  //

  RC1IE_bit = 1;                // Enable Rx1 intterupts

  return strlen(Message);
}

// state machine
// Reading the data from UART in the interuppt routine
void interrupt(){
char tmp;char val1='v'; int cnt=0;


  if (RCIF_bit == 1) {                           // Do we have uart rx interrupt request?
    tmp = UART_Rd_Ptr(); //UART1_Read();//
                           // Get received byte

// Process reception through state machine
// We are parsing only "OK" and "> " responses
    switch (gsm_state) {
      case  0: {
                response = -1;                   // Clear response
                if (tmp == 'O')                  // We have 'O', it could be "OK"
                  gsm_state = 1;                 // Expecting 'K'
                if (tmp == '>')                  // We have '>', it could be "> "
                  gsm_state = 10;                // Expecting ' '
                if (tmp == 'E')                  // We have 'E', it could be "> "
                  gsm_state = 30;                // Expecting 'R'
                if (tmp == 'Z')                  // We have 'E', it could be "> "
                  gsm_state = 40; i=0;               // Expecting 'R'
                if (tmp == 'U')                  // We have 'U', it could be "UNREAD"
                  gsm_state = 120;               // Expecting l
                break;
               }
      case  1: {
                if (tmp == 'K') {                // We have 'K' ->
                  response = GSM_OK;             // We have "OK" response
                  gsm_state = 20;                // Expecting CR+LF
                }
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 10: {
                if (tmp == ' ') {
                  response_rcvd = 1;             // We have "> " response
                  response = GSM_Ready_To_Receive_Message; // Set reception flag
                  responseID = response;         // Set response ID
                }
                gsm_state = 0;                   // Reset state machine
                break;
                }

      case 20: {
                if (tmp == 13)                   // We have 13, it could be CR+LF
                  gsm_state = 21;                // Expecting LF
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 21: {
                if (tmp == 10) {                 // We have LF, response is complete
                  response_rcvd = 1;             // Set reception flag
                  responseID = response;         // Set response ID
                }
                gsm_state = 0;                   // Reset state machine
                break;
               }

      case 30: {
                if (tmp == 'R')                  // We have 'R', it could be "ERROR"
                  gsm_state = 31;                // Expecting 'R'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 31: {
                if (tmp == 'R')                  // We have 'R', it could be "ERROR"
                  gsm_state = 32;                // Expecting 'O'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 32: {
                if (tmp == 'O')                  // We have 'O', it could be "ERROR"
                  gsm_state = 33;                // Expecting 'R'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 33: {
                if (tmp == 'R'){                 // We have 'R'
                  response_rcvd = 1;             // We have "ERROR" response
                  response = GSM_ERROR;          // Set reception flag
                  responseID = response;         // Set response ID
                }
                gsm_state = 0;                   // Reset state machine
                break;
                }

        case 40: { cnt=cnt+1;
                   txt1[i] = tmp;  val1=txt1[i]; i ++;  gsm_state=40;
                   if (tmp=='/'){for (i=i-1;i<=100;i++){txt1[i]=' ';} gsm_state=0;break;cnt=0; }
                   if (cnt>=100) {gsm_state=0;cnt=0;val=0;}

                }; break;


       case 120: {
                  if (tmp == 'N')
                    gsm_state = 121;
                  else
                    gsm_state = 0;
                 }; break;
       case 121: {
                  if (tmp == 'R')
                    gsm_state = 122;
                  else
                    gsm_state = 0;
                 }; break;
       case 122: {
                  if (tmp == 'E')
                    gsm_state = 123;
                  else
                    gsm_state = 0;
                 }; break;
       case 123: {
                  if (tmp == 'A')
                    gsm_state = 124;
                  else
                    gsm_state = 0;
                 }; break;
       case 124: {
                  if (tmp == 'D'){
                    response_rcvd = 1;             // We have "ERROR" response
                    response = GSM_UNREAD;         // Set reception flag
                    responseID = response;         // Set response ID
                    Unread_flag = 1;
                  }
                  gsm_state = 0;
                 }; break;
      default: {                                 // Unwanted character
                gsm_state = 0;                   // Reset state machine
               }
    }

 }

}

/******************************************************************************/
Attachments
haslina.rar
(439.18 KiB) Downloaded 158 times

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: display SMS at GLCD, received from UART originally from

#8 Post by saharul » 03 Nov 2015 05:35

Hi,

did you guys found the solution?

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: display SMS at GLCD, received from UART originally from

#9 Post by Aleksandar.Mitrovic » 03 Nov 2015 16:42

Hi,

Can you please tell me which example from this zipped file is the one that you are using?
There are couple of them in side.

Also, you can add a 0 to the string and end it like this.

Best regards,
Aleksandar

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: display SMS at GLCD, received from UART originally from

#10 Post by saharul » 04 Nov 2015 03:40

Thanks,


The name of the file is Haslina.
you can add a 0 to the string and end it like this.
. can give example how.. :D :D

Thanks :D :D

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: display SMS at GLCD, received from UART originally from

#11 Post by Aleksandar.Mitrovic » 04 Nov 2015 16:01

Hi,

For example like this:

Code: Select all

char txt1[] = "mikroElektronika";
txt1[5] = 0;
Glcd_Write_Text(txt1, 10, 2, 1);
On GLCD you will have "mikro" printed.

Best regards,
Aleksandar

Post Reply

Return to “mikroC General”