0

I have been working on a project. I have two sketches. One is to get GPS location and one is send location via SMS (GSM Module). I want to combine both sketches.

GPS sketch:

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

//long lat,lon; // create variable for latitude and longitude object
float flat, flon;

SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
TinyGPS gps; // create gps object

void setup(){
  Serial.begin(9600); // connect serial
  gpsSerial.begin(9600); // connect gps sensor
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
//  smartdelay(0);
}

void loop(){
  while(gpsSerial.available()){ // check for gps data
   if(gps.encode(gpsSerial.read())){ // encode gps data
   // gps.get_position(&lat,&lon); // get latitude and longitude
     gps.f_get_position(&flat, &flon); 
     String lat = String(flat,6);
     String lon = String(flon,6);
     Serial.print(lat);
     Serial.print(' ');
     Serial.println(lon);

   }
  }
}

GSM sketch:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

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);
}

int index=0;
long dlat=0,dlong=0;
char st[256],st1[256],st2[256];
 void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  // mySerial.println("cheking");
//   mySerial.println(string);
 }

void loop()
{
  if (Serial.available()>0)
     RecieveMessage();

 if (mySerial.available()>0)
 {
  //int st;
  st[index++] = mySerial.read();
//Serial.write(st[index-1]);

 if(index>=51&&index<=63){
  st1[index-51]=st[index-1];
   Serial.write(st1[index-51]);
   }
  if(index>=65&&index<=77){
  st2[index-65]=st[index-1];
  Serial.write(st2[index-65]);
  } 
 /*if(index==77)
 {
int i=0;
mySerial.print(st1);
    for(i=0;i<13;i++)
    {
      if(st1[i]!='.')
      dlat=dlat*10+(st1[i]-'0');
      if(st2[i]!='.')
      dlong=dlong*10+(st2[i]-'0');
       //Serial.print(dlat);
    } 

 //Serial.print(dlat);
 //Serial.print(" ");
 //Serial.print(dlong);
 */
  }
 }

I want to merge these two sketches so that I can send location via GSM sketch and receive location through GPS sketch. Any help will be appreciated.

Thanks.

  • The GPS will generate output a couple of times at every seconds and it wont be possible to send that via GSM (as the GSM will need some time to send data).You should have to give some time limit i.e suppose limit it to say send location data at every 3 secs (GSM might take about 2 secs to setup) – Mathews Sunny May 08 '17 at 03:51
  • `SoftwareSerial` is a bit depricated one you should avoid using it. – Mathews Sunny May 08 '17 at 03:53
  • @Billa I am not sending data from gsm to gps. I am sending data to gsm separately and gps is automatically getting its location. I just want these two codes to be merged into one. – Maulik Solanki May 08 '17 at 15:47
  • Yeah.I know you are sending from gps to gsm.but i can tell you it wont work as if you think as because it can cause some lags because of GSM .can you please add two screenshots of gps and gsm working – Mathews Sunny May 08 '17 at 15:52

1 Answers1

1

The following way you can merge your two sketches :

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

//long lat,lon; // create variable for latitude and longitude object
float flat, flon;

SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
TinyGPS gps; // create gps object
static const int RXPin = 8, TXPin = 9;
AltSoftSerial mySerial(RXPin, TXPin);

int index=0;
long dlat=0,dlong=0;
char st[256],st1[256],st2[256];
 void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  // mySerial.println("cheking");
//   mySerial.println(string);
 }


void setup(){
  Serial.begin(9600); // connect serial
  gpsSerial.begin(9600); // connect gps sensor
  mySerial.begin(9600);   // Setting the baud rate of GSM Module
  delay(100);
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
//  smartdelay(0);
}

void loop(){
  while(gpsSerial.available()){ // check for gps data
   if(gps.encode(gpsSerial.read())){ // encode gps data
   // gps.get_position(&lat,&lon); // get latitude and longitude
     gps.f_get_position(&flat, &flon); 
     String lat = String(flat,6);
     String lon = String(flon,6);
     Serial.print(lat);
     Serial.print(' ');
     Serial.println(lon);

     if (Serial.available()>0)
     RecieveMessage();

 if (mySerial.available()>0)
 {
  //int st;
  st[index++] = mySerial.read();
//Serial.write(st[index-1]);

 if(index>=51&&index<=63){
  st1[index-51]=st[index-1];
   Serial.write(st1[index-51]);
   }
  if(index>=65&&index<=77){
  st2[index-65]=st[index-1];
  Serial.write(st2[index-65]);
  } 
 /*if(index==77)
 {
int i=0;
mySerial.print(st1);
    for(i=0;i<13;i++)
    {
      if(st1[i]!='.')
      dlat=dlat*10+(st1[i]-'0');
      if(st2[i]!='.')
      dlong=dlong*10+(st2[i]-'0');
       //Serial.print(dlat);
    } 

 //Serial.print(dlat);
 //Serial.print(" ");
 //Serial.print(dlong);
 */
  }

   }
  }
}

P.S : I have changed the pins for GPS and rest part is same irrespective of adding AltSoftSerial it is a library same as SoftwareSerial (Download it from manage libraries and it requires pins 8 and 9)

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31