Showing posts with label GSM Interfacing with Microcontroller. Show all posts
Showing posts with label GSM Interfacing with Microcontroller. Show all posts

Receive SMS and turn on and off the devices


void main ()
{

clear();
lcdinit();
lcdcmd(0x80);
lcddata('K');

delay(10);
init();

SMSString("AT\r"); // AT commands to initialize gsm modem
delay(50);

SMSString( "ATe0\r"); // turn off echo
delay(50);

SMSString( "AT&W\r"); // save settings
delay(50);

SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay(50);
SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
delay(10);

//New msg indication.
// +CMTI: "SM",3
while(1)
{
clear();
IE=0X90;   // Enable serial interrupt
delay(10);
read_notification(msg1);
IE=0x00;
}
}


void read_notification(unsigned char *msg)
{
unsigned char array[15],i,index_no;
i=0;
do
{
       msg++;
        i++;
}while(*msg!='+' && i!=15); // to check for first '+'.
i=0;

do
{
     array[i]=*msg++;
     i++;
}while(*msg!='\n' && i!=15);      // Check for new line.

if(strncmp(array, "+CMTI", 5) == 0)
{
        array[14] = '\0';
        IE=0x00;
        lcdcmd(0x80);
lcdstr("NEW MSG");// Just for indication.
        delay(50);
        SMSString("AT+CMGR=");
        tx0(array[12]);
        SMSString("\r");
        IE=0x90;
        delay(80);
        read_text(msg1,rec_no,time_date);
        delay(100);
}
return;
}

How to send SMS using AT89c51

#include <REGX51.H>
#include <AT89X51.H>

unsigned char *command_AT = "AT\r";
unsigned char *command_CMGF = "AT+CMGF=1\r";

unsigned char *command_CMGS = "AT+CMGS=\"+910123456789\"\r";
unsigned char *message = "Hello Dear";
unsigned char CTRLZ = 0x1A;

void puts(unsigned char* ptr);
void putc(unsigned char chr);
void sendsms(void);
void initialize();

main()
{
initialize();
sendsms();
while(1);
}

void initialize()
{
SCON = 0x50; /*SCON: mode 1, 8-bit UART, enable receive */
TMOD |= 0x20; /*TMOD: timer 1, mode 2, 8-bit */
TH1 = 0xFD; /*TH1: for 9600 baud */
TR1 = 1; /*TR1: timer 1 run */
}

void sendsms()
{
puts(command_AT);
puts(command_CMGF);
puts(command_CMGS);
puts(message);
putc(CTRLZ);
}

void puts(char* p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
putc(*temp);
temp++;
}
}

void putc(unsigned char chr)
{
SBUF = chr;
while(TI==0); /*Wait until the character is completely sent */
TI=0; /*Reset the flag */