Arduino Mega - GSM Module - GPS Module

Hello everyone.

I am new in Arduino programming as you will understand from the simple code which i use.

After reading a lot, i'm still confused about the results. My project is to send SMS "W" to the GSM module, and receive an answer that will have the location in url form.
I use:

  • Arduino Mega 2560
  • GSM Module SIM900
  • GPS Module NEO6M

Using the Modules separately, i can send a simple message or get information about GPS in serial.
However, the problem is when i combine them. When i send SMS "G", i receive a blank message. But when the automated message is a string, i receive the string.

I think that i am close to the solution but i can't understand how to modify correclty my code as all my tries had no results.
I appreciate someone's help, as i am stuck and can't make the project more complicated with other componets (For instance addiing a fingerprint sensor, a temperature sensor, upload values on a server etc).
Maybe there are some silly mistakes, so i wait for your help on how the code will work.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define GSM_Module Serial1
#define GPS_Module Serial2 


char SMS_Receive;


TinyGPSPlus GPS;
String Latitude;
String Longitude;
String url_GoogleMAPS;


void setup()
{
  Serial.begin(9600);
  GPS_Module.begin(9600);
  GSM_Module.begin(9600);
}

void loop()
{
if (SMS_Request())
  {
      SMS_Send();
  }
  delay(10); 
}


void SMS_Send()
{
  GSM_Module.println("AT"); 
  updateSerial();
  GSM_Module.println("AT+CMGF=1"); 
  updateSerial();
  GSM_Module.println("AT+CMGS=\"+XXXXXXXXX\"");
  updateSerial();
  GSM_Module.print(url_GoogleMAPS); 
  updateSerial();
  GSM_Module.write(26);
}


void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    GSM_Module.write(Serial.read());
  }
  while(GSM_Module.available()) 
  {
    Serial.write(GSM_Module.read());
  }
}


boolean SMS_Request() 
  {
  if(GSM_Module.available() >0) 
    {
    SMS_Receive=GSM_Module.read();
    if(SMS_Receive=='W') 
      {
      delay(10);
      Serial.print(SMS_Receive);
      SMS_Receive=GSM_Module.read();
              Serial.print("Request received. \n");
              return true;
       }
     }
  return false;
}


void GPS_Info()
{
    while (GPS_Module.available() > 0)
    {
    GPS.encode(GPS_Module.read());
    if (GPS.location.isUpdated())
      {
     
      Serial.print("Latitude= "); 
      Serial.print(GPS.location.lat(), 6);  
      Latitude = String(GPS.location.lat(), 6);
      Serial.print(" Longitude= "); 
      Serial.println(GPS.location.lng(), 6); 
      Longitude = String(GPS.location.lng(), 6);
      url_GoogleMAPS = String("https://maps.google.com/?q=" + Latitude + "," + Longitude);
      Serial.println(url_GoogleMAPS);   
    }
  }
}

Can anyone guide me? The problem is probably in using the two modules together? How should I edit the code in order to have results?

You could jump forward ten years, and try the newer chipsets like 5320 and 7600 which have the navigation in the one single chip and command interface.

Depending on where you live, they’ll work for at least three years longer than your 2G solution.

lastchancename:
You could jump forward ten years, and try the newer chipsets like 5320 and 7600 which have the navigation in the one single chip and command interface.

Depending on where you live, they’ll work for at least three years longer than your 2G solution.

Thank you for the answer.

I want to complete my project with Arduino Mega and the two modules. Is there any possibility to make it work?

I think running two instances of Software Serial simultaneously could be a problem.

aarg:
I think running two instances of Software Serial simultaneously could be a problem.

So the code that i provide in my first post would never have the wanted results and is faulty? Or making some changes it might work? If yes, can you help me please?

About a month ago, I started this project with Arduino Uno... After some research in internet, I read that I should use the Arduino Mega which has 4 serial ports. Unfortunately, i can't have any results.

When i send SMS "G", i receive a blank message.

Is your serial printing showing that SMS_Request() is receiving your letter? The function is actually testing for a 'W' and not a 'G'.

If not, here's a recent thread where the OP was using the same type of receiving function with no success. A solution is presented there.
https://forum.arduino.cc/index.php?topic=672944.0

cattledog:
Is your serial printing showing that SMS_Request() is receiving your letter? The function is actually testing for a 'W' and not a 'G'.

If not, here's a recent thread where the OP was using the same type of receiving function with no success. A solution is presented there.
Arduino Nano GSM SMS Temp Request and Alarm - Programming Questions - Arduino Forum

Thank you for your responce, you are right about the letter.
Relying on my project's goals and the code that you suggested me here, I have another problem. I receive a SMS with the google maps link but the Latitude-Longitude are 0. The link that my phone receives is "Google Maps".

The code is working well but as @aarg wrote, these two modules cannot be used simultaneously. As a result, the coordinates are zero.
Can I find any solution for that?

The new code that I tried is:

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

#define GSM_Module Serial1 
#define GPS_Module Serial2 


TinyGPSPlus GPS;
double Latitude;
double Longitude;



char incomingChar; // Create variable to store incoming SMS characters


 
void setup()
{

  Serial.begin(9600);
  GSM_Module.begin(9600);
  GPS_Module.begin(9600);

  Serial.println("Initializing..."); 
  delay(1000);

  GSM_Module.print("AT+CMGF=1\r\n");
  delay(100);
  GSM_Module.print("AT+CNMI=2,2,0,0,0\r\n");
  delay(1000);


}

void loop()
{
    delay(2000); 
if (SMS_Request())
  {
      SMS_Send();
  }
  delay(100); 
}





void SMS_Send()
{
  GPS_Info();
  GSM_Module.print("\r");
  delay(1000);                 
  GSM_Module.print("AT+CMGF=1\r");   
  delay(1000);
  GSM_Module.println("AT + CMGS = \"+XXXXXXXXXX\"\r");
  delay(1000);    
  GSM_Module.print( "https://maps.google.com/?q=" + String(Latitude) + "," + String(Longitude));   //The text of the message to be sent
  delay(1000);
  GSM_Module.write(0x1A);
  delay(1000);
}



boolean SMS_Request()
{
if (GSM_Module.available() > 0) //If there is stuff in the buffer
{
  char textMessage[100] = {};
  byte numChars = GSM_Module.readBytes(textMessage, 100);//.readBytes returns number read
  textMessage[numChars] = '\0';//null Terminator
  Serial.println(textMessage); // Print the new message
  char * pch = strchr(textMessage, 'Q'); //pch is pointer to 'Q'
  if (pch) //found 'Q', pch is not NULL
  { Serial.println("found 'Q'");
    //send outgoing sms
    SMS_Send();
  }
  else
    Serial.println("No 'Q' found in message");
}
  return false;
}



boolean GPS_Info()
{

    while (GPS_Module.available() > 0)
    {
    GPS.encode(GPS_Module.read());
    if (GPS.location.isUpdated())
      {
     
      // Latitude in degrees
      Serial.print("Latitude= "); 
      Serial.print(GPS.location.lat(), 6);  
      Latitude = GPS.location.lat(), 6;

      // Longitude in degrees 
      Serial.print(" Longitude= "); 
      Serial.println(GPS.location.lng(), 6); 
      Longitude = GPS.location.lng(), 6;
      
    }
  }
}

aarg:
I think running two instances of Software Serial simultaneously could be a problem.

You appear to be running your modules on Serial1 and Serial2 and you probably do not need this #include <SoftwareSerial.h>

If both units work separately, then you probably have the wiring correct for each module to its hardware Serial port.

I do not understand your exact issue.

What does GPS_info give you for Lat and Long?
Are the values not incorporated properly into this

 GSM_Module.print( "https://maps.google.com/?q=" + String(Latitude) + "," + String(Longitude));   //The text of the message to be sent

A serial print out for debug might be helpful.