Menu

all about electronic and microcontrollers

Wednesday, July 23, 2014

GSM Click (Warning Action via SMS)

OK Folks, we will continue our experiments with this GSM module, and we will show how we can send an warning message. Obviously we will create the warning situation trough a button, but of course it can be changed with everything else for example a couple of movement sensors, etc.

Hardware setup:
After we spoke "the given situation" is set by a button set on RD7. The info led set on RB7 show the situation as follow:
  • After initialize, it flash for 2 seconds.
  • After the message is sent, it flashing ten times.
Some adjustments are needed on Easypic v7 motherboard to establish our connections. The adjustments are as follow:
  1. Turn on portB LEDs on SW3 (SW3.2).
  2. Place GSM click board into mikroBUS socket 1.
  3. Place power selection jumper (J5) into 3.3V position.


Video:

For hardware details, please study the EasyPic v7 and Telit GL865 datasheets.

Software:
The program is written in mikroC Pro for PIC (version v6.4.0).
Below is my software version:

warning action.c
/*
 * Project name:
     Warning Action via SMS.
 * Copyright:
     (c) Macovei Aureliu Raducu, 2014.
 * Revision History:
     1.1
 * Description:
     This is an Warning Action via SMS project who are able to send a 
     warning message for a given situation.
     In this experiment "the given situation" is one button set on RD7.
     The info led set on RB7 show the situation as follow:
         - After initialize, it flash for 2 seconds.
         - After the message is sent, it flashing ten times.
 * Test configuration:
     MCU:             PIC18F45k22
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf
     Ddev.board:       EasyPIC7
                      http://www.mikroe.com/easypic/
     Oscillator:      HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
     Ext. Modules:    GSM click
                      http://www.mikroe.com/click/gsm/
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     Adjust the number to which the SMS will be sent and recompile the project
     now have HEX file ready for programming.
*/
// Configure phone number on which the SMS warning will be sent
// Use international code + your phone number
char phoneNumber1[] = "your number1";    // This is the first number
char phoneNumber2[] = "your number2";    // This is the second number

#include "Telit_GL865.h"

// Pin definitions
sbit LD1 at LATB7_bit;                    // Set LED1 at port RB7
sbit LD1_Direction at TRISB7_bit;         // Set LED1 at port RB7

sbit T1 at RD7_bit;                       // Set Button at port RD7.
sbit T1_Direction at TRISD7_bit;          // Set Button at port RD7
// Pin definitions

void Delay_LD(){
  Delay_ms(100);                          // Led Delay 100ms
}

// Simple LED action indicating a button press
void Blink(){
  LD1 = 1;
  Delay_LD();
  LD1 = 0;
  Delay_LD();
  LD1 = 1;
  Delay_LD();
  LD1 = 0;
}

void check_status(){
 LD1 = 1;
 delay_ms(2000);           // Add 2s delay
 LD1 = 0;
}

void MCU_Init()
{
  ANSELA = 0;              // Set all ports as digital
  ANSELB = 0;
  ANSELC = 0;
  ANSELD = 0;
  ANSELE = 0;              // Set all ports as digital

  // GSM definitions
  PWRMON_Direction = 1;
  RST = 0;
  RST_Direction = 0;
  // GSM definitions

  // Button on RD7;
  T1_Direction = 1;             // Button pins are set as input
  // Button on RD7;

  // Led Indicator set on RB7
  LD1 = 0;                      // Turn OFF the LED
  LD1_Direction = 0;            // Set direction for LEDS
  // Led Indicator set on RB7

  UART1_Init(9600);       // Using default baud rate 9600
  Delay_ms(200);          // Wait a while till the GSM network is configured
}

// main function
void main() {
  MCU_Init();                   // Initialize MCU pins and modules
  if (GSM_Init() == 0)          // Initialize GSM module, if AT is received LED1 is ON
  check_status();               // Shown by LD1

  while(1){                     // Endless loop
  if (T1 == 1){                 // With Button1 we activate "the given situation"
    SendSMS(phoneNumber1);      // First things first, send the SMS
    delay_ms(3500);
    SendSMS(phoneNumber2);
    Blink();Blink();            // Blink ten times, after message sent
    Blink();Blink();
    Blink();
    }
  }
}

telite_GL865.c
#include "Telit_GL865.h"

void GSMcmd(char *ch)                 // GSM commands
{
 while(*ch)
 if(UART1_Tx_Idle() == 1)
 UART1_Write(*ch++);
 UART1_Write(0x0D);                  // Send CR - command terminate character
}

char GSM_Init(){
  char cmdTO = 0, readTO = 0;         // TimeOut counters
  char gsmRes[26]={0}, resCnt = 0;    // Response from GSM module
  while(PWRMON != 0){};               // Wait for POWER to be generated by the GSM module
  Delay_ms(3500);
  while(cmdTO++ < 5){                 // Check the communication by sending AT command for few times
    UART1_Read();
    GSMcmd("AT");                     // AT commamd.
    while(readTO++ < 10){
      Delay_us(300);                  // 1/9600 * 3 = 312.5us takse a UART HW buffer to fill up.
      while(UART1_Data_Ready()){      // Place the received bytes to buffer array(gsamRes[])
        readTO = 0;
        gsmRes[resCnt++] = UART1_Read();
        gsmRes[resCnt] = 0;
        if (resCnt == 25){
         resCnt = 0;
        }
      }
    }
    readTO = 0;
    if (strstr(gsmRes,"AT\r\r\nOK\r\n")){   // Check for occurrence of expected string
      GSMcmd("ATE1");                       // Few more GSM module configuration commands.
      GSMcmd("AT+CMGF=1");                  // Select Text Mode.
      GSMcmd("AT#GPIO=5,0,2");              // Set GPIO5 pin as RFTXMON OUTPUT
      return 0;
    }
  }
  return 1;
}

void SMSbegin(char *phoneNo){               // Sending an SMS requires three easy steps
                                            // 1. Begin with SMS command  +CMGS
  UART1_Write_Text("AT+CMGS=\"");           // AT+CMGS="+39xxxxxxxxxx"[CR]
  UART1_Write_Text(phoneNo);
  GSMcmd("\"");
}

void SMSwrite(char *text){
  UART1_Write_Text(text);                   // 2. After SMS command, SMS text is expected
}

void SMSsend(){
  GSMcmd("\x1A");     // 3. At the end CTRL + Z is sent as terminating character
}
// Function sends an SMS to predefined number with predefined text(warning)
void SendSMS(char *phone){
  SMSbegin(phone);
  Delay_ms(100);
  SMSwrite("WARNING!!\r\n");                            // This is our message line1
  SMSwrite("We have a situation here!\r\n");            // This is our message line2
  SMSwrite("(someone somehow has press the button)");   // This is our message line3
  SMSsend();
}

telite_GL865.h
// GSM definitions
sbit PWRMON           at PORTA.B2;
sbit PWRMON_Direction at TRISA2_bit;
sbit RST              at LATE1_bit;
sbit RST_Direction    at TRISE1_bit;
// GSM definitions

char GSM_Init();
void SendSMS(char *phone);

No comments:

Post a Comment

If you do not understand something, or if you make some aplication helped by this blog, let me know.