Go Down

Topic: Problem reading SMS and compare (Read 464 times) previous topic - next topic

psnap

I have huge problem  (and maybe misunderstand something) regarding compare incomming SMS with a string.
I'm comfused because it seems/should work, when print SMS to Monitor. Please look at my code below and the feedback from the monitor.

Code: [Select]
#include <SoftwareSerial.h>
#include <string.h>

String inData = "";

SoftwareSerial cell(2, 3);

void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
  Serial.flush();
  cell.flush();

  delay(10000); //Wait for GSM
 
  Serial.println("Ready ..");
}

void loop()
{
  if(cell.available() > 0)              // Test if we receive something on the serial port.
  {
    char recv = cell.read();            // Read characters into recv
    inData += recv;                     // Fill inData with characters received (recv)
    if (recv == '\n')                   // If receiving a newline char
    {
      Serial.print("-------------\n");  // Print control chars (--) to ensure we have New Line and ready to compare
      Serial.print(inData);             // Print full string from inData
     
      if (inData == "xxx")              // Compare inData with "xxx"
      {
        Serial.print("OK!\n");          // If xxx is received, then print OK!
      }
      inData="";                        // Clear inData buffer
    }
  }
}


/*
>>>>>>>This is what I'm receiving from incomming SMS on Serial Monitor<<<<<<<

Ready ..

-------------


-------------
+CMT: "+4512312312","","13/05/07,13:56:24+08"

-------------
xxx

*/


wildbill

You're adding the newline character you receive to the end of the string, so your comparison fails. try checking against "xxx\n" instead, or don't concatenate the newline.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy