i had modified my program. here it is..
#include <stdio.h>
#include <NewSoftSerial.h>
#define powerOn 4
enum gsmstate {
gsmWaiting,
gsmReceiving,
gsmExecuting
};
enum gsmstate dvcState;
int i=0, j=0, msgread=0;
char temp, temp1[20], msgnum[3];
NewSoftSerial gsmRxTx (2, 3); //(rxpin, txpin) declaration of new rx/tx pin
void setup(){
Serial.begin(9600);
gsmRxTx.begin(9600);
digitalWrite(powerOn, HIGH); //connect powerOn of gsm module to pin 4. this will open gsm module
delay(1500);
digitalWrite(powerOn, LOW);
delay(15000);
dvcState = gsmWaiting;
startUp();
}
void chk_msg();
void msgopen();
void startUp(){
gsmRxTx.print("AT\r\n"); //send AT serially, expect OK as gsm response
delay(500);
gsmRxTx.println("ATE0");
delay(500);
gsmRxTx.println("AT+CMGF=1");
delay(500);
gsmRxTx.println("AT+CNMI=2,1,0,0,0");
delay(5000);
}
void loop(){
if (msgread == 0)chk_msg();
else msgopen();
delay(50);
}
void chk_msg(){
char *s = NULL;
switch(dvcState){
case gsmWaiting:
if (gsmRxTx.available()){
if (gsmRxTx.read() == '+'){
memset(temp1,0,sizeof(temp1));
dvcState = gsmReceiving;
}
}
break;
case gsmReceiving:
if (gsmRxTx.available())
temp = gsmRxTx.read();
if (temp != 13){
temp1[i] = temp;
temp1[i+1] = 0;
i++;
}
else {
dvcState = gsmExecuting;
temp = 0;
i = 0;
}
break;
case gsmExecuting:
Serial.println(temp1);
//memset(msgnum,0,sizeof(msgnum));
if (strncmp(temp1,"CMTI",4) == 0){
s = strchr(temp1, ',');
s++;
while (*s != 0){
msgnum[j] = *s;
msgnum[j+1] = 0;
j++;
s++;
}
msgread = 1;
}
j = 0;
s = NULL;
dvcState = gsmWaiting;
Serial.println("DUMAAN DITO.");
break;
}
}
void msgopen(){
char tempA=0, tempA1[100]="0";
int count=0, i=0;
memset(tempA1,0,sizeof(tempA1));
gsmRxTx.print("AT+CMGR=");
gsmRxTx.println(msgnum);
if (gsmRxTx.available()){
tempA = gsmRxTx.read();
}
if (tempA == 13) count++;
else if (count < 2){
tempA1[i] = tempA;
tempA1[i+1] = 0;
i++;
}
gsmRxTx.flush();
Serial.println(tempA1);
dvcState = gsmWaiting;
count=0;
i=0;
msgread=0;
}
flashed on the serial monitor:
CMTI: "SM",19
DUMAAN DITO.
CMGR: "REC UNREAD","+639054888383","","11(47/~5,1Q:20:26+32"
DUMAAN DITO.
as you will notice, there's trash message at the time stamp. also the real message (which should have been "TEST") was not read by the variable tempA1.
after the last line "DUMAAN DITO.", arduino cannot recognize the new incoming message.
what could be the problem with my code. can someone please help me. thanks