Need help with GSM module with arduino

i have got my arduino to work with the gsm900 module.
i wrote a code that send the messg sent from a phone on my serial moniter.
the problem what im facing is that the messg recived in the serial moniter comes with the messg and a string of data. like the time stamp and the mode and also the senders number. i just need olny the number that i send from my phone to the module. so that i can save it as a string value. i dont want any other data. because if i have to save the data as a string it saves all data. i just want the number that i send.
it would be grateful if anyone could help me out.
any type of hints or suggestions will do.

If you get the data in a cString or a String you can parse it to extract the relevant content.

#include <EEPROM.h>

#include <SoftwareSerial.h>
String Mobile = "";
SoftwareSerial gsmSerial(6, 7);
void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
  }
}
String readStringFromEEPROM(int addrOffset)
{
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];
  for (int i = 0; i < newStrLen; i++)
  {
    data[i] = EEPROM.read(addrOffset + 1 + i);
  }
  data[newStrLen] = '\ 0'; // !!! NOTE !!! Remove the space between the slash "/" and "0" (I've added a space because otherwise there is a display bug)
  return String(data);
}

void setup() {
  Serial.begin(9600);
  gsmSerial.begin(9600);
  gsmSerial.println("AT+CNMI=2,2,0,0,0");
}
void loop() {
  while (gsmSerial.available()) {
    gsmSerial.read();
  }

  get_gsm();
}
void get_gsm()
{

  gsmSerial.listen();



  while (gsmSerial.available() > 0)
  { Serial.println("active");
    if (gsmSerial.find("Change"))
      delay(30000);
    gsmSerial.read();
    delay(1000);
    while (gsmSerial.read() == 0) {}
    Mobile = gsmSerial.readString();
    writeStringToEEPROM(0, Mobile);

this my code
this is the string i get .its all in numericals.
+CMT: "+senders phone number","","date,time"
recived messg
i want the reciverd messg to be take out and be saved as a string.

Noticed that ?

It does not happen if you use code tags…

So please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s unreadable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

—-

i want the reciverd messg to be take out and be saved as a string.

Seems to me it’s everything after the first new line …

Thanks alot sir for being so helpful and im sorry about the code part.
and yes everything after the first new line.

So if you keep the answer in a string, search for the position of ‘\n' using indexOf() and then extract the substring (using substring() ) from the next character till the end

Thanks alot sir for the help i got it to work.
now i can diffrentiate the messg recived and take out required data from it.
i would like to ask you somthing.
is it possible to input a string in a argument.
eg.
mySerial.println("AT+CMGS="+ZZxxxxxxxxxx"");

i used the saved string to write to EEPROM.
and i want to input that data from the EEPROM in the place of (+ZZxxxxxxxxxx).
and again
thanks alot for the help and time.

Sure, the easiest way is to print in multiple statements

mySerial.print("AT+CMGS=");
mySerial.println(your_string__here);
void loop() {
 
  gsmSerial.read();
  if (gsmSerial.find("hello"))
  {
    Serial.println("hello");
}


  if (gsmSerial.find("light"))
  {
    Serial.println("light");
}
  }

here i hav another problem.
in this void loop im trying to run two if statement in which the input is from a gsm module. the problem im facing here is that the code compiles but they dont run some times. mostly if one runs the other doesnot. its random.

i think im not running the gsmSerial.read in a loop or im stuck in an if statement when it run.

The challenge is that you listen for hello until timeout and if you did find it then print it but you don’t listen for light whilst you are expecting hello…

What you need to write is a custom function that will wait for some keywords. One way to do so if there are no end marker is to listen and accumulate the data into a rolling buffer and keep checking against the presence of keywords. The buffer needs to be at least as long as the longest keyword

void loop()
{
  while (gsmSerial.available()) {
    gsmSerial.read();
    hello();
    light();
  }

can i do this.
i have tryd it but i think im not doing it the right way.
i added this in my void loop
and added both the if statement in a void function.

I’m not sure what the functions do but the read() would just eat one byte and not store it anywhere so answer is likely No…

I think you are missing some understanding about how the serial line works, I would suggest to study Serial Input Basics to handle this

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