GSM GPS not getting data together

I am trying to implement a mechanism where any number who calls the gsm module automatically cancel (give a busy tone) and verify the number with the stored number in code. If the number is same it pass on some message and if not, it pass on some alert message. Along with the message it also pass the location where the gps module connected with arduino is present. Here both gsm and gps are cnnected to same arduino (in my case UNO). This both module when used independently, works perfectly well but when interface together, I am actually not able to receive the coordinates of GPS.
GSM used here is SIM 900. Can you help me with the code or the issue due to which this is not working?

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

String s,sta,num=""; //num has a registered number
TinyGPSPlus gps;
SoftwareSerial ss(6, 7); //used for GPS
SoftwareSerial mySerial(3, 2); //used for GSM
char la_t[10]; //for latitude
char lon_g[10]; //for longitude

void setup() {
  Serial.begin(9600);
  ss.begin(9600);
  delay(100);
  mySerial.begin(9600);
  Serial.println();
  mySerial.println("ATE0");
}

void loop(){

  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();
      
  if(mySerial.available()>0)
    sta=mySerial.readString(); //read incoming call string
  s=sta.substring(21,31); //extract number
  if(s.length()==10){
      mySerial.println("ATH\r\n"); //cancel the call
      if(s.equals(num)){
        Serial.println(F("Number Verified"));
        Serial.println("Your Vehicle has been detected at Latitude: " + String(la_t) + ", Longitude: " + String(lon_g));
      }
      else{
        Serial.println(F("Number Not Verified"));
        Serial.println("An effort was made to access Location of your Vehicle. Number is "+s+". Latitude: " +String(la_t)+ " , Longitude: " + String(lon_g));
      }
      delay(20000);    
  }
}

void displayInfo(){
  dtostrf(gps.location.lat(),9,6,la_t);
  dtostrf(gps.location.lng(),9,6,lon_g);
}
SoftwareSerial ss(6, 7);
SoftwareSerial mySerial(3, 2);

Whenever I see this, I know that the programmer is using the wrong hardware. Only one instance of SoftwareSerial can listen at a time. If you are listening to the ss (whatever the hell that is), then you can not be listening to the mySerial (whatever the hell that is).

What is wrong with using names that make sense? gsm and gps come to mind.

PaulS:

SoftwareSerial ss(6, 7);

SoftwareSerial mySerial(3, 2);



Whenever I see this, I know that the programmer is using the wrong hardware. Only one instance of SoftwareSerial can listen at a time.

Is there exist a solution for the same? Can you tell me what should I do to make it work?

The thing which I even tried was to connect GSM to 0,1 pin but in that case it didn't receive the incoming call at all and thus didn't cancel it or verify it. Should I post the code for the same here if you could help?

If you want two soft serial ports you may try NewSoftSerial:

http://arduiniana.org/libraries/newsoftserial/

You can then switch easy what port you can listen.

However, uno have one hardware port and any digital pins can be used for software. Unless you have no need to use 3rd port as hardware, always you can use one hardware and another software serial.

For instance, I use software serial to listen GPS which sending data at 4800 baud and send received data through hardware at 115200 to RS232 board and PC. This way there will be no conflict.

arduino_314:
If you want two soft serial ports you may try NewSoftSerial

No, this was incorporated and renamed SoftwareSerial. The OP is already using a slightly newer version than that.

But your suggestion to use HardwareSerial (i.e., Serial) for one of the devices is a good one. You could use a second software serial port to print debug info to a TTL Serial-to-USB converter module (aka FTDI adapter).

@Kavish5, I would also suggest using AltSoftSerial instead of SoftwareSerial. AltSoftSerial is much more efficient and does not block interrupts for long periods of time. This can interfere with other parts of your sketch, or with other libraries. It only works on pins 8 and 9, though.

If you can't switch the GSM or the GPS to pins 8 & 9 (Really? Are you sure?), then my NeoSWSerial library is second best. It works on any pins, but it only works at bauds 9600, 19200 and 38400.

For the GPS library, I would also suggest my NeoGPS library. It is smaller, faster, more reliable and more accurate than all other libraries, and the examples are correctly structured. Many people have trouble modifying other libraries' examples.

Using the String class can cause headaches later, at random times, and using delay can break other parts of your sketch (like reading the GPS). Serial Input Basics on the Useful Links page would be good for you to read.

Cheers,
/dev

PaulS:

SoftwareSerial ss(6, 7);

SoftwareSerial mySerial(3, 2);



Whenever I see this, I know that the programmer is using the wrong hardware.

Whenever I see this and string concatenation, I know that the programmer is novice in Arduino

BTW, I do not see any reference about here:
http://forum.arduino.cc/index.php?topic=97455.0

Why not add it to FAQ and later just point on?

celsius = (5/9) * (fahrenheit-32);

Whenever I see this, I know that "the programmer" is novice in C. Etc...

Why tensions about? For that is this forum, to help by pointing on right way or directly answer, isn't it?

Why tensions about? For that is this forum, to help by pointing on right way or directly answer, isn't it?

It does no good to try to fix code that clearly needs to be running on different hardware.

Thank you for the help and all but I have already spent a week or more before reaching here and still can't work out on this thing. This is the same mechanism code as mention above but with only one software serial. The issue here is it is never reaching the ATH command or beyond that and also I am not able to send any message as well.

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

TinyGPSPlus gps;
SoftwareSerial ss(6, 7); //used for gps pins

//has used 0,1 pin for gsm

String sms,sta,s,num=""; //Number goes here
char la_t[10];
char lon_g[10];

void setup() {
  Serial.begin(9600);
  Serial.println("AT+CMGF=1");//set sms to text mode
  ss.begin(9600);
  Serial.println("ATE0");
  delay(100);
}

void loop(){
  if(Serial.available())
    sta=Serial.readString();
  s=sta.substring(21,31); //extract calling number
  if(s.length()==10){
      Serial.println("ATH\r\n"); //cancel the call
      if(s.equals(num))
        sms="Your Vehicle has been detected at Latitude: " + String(la_t) + " , Longitude: " + String(lon_g);
      else
        sms="An effort was made to access Location of your Vehicle. Number is "+s+" Latitude: " + String(la_t) + ", Longitude: " + String(lon_g);
      send_sms(); //message sending
      delay(20000);    
  }  
  if(ss.available()) {
    if(gps.encode(ss.read()))
      displayInfo();
  }
}


void displayInfo(){ 
  dtostrf(gps.location.lat(),9,6,la_t);
  dtostrf(gps.location.lng(),9,6,lon_g);
}


void send_sms(){
  
  Serial.println("AT+CMGF=1");
  delay(200);
  Serial.println("AT+CMGS=\"+xxx\"\r");  //Number goes here
  delay(200);
  Serial.println(sms);
  delay(200); 
  Serial.write(26);// ASCII code of CTRL+Z
  
}

Someway somehow please help me edit this code to make it work. Atleast it should cancel the call and send appropriate message with location.

Someway somehow please help me edit this code to make it work.

Sure. As soon as you get the right hardware. A Mega, for example, with 4 hardware serial ports.

You can't debug your code when the GSM is connected to the only hardware serial pins.

And, you can't use more than one instance of SoftwareSerial. So, you really need hardware that has two or more sets of hardware serial pins.

Until then, all that will happen is that you'll keep pounding your head against the wall.

PaulS:
You can't debug your code when the GSM is connected to the only hardware serial pins.

Until then, all that will happen is that you'll keep pounding your head against the wall.

Yes bro I am actually pounding my head against the wall.
Yes I know that we can't even debug that thing

How is everything connected?

Which SIM900 module is it?

You could connect the Serial TX (pin1) to the GSM rx. This would let you send AT commands, but you would also see them on the Serial Monitor. You could also do debug prints, just be sure they don't start with "AT" or the GSM will try to interpret it as a command.

The problem is connecting the GSM tx to the Arduino Serial RX (pin0). This will prevent you from uploading a new sketch. You could simply disconnect the GSM from pin 0 when you're ready to upload, then reconnect it. Some people put a switch on the GSM tx-to-Arduino pin 0 wire, but your module might have jumper blocks that are easy to remove for uploading. This is the most reliable way to use GSM and GPS at the same time.

A benefit is that you can test it without the GSM by simply typing into the Serial Monitor window and pressing Send. The Arduino will receive it and interpret it like it came from the GSM. Here is a sketch I used without a GSM:

#include <NeoSWSerial.h>
#include <NMEAGPS.h>

      char    sta[80];
      uint8_t count = 0;
const char    num[] = "123456789"; // a registered number

NMEAGPS gps;
gps_fix fix;

NeoSWSerial      gps_port(6, 7);
HardwareSerial & gsm_port = Serial; // an alias for Serial

void setup() {
  Serial.begin(9600);
  Serial.println( F("Kavish5.ino") );

  gps_port.begin(9600);

//  gsm_port.begin(9600); Serial already started...
  gsm_port.println("ATE0"); // really goes out Serial
}


void loop()
{

  while (gps.available( gps_port )) {
    fix = gps.read(); // save fix for later use
  }

  if (gsm_port.available()>0) {
    char c = gsm_port.read();
    if (c == '\n') {
      sta[ count ] = '\0'; // NUL-terminate the C string (char array)

      gsm_port.println("ATH\r\n"); //cancel the call

      if (count >= 31)  {

        if (strncmp( &sta[21], num, sizeof(num)-1 ) == 0) {
          Serial.println( F("Number Verified") );
          Serial.print( F("Your Vehicle has been detected at ") );
        } else {
          Serial.println( F("Number Not Verified") );
          Serial.print( F("An effort was made to access Location of your Vehicle.\nNumber was ") );
          Serial.write( &sta[21], count-21 );
          Serial.println();
        }
        Serial.print( F("Latitude: ") );
        if (fix.valid.location)
          Serial.print( fix.latitude(), 6 );
        else
          Serial.print( '?' );
        Serial.print( F(" , Longitude: ") );
        if (fix.valid.location)
          Serial.print( fix.longitude(), 6 );
        else
          Serial.print( '?' );
        Serial.println();

        delay(20000);

      }
      
      count = 0; // reset for next line

    } else {
      // Not the end of the line, save another (printable) char
      if ((c >= ' ') && (count < sizeof(sta)-1)) {
        sta[ count++ ] = c;
      }
    }
  }
}

It uses NeoGPS and NeoSWSerial, and it does not use the String class. It is much smaller and faster than your original (non-working) sketch.

There is a way you could use two software serial ports. This would not be as reliable, because you can only listen to one at a time. Also, SoftwareSerial will not let you transmit and receive at the same time, so you have to use something else. You can only have one AltSoftSerial, so you would have to use two NeoSWSerial.

Try to get the jumper/switch from GSM tx to Arduino RX pin 0 working first.

Cheers,
/dev

/dev:
There is a way you could use two software serial ports. This would not be as reliable, because you can only listen to one at a time. Also, SoftwareSerial will not let you transmit and receive at the same time, so you have to use something else. You can only have one AltSoftSerial, so you would have to use two NeoSWSerial.

Try to get the jumper/switch from GSM tx to Arduino RX pin 0 working first.

Cheers,
/dev

Your code is really very good and thank you for that effort but i would try to implement it after I complete it with this ongoing one.

Can someone help me reduce the size of this code especially the variable section? I am having around 530B on UNO and some functions are not working properly due to lack of space. Just need to reduce it by atleast 100B.

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

TinyGPSPlus gps;
SoftwareSerial ss(6, 7); //use for GPS

File myFile;
String buffer[10];
boolean SDfound,flag=0;

byte i = 0,n,old_min,old_hour,in;
String date_="",time_="";
String sta,s,num=""; //Number goes here
char la_t[10];
char lon_g[10];

void setup() {
  
  Serial.begin(9600);
  delay(100);
  ss.begin(9600);
  Serial.println("ATE0");
  
  if (SDfound == 0) {
    if (!SD.begin(4)) {
      Serial.println(F("The SD card cannot be found"));
      while(1);
    }
  }
  SDfound = 1;
  
  myFile = SD.open("Welcome.txt");
  if (!myFile) {
    Serial.println(F("The text file cannot be opened"));
    while(1);
  }

  while (myFile.available()) {
    buffer[i++] = myFile.readStringUntil('\n'); 
    delay(50);
  }
  buffer[i++]=num;
  
  myFile.close();

}

void loop(){
  
  if(Serial.available()>0)
    sta=Serial.readString();
  
  s=sta.substring(21,31);
  if(s.length()==10){
      Serial.println("ATH\r\n");
      for(n=0;n<i;n++){
        if(s.equals(buffer[i])){
          flag=1;
          break;
        }
      }
      if(flag==1){
        in=0;
        flag=0;
      }
      else
        in=1;
      Serial.println(in);
      send_sms(n,in);
      delay(20000);    
  }
  
  while(ss.available()>0)
    if(gps.encode(ss.read()))
      displayInfo();  

}


void displayInfo(){ 
  
  collect();
  
  if (gps.time.isValid() && n==1){
    n=0;
    old_hour=gps.time.hour();
    old_min=gps.time.minute();
  }

  if(gps.time.isValid()){
    if(gps.time.minute()-old_min>=1 || gps.time.minute()-old_min<=-1){
      input();        
      old_min=gps.time.minute();
    }
    if(gps.time.hour()-old_hour>=1 || gps.time.hour()-old_hour<=-1){
      input();
      old_hour=gps.time.hour();
    }
  }
}

void input() {
    myFile = SD.open("Data_log.txt",FILE_WRITE);
    if (!myFile) {
      Serial.print(F("The text file cannot be opened"));
      while(1);
    }
    else {
      collect();
      myFile.println("Location: Latitude = "+ String(la_t) +" Longitude = " + String(lon_g) + "\n" + date_ + " and " + time_);
      Serial.println(F("Added"));
      delay(150);
    }
    myFile.close();
}

void collect() {
  if (gps.location.isValid() && gps.date.isValid() && gps.time.isValid()){
    time_="";
    date_="";
    time_="Time:"+String(gps.time.hour())+":"+String(gps.time.minute());
    date_="Date:"+String(gps.date.day())+"/"+String(gps.date.month())+"/"+String(gps.date.year());
    dtostrf(gps.location.lat(),9,6,la_t);
    dtostrf(gps.location.lng(),9,6,lon_g);
  }
}

void send_sms(byte x,byte y){
  Serial.println("AT+CMGF=1");
  delay(200);
  String msg="AT+CMGS=\"+91"+ (String)buffer[x] +"\"\r";
  if(y==0){    
    Serial.println(msg);  
    delay(200);
    Serial.println("Vehicle Tracked. Location: " + String(la_t) + "," + String(lon_g));
  }
  if(y==1){
    Serial.println(msg);  
    delay(200);
    Serial.println("Number " + s + " tried to access Location: " + String(la_t) + "," + String(lon_g));
  }
  if(y==2){
    for(n=0;n<i;n++){
      String msg="AT+CMGS=\"+91"+ (String)buffer[n] +"\"\r";
      Serial.println(msg);  
      delay(200);
      Serial.println("Accident Detected. Location: " + String(la_t) + "," + String(lon_g));
    }
  }
  if(y==3){
      Serial.println("AT+CMGS=\"+91" + num + "\"\r");  
      delay(200);
      Serial.println("Movement Detected. Location: " + String(la_t) + "," + String(lon_g));
  }
  delay(200); 
  Serial.write(26);
}

Any idea on this code which can reduce its size is highly appreciable and please suggest me anything on this code only for time being.

      myFile.println("Location: Latitude = "+ String(la_t) +" Longitude = " + String(lon_g) + "\n" + date_ + " and " + time_);

Quit pissing away resources on the String class. There is NOTHING to be gained doing it that way, over:

      myFile.print("Location: Latitude = ");
      myFile.print(la_t);
      myFile.print(" Longitude = ");
      myFile.print(lon_g);
      myFile.print("\n");
      myFile.print(date_ );
      myFile.print(" and ");
      myFile.println(time_);

Of course, you need to get rid of date_ and time_, too, and print the time and date one value at a time.

  String msg="AT+CMGS=\"+91"+ (String)buffer[x] +"\"\r";

Is there some reason that you think you need to cast a String to a String?

Absolutely NOTHING that the code is doing needs Strings.

PaulS:
Of course, you need to get rid of date_ and time_, too, and print the time and date one value at a time.

Thanks for the help. The only task remaining in this is accident detection using gyroscope and accelerometer. Can someone help me with that? I mean conditions and how to work over it. Never used them at all by any means so.

The only task remaining in this is accident detection using gyroscope and accelerometer.

An accelerometer gives instantaneous acceleration values. Acceleration is a change in velocity, which has both a speed and a direction. So, any large acceleration value would mean a large change in velocity or direction, both of which would be indicative of a collision.

I have no idea what you intend to do with the gyroscope.

PaulS:
An accelerometer gives instantaneous acceleration values. Acceleration is a change in velocity, which has both a speed and a direction. So, any large acceleration value would mean a large change in velocity or direction, both of which would be indicative of a collision.

I have no idea what you intend to do with the gyroscope.

They are just integrated module so I wrote....Nothing much.....Can you provide a simple code for accident detection through this? Didn't got any easy to understand code

Just one more issue.....help me out of this....The sketch works perfectly fine as of now but the only bug is the message sending.........The first time it detects the number it verifies it and do the need full but after the delay gets over it again send the message continuously with illegal access attempt (in=1) and number extracted from string in s be ERROR

You don't answer questions.

You haven't tried a single suggestion.

You don't show your code when you ask a question.

You ask for code, then don't try it.

Why are you here?

/dev:
You haven't tried a single suggestion.

I have tried all suggestions (which ever I understood) and has implemented as well.

/dev:
You don't show your code when you ask a question.

The code was same only....the serial data on gsm is not getting flushed after each call....it is as simple as that

/dev:
You ask for code, then don't try it.

I wanted help on my code and not someone else already written code. I appreciate that but that way I wont learn anything and so I didn't used that. Sorry but thats what I believe.

/dev:
Why are you here?

To learn

And talking about the code.....have a look at this

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

TinyGPSPlus gps;
SoftwareSerial ss(6, 7); //gps works on it

//gsm on serial 0,1

File myFile;
String buffer[10];
boolean SDfound,flag=0,flag1,buttonPressed;

byte i = 0,n,p = 1,old_min,old_hour,old_min_track,in,buttonPin = 3;
double old_dist_lon,old_dist_lat;
String sta,s,num=""; //number goes here
char la_t[10];
char lon_g[10];

void setup() {

  Serial.begin(9600);
  delay(100);
  ss.begin(9600);
  pinMode(buttonPin, INPUT);
  Serial.println("ATE0");
  if (SDfound == 0) {
    if (!SD.begin(4)) {
      Serial.println(F("The SD card cannot be found"));
      while(1);
    }
  }
  SDfound = 1;
  
  myFile = SD.open("Welcome.txt");
  if (!myFile) {
    Serial.println(F("The text file cannot be opened"));
    while(1);
  }

  while (myFile.available()) {
    buffer[i++] = myFile.readStringUntil('\n'); 
    delay(50);
  }
  buffer[i++]=num;

  myFile.close();

}

void loop(){
  
  if(Serial.available()>0)
    sta=Serial.readString();
  
  s=sta.substring(21,31);
  if(s.length()==10){
      Serial.println("ATH\r\n");
      for(n=0;n<i;n++){
        if(s.equals(buffer[n])){
          flag=1;
          break;
        }
      }
      if(flag==1){
        in=0;
        flag=0;
      }
      else
        in=1;
      Serial.println(in);
      send_sms(n,in);
      delay(2000);    
  }
  if(buttonPressed==LOW)
    buttonPressed = digitalRead(buttonPin);
  while(ss.available()>0)
    if(gps.encode(ss.read()))
      displayInfo();  

}  

void displayInfo(){ 
  
  collect();
    
  if (gps.time.isValid() && p==1){
    p=0;
    old_hour=gps.time.hour();
    old_min=gps.time.minute();
    old_min_track=gps.time.minute();
    old_dist_lat=gps.location.lat();
    old_dist_lon=gps.location.lng();
  }

  if(gps.time.isValid()){
    if(gps.speed.mps()>11 && !buttonPressed && (gps.time.minute()-old_min>=1 || gps.time.minute()-old_min<=-1))
      send_sms(i,3); 
    if(buttonPressed){
      //Serial.println(gps.speed.mps());
      //Serial.println(gps.time.minute());
      //Serial.println(old_min_track);
      if(gps.time.minute()-old_min_track>=1 || gps.time.minute()-old_min_track<=-1){
        if(gps.location.lat()-old_dist_lat>0.001 || gps.location.lng()-old_dist_lon>0.001){
          flag1=0;
          //Serial.println("ABC");
        }
        else{
          old_dist_lat=gps.location.lat();
          old_dist_lon=gps.location.lng();
          flag1=1;
        }
      }
      if(flag1==1){
        buttonPressed=LOW;
        //Serial.println("FLAG1 LOW");
      }
    }
    if(gps.time.minute()-old_min>=1 || gps.time.minute()-old_min<=-1){
      input();        
      old_min=gps.time.minute();
    }
    if(gps.time.hour()-old_hour>=1 || gps.time.hour()-old_hour<=-1){
      input();
      old_hour=gps.time.hour();
    }
  }
}

void input() {
    myFile = SD.open("Data_log.txt",FILE_WRITE);
    if (!myFile) {
      Serial.print(F("The text file cannot be opened"));
      while(1);
    }
    else {
      collect();
      myFile.println(F("Location: Latitude = "));
      myFile.print(la_t);
      myFile.print(F(", Longitude = "));
      myFile.print(lon_g);
      myFile.print("\n");
      myFile.print(F("Time: "));
      myFile.print(gps.time.hour());
      myFile.print(":");
      myFile.print(gps.time.minute());
      myFile.print(F(" Date: "));
      myFile.print(gps.date.day());
      myFile.print("/");
      myFile.print(gps.date.month());
      myFile.print("/");
      myFile.print(gps.date.year());
      Serial.println(F("Added"));
      delay(150);
    }
    myFile.close();
}

void collect() {
  if (gps.location.isValid() && gps.date.isValid() && gps.time.isValid()){
    dtostrf(gps.location.lat(),9,6,la_t);
    dtostrf(gps.location.lng(),9,6,lon_g);
  }
}

void send_sms(byte x,byte y){
  if(gps.location.isValid()){
    delay(200);
    if(y==0){  
      /*String msg="AT+CMGS=\"+91"+ buffer[x] +"\"\r";  
      Serial.println("AT+CMGF=1");
      delay(200);
      Serial.println(msg);  */
      delay(200);
      Serial.println("Vehicle Tracked. Location: " + String(la_t) + "," + String(lon_g));
    }
    if(y==1){
      /*String msg="AT+CMGS=\"+91"+ buffer[x] +"\"\r";
      Serial.println("AT+CMGF=1");
      delay(200);
      Serial.println(msg); */ 
      delay(200);
      Serial.println("Number " + s + " tried to access Location: " + String(la_t) + "," + String(lon_g));
    }
    if(y==2){
      for(n=0;n<i;n++){
        /*Serial.println("AT+CMGF=1");
        delay(200);
        String msg="AT+CMGS=\"+91"+ buffer[n] +"\"\r";
        Serial.println(msg);  */
        delay(200);
        Serial.println("Accident Detected. Location: " + String(la_t) + "," + String(lon_g));
        delay(200);
        Serial.write(26);
      }
    }
    if(y==3){
      /*Serial.println("AT+CMGF=1");
      delay(200);
      String msg="AT+CMGS=\"+91"+ num +"\"\r";
      Serial.println(msg);  */
      delay(200);
      Serial.println("Movement Detected. Location: " + String(la_t) + "," + String(lon_g));
    }
    delay(200); 
    Serial.write(26);
  }  
}