Go Down

Topic: GSM String nothing happens (Read 1 time) previous topic - next topic

vrooms23

I have got mostly problem for String sms, never happened to me, but other code for "char inchar;" one character each its working fine turn on LED when send message are ?lamp on and ?lamp off.
Show code this;

Code: [Select]
// Example 27.2

#include <SoftwareSerial.h>
char inchar; //Will hold the incoming character from the Serial Port.
SoftwareSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

int led1 = 6;
int led2 = 10;
int resetGSM = 9;

void setup()
{
 // prepare the digital output pins
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(resetGSM, OUTPUT);
 pinMode(TX, OUTPUT);
 
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(resetGSM, HIGH);
 delay(1000);  
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(resetGSM, LOW);
 //Initialize GSM module serial port for communication.
 cell.begin(2400);
 delay(1000);
 
int i=0;
while (i<10){    
 //delay(1000); // give time for GSM module to register on network etc.
 digitalWrite(led2, HIGH);
 delay(1000);  
 digitalWrite(led2, LOW);
   delay(1000); // give time for GSM module to register on network etc.
i++;
}
 
 
 
 delay(500); // give time for GSM module to register on network etc.lay(30000); // give time for GSM module to register on network etc.
 cell.println("AT+CMGF=1"); // set SMS mode to text
 delay(200);
 cell.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
 delay(200);
}

void loop()
{
 //If a character comes in from the cellular module...
 if(cell.available() >0)
 {
   inchar=cell.read();
   if (inchar=='?')
   {
     delay(10);
     inchar=cell.read();
     if (inchar=='l')  //  Lamp
     {        
       delay(10);
       inchar=cell.read();
       if (inchar=='a')
    {
     delay(10);
    inchar=cell.read();
    if (inchar=='m')
     {
       delay(10);
       inchar=cell.read();
       if (inchar=='p')
        {
          delay(10);
          inchar=cell.read();
          if (inchar==' ')  
           {
             delay(10);
             inchar=cell.read();
             if (inchar=='o')
              {
                delay(10);
                inchar=cell.read();
                if (inchar=='n')
                 {
                 digitalWrite(led1, HIGH);
                 }  
                   else if (inchar=='f')
                    {
                      delay(10);
                      inchar=cell.read();
                      if (inchar=='f')
                       {
                       digitalWrite(led1, LOW);
                       }
                       
                    }
                   delay(10);
                   cell.println("AT+CMGD=1,4"); // delete all SMS
                    }
                    }
                   }
                  }
                }
     }
   }
 }
}


Now use String, its nothing happened to me, I always use 2 and 3pin for SoftwareSerial as same with char inchar, show code this;

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial cell(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;
//control pins of relay.
int relay_a = 6;
int relay_b = 10;
int relay_c = 11;
int relay_d = 12;
int resetGSM = 9;
int TX = 5;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );

void setup()
{
 Serial.begin(9600);                 // the GPRS baud rate
 // Initialize  PINs
 pinMode( 6, OUTPUT );
 pinMode( 10, OUTPUT );
 pinMode( 11, OUTPUT );
 pinMode( 12, OUTPUT );
 
 digitalWrite( 6, LOW );
 digitalWrite( 10, LOW );
 digitalWrite( 11, LOW );
 digitalWrite( 12, LOW );
 pinMode(resetGSM, OUTPUT);
 
 digitalWrite(6, HIGH);  // for Test all Leds on then off
 digitalWrite(10, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(12, HIGH);
 digitalWrite(resetGSM, HIGH);
 delay(1000);  
 digitalWrite(6, LOW);
 digitalWrite(10, LOW);
 digitalWrite(11, LOW);
 digitalWrite(12, LOW);
 digitalWrite(resetGSM, LOW);

 cell.begin(2400);
 delay(1000);

 int i=0;
 while (i<10){    
 //delay(1000); // give time for GSM module to register on network etc.
 digitalWrite(6, HIGH);
 delay(1000);  
 digitalWrite(6, LOW);
 delay(1000); // give time for GSM module to register on network etc.
 i++;
}
   
 
 
 
cell.println("AT+CMGF=1");
delay(200);

 //cell.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
 //delay(200);

 
}

void loop()
{
   char cellInByte;
   if(cell.available())
   {      
       cellInByte = (unsigned char)cell.read();
      delay(5);
       
       // -------------------------------------------------------------------
       // EN: Program also listen to the GPRS shield message.
       // -------------------------------------------------------------------
      // EN: If the message ends with <CR> then process the message
       if( cellInByte == 13 ){
         // EN: Store the char into the message buffer
         ProcessGprsMsg();
        }
        if( cellInByte == 10 ){
           // EN: Skip Line feed
        }
        else {
          // EN: store the current character in the message string buffer
          msg += String(cellInByte);
        }
    }  
}
// 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 ){
 cell.println( "AT+CMGR=" );
 cell.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;
}



Something is wrong with code for String?

vrooms23

I solved, I searched websites found right one code use SIM900.h and SoftwareSerial.h for my GSM Shield which SIM900.h (libraries GSMSHIELD) what need use #include SIM900.h .

Go Up