0% found this document useful (0 votes)
101 views5 pages

GSM Code

This document defines constants and functions for sending and receiving SMS messages using a GL865 GSM module connected via UART. It initializes the UART interfaces and GSM module, sets the message format to text, and defines an interrupt service routine to handle received data on UART5 connected to the GSM module. The main function demonstrates sending an SMS message and entering a loop to wait for responses on the serial interface.

Uploaded by

yateesh0988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views5 pages

GSM Code

This document defines constants and functions for sending and receiving SMS messages using a GL865 GSM module connected via UART. It initializes the UART interfaces and GSM module, sets the message format to text, and defines an interrupt service routine to handle received data on UART5 connected to the GSM module. The main function demonstrates sending an SMS message and entering a loop to wait for responses on the serial interface.

Uploaded by

yateesh0988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#define BUFFER_SIZE 500

// Buffer fr empfangenen GPS String

// expected Messages *********************************************************


// '\0' important for strstr, otherwise it won^t work
const char GSM_OK []
= {'O','K','\0'};
// Check for modem
response message OK CR LF
const char GSM_CONNECT []
= {'C','O','N','N','E','C','T','\0'};
// Check for modem response message CONNECT
const char GSM_ERROR []
= {'E','R','R','O','R','\0'};
// Che
ck for modem response message CONNECT
const char GSM_NOCARRIER [] = {'N','O',' ','C','A','R','R','I','E','R','\0'};
// Check for modem response message NO CARRIER
const char SIM_READY[]
= {'#','Q','S','S',':',' ','3','\0'}; // Check for
modem response message #QSS: 3
const char GSM_Ready_To_Receive_Message [] = {'>',' ','\0'}; // We have "> "
response
// 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 in
box
const char atc6[] = "AT+CMGL=\"ALL\"";
// Check status of received SMS
const char atc10[]= "AT+CGMM";
// check Hardware GSM Module (
Page 83 AT Commands Reference )
const char atc11[]= "AT+CIMI";
// Request International Mobile
Subscriber Identify (IMSI) ( Page 85 AT Commands Reference )
const char atc12[]= "AT+CGSN";
// Request Product Serial Numbe
r Identification (IMEI) Page 84 AT Commands Reference
const char atc13[]= "AT&V";
// Display Current Base Configu
ration And Profile Page 53 AT Commands Reference
const char atc14[]= "AT&V2";
// Display Last Connection Stat
istics Page 55 AT Commands Reference
const char atc15[]= "AT#QSS?";
// Query SIM Status
const char atc16[]= "AT#QSS=2";
// Query SIM Status #2 == SIM I
NSERTED and PIN UNLOCKED(Page 310)
const char atc17[]= "AT+CNMI=1,1,0,0,0";
// +CNMI - New Message Indicati
ons To Terminal Equipment Page 213 om AT Commands reference guide
const char atc20[]= "AT#FTPPUT=\"";
// create FTP File
// ** phone numbers ************************************************
char phone_number[20] = "+41xxxxxxxxx";
// SMS Message string
char SMS_Message[160] = "Hello World SMS" ;
sbit
sbit
sbit
sbit

GL865_ON_OFF at RC3_bit;
//
RTS == Hardware Handshake
GL865_ON_OFF_Direction at TRISC3_bit;
RTS at RC4_bit;
RTS_Direction at TRISC4_bit;

unsigned char uart_rd;

unsigned char buffer[BUFFER_SIZE];


int write;

// buffer

// - UART 2 == Terminal zu PC
// void UART2interrupt() iv IVT_UART_2 ilevel 6 ics ICS_AUTO {
void UART2interrupt() iv IVT_UART_2 ilevel 7 ics ICS_AUTO{
uart_rd = UART2_Read();
// read the received data
UART5_Write(uart_rd);
// and send data via UART
U2RXIF_bit = 0;
// Interrupt register flag zurcksetzen auf 0
}
// - UART 5 == Modem
//void UART5interrupt() iv IVT_UART_5 ilevel 6 ics ICS_AUTO {
void UART5interrupt() iv IVT_UART_5 ilevel 7 ics ICS_AUTO{
uart_rd = UART5_Read();
// read the received data
buffer[write] = uart_rd;
write = write + 1;
// Buffer index
UART2_Write(uart_rd);
if (write == (BUFFER_SIZE-1))
write = 0;
U5RXIF_bit = 0;
// Interrupt register
}

void InitUart2Interrupt();
void InitUart5Interrupt();
int search_in_string(char*);
int wait_response(char*);
void send_to_GL865(char*);
void send_Msg(char*);
// funcion for sending Text messages
void main(){
int i;
AD1PCFG = 0xFFFF;
JTAGEN_BIT = 0;
TRISA = 0XFFFF;
TRISB = 0XFFFF;
TRISC = 0X0000;

// Configure AN pins as digital I/O

UART2_Init(115200);
Delay_ms(100);
UART5_Init(115200);
Delay_ms(100);

// Initialize UART module at 9600 bps


// Wait for UART module to stabilize
// Initialize UART module at 9600 bps

InitUart2Interrupt();
InitUart5Interrupt();
EnableInterrupts();

// Port A ist input


// PORT B ist input
// PORT C ist output Relais 1 bis 4

// Enable interruts as previously set

UART2_Write_Text("UART started succesfully");


UART2_Write(13);
UART2_Write(10);
// Set RTS pin to zero (we will use only RX i TX) -- Hardware Handshake
RTS_Direction = 0;
// RTS pin

RTS = 0;
// erase buffer
for(i=0;i<BUFFER_SIZE;i++){
buffer[i]='0';
}
write = 0;
// Port: RC3 == Reset des Modems // Turn on the GM862 module
GL865_ON_OFF = 1;
// hardware reset
Delay_ms(1500);
// hold it at least for two seconds
GL865_ON_OFF = 0;
Delay_ms(1500);
// hold it at least for two seconds

/* initialize Modem GL865 */


send_to_GL865(atc0);
// send "AT" string until gsm sets up its baud rade
wait_response(GSM_OK);
/* INIT SIM CARD */
// send_to_GL865(atc1);
// wait_response(GSM_OK);

// Disable command echo

send_to_GL865(atc16);
// Query SIM Status #2 == SIM INSERTED and PIN UNLO
CKED(Page 310)
wait_response(SIM_READY);
/* Set Message Receive Mode */
send_to_GL865(atc17);
wait_response(GSM_OK);
Send_Msg(SMS_Message); // send message "hello World"
while (1) {

// Endless loop wait fo interrupt to occu

rs
WDTCONSET = 0x01;

// Watchdog service

}
}
void InitUart5Interrupt(){
U5IP0_bit = 1;
U5IP1_bit = 1;
U5IP2_bit = 1;
WDTCONSET = 0x01;
U5RXIE_bit = 1;

// Set UART5 interrupt


// Set interrupt priorities
// Set UART5 interrupt to level 7
// Set UART Receive Interrupt Enable/off

}
void InitUart2Interrupt(){
U2IP0_bit = 1;
U2IP1_bit = 1;
U2IP2_bit = 1;
WDTCONSET = 0x01;
U2RXIE_bit = 1;
}

// Set UART2 interrupt


// Set interrupt priorities
// Set UART2 interrupt to level 7
// Set UART Receive Interrupt Enable/off

int wait_response(char* Msg){


int res;
res = 0;
while(res == 0){
Delay_ms(500);
UART2_Write_Text(".");
res = search_in_string(Msg);
if(res == -1){ // "ERROR"
return -1;
break;
}
if(res == 1){
return 1;
break;
}
}
delay_ms(1000);
}
int search_in_string(char* Msg){ /* search for Msg in buffer[BUFFER] and retu
rn 1 for found Message or 0 for not */
char *ptr = 0;
int i=0;

// address operator &, * inhalt operator

if(strstr(buffer,GSM_ERROR)) // found error


return -1;
ptr = strstr(buffer,Msg);
if(ptr > 0){
write = 0;
// when found the Message, set Buffer index back
to 0; new messages will be stored in the beginning of BUFFER string
for(i=0;i<BUFFER_SIZE;i++){
buffer[i]='0';
}
return 1; // found message
}
else
return 0; // found nothing
}
void send_to_GL865(char *s1)
{
while(*s1) {
UART5_Write(*s1++);
}
UART5_Write(0x0D);
UART5_Write(0x0A);
WDTCONSET = 0x01;
}
void Send_Msg(char* Msg){
char atc[50];

//Send string to Modem plus a CR

// Watchdog service

send_to_GL865(atc2);
// Set message type as TXT
Wait_response(GSM_OK);
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 -- ein " am ende des
Kommandos
send_to_GL865(atc);
// send AT command for SMS sending
Wait_response(GSM_Ready_To_Receive_Message); // wait for ">" signal
send_to_GL865(Msg);
UART5_Write(26);
Wait_response("+CMGS:");
WDTCONSET = 0x01;
// Watchdog service
}

You might also like