Interfacing GSM 900 and GPS using Arduino UNO

Hello all,

In my project I'm trying to interface GSM 900 module with L80 GPS module in such a way that when pin 12 of Arduino is high, then using GSM, the present coordinate of the location where the user is present has to be send to the predefined number. Below I'm pasting GSM and GPS codes, please help me to club those for my requirement.

Thank You,

//GSM

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10); //  RX, TX

void setup()
{
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}


void loop()
{
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'r':
      RecieveMessage();
      break;
  }

 if (mySerial.available()>0)
   Serial.write(mySerial.read());
}


 void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("Accident Alert");// The SMS text you want to send
  delay(100);
   mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}


 void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
 }
//GPS
#include <TinyGPS.h>

TinyGPS gps;  //Creates a new instance of the TinyGPS object


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

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (Serial.available())
    {
      char c = Serial.read();
      //Serial.print(c);
      if (gps.encode(c)) 
        newData = true;  
    }
  }

  if (newData)      //If newData is true
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);   
    Serial.print("Latitude = ");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" Longitude = ");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);

  }
 
  Serial.println(failed);
 // if (chars == 0)
   // Serial.println("** No characters received from GPS: check wiring **");
}

What does "club those" mean?

ieee488:
What does "club those" mean?

I mean, I wanted to combine those GSM and GPS codes together to work for my specification mentioned above.

Which Arduino do you have?

Something like this would work on an UNO:

//GSM

//---------------------
//  Pick a serial port for the GPS

// BEST choice is a HardwareSerial port:
//    Use Serial1 on a Mega, Leo or Due board
//    You could use Serial on any board (disconnect GPS
//       when uploading over USB).
//#define gsmPort Serial1

// 2nd best:
//#include <AltSoftSerial.h>
//AltSoftSerial gsmPort; // must be on specific pins (8 & 9 for an UNO)

// 3rd best: must be baud rate 9600, 19200 or 38400
#include <NeoSWSerial.h>
NeoSWSerial gsmPort(9, 10);

// Worst: SoftwareSerial NOT RECOMMENDED
//---------------------

#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial // GPS TX connected to Arduino RX pin 0

void setup()
{
  gsmPort.begin(9600);
  Serial.begin(9600);
  //gpsPort.begin( 9600 ); // Not needed if GPS on pin 0
}


void loop()
{
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'r':
      RecieveMessage();
      break;
  }

 if (gps.available( gpsPort ))
   fix = gps.read();
}


 void SendMessage()
{
  gsmPort.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  gsmPort.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); // Replace x with mobile number
  delay(1000);
  gsmPort.println("Accident Alert");// 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);
}


 void RecieveMessage()
{
  gsmPort.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
 }

SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch. This sketch uses my NeoSWSerial library. AltSoftSerial would be even better, if you could use pins 8 & 9 for the GSM module.

This sketch also uses my NeoGPS library. It is smaller, faster and more accurate than all other GPS libraries. If you want to try it, NeoGPS and NeoSWSerial are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

Cheers,
/dev

Thanks for the support.
But, I need to get the location coordinates only when pin 12 of my Arduino is ON, i.e 5V. How to I alter below code to get the above mentioned specification.

switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'r':
      RecieveMessage();
      break;
  }

Do you mean

  if (digitalRead( 12 ))

Maybe you could try some things...

While uploading to my Arduino UNO board, I'm getting this error, please help me to solve this.

Sketch uses 9022 bytes (29%) of program storage space. Maximum is 30720 bytes.
Global variables use 502 bytes (24%) of dynamic memory, leaving 1546 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x0e
Problem uploading to board.  See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x0e
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x0e
1 Like

From How to Use the forum, section 11:

Describe how you have connected things like switches. Are they wired to ground? Or +5V? Are there pull-up or pull-down resistors? Post a circuit if there is doubt.

I suspect you have something connected to RX pin 0.

Thanks. I got it right.

Hi.. I am facing a problem while interfacing GSM SIM900 module and GPS module with Arduino NG board with Atmega 168 microcontroller. I am connecting both GSM and GPS modules via 2 different Software Serial ports at a time. The program is using .listen() function to access each of the ports at two different stages. Unfortunately my GSM module (which is called first) is not responding with AT commands. The program worked fine while the GPS module was not interfaced and only one Software Serial was used.

What can be causing the problem?

Is it not possible to use 2 Software Serials with the Arduino NG board?

Should I change the microcontroller to Atmega 328 or others?

As discussed in the other topics in the blog , is the use of hardware serial for one device and Software serial for the other the only solution ?

Please share your suggestions.

Is it not possible to use 2 Software Serials with the Arduino NG board?

It is not.

is the use of hardware serial for one device and Software serial for the other the only solution ?

As stated in Reply #3, it is BEST to use Serial for one device and AltSoftSerial for the other device. Additional information here. It says:

NeoSWSerial can be used with AltSoftSerial at the same time, allowing your sketch to have two extra serial ports.

You must be aware that while they can receive at the same time, transmitting on NeoSWSerial blocks receiving on AltSoftSerial. Transmitting on AltSoftSerial does not block anything. If your sketch must transmit on both and receive on both at the same time, this will not work for you.

Understanding the timing of the two devices's TX and RX is crucial. Do the devices send at any time, perhaps when the Arduino is transmitting? Is there a way to avoid simultaneous TX and RX (see this for GPS behavior).

Cheer,
/dev

@Dev .. Thanks for the suggestion :slight_smile: I will try using AltSoft Serial instead of Software Serial for one of the devices.

This is how my project works: Once a passcode is received by the GSM module, the current latitude and longitude coordinates read by the GPS module are sent as an SMS to the sender's mobile. Hence the simulataneous TX and RX by the GPS and GSM modules are not required.

Hence I guess connecting the GSM module to AltSoft Serial and GPS module to the hardware serial ports (after uploading the program) would not be an issue. Correct me if I'm wrong! :slight_smile:

And another question: Is not possible to use Atmega 328 with Arduino NG board?

I'm unable to upload any programs to this combination. The board works only with Atmega 168. Atmega 328 is observed to be working with Arduino Pro/Mini boards but not with Arduino NG.

Please advice..

connecting the GSM module to AltSoft Serial and GPS module to the hardware serial ports (after uploading the program) would not be an issue.

Correct.

This is how my project works:

Since you are using GSM RX/TX (at any time) and GPS RX (no TX), you could use AltSoftSerial for GSM and NeoSWSerial for GPS. You could still use GPS TX during setup to configure the GPS if necessary, blocking GSM for a short time. After that, you won't need to use GPS TX any more (during loop).

Is not possible to use Atmega 328 with Arduino NG board?

I'm not sure. After a quick look, it appears that an Arduino NG board with a 328 installed should act like an UNO. Did you select "UNO" as the board type before uploading?

Maybe someone else knows... this question should be a separate post, perhaps in the Microcontrollers sub-forum.

-dev:
After a quick look, it appears that an Arduino NG board with a 328 installed should act like an UNO. Did you select "UNO" as the board type before uploading?

@Dev.. The board is pre installed with Atmega 168. I tried uploading the program by removing 168 and installing Atmega 328 in the same board. I selected the board type as "Arduino NG or older". I guess that's the reason for the issue. Thanks for the suggestion :slight_smile: Will try uploading the program to Arduino Pro/Mini board with Atmega 328.

-dev:
Since you are using GSM RX/TX (at any time) and GPS RX (no TX), you could use AltSoftSerial for GSM and NeoSWSerial for GPS. You could still use GPS TX during setup to configure the GPS if necessary, blocking GSM for a short time. After that, you won't need to use GPS TX any more (during loop).

Will consider using NeoSWSerial for the GPS module as an alternative option if it doesn't work with the hardware serial ports.

The link which you shared says it is still possible to print the debugging statements in the Serial window even if the GPS module is connected to the H/W serial ports. Need to check out if this works as the GPS module also works in the 9600 bps baud rate.

A question: Does NeoSWSerial for GPS have any compatibility issue with the TinyGPS++ library ?

Does NeoSWSerial for GPS have any compatibility issue with the TinyGPS++ library?

No, but the author of NeoGPS would be disappointed. :wink: I wrote NeoGPS because I kept trying to fix other libraries. Here is a post that describes how NeoGPS solves all of those problems and is still smaller, faster, more reliable and more accurate.

Even if you don't use NeoGPS, be sure to look at the examples and the Troubleshooting page. It could save you some headaches later... We see many posts here about other libaries's examples not working after they were modified. Stay away from delay and String.

it is still possible to print the debugging statements in the Serial window even if the GPS module is connected to the H/W serial ports. Need to check out if this works

Yes, that works. If you connect the Arduino TX pin 1 to the GPS RX, then everything you print to Serial goes to both the Serial Monitor window and the GPS device.

This works, because it's ok if you see the GPS configuration commands (if any) on the Serial Monitor, before your debug prints appear. And the GPS device ignores anything that doesn't start with a '$' character; it ignores all your debug prints.

Cheers,
/dev

@Dev.. Surprised to know that you are the author of NeoGPs !! :slight_smile: :slight_smile: Thanks for creating a new library for fighting the issues with the other libraries.. As you said.. with TinyGPS++ library too I found it difficult to execute a program with a simple modification of including curly braces after an 'if statement' !!! Only the examples worked but not the modified ones..

Stay away from delay and String.

You mean.. String functions and delay commands in arduino ?? I read many discussions in forums about the poor performance caused by the use of String functions. But delay?? Delay is needed after every GSM command!! And it worked for me with only GSM connected to arduino via Softserial .

Delay is needed after every GSM command!!

Yes, that's ok as long as you don't care that GPS updates are lost during the delay. Using it constantly during loop will cause all GPS data to be lost.

it worked for me with only GSM connected to arduino via Softserial

What worked? Did you mean SoftwareSerial?

-dev:
What worked? Did you mean SoftwareSerial?

No. I meant.. delay function. I used it in my program in which only GSM was interfaced with Arduino (via SoftwareSerial). As you said , maybe it affects GPS program. Still to try out...