Arduino Mega 2560 Module GSM Serial.println problem

Hello,
I have problem with my program.
I tried to send an SMS received from my GSM module to my PC via the "Code_alarme" serial communication. However, the serial cannot display the message.
This is my code:

String message="";
int start=0;
int endsend=0;
String code_alarme="";
void setup() {

 // initialize both serial ports:

 Serial.begin(9600);

 Serial1.begin(9600);

 delay(2000);

 Serial1.println("AT"); 
 delay(100);
 Serial1.println("AT+CMGF=1"); 
 delay(100);
 // Set module to send SMS data to serial out upon receipt 
 Serial1.println("AT+CNMI=2,2,0,0,0");
   delay(100);
 
}

void loop() {

 // read from port 1, send to port 0:

 if (Serial1.available()) {

   //int inByte = Serial1.read();
   message=Serial1.readString();
  // Serial.println(message);
   if(message.indexOf("@")>=0)
   {
    // Serial.print(message);
    // Serial.println(message.length());
     start=message.indexOf("@");
     endsend=message.indexOf("!");
     //Serial.println(message.indexOf("@"));
     //Serial.println(message.indexOf("!"));
     code_alarme= message.substring(start, endsend); 
     Serial.println(code_alarme);
     delay(200);
   }
   //message="";
   start=0;
   endsend=0;
   //code_alarme="";
 }

}

Someone can help me.
Thank you in advance.

Please change the quote tags to code tags.

aarg:
Please change the quote tags to code tags.

I do it

message=Serial1.readString();

What is the message you are trying to parse. What do you expect to be printed with code_alarme?

I want to extract the received SMS in arduino and save it in a variable.

I want to extract the received SMS in arduino and save it in a variable.

message=Serial1.readString();
Serial.println(message);

Are you receiving any message?
Is your question about parsing information from the message?
If so, what is the message received, and what information do you want to extract and save?

yes I want to extract message

yes I want to extract message

If so, what is the message received, and what information do you want to extract and save?

Instead of String Object functions like .indexof(), I use the c-string functions like strstr(), strcmp(), strtok() and strcpy() to parse information from the received message, as a null terminated character array.

The specifics of how to do that will depend on the messages you receive, but you have refused to post any message, in spite of being asked several times.

Here is an example how I parse the number and message from a +CMT response

//+CMT: "+447xxxxxxxx","","18/09/21,20:54:59+04" SITE WHATS HAPPENING
//enter the message from serial monitor for testing
//actual modem rely message begins with<CR><LF>
char Grsp[100];//size for max message
char str1[30];//size appropriately for parsed characters
char str2[30];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;
void setup()
{
  Serial.begin(115200);
  //set ms timeout for Serial.readBytes()
  Serial.setTimeout(100);
}

void loop() {

  while (Serial.available() > 0)
  {
    //readBytes returns number read not zero referenced
    byte numChars = Serial.readBytes(Grsp, 100);
    Grsp[numChars] = '\0';//null Terminate
    newMessage = true;
    Serial.println(Grsp);
  }
//can define a series of prefix messages for match

  //if (strncmp(Grsp, "+CMT", 4) == 0 && newMessage == true)
  //change to accomodate lead characters <CR> <LF>
  if(strstr(Grsp, "+CMT") != 0 and newMessage == true)
  {
    Serial.println("prefixMatch +CMT");
    prefixMatch = true;
    newMessage = false;
  }
  else if (newMessage == true)
  {
    Serial.println("no prefixMatch");
    newMessage = false;
  }

  if (prefixMatch == true)
  {
    prefixMatch = false;
    char* strtokIndx;
    //find first "
    strtokIndx = strtok(Grsp, " \" ");//need excape for " delimiter
    //find second "
    strtokIndx = strtok(NULL, " \" ");
    strcpy(str1, strtokIndx); //characters between first set of ""
    for (byte i = 0; i < 3; i++) //skip3 " delimiters
    {
      strtokIndx = strtok(NULL, " \" ");
      Serial.println(strtokIndx);
    }
    //find ending character message
    strtokIndx = strtok(NULL, ""); //last " to end of string no delimiter \0 terminator
    strcpy(str2, strtokIndx + 1); //skip space preceding SITE
    
    Serial.print("Number = ");
    Serial.println(str1);
    Serial.print("Ending message = ");
    Serial.println(str2);
  }
}

Suggest you start with just printing what you receive.
Here I am using my SafeString library (available from the Arduino library manager) It does most of the work for you.
When you come to parse the SMS line, the SafeString char handling functions are safer to uses the the low level c-string methods. SafeString can output error msgs to show you were your char handling code when wrong

#include <SafeStringReader.h> 
// install SafeString library from the library manager
// see the tutorial https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

createSafeStringReader(sfReader,120,'\n');  // reads upto 100 char line terminated by \n'

void setup() {
  Serial.begin(115200); // <<<<<<<<<<< make debug output as fast as possible
  Serial1.begin(9600);  // << sms connection
  for (int i=10; i>0; i--) {
    Serial.priint(i); Serial.print(' ');
  }
  Serial.println();
  SafeString::setOutput(Serial); // turn on error msgs
  sfReader.conect(Serial1);  // read from SMS
}

void loop() {
  if (sfReader.read()) {
    // go a line of input
    sfReader.trim(); // remove \r
    Serial.println(sfReader);
    if (sfReader.startsWith("+CMT")) {
      // parse the data see the GPS parsing example in
      
    } else {
      // ignore
    }
    sfReader.clear(); // for next read
  }
}

Note carefully the Serial is set to 115200 so it is less likely to block your loop and cause you to miss the SMS msgs.
see Text I/O for the Real World for other solutions to this problem.

Actually the code above is too simplistic. Working with SMS boards can be tricky. Do you have to poll for each msg or have you set the board to just send stuff.
Depending on your board (which do you have) you may need to poke it to see if there is a received message stored and then request that stored message and delete it.
I suspect your problem is you are not asking the board to send you the received message. Check the cmds available for your board

cattledog:
Instead of String Object functions like .indexof(), I use the c-string functions like strstr(), strcmp(), strtok() and strcpy() to parse information from the received message, as a null terminated character array.

The specifics of how to do that will depend on the messages you receive, but you have refused to post any message, in spite of being asked several times.

Here is an example how I parse the number and message from a +CMT response

//+CMT: "+447xxxxxxxx","","18/09/21,20:54:59+04" SITE WHATS HAPPENING

//enter the message from serial monitor for testing
//actual modem rely message begins with
char Grsp[100];//size for max message
char str1[30];//size appropriately for parsed characters
char str2[30];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;
void setup()
{
 Serial.begin(115200);
 //set ms timeout for Serial.readBytes()
 Serial.setTimeout(100);
}

void loop() {

while (Serial.available() > 0)
 {
   //readBytes returns number read not zero referenced
   byte numChars = Serial.readBytes(Grsp, 100);
   Grsp[numChars] = '\0';//null Terminate
   newMessage = true;
   Serial.println(Grsp);
 }
//can define a series of prefix messages for match

//if (strncmp(Grsp, "+CMT", 4) == 0 && newMessage == true)
 //change to accomodate lead characters
 if(strstr(Grsp, "+CMT") != 0 and newMessage == true)
 {
   Serial.println("prefixMatch +CMT");
   prefixMatch = true;
   newMessage = false;
 }
 else if (newMessage == true)
 {
   Serial.println("no prefixMatch");
   newMessage = false;
 }

if (prefixMatch == true)
 {
   prefixMatch = false;
   char* strtokIndx;
   //find first "
   strtokIndx = strtok(Grsp, " " ");//need excape for " delimiter
   //find second "
   strtokIndx = strtok(NULL, " " ");
   strcpy(str1, strtokIndx); //characters between first set of ""
   for (byte i = 0; i < 3; i++) //skip3 " delimiters
   {
     strtokIndx = strtok(NULL, " " ");
     Serial.println(strtokIndx);
   }
   //find ending character message
   strtokIndx = strtok(NULL, ""); //last " to end of string no delimiter \0 terminator
   strcpy(str2, strtokIndx + 1); //skip space preceding SITE
   
   Serial.print("Number = ");
   Serial.println(str1);
   Serial.print("Ending message = ");
   Serial.println(str2);
 }
}

Thank you for your ansewer I tried , the code with GSM module but I can get an ansewer from the GSM Module connected to Serial1.
I used this code :

char Grsp[100];//size for max message
char str1[30];//size appropriately for parsed characters
char str2[30];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;

void setup() {
  Serial.begin(9600);
    Serial.setTimeout(100);
  Serial1.begin(9600);
  //set ms timeout for Serial.readBytes()
  Serial1.setTimeout(100);
 // gsminit();

}

void loop() {

  while (Serial1.available() > 0)
  {
  
    //readBytes returns number read not zero referenced
    byte numChars = Serial1.readBytes(Grsp, 100);
    delay(100);
    Grsp[numChars] = '\0';//null Terminate
    newMessage = true;
    Serial.print("Grsp :"); Serial.println(Grsp);
    
  }
//can define a series of prefix messages for match

  //if (strncmp(Grsp, "+CMT", 4) == 0 && newMessage == true)
  //change to accomodate lead characters <CR> <LF>
  if(strstr(Grsp, "+CIEV") != 0 and newMessage == true)
  {
    Serial.println("prefixMatch +CMT");
    prefixMatch = true;
    newMessage = false;
  }
  else if (newMessage == true)
  {
    Serial.println("no prefixMatch");
    newMessage = false;
  }

  if (prefixMatch == true)
  {
    prefixMatch = false;
    char* strtokIndx;
    //find first "
    strtokIndx = strtok(Grsp, " \" ");//need excape for " delimiter
    //find second "
    strtokIndx = strtok(NULL, " \" ");
    strcpy(str1, strtokIndx); //characters between first set of ""
    for (byte i = 0; i < 3; i++) //skip3 " delimiters
    {
      strtokIndx = strtok(NULL, " \" ");
      Serial.println(strtokIndx);
    }
    //find ending character message
    strtokIndx = strtok(NULL, ""); //last " to end of string no delimiter \0 terminator
    strcpy(str2, strtokIndx + 1); //skip space preceding SITE
   
    Serial.print("Number = ");
    Serial.println(str1);
    Serial.print("Ending message = ");
    Serial.println(str2);
  }
}

void gsminit()
{
  delay(5000);
  Serial1.println("AT"); // Lancement module GMS
  Serial1.println("AT+CMGF=1"); // Lancement mode SMS
  Serial1.println("AT+CNMI=2,2,0,0,0"); // En attente reception
}

The consol text :

12:16:28.440 -> Grsp :
12:16:28.440 -> no prefixMatch
12:16:28.682 -> Grsp :
12:16:28.888 -> Grsp :
12:16:28.922 -> no prefixMatch
12:16:29.128 -> Grsp :
12:16:29.369 -> Grsp :
12:16:29.369 -> no prefixMatch
12:16:29.608 -> Grsp :
12:16:29.848 -> Grsp :
12:16:29.848 -> no prefixMatch
12:16:30.086 -> Grsp :
12:16:30.359 -> Grsp :
12:16:30.568 -> Grsp :
12:16:30.774 -> Grsp :
12:16:30.808 -> no prefixMatch

Thank you for your ansewer I tried , the code with GSM module but I can get an ansewer from the GSM Module connected to Serial1.

12:16:28.440 -> Grsp :

There may be a language issue, as you can NOT get an answer from the GSM module connected to Serial1.

The Mega appears to be receiving something as it enters the section but there is no printable message.

while (Serial1.available() > 0)

Serial1 is correct for the connection between the module and the Mega. You have been asked several times if you are receiving a message, and I have not been clear on your answer. You said to want to break down/analyse/parse the received message, but now I don't see any message on which you can do that.

If you have ever received a message over the GSM module, can you please provide the code and wiring diagram which did that.

I suggest you start over with this tutorial about wiring and power. It is written for a UNO, but you will use Serial1 of the Mega instead of software serial. As well as providing correct and adequate power, make sure to connect the Rx to Tx and Tx to Rx. They are cross connected.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.