Help with SoftwareSerial read string

Hey!!

I having an sim900 GPRS shield and having some issue with it. I communicate with it by SoftwareSerial. When I use my phone to call the sim900, it serial print “RING”, I want to use that to something. For example to turn on an LED when the sim900 is ringing. I have read the topics about serial.readString but can’t get it to work. Here is my code that I have used:

#include <SoftwareSerial.h>

SoftwareSerial sim900(2, 3);

String simRead;
int led = 14;

void setup() {
  // put your setup code here, to run once:
  sim900.begin(19200);
  Serial.begin(19200);

  pinMode(led,OUTPUT);
}

void loop() {  
  if (sim900.available() > 0){
    simRead = sim900.readString();

    if (simRead == "RING"){
      digitalWrite(led,HIGH);
      delay(500);
    }
  }
}

Please help!

Thanks!
-HansDia

try printing what is read from the software serial to the serial monitor (Serial.print) to see what string is actually pick up

1 Like

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

When I use my phone to call the sim900, it serial print “RING”

You have offered NO proof of that. I’d guess that it prints “RING” or “RING” or “RING”. If you insist on using Strings, you MUST become familiar with the trim() method. And, you need to stop anonymously printing crap. Print something before and after every string or String, so you KNOW exactly what is in the string or String.

Thanks for the answers, but I can't understand how.

I want it to do:

simRead = sim900.readString();

If (simRead = "RING"){
digitalWrite(led,HIGH);
delay(500);

}

if (simRead = "NO CARRIER"){  //sim900 serial print NO CARRIER when the phone call is hang up
digitalWrite(led,LOW);
delay(500);
}

Thanks again! :wink:
-HansDia

= is an assignment
== is a compare

Thanks for the answers, but I can't understand how.

You need to read up on how serial data is supposed to be handled.

The available() method tells you how much data is in the buffer, waiting to be read.

The read() method gets ONE byte from the buffer.

The readString() method uses some terminator to determine when to stop reading, or it stops when it has seen no new data for a while. The terminator, if found, IS added to the String that is returned. The String will NOT be just "RING" until you strip the terminator off.

You do NOT compare Strings using =. You can compare them using ==.

The if statement is NOT capitalized.