GSM shield receiving SMS (how to compare char with string)

I'm currently doing a project which will compare the SMS that is being received to "admin". If it's true it will print success and if it's not it will print fail. How i got the error "invalid conversion from 'char' to 'const char' ".
Please help me!! I'm left with 2 weeks.
Many thanks in advanced!

/*
SMS receiver

This sketch, for the Arduino GSM shield, waits for a SMS message
and displays it through the Serial port.

Circuit:

  • GSM shield attached to and Arduino
  • SIM card that can receive SMS messages

created 25 Feb 2012
by Javier Zorzano / TD

This example is in the public domain.

*/

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println("SMS Messages Receiver");

// connection state
boolean notConnected = true;

// Start GSM connection
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}

Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}

void loop()
{
char c;

// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");

// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);

// An example of message disposal
// Any messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}

// Read message bytes and print them
while (c=sms.read())
Serial.print(c);

** if (strcmp(c, "admin") == 0)** << This is where the error occured
{
Serial.println("Success!");
}
else
{
Serial.println("Fail!");
}

Serial.println("\nEND OF MESSAGE");

// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}

delay(1000);

}

how to compare char with string

Does this really make sense? That is, in effect, what the compiler is asking you. A character, like 'm', is NEVER going to match a string, like "admin". There's no sense even trying to ask.

while (c=sms.read())
      Serial.print(c);

Read and (effectively) throw away all but the last character from the message.

 if (strcmp(c, "admin") == 0)

Then, try to see if the last letter equals a whole bunch of letters. If this were even possible to ask, the answer will never be true.

You need to be saving the values read from the sms into an array, and then asking if the array equals "admin".

How can i save the values read from the sms into an array? Sorry, i'm still a beginner in programming :o

How can i save the values read from the sms into an array?

First you create an array.

char smsData[80]; // Make it bigger if needed

Then, you create an index into the array:

byte smsIndex = 0;

Then, as you read data, you store it in the array:

while (c=sms.read())
{ // Always use curly braces with while statements!
      Serial.print(c);
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0'; // Keep string NULL terminated
} // Always use curly braces with while statements!

At some point, you need to reset smsIndex to 0. When is up to you, but it needs to be done before reading the next message.

got it. It's working now. Thanks buddy!

I have another problem now. When I receive the first SMS, it compares with "admin" and it works fine. But however, when I receive the second SMS, it doesn't compares with "1234" but compares with "admin" instead. However, the serial monitor do print "INDEX = 2!" which means it did enter the if (index == 2) loop.
Anyone can help? Thanks!

/*
 SMS receiver
 
 This sketch, for the Arduino GSM shield, waits for a SMS message 
 and displays it through the Serial port. 
 
 Circuit:
 * GSM shield attached to and Arduino
 * SIM card that can receive SMS messages
 
 created 25 Feb 2012
 by Javier Zorzano / TD
 
 This example is in the public domain.
 
 http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
 
*/

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
int index = 0;

// Array to hold the number a SMS is retreived from
char senderNumber[20];  

void setup() 
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  } 

  Serial.println("SMS Messages Receiver & Sender");
    
  // connection state
  boolean notConnected = true;
  
  // Start GSM connection
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
  pinMode(sensorPin, INPUT);  // sensor is the input
  pinMode(ledPin, OUTPUT);  // led light is the output
}

void loop() 
{
  char c;
  char smsData[80];
  byte smsIndex = 0;
  
  // If there are any SMSs available()  
  if (sms.available())
  {
    Serial.println("Message received from:");
   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
   
    // Read message bytes and print them
    while(c=sms.read())
    {
      Serial.print(c);
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0';  // Keep string NULL terminated
    }
    
     if (strcmp(smsData, "admin") == 0)
    {
      Serial.println("\nOKAY!");
      char txtMsg[200]= "Please enter password: ";
      Serial.println("\nSENDING");
      Serial.println();
      Serial.println("Message:");
      Serial.println(txtMsg);
        
      // send the message
      sms.beginSMS(senderNumber);
      sms.print(txtMsg);
      sms.endSMS(); 
      Serial.println("\nCOMPLETE!\n");
      index = 2; 
           
      // Read message bytes and print them
      while(c=sms.read())
      {
        Serial.print(c);
        smsData[smsIndex++] = c;
        smsData[smsIndex] = '\0';  // Keep string NULL terminated
      }
      
      if (index == 2)
      {
        Serial.println("INDEX = 2!");
        if (strcmp(smsData, "1234" == 0))
        {
          Serial.println("\nOKAY!");
          char txtMsg[200]= "Please enter: ""\n"" ""1"" to change recipent's number, ""\n"" ""2"" to change SMS content ";
          Serial.println("\nSENDING");
          Serial.println();
          Serial.println("Message:");
          Serial.println(txtMsg);
              
          // send the message
          sms.beginSMS(senderNumber);
          sms.print(txtMsg);
          sms.endSMS(); 
          Serial.println("\nCOMPLETE!\n");
          index = 3;
        }
      }
    }  
    else if (strcmp(smsData, "admin") != 0)
    {
      Serial.println("\nFAIL!");
    } 
      
    Serial.println("\nEND OF MESSAGE");
        
    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
    delay(1000);
  }
}
      char txtMsg[200]= "Please enter password: ";

If you can't count, don't try. The compiler can. Get rid of the 200. The string is an order of magnitude shorter than that.

Anyone can help?

Without seeing your serial output? No.

This is the updated code. The program would not enter the "else if (index == 2)" loop. Attached is the screenshot of the serial monitor.

/*
 SMS receiver
 
 This sketch, for the Arduino GSM shield, waits for a SMS message 
 and displays it through the Serial port. 
 
 Circuit:
 * GSM shield attached to and Arduino
 * SIM card that can receive SMS messages
 
 created 25 Feb 2012
 by Javier Zorzano / TD
 
 This example is in the public domain.
 
 http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS
 
*/

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
int index = 0;

// Array to hold the number a SMS is retreived from
char senderNumber[20];  

void setup() 
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  } 

  Serial.println("SMS Messages Receiver");
    
  // connection state
  boolean notConnected = true;
  
  // Start GSM connection
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() 
{
  char c;
  char smsData[80];
  byte smsIndex = 0;
  
  // If there are any SMSs available()  
  if (sms.available())
  {
    Serial.println("Message received from:");
   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
   
    // Read message bytes and print them
    while(c=sms.read())
    {
      Serial.print(c);
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0';  // Keep string NULL terminated
    }
    
     if (strcmp(smsData, "admin") == 0)
    {
      Serial.println("\nOKAY!");
      char txtMsg[]= "Please enter password: ";
      Serial.println("\nSENDING");
      Serial.println();
      Serial.println("Message:");
      Serial.println(txtMsg);
        
      // send the message
      sms.beginSMS(senderNumber);
      sms.print(txtMsg);
      sms.endSMS(); 
      Serial.println("\nCOMPLETE!\n");
      index = 2; 
      smsIndex = 0;
      // Read message bytes and print them
      while(c=sms.read())
      {
        Serial.print(c);
        smsData[smsIndex++] = c;
        smsData[smsIndex] = '\0';  // Keep string NULL terminated
      }
    }
    else if (index == 2)
    {
        Serial.println("INDEX = 2!");
        if (strcmp(smsData, "1234" == 0))
        {
          Serial.println("\nOKAY!");
          char txtMsg[]= "Please enter: ""\n"" ""1"" to change recipent's number, ""\n"" ""2"" to change SMS content ";
          Serial.println("\nSENDING");
          Serial.println();
          Serial.println("Message:");
          Serial.println(txtMsg);
              
          // send the message
          sms.beginSMS(senderNumber);
          sms.print(txtMsg);
          sms.endSMS(); 
          Serial.println("\nCOMPLETE!\n");
          index = 3;
        }
        else
        {
          Serial.println("Wrong Password! Please try again!");
          char txtMsg[]= "Wrong Password! Please try again!";
          Serial.println("\nSENDING");
          Serial.println();
          Serial.println("Message:");
          Serial.println(txtMsg);
              
          // send the message
          sms.beginSMS(senderNumber);
          sms.print(txtMsg);
          sms.endSMS(); 
          Serial.println("\nCOMPLETE!\n");
        }
    }
    else 
    {
      Serial.println("\nFAIL!");
    }       
    Serial.println("\nEND OF MESSAGE");
        
    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
    delay(1000);
  }
}

The program would not enter the "else if (index == 2)" loop.

You are wrong. You have a fundamental misunderstanding of how serial data arrives. You think it arrives instantaneously, while in reality it arrives ssslllooowwwlllyyy.

  if (sms.available())
  {

If there is at least one character available...

    Serial.println("Message received from:");
   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

Diddle away some time sending data to the serial port. While you are doing that, some more data arrives.

    // Read message bytes and print them
    while(c=sms.read())
    {
      Serial.print(c);
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0';  // Keep string NULL terminated
    }

Read a character. Diddle away some time sending it back out.

It is that diddling that makes you lucky in getting the whole short message.

     if (strcmp(smsData, "admin") == 0)
    {
      Serial.println("\nOKAY!");
      char txtMsg[]= "Please enter password: ";
      Serial.println("\nSENDING");
      Serial.println();
      Serial.println("Message:");
      Serial.println(txtMsg);
        
      // send the message
      sms.beginSMS(senderNumber);
      sms.print(txtMsg);
      sms.endSMS(); 
      Serial.println("\nCOMPLETE!\n");
      index = 2;

Since you got lucky, and the whole message arrived, send a message, print some stuff, and set index to 2.

      smsIndex = 0;
      // Read message bytes and print them
      while(c=sms.read())
      {
        Serial.print(c);
        smsData[smsIndex++] = c;
        smsData[smsIndex] = '\0';  // Keep string NULL terminated
      }

Read the response to the message you sent. Well, guess what. At this point, nanoseconds after you sent the message and set index to 2, NOTHING has arrived, so the while loop is skipped. That being the last thing that happens if "admin" arrived,

    Serial.println("\nEND OF MESSAGE");
        
    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
    delay(1000);

happens, and loop() ends, and gets called again.

  if (sms.available())
  {
    Serial.println("Message received from:");
   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
   
    // Read message bytes and print them
    while(c=sms.read())
    {
      Serial.print(c);
      smsData[smsIndex++] = c;
      smsData[smsIndex] = '\0';  // Keep string NULL terminated
    }
    
     if (strcmp(smsData, "admin") == 0)
    {

Since you diddled around for a second, there might be a response. Or, there might not be, and loop() would be called again.

When there is a response, it isn't "admin" (presumably), so the if test evaluates to false, and the body is skipped.

    }
    else if (index == 2)
    {
        Serial.println("INDEX = 2!");

After sending the message, you set index to 2, so, clearly this test will be true.

You need to determine what actually is contained in an sms message, including any carriage returns, line feeds, etc. Something indicates that a message is complete. Diddling around, in the hopes that all the useful information will arrive while you are waiting is NOT the way to write robust communications code.

When you send a message, you have two choices. One choice is to handle the communication asynchronously. That is, you send a message and forget about it. Any reply that comes in later will be treated as a unique event. Incoming data is buffered, as it arrives, and dealt with when the packet is complete.

The problem here is that the reply needs to contain more information, such as including something that indicates what the reply is in response to.

The other way to deal with message/reply type communications is to send a message and wait for the complete reply. You are not currently waiting, and you are most certainly not checking that the reply is complete.

The problem with this approach is that the Arduino does nothing else while waiting for the reply.

It is up to you to decide which of these options is best in your situation, and to implement them correctly.