Help needed with GSM click project

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
Shahrizone
Posts: 4
Joined: 26 Jun 2019 09:25

Help needed with GSM click project

#1 Post by Shahrizone » 26 Jul 2019 03:44

Hi everyone,

I would like to seek an assistance with my project using GSM click...As you can see, i'm trying to send the data that I need using it but the example provided from Libstock (GSM Click) for Easypic 7 need to send the "Set RD2 RD3 RD4 Status?" first in order to obtain the data. Can anyone help me with the code as I need the data to be send to the GSM every 5 minutes without requesting it like from the example. Your help is really appreciate. Thank you very much :o


The modify code can be seen as below.

Code: Select all

/*
 * Project name:
    GSM click - SMS
 * Copyright:
     (c) MikroElektronika, 2011.
 * Revision History:
     20110516:
       - initial release(SZ);
     20120926:
       - revision for GSM click (JK);
 * Description:
     Program uses GSM module GL865 for sending and receiving SMS.
     User can control EasyPIC v7 development board with SMS, and receive status
     of one analog input, digital input port and digital output port.
 * Test configuration:
     MCU:             PIC18F45K22
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf
     dev.board:       EasyPIC7
                      http://www.mikroe.com/easypic/
     Oscillator:      HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
     Ext. Modules:    GSM click : ac:GSM_click
                      http://www.mikroe.com/click/gsm/
                      Telit GM865-QUAD
                      http://www.telit.com/en/products.php?p_id=3&p_ac=show&p=110
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     - Due different mobile operators some adjustments may be required, please consult Telit AT commands
       reference guide at http://www.telit.com/module/infopool/download.php?id=542
     - The SMS messages (which GM865 receives) should contain commands: "Set", "Clear" and follows "RD0" "RD1" ... and "Status?" (it is case sensitive and without quotes).
       In example, "Set RD1"  command turns ON the LED on RD1, "Clear RD4" turns OFF  LED on RD4 ... etc...
       You can send only the "Status?" inquiry via your SMS (or add it to the SMS containg relay commands) and then you'll receive an INFO about the state of 
       PORTD leds (8 digital outputs), PORTB LEDs (8 digital inputs) and 1 analog input. 
       An example of one control message is below:
       "Set RD0 RD4 RD6 Clear RD2 RD5 Status?"  (Note that there is no need for separaion chars (spaces in this case)).
       Valid message is also: "SetRD0RD4RD6ClearRD2RD5Status?"; just be careful about the case of letters.
       
     - Turn on portB and D LEDs on SW3 (SW3.2, SW3.4).
     - Use PORTB three state switch to pull up/down pins and set digital inputs. (RB0 is also connected to CTS pin on GM865)
     - Place GSM click board into mikroBUS socket 1.
     - Place power selection jumper (J5) into 3.3V position.
 */

// 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 RE0_bit;
sbit RTS_Direction at TRISE0_bit;

char Digital_INPUT            at PORTB;
char Digital_INPUT_Direction  at TRISB;
char Digital_OUTPUT           at LATD;
char Digital_OUTPUT_Direction at TRISD;

sbit GM862_ON_OFF at RA2_bit;
sbit GM862_ON_OFF_Direction at TRISA2_bit;
//

// SMS Message string
char SMS_Message[300];

// ADC data string
char ADC1_data[6];

// phone number string
char phone_number[] = "+6011********";
char enviromental_data[] = "TEMP=24deg Humidity =80%";
// 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) {
    UART_Wr_Ptr(*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(){
  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;
  
  Digital_INPUT_Direction = 0xFF;
  Digital_OUTPUT_Direction = 0;
  Digital_OUTPUT = 0;

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

  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);

  Wait();                 // Wait a while till the GSM network is configured

// 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
 Digital_OUTPUT = 0xFF;
 Delay_ms(500);
 Digital_OUTPUT = 0;

  // 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

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

    if (Unread_flag){
      if (PORT_flag){        // Turn ON/OFF port D LEDs if there were new commands
        PORT_flag = 0;
        Digital_Output = Digital_O;
      }

      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[30];
  char PORTB_temp, i, PORTD_temp;

  RC1IE_bit = 0;                // Disable Rx1 intterupts

  Message[0] = '\0';

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

strcat(Message, enviromental_data);
  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 i;
  if (RCIF_bit == 1) {                           // Do we have uart rx interrupt request?
    tmp = UART_Rd_Ptr();                         // 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 == 'S')                  // We have 'S', it could be "Status?" or "Set"
                  gsm_state = 40;                // Expecting 't'
                if (tmp == 'R')                  // We have 'R', it could be "RDx" or
                  gsm_state = 50;                // Expecting D
                if (tmp == 'C')                  // We have 'C', it could be "Clear"
                  gsm_state = 110;               // Expecting l
                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: {
                if (tmp == 't')                  // We have 't', it could be "Status?"
                  gsm_state = 41;                // Expecting 'a'
                else
                  if (tmp == 'e')                // We have 'e'. it could be "Set"
                    gsm_state = 100;
                  else
                    gsm_state = 0;               // Reset state machine
               }; break;
      case 41: {
                if (tmp == 'a')                  // We have 'a', it could be "Status?"
                  gsm_state = 42;                // Expecting 't'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 42: {
                if (tmp == 't')                  // We have 't', it could be "Status?"
                  gsm_state = 43;                // Expecting 'u'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }

       case 43: {
                if (tmp == 'u')                  // We have 'u', it could be "Status?"
                  gsm_state = 44;                // Expecting 's'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }

       case 44: {
                if (tmp == 's')                  // We have 's', it could be "Status?"
                  gsm_state = 45;                // Expecting '?'
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }

       case 45: {
                if (tmp == '?'){                 // We have '?'
                  status_req = 1;                // Status has been requested!
                }
                gsm_state = 0;                   // Reset state machine
                break;
                }

       case 50: {
                if (tmp == 'D')                  // We have 'D', it could be "RDx"
                  gsm_state = 51;                // Expecting number
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
       case 51: {
                if (tmp >= '0' && tmp <= '7'){   // We have pin number, it could be "RDx"
                  if (set_stat)
                    Digital_O |= 1 << (tmp - '0');
                  if (clr_Stat)
                    Digital_O &= (0xFF ^(1 << (tmp - '0')));
                }
                  PORT_flag = 1;
                  gsm_state = 0;                 // Reset state machine
               }; break;

       case 100: {
                   if (tmp == 't'){              // We have 't', we received set command
                     set_stat = 1;               // Set following bits
                     clr_stat = 0;               // Do not clear following bits
                   }
                   gsm_state = 0;
                 }; break;
       case 110: {
                   if (tmp == 'l'){              // We have 'l', it could be Clear
                     gsm_state = 111;
                   }
                   else
                     gsm_state = 0;
                 }; break;
       case 111: {
                   if (tmp == 'e'){              // We have 'e', it could be Clear
                     gsm_state = 112;
                   }
                   else
                     gsm_state = 0;
                 }; break;
       case 112: {
                   if (tmp == 'a'){              // We have 'a', it could be Clear
                     gsm_state = 113;
                   }
                   else
                     gsm_state = 0;
                 }; break;
       case 113: {
                   if (tmp == 'r'){              // We have 'r', we have received Clear
                     clr_stat = 1;               // Clear following bits
                     set_stat = 0;               // Do not set following bits
                   }
                   gsm_state = 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
               }
    }
    // parse phone number on which we should reply



 }
}

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


Post Reply

Return to “User Projects”