Sending GPS data via SMS with GSM PLEASE HELP!

Hello,

I am doing a project in college where I am trying to receive a gps location when requested for via sms text message. The equipment I have to test my code is: Arduino mega, (GSM) SIM800L, (GPS) GY-NEO6…

I have made my own PCB and will be assembling all of the components together shortly but are having trouble with the program.

So far I have been able to send an sms from the gsm using this code:

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup() 
{
  Serial.begin(9600);
  Serial.println("GSM TEST.");
  
  if (gsm.begin(2400))
  {
    Serial.println("\nstatus=READY");
    started=true;  
  }
  
  else Serial.println("\nstatus=IDLE");
  
  if(started)
  {
    
    if (sms.SendSMS("+44----------------", "MESSAGE FROM ARDUINO"))
      Serial.println("\nSMS sent OK");
  }

};

void loop() 
{
  if(started){
    
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
    }
    delay(1000);
  }
};

And are able to read the GPS data via the NEO using this code:

#include <SoftwareSerial.h>
 
//Create software serial object to communicate with GPS
SoftwareSerial gps(51, 50);
 
void setup() {
  //Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  while(!Serial);
   
  //Being serial communication with Arduino and GPS Module //Important rate must be 9600
  gps.begin(9600);
  delay(1000);
   
  Serial.println("Setup Complete!");
}
 
void loop() {
  //Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
  if(gps.available()){
    Serial.write(gps.read());
  }
  //Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
  if(Serial.available()){    
    gps.write(Serial.read());
  }
}

But I am having trouble combining both of the codes together to do what I need and have been searching online and trying different things for weeks.

I read somewhere that it is better to use hardware serial because I am using an Arduino Mega, which is confusing me a little more as most of the tutorials online use Uno’s with software serial. I am unsure if it is better to use software serial or hardware serial.

I also tried this code that I found in another post but it’s only showing the gps data in the serial monitor, no message is being sent etc:

// GSM
#define gsmPort Serial2 // GSM TX connected to Arduino RX2 pin 17,
                        // GSM RX connected to Arduino TX2 pin 16

// GPS
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial1 // GPS TX connected to Arduino RX1 pin 19

void setup()
{
  gsmPort.begin( 9600 );
  Serial .begin( 9600 );
  gpsPort.begin( 9600 );
}


void loop()
{
  // For testing, take commands from the Serial Monitor window
  if (Serial.available()) {
    switch(Serial.read()) {
      case 's':
        SendMessage();
        break;
    }
  }

  // Process GPS characters and update the fix structure when
  //    new information arrives.
  if (gps.available( gpsPort )) {
    fix = gps.read();

    // print whatever you want (or nothing)
    Serial.print( F("Loc:") );
    if (fix.valid.location) {
      Serial.print( fix.latitude(), 6 );
      Serial.print( ',' );
      Serial.print( fix.longitude(), 6 );
    }
    Serial.println();
  }
}


void SendMessage()
{
  gsmPort.println( F("AT+CMGF=1") );    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  gsmPort.println( F("AT+CMGS=\"+===========\"\r") ); // Replace x with mobile number
  delay(1000);
  gsmPort.println( F("Hello") );// The SMS text you want to send
  gsmPort.print( fix.longitude(), 5);
  gsmPort.print( ',' );
  gsmPort.print( fix.latitude(), 5 );
  delay(100);
  gsmPort.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

Any help would be greatly appreciated!

Thank you

With 4 hardware serial ports, why are you creating a SoftwareSerial port?

Why change completely how you read GPS data when you changed the port and used Serial2? Try the middle code, but get rid of the instance called gps, and replace gps with Serial2. Does THAT work? There is no point in trying to use the data until you can GET the data.

I am unsure if it is better to use software serial or hardware serial.

Like PaulS suggested, HardwareSerial is always better than SoftwareSerial.

I would suggest getting the first GSM sketch working on a HardwareSerial port (Serial1, Serial2 or Serial3).

When that works, configure the third NeoGPS sketch so it works on a different HardwareSerial port (not the same as the GSM).

Then you can merge the two programs.

The first program shows you how to send SMS messages: gsm.begin, then sms.SendSMS.

The third NeoGPS program shows you how to get GPS updates, once per second: gps.available, then gps.read.

You have to decide

  • How often to send an SMS. You can use the 1-second GPS fixes to measure the time interval:
int fixCount 0 = 0;

void loop()
{
  if (gps.available( gpsPort )) {
    fix = gps.read();
    fixCount++;  // increment the 1-second "clock"

    if (fixCount >= SMS_INTERVAL) {  // how often?
      fixCount = 0; // reset clock

      // format fix.xxx into a char array for sending

      // sms.SendSMS( ... )
    }
  }
}
  • What GPS information you want to put in the message. You can Serial.print the fix fields to make sure you know how to format the message. When it looks good, learn how to put those bytes in a char array and then print just the char array. When that works, you can pass that char array to sms.SendSMS.

Try some things, and ask questions when you get stuck. Be sure to post your new code or error messages (in code tags).

Cheers, /dev