I am using Arduino UNO along with GSM modem mini GSM800A for receiving SMS messages. Here's the complete code. It works but I feel it's not that reliable. I am planning to modify the printserial() function to avoid the strstr() function. How can I improve the program for more reliable reception of SMS messages?
#include <AltSoftSerial.h> //Buffer size 256
AltSoftSerial altSerial; // pin 8 & 9
char comm_buf[256];
bool printSerial(char* response= NULL);
void sentNotification(char* msg);
void SyncOrResetGsmAndTx();
void MySetup();
void setup()
{
}
void loop()
{
MySetup();
while(true){
MyLoop();
}
}
void SyncOrResetGsmAndTx(){
while(true){
bool result = false;
unsigned long start_time = millis();
while(result == false){
altSerial.println("AT");
delay(3000);
if(printSerial("OK")){
result = true;
Serial.println("Synchronization with GSM modem sucessful.");
altSerial.println("AT+CMGF=1");
delay(3000);
// Disable messages about new SMS from the GSM module
altSerial.println("AT+CNMI=2,0");
delay(3000);
printSerial();
altSerial.println("AT+CPMS=\"SM\",\"SM\",\"SM\"");
delay(3000);
printSerial();
return;
}
else Serial.println("Failed to synchronize with GSM modem.");
if( millis() - start_time> 10000UL) {
Serial.println("GSM init timeout.");
break;
}
}//while result == false
if( result == false){
// time to reboot gsm
digitalWrite(11,HIGH);
delay(300); // Pull down 200ms to LOW
digitalWrite(11,LOW);
delay(10000UL); // wait 10 sec for initialization of GSM
// it will retry
}
} //while(true)
}
void MySetup() {
pinMode(11,OUTPUT); // GSM Reset PIN
digitalWrite(11,LOW);
pinMode(13,OUTPUT);
bool result;
Serial.begin(9600);
while (!Serial);
Serial.println("Hardware Serial initialized.");
altSerial.begin(9600);
delay(3000);
Serial.println("Software Serial initialized.");
// GetStartSignalFromPC();
// Serial.println("\nSynced with PC.\n");
SyncOrResetGsmAndTx();
Serial.println("\nAggregator is listening for sms...\n");
}
void MyLoop(){
char msg[256];
char* p_char;
char* p_char1;
static unsigned long start_time = millis();
altSerial.println("AT+CMGL=\"ALL\""); // find unread sms from memory
delay(5000);
if( printSerial("+CMGL:")){
p_char = strchr(comm_buf,':');
if (p_char != NULL) {
p_char1 = strchr(p_char+1, ',');
*p_char1 = 0;
char index = atoi(p_char+1);
altSerial.print("AT+CMGR="); //read from memory
altSerial.println((int)index);
delay(5000);
if( printSerial("+CMGR")){
p_char = strstr(p_char+1, "StnID");
if (p_char != NULL) {
p_char1 = strchr((char *)(p_char), 0x0d); // find <CR> as the end of SMS string
if (p_char1 != NULL) {
//Serial.println("\n<CR> found\n");
*p_char1 = 0;
strcpy(msg,p_char);
sentNotification(msg);
}
else
{
//Serial.println("\n<CR> not found\n");
}
}//if (p_char
altSerial.print("AT+CMGD="); //delete from memory
altSerial.println((int)index);
delay(5000);
printSerial();
start_time = millis();
} // if("+CMGR"
}//if (p_char
} //if("+CMGL"
else
if( millis() - start_time > 300000UL)
{
start_time = millis();
altSerial.println("AT");
delay(3000);
if(!printSerial("OK")) SyncOrResetGsmAndTx();
}
} // loop
bool printSerial(char* response)
{
int i = 0;
while(altSerial.available()>0 && i<254){
char c = altSerial.read();
//Serial.print(c);
comm_buf[i++] = c;
}
comm_buf[i++] = '\0';
if(response != NULL){
char* p = NULL;
p = strstr(comm_buf, response);
if( p!= NULL) return true;
}
return false;
}
void sentNotification(char* msg)
{
int l = strlen(msg);
for(int i=0;i< l; i++)
{
char ch = msg[i];
Serial.print(ch);
delay(10);
}
delay(4000);
}