sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D4 at RC2_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D6 at RC4_bit;
sbit LCD_D7 at RC5_bit;
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
char IncData;
int x = 0;
char RcvdMsg[60] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[20];
int MsgLength = 0;
void RecSMS();
void ClearBuffers();
void Config();
void main() {
lcd_init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,2,"UNIVERSITY OF ");
Lcd_Out(2,2,"DAR ES SALAAM");
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"GSM BASED ON");
Lcd_Out(2,2,"STREET LIGHTS");
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"***WELCOME****");
Lcd_Out(2,1,"WAITING FOR SMS");
Delay_ms(5000);
UART1_init(9600);
Delay_ms(100);
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE=1;
TRISD=0X00;
PORTD=0X00;
TRISB = 1;
Config();
while(1)
{ while(!UART1_Data_Ready());
if (UART1_Data_Ready())
{
IncData = UART1_Read();
//UART1_Write_Text(IncData);
if(IncData == '+'){RcvdCheck = 1;}
if((IncData == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((IncData == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((IncData == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){index = 0;RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(IncData == '\n')
{RcvdEnd++;}
if(RcvdEnd == 3)
{RcvdEnd = 0;}
RcvdMsg[index] = IncData;
index++;
if(RcvdEnd == 2)
{RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
//PortD.F3 = 1;
UART1_Write_Text("MOBILE NO: ");
for(x = 4;x < 17;x++)
{
MsgMob[x-4] = RcvdMsg[x];
UART1_Write(MsgMob[x-4]);
}
UART1_Write(0x0D);
UART1_Write_Text("SMS Text: ");
for(x = 46;x < MsgLength;x++)
{
MsgTxt[x-46] = RcvdMsg[x];
UART1_Write(MsgTxt[x-46]);
}
if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '1')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'N'))
{
PORTD.F0 = 1;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L1 TURNED");
LCD_Out(2,1,"ON SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 1 TURNED ON SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L1ON();}
if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '1')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'F')&&(MsgTxt[11] == 'F'))
{
PORTD.F0 = 0;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L1 TURNED");
LCD_Out(2,1,"OFF SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 1 TURNED OFF SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L1OF();}
if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '2')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'N'))
{
PORTD.F1 = 1;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L2 TURNED");
LCD_Out(2,1,"ON SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 2 TURNED ON SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L2ON();}
if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '2')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'F')&&(MsgTxt[10] == 'F'))
{
PORTD.F1 = 0;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L2 TURNED");
LCD_Out(2,1,"OFF SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 2 TURNED OFF SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L2OF();}
if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '3')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'N'))
{
PORTD.F2 = 1;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L3 TURNED");
LCD_Out(2,1,"ON SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 3 TURNED ON SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L2ON();}
else if((MsgTxt[0] == 'S')&&(MsgTxt[1] == 'T')&&(MsgTxt[2] == 'R')&&(MsgTxt[3] == 'T')&&(MsgTxt[4] == 'A')&&(MsgTxt[5] == 32)&&(MsgTxt[6] == 'L')&&(MsgTxt[7] == '3')&&(MsgTxt[8] == 32)&&(MsgTxt[9] == 'O')&&(MsgTxt[10] == 'N'))
{
PORTD.F2 = 1;
Delay_ms(1000);
UART1_Write_Text("AT+CMGD=1\r\n");
Lcd_init();
Lcd_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"STRTA L3 TURNED");
LCD_Out(2,1,"ON SUCCESSFULLY");
Delay_ms(4000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"WAITING FOR ");
Lcd_Out(2,1,"ANOTHER NEW SMS");
UART1_Write_Text("AT+CMGS="+255717677776"\r");
Delay_ms(500);
UART1_Write_Text("STREET-A LIGHT 3 TURNED ON SUCCESSFULLY");
UART1_Write(26);
Delay_ms(1000);
}//L2ON();}
ClearBuffers();
}
}
}
}
}
void ClearBuffers()
{
strcpy(RcvdMsg,"");
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
strcpy(MsgMob,"");
strcpy(MsgTxt,"");
MsgLength = 0;
}
void Config()
{
Delay_ms(2000);
UART1_Write_Text("ATE0\r\n");
Delay_ms(1000);
UART1_Write_Text("AT\r\n");
Delay_ms(1000);
UART1_Write_Text("AT+CMGF=1\r\n");
Delay_ms(1000);
UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n");
Delay_ms(1000);
}