How to recive SMS from gsm modem?

Hello, All!

I don't good programmer, and can't finde example of code for recive SMS. (Target - on & off lite from SMS).

Do you have examle?

My creazy code for read response from gsm modem, but it dont work :slight_smile: Can you help me? Please.

char serInString[]="";

int led2 = 13;

void readSerialString (char strArray) {
int i = 0;
if(Serial.available()) {
// Serial.print("reading Serial String: "); //optional: for confirmation
while (serialAvailable()){
strArray = Serial.read();
_ //if( (strArray == 'K') && (strArray[i-1] == 'O') )_
_
digitalWrite(led1, HIGH);_
_
i++;_
_
Serial.print(strArray[(i-1)], BYTE); //optional: for confirmation*_
* //Serial.print("\r\n"); *
}
if (strArray == "RING") {digitalWrite(led2, HIGH);}
//digitalWrite(led2, HIGH);
_ if (strArray != 0) {
while(strArray != 0) {
//Serial.print( strArray );
strArray = 0; // optional: flush the content
* i++;
}
}
//Serial.println(); //optional: for confirmation*

* }
}
void printSerialString(char *strArray) {
int i=0;
if (strArray != 0) {
while(strArray != 0) {
//Serial.print( strArray );
strArray = 0; // optional: flush the content
i++;
}
}
}
void setup() {
Serial.begin(9600);
// pinMode(led1, OUTPUT); // declare LED as output*

// pinMode(led2, OUTPUT); // declare LED as output
}
void loop () {
* readSerialString(serInString);
delay(1000);
}*_

It would really help if you could say in exactly what way it doesn't work. A clear description of what you expect it to do, and what it actually does, would let us try to debug the code.

But one problem that I can see right away from the code is that you define the global 'serInString' as a character array with just one element, an end-of string character. Then, you try to read data into that string. Well, if the array is only one byte long, you'll overwrite memory rather badly! You need to decide how long the maximum string will be, and allocate that many bytes, plus room for the terminating byte. If you think 64 characters will be enough:

char serInString[65] = "";

That declaration makes a string variable with enough room to expand. Remember, C does not support dynamically-resized arrays.

Thank you, Anachrocompute[ch1082]!
Now, i change code. It work, but onley if i print in Serial. (see comments in code)
How to compare correctly? (RING -> onLed, off->offLed)

char serInString[65]="";
char ring[5]="RING";
char offLite[4]="off";
int ringCounter=0;
int offLiteCounter=0;

int led2 = 13;

void readSerialString (char strArray) {
ringCounter=0;
offLiteCounter=0;
int i = 0;
if(Serial.available()) {
// Serial.print("reading Serial String: "); //optional: for confirmation
while (serialAvailable()){
strArray = Serial.read();
_ //if( (strArray == 'K') && (strArray[i-1] == 'O') )_
_
i++;_
_
Serial.print(strArray[(i-1)], BYTE); //if i comment this, not wotking :frowning:_
_
Serial.print(ring[(i-1)], BYTE); //if i comment this, not wotking :(*_

* if ((strArray[(i-1)]) == (ring[(i-1)])) {ringCounter++; if (ringCounter == 4) {digitalWrite(led2, HIGH);}}*
* if (strArray[(i-1)] == offLite[(i-1)]) {offLiteCounter++; if (offLiteCounter == 3) {digitalWrite(led2, LOW);}}*

* Serial.print("\r\n"); //if i comment this, not wotking :(*
}
*//Serial.print(strArray[65], BYTE); *
//if (strArray[65] == ring[5]) {digitalWrite(led2, HIGH);}
//digitalWrite(led2, HIGH);
_ if (strArray != 0) {
while(strArray != 0) {
//Serial.print( strArray );
strArray = 0; // optional: flush the content
* i++;
}
}
//Serial.println(); //optional: for confirmation*

* }
}
void printSerialString(char *strArray) {
int i=0;
if (strArray != 0) {
while(strArray != 0) {
//Serial.print( strArray );
strArray = 0; // optional: flush the content
i++;
}
}
}
void setup() {
Serial.begin(9600);
// pinMode(led1, OUTPUT); // declare LED as output*

// pinMode(led2, OUTPUT); // declare LED as output
}
void loop () {
* readSerialString(serInString);
}*_