#include <ez8.h>#include <sio.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#define MSG_LEN 250#define TESTING 1unsigned int msg_index;char msg[MSG_LEN];int cnum=0, start=0, fan=0, rad=0;void clear_msg(void){int i;for(i=0;i<MSG_LEN;i++) msg[i]='\0';msg_index=0;}void delay(int loop){int i,j,k;for (i=loop; i>=0; i--) for (j=0;j<0xFF;j++) for (k=0;k<0xFF;k++);}void init_port(void){ PDDD &= ~0x03; PDAF &= ~0x03; PDOC &= ~0x03; PDHDE |= 0x03; PDOUT &= ~0x03; PADD &= ~0x40; PAAF &= ~0x40; PAOC &= ~0x40; PAHDE |= 0x40; PAOUT &= ~0x40; PCDD |= 0x70; PCDD &= ~0x0F; PCAF &= ~0x7F; PCOC &= ~0x0F; PCHDE |= 0x0F; PCOUT &= ~0x0F;}#pragma interruptvoid read_uart0(void){ if (U0STAT0 & 0x80) msg[msg_index++]=U0RXD; }void initial_uart0(void){ DI(); SET_VECTOR(UART0_RX, read_uart0); IRQ0ENH |= 0x10; IRQ0ENL |= 0x10; IRQ0 &= ~0x10; U0CTL1 &= ~0x02; EI();}void at_at(void){select_port(_UART0);clear_msg();printf("AT\n");delay(1);if(TESTING){select_port(_UART1); printf("%s", msg); }delay(1);}//you can notice sa init_GSM na madaming beses tina-toggle yung output ng PD0, gusto ku lang kasi makita thru LED yung //progress nang initialization if hindi nakakabit sa hyperterminal .. :Dvoid init_GSM(void){ PDOUT ^= 0x01; delay(20); PAOUT |= 0x40; PDOUT ^= 0x01; delay(20); PAOUT &= ~0x40; PDOUT ^= 0x01; delay(1); at_at(); PDOUT ^= 0x01; do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+CFUN=1\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); at_at(); do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+COPS=0\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); at_at(); do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+CMGF=1\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); at_at(); do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+CSDH=0\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); at_at(); do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+CPMS=\"SM\",\"SM\",\"SM\"\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); at_at(); do{ PDOUT ^= 0x01; select_port(_UART0); clear_msg(); printf("AT+CNMI=2,0,0,0,0\n"); delay(1); if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1); }while(strstr(msg,"ERROR")!=NULL); clear_msg(); PDOUT ^= 0x01;}void sendREP(char *data){ at_at();do{clear_msg();select_port(_UART0);printf("AT+CMGS=\"09175781216\"\n");delay(1);if (TESTING) {select_port(_UART1); printf("%s",msg);}delay(1);clear_msg();select_port(_UART0);printf("%s",data); putch(0x1A);while(strstr(msg,"ERROR")==NULL && strstr(msg,"OK")==NULL) delay(1);if (TESTING) {select_port(_UART1); printf("%s",msg);}delay(1);}while(strstr(msg,"ERROR")!=NULL);clear_msg();}void readSMS(void){ cnum=0; start=0; fan=0; rad=0; select_port(_UART0); clear_msg(); printf("AT+CMGR=1\n"); while(strstr(msg,"ERROR")==NULL && strstr(msg,"OK")==NULL) delay(1); if(strstr(msg,"9175781216")!=NULL){ cnum=1; select_port(_UART1); printf("\ncnum=1\n"); } if(strstr(msg,"START")!=NULL){ start=1; select_port(_UART1); printf("\nstart=1\n"); } if(strstr(msg,"OFF")!=NULL){ start=2; select_port(_UART1); printf("\nstart=2\n"); } if(strstr(msg,"RADON")!=NULL){ rad=1; select_port(_UART1); printf("\nrad=1\n"); } if(strstr(msg,"RADOFF")!=NULL){ rad=2; select_port(_UART1); printf("\nrad=2\n"); } if(strstr(msg,"FANON")!=NULL){ fan=1; select_port(_UART1); printf("\nfan=1\n"); } if(strstr(msg,"FANOFF")!=NULL){ fan=2; select_port(_UART1); printf("\nfan=2\n"); } select_port(_UART0); clear_msg(); printf("AT+CMGD=1\n"); delay(1); if(strstr(msg,"OK")!=NULL){ select_port(_UART1); printf("\nMAY NADELETE NA MESSAGE\n"); } if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1);}//PC4,PC5,PC6 serves as an input if ignition is on, fan is on, radio is on, respectively.. nakaparallel kasi yung relays sa switches at key //ng car..void engstr(void){unsigned char a = 0x00; a=PCIN & 0x10; if(a==0x00){ select_port(_UART1); printf("\nEngine started sir..\n"); PCOUT ^= 0x02; delay(40); PCOUT ^= 0x01; delay(14); PCOUT ^= 0x01; at_at(); at_at(); sendREP("Engine started sir.."); } if(a==0x10){ select_port(_UART1); printf("\nCannot perform task, engine already started sir..\n"); sendREP("Cannot perform task, engine already started sir.."); }}void engoff(void){unsigned char a = 0x00; a=PCIN & 0x10; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, engine is already off sir..\n"); sendREP("Cannot perform task, engine is already off sir.."); } if(a==0x10){ select_port(_UART1); printf("\nEngine is off sir..\n"); PCOUT ^= 0x02; sendREP("Engine is off sir.."); }}void fanstr(void){unsigned char a = 0x00; a=PCIN & 0x20; if(a==0x00){ select_port(_UART1); printf("\nFan turned on sir..\n"); PCOUT ^= 0x04; sendREP("Fan turned on sir.."); } if(a==0x20){ select_port(_UART1); printf("\nCannot perform task, fan already turned on sir..\n"); sendREP("Cannot perform task, fan already turned on sir.."); }}void fanoff(void){unsigned char a = 0x00; a=PCIN & 0x20; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, fan is already off sir..\n"); sendREP("Cannot perform task, fan is already off sir.."); } if(a==0x020){ select_port(_UART1); printf("\nFan is off sir..\n"); PCOUT ^= 0x04; sendREP("Fan is off sir.."); }}void radstr(void){unsigned char a = 0x00; a=PCIN & 0x40; if(a==0x00){ select_port(_UART1); printf("\nRadio turned on sir..\n"); PCOUT ^= 0x08; sendREP("Fan turned on sir.."); } if(a==0x40){ select_port(_UART1); printf("\nCannot perform task, radio already turned on sir..\n"); sendREP("Cannot perform task, radio already turned on sir.."); }}void radoff(void){unsigned char a = 0x00; a=PCIN & 0x40; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, radio is already off sir..\n"); sendREP("Cannot perform task, radio is already off sir.."); } if(a==0x020){ select_port(_UART1); printf("\nRadio is off sir..\n"); PCOUT ^= 0x08; sendREP("Radio is off sir.."); }}void main(void){ init_uart(_UART0, _DEFFREQ, _DEFBAUD); init_uart(_UART1, _DEFFREQ, _DEFBAUD); init_port(); initial_uart0(); init_GSM(); PDOUT &= ~0x01; PDOUT |= 0x02; while(1){ readSMS(); if((cnum==1)&&(start==1)){ engstr(); } if((cnum==1)&&(start==2)){ engoff(); } if((cnum==1)&&(start==0)&&(fan==0)&&(rad==0)){ select_port(_UART1); printf("\nCode unknown sir..\n"); sendREP("Code unknown sir.."); } if((cnum==1)&&(fan==1)){ fanstr(); } if((cnum==1)&&(fan==2)){ fanoff(); } if((cnum==1)&&(rad==1)){ radstr(); } if((cnum==1)&&(rad==2)){ radoff(); } cnum=0; start=0; rad=0; fan=0; }}
#include <ez8.h>#include <sio.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#define MSG_LEN 250#define TESTING 1unsigned int msg_index;char msg[MSG_LEN];int cnum=0, start=0, fan=0, rad=0;// editedvoid clear_msg(void){msg[0]='\0';msg_index=0;}void delay(int loop){int i,j,k;for (i=loop; i>=0; i--) for (j=0;j<0xFF;j++) for (k=0;k<0xFF;k++);}void init_port(void){ PDDD &= ~0x03; PDAF &= ~0x03; PDOC &= ~0x03; PDHDE |= 0x03; PDOUT &= ~0x03; PADD &= ~0x40; PAAF &= ~0x40; PAOC &= ~0x40; PAHDE |= 0x40; PAOUT &= ~0x40; PCDD |= 0x70; PCDD &= ~0x0F; PCAF &= ~0x7F; PCOC &= ~0x0F; PCHDE |= 0x0F; PCOUT &= ~0x0F;}#pragma interruptvoid read_uart0(void){ if (U0STAT0 & 0x80) msg[msg_index++]=U0RXD; }void initial_uart0(void){ DI(); SET_VECTOR(UART0_RX, read_uart0); IRQ0ENH |= 0x10; IRQ0ENL |= 0x10; IRQ0 &= ~0x10; U0CTL1 &= ~0x02; EI();}void RenameThisCodeNaLangHehe(char *myString){select_port(_UART0);clear_msg();printf(myString);delay(1);if(TESTING){select_port(_UART1); printf("%s", msg); }delay(1);}void at_at(void){RenameThisCodeNaLangHehe("AT\n");}const char * const str_ptr[]={"AT+CFUN=1\n","AT+COPS=0\n","AT+CMGF=1\n","AT+CPMS=\"SM\",\"SM\",\"SM\"\n", "AT+CNMI=2,0,0,0,0\n"};//you can notice sa init_GSM na madaming beses tina-toggle yung output ng PD0, gusto ku lang kasi makita thru LED yung //progress nang initialization if hindi nakakabit sa hyperterminal .. :Dvoid init_GSM(void){ unsigned char ctr; PDOUT ^= 0x01; delay(20); PAOUT |= 0x40; PDOUT ^= 0x01; delay(20); PAOUT &= ~0x40; PDOUT ^= 0x01; delay(1); PDOUT ^= 0x01; for(ctr=0;ctr<5;ctr++) { at_at(); do{ PDOUT ^= 0x01; RenameThisCodeNaLangHehe(str_ptr[ctr]); }while(strstr(msg,"ERROR")!=NULL); } clear_msg(); PDOUT ^= 0x01;}void sendREP(char *data){ at_at();do{RenameThisCodeNaLangHehe("AT+CMGS=\"09175781216\"\n");/*clear_msg();select_port(_UART0);printf("AT+CMGS=\"09175781216\"\n");delay(1);if (TESTING) {select_port(_UART1); printf("%s",msg);}delay(1);*/clear_msg();select_port(_UART0);printf("%s",data); putch(0x1A);while(strstr(msg,"ERROR")==NULL && strstr(msg,"OK")==NULL) delay(1);if (TESTING) {select_port(_UART1); printf("%s",msg);}delay(1);}while(strstr(msg,"ERROR")!=NULL);clear_msg();}void readSMS(void){ cnum=0; start=0; fan=0; rad=0; select_port(_UART0); clear_msg(); printf("AT+CMGR=1\n"); while(strstr(msg,"ERROR")==NULL && strstr(msg,"OK")==NULL) delay(1); if(strstr(msg,"9175781216")!=NULL){ cnum=1; select_port(_UART1); printf("\ncnum=1\n"); } if(strstr(msg,"START")!=NULL){ start=1; select_port(_UART1); printf("\nstart=1\n"); } if(strstr(msg,"OFF")!=NULL){ start=2; select_port(_UART1); printf("\nstart=2\n"); } if(strstr(msg,"RADON")!=NULL){ rad=1; select_port(_UART1); printf("\nrad=1\n"); } if(strstr(msg,"RADOFF")!=NULL){ rad=2; select_port(_UART1); printf("\nrad=2\n"); } if(strstr(msg,"FANON")!=NULL){ fan=1; select_port(_UART1); printf("\nfan=1\n"); } if(strstr(msg,"FANOFF")!=NULL){ fan=2; select_port(_UART1); printf("\nfan=2\n"); } select_port(_UART0); clear_msg(); printf("AT+CMGD=1\n"); delay(1); if(strstr(msg,"OK")!=NULL){ select_port(_UART1); printf("\nMAY NADELETE NA MESSAGE\n"); } if(TESTING){select_port(_UART1); printf("%s", msg); } delay(1);}//PC4,PC5,PC6 serves as an input if ignition is on, fan is on, radio is on, respectively.. nakaparallel kasi yung relays sa switches at key //ng car..void engstr(void){unsigned char a = 0x00; a=PCIN & 0x10; if(a==0x00){ select_port(_UART1); printf("\nEngine started sir..\n"); PCOUT ^= 0x02; delay(40); PCOUT ^= 0x01; delay(14); PCOUT ^= 0x01; at_at(); at_at(); sendREP("Engine started sir.."); } if(a==0x10){ select_port(_UART1); printf("\nCannot perform task, engine already started sir..\n"); sendREP("Cannot perform task, engine already started sir.."); }}void engoff(void){unsigned char a = 0x00; a=PCIN & 0x10; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, engine is already off sir..\n"); sendREP("Cannot perform task, engine is already off sir.."); } if(a==0x10){ select_port(_UART1); printf("\nEngine is off sir..\n"); PCOUT ^= 0x02; sendREP("Engine is off sir.."); }}void fanstr(void){unsigned char a = 0x00; a=PCIN & 0x20; if(a==0x00){ select_port(_UART1); printf("\nFan turned on sir..\n"); PCOUT ^= 0x04; sendREP("Fan turned on sir.."); } if(a==0x20){ select_port(_UART1); printf("\nCannot perform task, fan already turned on sir..\n"); sendREP("Cannot perform task, fan already turned on sir.."); }}void fanoff(void){unsigned char a = 0x00; a=PCIN & 0x20; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, fan is already off sir..\n"); sendREP("Cannot perform task, fan is already off sir.."); } if(a==0x020){ select_port(_UART1); printf("\nFan is off sir..\n"); PCOUT ^= 0x04; sendREP("Fan is off sir.."); }}void radstr(void){unsigned char a = 0x00; a=PCIN & 0x40; if(a==0x00){ select_port(_UART1); printf("\nRadio turned on sir..\n"); PCOUT ^= 0x08; sendREP("Fan turned on sir.."); } if(a==0x40){ select_port(_UART1); printf("\nCannot perform task, radio already turned on sir..\n"); sendREP("Cannot perform task, radio already turned on sir.."); }}void radoff(void){unsigned char a = 0x00; a=PCIN & 0x40; if(a==0x00){ select_port(_UART1); printf("\nCannot perform task, radio is already off sir..\n"); sendREP("Cannot perform task, radio is already off sir.."); } if(a==0x020){ select_port(_UART1); printf("\nRadio is off sir..\n"); PCOUT ^= 0x08; sendREP("Radio is off sir.."); }}void main(void){ init_uart(_UART0, _DEFFREQ, _DEFBAUD); init_uart(_UART1, _DEFFREQ, _DEFBAUD); init_port(); initial_uart0(); init_GSM(); PDOUT &= ~0x01; PDOUT |= 0x02; while(1){ readSMS(); if((cnum==1)&&(start==1)){ engstr(); } if((cnum==1)&&(start==2)){ engoff(); } if((cnum==1)&&(start==0)&&(fan==0)&&(rad==0)){ select_port(_UART1); printf("\nCode unknown sir..\n"); sendREP("Code unknown sir.."); } if((cnum==1)&&(fan==1)){ fanstr(); } if((cnum==1)&&(fan==2)){ fanoff(); } if((cnum==1)&&(rad==1)){ radstr(); } if((cnum==1)&&(rad==2)){ radoff(); } cnum=0; start=0; rad=0; fan=0; }}