[SOLVED] GSM remote relay control issue... Can't delete sms memory

Hi,

I am new to arduino & trying to find out how to control relays by sending sms to my arduino platform. I am using naked SIM300 GSM module with Tx, Rx & GND pins. I am using InduinoX board (UNO Clone). Pin 4,5,6,7 are connected to 4 relays with common ground. I have a sample code…

#include <SoftwareSerial.h>
 

// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
 
void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
  pinMode( 4, OUTPUT ); 
  pinMode( 5, OUTPUT ); 
  pinMode( 6, OUTPUT ); 
  pinMode( 7, OUTPUT ); 
  digitalWrite( 4, LOW ); 
  digitalWrite( 5, LOW ); 
  digitalWrite( 6, LOW );
  digitalWrite( 7, LOW );
 Serial.println( "AT+CMGF=1" ); 
 delay(200);
}
 
void loop()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }   
}
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
  }
   if( sms.indexOf("onc") >= 0 ){
    digitalWrite(  relay_c, HIGH );
  }
  if( sms.indexOf("ond") >= 0 ){
    digitalWrite(  relay_d, HIGH );
  }
  if( sms.indexOf("offa") >= 0 ){
    digitalWrite(  relay_a, LOW );
  }
  if( sms.indexOf("offb") >= 0 ){
    digitalWrite(  relay_b, LOW );
  }
  if( sms.indexOf("offc") >= 0 ){
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("offd") >= 0 ){
    digitalWrite(  relay_d, LOW );
  }
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
  Serial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  Serial.print( "AT+CMGR=" );
  Serial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
  if( msg.indexOf( "Call Ready" ) >= 0 ){
   //  Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
  
  // EN: unsolicited message received when getting a SMS message
  if( msg.indexOf( "+CMTI" ) >= 0 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }
  
  // EN: +CMGR message just before indicate that the following GRPS Shield message 
  //     (this message) will contains the SMS body 
  if( SmsContentFlag == 1 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

Now, I am trying to modify the code because my sim300 module has only 25 sms memory.
From the AT command set I learned to delete the entire sms memory by AT+CMGDA=“DEL ALL” if TEXT mode u[/u] is selected. Now I am trying to implement the “DELETE SMS” code into the main example code.

I have tried a few alterations but they are not working. I can see what actually happens in the serial communication from the SerialMonitor option of Arduino. The first two Images are my attempt to delete the sms memory & it is the screenshot of the system after sending the “onb” (relay b ON) mesege to sim300. There is no confirmation of receiving any sms, neither my code worked at all. :frowning:

The example code works fine but the issue is it cant delete the SMS memory. Kindly check my altered code & give any suggestion to get my code into work.

First Attempt…

Nothing happens after sending sms to gsm module

Second Attempt…

How the example code works…

AT+CMGR=8 confirms that the new sms has been strored & read from no. 8 sms location.

the example code prints that a new sms arrived & activates the relay but my code doesn’t…

What should be altered…? any clue?
Thanks for putting time to read my post… cheers

Hello,
I have added the following code in the void loop () so that every time the RESET happens, it will erase the whole SMS memory first...

Serial.write("AT+CMGDA=");
Serial.write("34"); //ASCII of "
Serial.write("DEL ALL");
Serial.write("34");
Serial.write("13"); //ASCII of ENTER
Serial.write("10"); //ASCII of\n
delay(1000);

The At command for deleting the whole sms memory in text mode is
AT+CMGF=1 // Select TEXT MODE
AT+CMGDA="DEL ALL" // Delete SMS Memory

Am I doing something wrong in the code? Any suggestions will be highly appreciated.
Thanks

I have sorted out the problem. I was doing wrong with the delays. My SIM300 modem takes 1 sec for selecting text mode & approx 5 secs for deleting the whole SMS memory. I have added the appropriate delay & finally I am getting the expected output. :slight_smile:

The code is here...

  Serial.println( "AT+CMGF=1" ); 
 delay(2000);
 Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." );

Cheers...

The next step is to make the process asynchronous. Is there some reason you need to wait until the messages are deleted?

nope, It would be great if the delay for the DEL SMS can be overcome somehow. But the SMS memory has to be deleted because the GSM modem has only 25 sms memory. I wanted to delete the whole sms memory after every reset. any suggestions?

I’m a new GSM, got SIM900 like same http://www.tinyosshop.com/index.php?route=product/product&path=65_118&product_id=464, your GSM is SIM300 right? what about SIM900 can work with use your code fine? I tried to use with your code, but mine use 2 and 3, I put;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

Its working Serial Monitor show;

AT+CMGF=1
AT+CMGDA=“DEL ALL”
Let’s Test…

that looks fine, but not working turn on test Led, always never happen to me! not sure about SIM900, baud etc, I know changed outputs 13,12,11, and 10. I use with #include “SIM900.h” myself for receive and send all fine. your code not put #include “SIM900.h” for my SIM900 right?

Also my SIM900 changed Jumper RX&TX, 0,1 from 2,3, and remove SoftwareSerial mySerial(2, 3); then rest code like yours, I tried to turn on led so still nothing, maybe different with libraries.

I was doing with other code,

#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(2, 3);
 
// EN: String buffer for the GPRS shield message

String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message

int SmsContentFlag = 0;
 
// EN: Pin of the LED to turn ON and OFF depending on the received message

int ledPin = 13;
 
// EN: Code PIN of the SIM card (if applied)

//String SIM_PIN_CODE = String( "XXXX" );
 
void setup()
{
  mySerial.begin(2400);               // the GPRS baud rate   
  Serial.begin(9600);                 // the GPRS baud rate
 
  // Initialize la PIN
  pinMode( ledPin, OUTPUT ); 
  digitalWrite( ledPin, LOW ); 
}
 
void loop()
{
    char SerialInByte;
 
    if(Serial.available())
    {
       mySerial.print((unsigned char)Serial.read());
     }  
    else  if(mySerial.available())
    {
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();
 
        // EN: Relay to Arduino IDE Monitor
   
        Serial.print( SerialInByte );
 
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
   
        // -------------------------------------------------------------------
 
        // EN: If the message ends with <CR> then process the message
       
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
        
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed

         }
         else {
           // EN: store the current character in the message string buffer
 
           msg += String(SerialInByte);
         }
     }   
}
 
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.

void ProcessSms( String sms ){
  Serial.print( "ProcessSms for [" );
  Serial.print( sms );
  Serial.println( "]" );
 
  if( sms.indexOf("on") >= 0 ){
    digitalWrite( ledPin, HIGH );
    Serial.println( "LED IS ON" );
    return;
  }
  if( sms.indexOf("off") >= 0 ){
    digitalWrite( ledPin, LOW );
    Serial.println( "LED IS OFF" );
    return;
  }
}
 
// EN: Send the SIM PIN Code to the GPRS shield

//void GprsSendPinCode(){
//  if( SIM_PIN_CODE.indexOf("XXXX")>=0 ){
//    Serial.println( "*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***" );
//    Serial.println( "*** Please, define the SIM_PIN_CODE variable . ***" );
//    return;
// }
//  mySerial.print("AT+CPIN=");
// mySerial.println( SIM_PIN_CODE );

 
// EN: Request Text Mode for SMS messaging

void GprsTextModeSMS(){
  mySerial.println( "AT+CMGF=1" );
}
 
void GprsReadSmsStore( String SmsStorePos ){
  // Serial.print( "GprsReadSmsStore for storePos " );
  // Serial.println( SmsStorePos ); 
  mySerial.print( "AT+CMGR=" );
  mySerial.println( SmsStorePos );
}
 
// EN: Clear the GPRS shield message buffer

void ClearGprsMsg(){
  msg = "";
}
 
// EN: interpret the GPRS shield message and act appropiately

void ProcessGprsMsg() {
  Serial.println("");
  Serial.print( "GPRS Message: [" );
  Serial.print( msg );
  Serial.println( "]" );
 
//  if( msg.indexOf( "+CPIN: SIM PIN" ) >= 0 ){
//     Serial.println( "*** NEED FOR SIM PIN CODE ***" );
 //    Serial.println( "PIN CODE *** WILL BE SEND NOW" );
 //    GprsSendPinCode();
//  }
 
  if( msg.indexOf( "Call Ready" ) >= 0 ){
     Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
 
  // EN: unsolicited message received when getting a SMS message
  // FR: Message non sollicité quand un SMS arrive
  if( msg.indexOf( "+CMTI" ) >= 0 ){
     Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
     Serial.print( "SMS stored at " );
     Serial.println( SmsStorePos );
 
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
 
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }
 
  // EN: +CMGR message just before indicate that the following GRPS Shield message 
  //     (this message) will contains the SMS body

  if( SmsContentFlag == 1 ){
    Serial.println( "*** SMS MESSAGE CONTENT ***" );
    Serial.println( msg );
    Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
 
  ClearGprsMsg();
  // EN: Always clear the flag

  SmsContentFlag = 0; 
}

Serial Monitor, finally got details here;

GPRS Message:

+CMT: “+614xxxxxxxx”,"",“13/08/16,23:25:31+38”

GPRS Message: [
+CMT: “+614xxxxxxxx”,"",“13/08/16,23:25:31+38”]

on

GPRS Message: [
on]

But nothing working with Led turn on, still no idea with SIM900 is different.

I have got a notice, when Serial Monitor started;

AT+CMGF=1
AT+CMGDA="DEL ALL"
Let's Test...

Then I checked and typed AT+CMGL="ALL" by libraries name SIM900.h use GSM_GPRSLibrary_Client, but READ still have message, didn't delete when AT+CMGDA="DEL ALL", maybe reason with my SIM900 something wrong.

I heard SIM900 can work on both Software Serial and Hardware Serial. In order to use Software Serial arduino serial monitor is required. My requirement is arduino and SIM900 module should be run on standalone. (Without connecting PC via USB) I have tried this but it is not work without serial monitor. Can I do this using Hardware Serial? Or any other workaround for this run standalone?

Thanks nightcrawler218, i faced similar problem on my GSM SIM900 and so i tried out your solution. It worked. :slight_smile: