String Delcaration and Comparison

I am trying to control some electronics using text messaging. I am using a SIM900 GSM Shield. My problem comes when I am trying input the text message as a string and compare it to another.

I tried to do what I wanted by comparing each character of the message. It is very hard. I got it to work but I hated making changes to my code because it took me several minutes each time just to figure out what I had done. The idea of using string comparison seems a little bit more confusing for me to understand but in the end I am sure it will be worth it. I am having troubles though. For some reason my program is having a hard time recognizing equivalent strings. I kind of this that this is probably because I am declaring the strings wrong. For some reason when I text it "on:" it spits back out on the serial port a whole mess of numbers. Some of them being

111
110
58

These turn out to be the decimal representation of o n : I assume that the reason they are coming in separate is because I must be doing something wrong.

How to I properly declare compare these strings. (see last 10 lines or so of code)

Thanks,
Jake

// Example 55.4
 
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int sensorPin = A0;
int sensorValue = 0;
 
void setup()
{
  Serial.begin(19200); // for serial monitor
  SIM900.begin(19200); // for GSM shield
  SIM900power();  // turn on shield
  delay(20000);  // give time to log on to network.
 
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100);
}
 
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
  Serial.println("ready");
}
 
void loop()
{
  // Now we simply display any text that the GSM shield sends out on the serial monitor
  if(SIM900.available() >0)
  {
    String incoming_string=String(SIM900.read()); //Get the character from the cellular serial port.
    Serial.println(incoming_string); //Print the incoming character to the terminal.
    if(incoming_string=="on:")digitalWrite(13, HIGH);
    if(incoming_string=="off:")digitalWrite(13, LOW);
    if(incoming_string=="sensor:")
      {
        sensorValue = analogRead(sensorPin);
      }
    
  }
}

Look into char arrays and parsing strings. There are many examples you can find with a quick search.

If you are capturing a String, then you can test the captured String using the indexOf() function like below.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}
    String incoming_string=String(SIM900.read()); //Get the character from the cellular serial port.
    Serial.println(incoming_string); //Print the incoming character to the terminal.
    if(incoming_string=="on:")digitalWrite(13, HIGH);

If the first line above really does read one character then it is never going to equal "on" in the third line.

UKHeliBob:

    String incoming_string=String(SIM900.read()); //Get the character from the cellular serial port.

Serial.println(incoming_string); //Print the incoming character to the terminal.
    if(incoming_string=="on:")digitalWrite(13, HIGH);



If the first line above really does read [u]one[/u] character then it is never going to equal "on" in the third line.

That makes sence but then how do i make that linegrab the whole text message?

That makes sence but then how do i make that linegrab the whole text message?

You call read() in a loop, until the end of message marker arrives.

zoomkat:
If you are capturing a String, then you can test the captured String using the indexOf() function like below.

Zoomkat and the rest of the world,

I was able to implement the sample code you gave me. It works well to get the string running.

The problem I am having now is that every time I send a text message it sends me extra stuff like...

+CMT: "+13852440322","","14/01/13,16:25:22-28"
on:
+CMT: "+13852440322","","14/01/13,16:25:34-28"
off:
+CMT: "+13852440322","","14/01/13,16:26:11-28"
why won't this work

for some reason when I use your indexOf it can't find the on: or off: command. WHY? I even tried different comparisons like lastIndexOf and endsWith I even tried a trim function with the endsWith but with no luck. I really don't understand it couldn't find the on: in the string..? If you could help me figure this out this would be helpful.

I would also like to make this program a state machine in the future so I could throw in a time component like "on12.25hr" I see you have a dela(3) in your code. Will that still work with a state machine? Also for more future how could I extract a number out of my string i.e. (on12.25hr) and use it to set the timer? I don't think the toInt() function will work seeing how I have characters.

my questions come with an order of importance but help with any of them would be great!

Here is my code.

// Example 55.4
 
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int sensorPin = A0;
int sensorValue = 0;
int ledPin = 13;
String readString;
 
void setup()
{
  Serial.println("Start-up");
  Serial.begin(19200); // for serial monitor
  SIM900.begin(19200); // for GSM shield
  SIM900power();  // turn on shield
  delay(1000);  // give time to log on to network.
 
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(1000);
  Serial.println("Ready to Recieve\n");
}
 
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);

}
 
void loop()
{
  // Now we simply display any text that the GSM shield sends out on the serial monitor
  while(SIM900.available())
  {
    delay(3);  
    char c = SIM900.read();
    readString += c; 
  }
  if (readString.length() >0) 
  {
    readString.trim();
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  }
}

The problem I am having now is that every time I send a text message it sends me extra stuff like...

The code is probably just printing out what is actually being sent to the arduino. You can try the code below and see if the "setting LED pin HIGH" and "setting LED pin LOW" get printed to the serial monitor. This would indicate the "on" and "off" are being detected. If these get printed out and the pin 13 LED does not change state, then you need possibly try another pin. If you don't get the serial monitor printout, then you may need to use a delimiter to determine the end of the data being sent back (your code has a lot of delays).

// Example 55.4
 
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int sensorPin = A0;
int sensorValue = 0;
int ledPin = 13;
String readString;
 
void setup()
{
  Serial.println("Start-up");
  Serial.begin(19200); // for serial monitor
  SIM900.begin(19200); // for GSM shield
  SIM900power();  // turn on shield
  delay(1000);  // give time to log on to network.
 
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(1000);
  Serial.println("Ready to Recieve\n");
}
 
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);

}
 
void loop()
{
  // Now we simply display any text that the GSM shield sends out on the serial monitor
  while(SIM900.available())
  {
    delay(3);  
    char c = SIM900.read();
    readString += c; 
  }
  if (readString.length() >0) 
  {
    readString.trim();
    //Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("setting LED pin HIGH");
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
      Serial.println("setting LED pin low");
    }

    readString="";
  }
}