GY-GPS6MV2 Altitude

Hi to all,

I am using Arduino UNO, GY-GPS6MV2,

The problem is I am not getting Altitude values, remaning everything is o.k,

I am testing this Roof of my building.

And my code is,

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;

SoftwareSerial ss(2, 3);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{
  Serial.begin(9600);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.println("Sats HDOP Latitude  Longitude  Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)     (deg)      Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("-------------------------------------------------------------------------------------------------------------------------------------");

  ss.begin(9600);
}

void loop()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  print_date(gps);
  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  Serial.println();
  
  smartdelay(1000);
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

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

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartdelay(0);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour+5, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);
}

Thanks In Advance,

Hi,
So you see all the other values on the serial monitor, including more than 1 satellite in the satellite column.
Is there no altitude reading or the altitude reading does not change?
How much change is it from ground to the roof.

Tom... :slight_smile:

That code is crap. The smartdelay() function is anything but smart.

Get rid of the stupid while loop. Move the remaining code to the top of loop(). It makes no sense to try to use the data from the GPS until decode() admits that it got good data.

Hi,
I'm using SKYLAB GPS and FullExample.ino from TinyGPS++ example and have not had any problems.
SKYLAB SKM53 and also A-2235H units, both are fine.

Tom..... :slight_smile:

Manidhar:
I am using Arduino UNO, GY-GPS6MV2,

The problem is I am not getting Altitude values, remaning everything is o.k,

I am testing this Roof of my building.

How many satellites does your report show?

The minimum number of satellites in view required are:

  • UTC time reception (will be bufferered in GPS RTC): 1 satellite
  • coordinate reception: 3 satellites
  • altitude reception: 4 satellites

With 4 or more satellites you also should get altitude values.
(But altitude with GPS is not very accurate)

Hi,

I am not getting satellites also,

my result screen shot shown bellow,

Manidhar:
I am not getting satellites also,

my result screen shot shown bellow,

You are getting one valid GPS sentence per second and about 2 or 3 checksum fails.

Your serial connection to the GPS receiver is generating a lot of failures. In a good connection the "checksum fail" should stay at 0 or 1 and then not increase!

Possibly you are using one of the worst libraries that comes with the Arduino IDE: SoftwareSerial!

SoftwareSerial cannot be used with hardware 'Serial' at the same time at a baudrate of more than 4800 baud without producing a lot of errors.

Would please describe:

  • which Arduino board are you using?
  • which code are you using (post the sketch in code tags)?

If your code contains the line:

#include <SoftwareSerial.h>

you will need to throw this library out of your code and use a better one.

In case your board has no second hardware serial connection, I'd recommend this third party library for software serial emulation:
https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

Thank you very much jurs,

I tried Your suggestion, I got great results,

but you said,

jurs:
You are getting one valid GPS sentence per second and about 2 or 3 checksum fails.

Your serial connection to the GPS receiver is generating a lot of failures. In a good connection the "checksum fail" should stay at 0 or 1 and then not increase!

in this increasing checksum fail valus and changing Altitude values and I am not disturbing the GPS connections.

jurs:
Would please describe:

  • which Arduino board are you using?
  • which code are you using (post the sketch in code tags)?

I am using Arduino UNO, and GY-GPS6MV2 module

And my updated code is

#include <AltSoftSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;
                                        //  Transmit  Receive   PWM Unusable
AltSoftSerial altSerial;// Arduino Uno        9         8         10

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{
  Serial.begin(9600);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.println("Sats HDOP Latitude  Longitude  Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)     (deg)      Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("-------------------------------------------------------------------------------------------------------------------------------------");

   altSerial.begin(9600);
}

void loop()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
   // while (ss.available())
   //   gps.encode(ss.read());
      
  delay(1000);
  smartdelay(1000);
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  print_date(gps);
  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  Serial.println();
  
  //delay(1000);
  
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (altSerial.available())
      gps.encode(altSerial.read());
  } while (millis() - start < ms);
}

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

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartdelay(0);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour+5, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);
}

Thanks in advance,

Manidhar:
Thank you very much jurs,

I tried Your suggestion, I got great results,

So now you get more valid "sentences" than "Checksum Fail".
Previously it had been the other way round.
You get satellites and altitude.
But the "Checksum Fail" is still increasing.

I think this is caused by using delay():

 delay(1000); 
 smartdelay(1000);

During "delay(1000);" the Serial input buffer is flooded with incoming characters that are not handled, but lost. Arduino is only able to buffer 63 characters, but at 9600 baud there may be sent up to 960 charactes per second.

AVOID 'delay()'!

And you should also avoid your weird 'smartdelay()'.
That 'smartdelay' is 'busy waiting' and it is hardly any better than a simple 'delay()'.
Busy waiting is locking up your code for other things you might want to run "at the same time". So you should avoid 'busy waiting' as well as 'delay'.

I've tried to fix your code:

#include <AltSoftSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;
                                        //  Transmit  Receive   PWM Unusable
AltSoftSerial altSerial;// Arduino Uno        9         8         10


static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{
  Serial.begin(9600);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.println("Sats HDOP Latitude  Longitude  Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)     (deg)      Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("-------------------------------------------------------------------------------------------------------------------------------------");

   altSerial.begin(9600);
}

void printGPS()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  print_date(gps);
  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  Serial.println();
}

void loop()
{
  static unsigned long lastPrintTime;
  
  // 1st task: always handle incoming characters from GPS in the loop
  while (altSerial.available()) gps.encode(altSerial.read());
  
  // 2nd task: each 1000 milliseconds print the data
  if (millis()-lastPrintTime>= 1000)
  {
    lastPrintTime+= 1000;
    printGPS();
  }
}

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

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour+5, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
}

Please watch out for the loop function and how 'lastPrintTime' is used. This is a timestamp in milliseconds since the last GPS output was sent to Serial. And in each run of the loop function there is calculated the current time difference since the last output was sent. When the time difference reaches 1000 milliseconds, then the timestamp is updated and new output is sent to Serial.

Thanks a lot jurs,

I got the final result what i am expecting. my final result is shown below screen shot.

but one more thing in the AltsoftSerial how to use two AltSoft Serial's.

Thanks in advance.

Manidhar:
I got the final result what i am expecting. my final result is shown below screen shot.

'Checksum Fail' stays at zero, that's it!

Manidhar:
but one more thing in the AltsoftSerial how to use two AltSoft Serial's.

You cannot emulate more than one serial with AltSoftSerial.

If you need to have more serial ports you will have to use a board which provides more serial ports.

UNO ==> one serial port (Serial)
LEONARDO ==> two serial ports (Serial, Serial1)
MEGA2560 ==> four serial ports (Serial, Serial1, Serial2, Serial3)
From that, 'Serial' is used for the USB serial connection Arduino<->PC.

You then can emulate one addition serial port with AltSoftSerial.

And perhaps you can add one additional serial port with SoftwareSerial. But as SoftwareSerial is a very crappy library, this will probably not work without transmission errors.

Thanks jurs,

Sorry for asking many times,

In my project I am compulsory use the Arduino UNO because size and cost,

At my project I want to collect and send the compass, gps values to the authorised person by using sim900.

I am doing this project since 40days , it's individually well work, but when i am combining all these it won't work.

I can't figure out the problem, please help me to complete my project.

I am using the modules of this project is, Arduino UNO, GY-88(Compass and gyro), GY-GPS6MV2, GSM SIM900.

I am attaching the libraries and code to the following.

GSM_Shield.zip (30.6 KB)

GYSenso.zip (126 KB)

and my code is,

#include <AltSoftSerial.h>
#include <GSM_Shield.h>

#include <SoftwareSerial.h>
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*********   GSM    ****************/
char number[]="+919490974707";  //Destination number
char text[]="hello world";  //SMS to send
byte type_sms=SMS_UNREAD;      //Type of SMS
byte del_sms=0;                //0: No deleting sms - 1: Deleting SMS
GSM gsm;
char sms_rx[122]; //Received text SMS
char number_incoming[20];
int call;
int pos_sms_rx;
char messagearray[100];
byte smsflag=0;
#include <TinyGPS.h>

TinyGPS gps;
                                       //  Transmit  Receive   PWM Unusable
AltSoftSerial altSerial;// Arduino Uno        9         8         10
char xang[10],yang[10],comp[10],tilt[10],gpslat[10],gpslon[10];
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
float flat, flon;
 unsigned long age, date, time, chars = 0;
 unsigned short sentences = 0, failed = 0;
 static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
/***********  COMPASS    *****************/
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>
#include "MPU6050.h"
HMC5883L compass;
MPU6050 accelgyro;
int error = 0;
float compassval;
void getdata();
void setup()
{
 Serial.begin(9600);
  altSerial.begin(9600);
  Wire.begin();
 accelgyro.setI2CMasterModeEnabled(false);
 accelgyro.setI2CBypassEnabled(true) ;
 accelgyro.setSleepEnabled(false);
 Serial.begin(9600);
 Wire.begin(); // Start the I2C interface.
 compass = HMC5883L(); // Construct a new HMC5883 compass.
 error = compass.SetScale(1.3); // Set the scale of the compass. 
 error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
   Serial.begin(9600);
 Serial.println("system startup");
 gsm.TurnOn(9600);          //module power on
 gsm.InitParam(PARAM_SET_1);//configure the module 
 gsm.Echo(0);               //enable AT echo
 Serial.println("Ready  ........");
}
void compass123()
{
  MagnetometerRaw raw = compass.ReadRawAxis();
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis) for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
  heading += declinationAngle;
  if(heading < 0)
    heading += 2*PI;
  if(heading > 2*PI)
    heading -= 2*PI;
  float headingDegrees = heading * 180/M_PI;
  Serial.println(" ");
   Serial.print(headingDegrees);
   compassval = headingDegrees;
}
void Send_SMS()
{
        Serial.print("Send SMS to ");
        Serial.println(number);
        error=gsm.SendSMS(number,messagearray); 
        if (error==0)  //Check status
        {
            Serial.println("SMS ERROR \n");
        }
        else
        {
            Serial.println("SMS OK \n");           
        }
}
void Check_SMS()  //Check if there is an sms 'type_sms'
{
    char pos_sms_rx;  //Received SMS position   
    pos_sms_rx=gsm.IsSMSPresent(type_sms);
    if (pos_sms_rx!=0)
    {
      smsflag=1;
      //Read text/number/position of sms
      gsm.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
      Serial.print("Received SMS from ");
      Serial.print(number_incoming);
      Serial.print("(sim position: ");
      Serial.print(word(pos_sms_rx));
      Serial.println(")");
      Serial.println(sms_rx);
      if (del_sms==1)  //If 'del_sms' is 1, i delete sms
      {
        error=gsm.DeleteSMS(pos_sms_rx);
        if (error==1)Serial.println("SMS deleted");     
        else Serial.println("SMS not deleted");
      }
    }
    return;
}
void printGPS()
{
 smartdelay(1000);
 print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
 gps.f_get_position(&flat, &flon, &age);
 print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
 print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
 print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
}
void getdata()
{
// printGPS();
 compass123();
 ftoa(flat,gpslat,2);
 ftoa(flon,gpslon,2);
 strcat(messagearray,gpslat);
 strcat(messagearray,"@");
 strcat(messagearray,gpslon);
 strcat(messagearray,"@");
 ftoa(compassval,comp,2);
 strcat(messagearray,comp);
 strcat(messagearray,"@");
           
 Send_SMS();
}
void clearbuf(char *b)// clearing the buffer
{
 while(*b)
   *b++='\0';
}
void loop()
{
 static unsigned long lastPrintTime;
 char c[10],d[10];
 int i,j,k,e,m;
   Check_SMS();  //Check if there is SMS
   if(smsflag==1)
   {
     smsflag=0;
      while(sms_rx[i]!='@')
           {
               c[j]=sms_rx[i];
               i++;
               j++;
           }
           i++;
           c[j]='\0';
           while(sms_rx[i]!='\0')
           {
               d[k]=sms_rx[i];
               i++;
               k++;
           }   
          if(!strcmp(c,"getdata"))
          {
             getdata();
          }
 }
 compass123(); 
 // 1st task: always handle incoming characters from GPS in the loop
/* while (altSerial.available()) gps.encode(altSerial.read());
 // 2nd task: each 1000 milliseconds print the data
 if (millis()-lastPrintTime>= 1000)
 {
   lastPrintTime+= 1000;
   printGPS();
 }*/
 printGPS();
}
static void smartdelay(unsigned long ms)
{
 unsigned long start = millis();
 do
 {
   while (altSerial.available())
     gps.encode(altSerial.read());
 } while (millis() - start < ms);
}
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);
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
 char sz[32];
 if (val == invalid)
   strcpy(sz, "*******");
 else
   sprintf(sz, "%ld", val);
 sz[len] = 0;
 for (int i=strlen(sz); i<len; ++i)
   sz[i] = ' ';
 if (len > 0)
   sz[len-1] = ' ';
 Serial.print(sz);
 smartdelay(0);
}
static void print_date(TinyGPS &gps)
{
 int year;
 byte month, day, hour, minute, second, hundredths;
 unsigned long age;
 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
 if (age == TinyGPS::GPS_INVALID_AGE)
   Serial.print("********** ******** ");
 else
 {
   char sz[32];
   sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
       month, day, year, hour+5, minute, second);
   Serial.print(sz);
 }
 print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
 smartdelay(0);
}
static void print_str(const char *str, int len)
{
 int slen = strlen(str);
 for (int i=0; i<len; ++i)
   Serial.print(i<slen ? str[i] : ' ');
 smartdelay(0);
}
void reverse(char *str, int len)
{
   int i=0, j=len-1, temp;
   while (i<j)
   {
       temp = str[i];
       str[i] = str[j];
       str[j] = temp;
       i++; j--;
   }
}
int intToStr(int x, char str[], int d)
{
   int i = 0;
   while (x)
   {
       str[i++] = (x%10) + '0';
       x = x/10;
   }
   while (i < d)
       str[i++] = '0';
   reverse(str, i);
   str[i] = '\0';
   return i;
}
void ftoa(float n, char *res, int afterpoint)
{
   int ipart = (int)n;
   float fpart = n - (float)ipart;
   int i = intToStr(ipart, res, 0);
   if (afterpoint != 0)
   {
       res[i] = '.';  // add dot
       fpart = fpart * pow(10, afterpoint);
       intToStr((int)fpart, res + i + 1, afterpoint);
   }
}

printing garbage.

Manidhar:
I am using the modules of this project is, Arduino UNO, GY-88(Compass and gyro), GY-GPS6MV2, GSM SIM900.

Sorry, while I can tell you something about receiving GPS, I cannot tell you anything about your GSM or compass module.

So I just can tell you that the code you posted in #12 is buggy with GPS handling:

  • GPS received char handling ==> commented out, not working at all
  • timed output once per second on Serial ==> commented out, not working at all
  • instead of correct handling "printGPS();" is called each time the loop() function is running

Correct handling of incoming GPS characters and printing data once per second would be exactly like I posted it:

  // 1st task: always handle incoming characters from GPS in the loop
  while (altSerial.available()) gps.encode(altSerial.read());
  
  // 2nd task: each 1000 milliseconds print the data
  if (millis()-lastPrintTime>= 1000)
  {
    lastPrintTime+= 1000;
    printGPS();
  }

With that code "printGPS();" is just called once per second as it ought to be.

jurs:
With that code "printGPS();" is just called once per second as it ought to be.

I think I should mention that many people have trouble with a big batch print like this, unless they coordinate it with the GPS "quiet time". They usually end up losing data because the prints block, and the 63-char input buffer overflows.

You might want to look here, here, or here for some suggestions. The basic idea is to only do the second task (printing) when the first task (reading) hasn't gotten anything for 5 milliseconds.

If you're feeling adventurous, check out the example programs for a library I wrote, called NeoGPS. I don't think I'd suggest using this advanced C++ code, but the example programs show some reliable techniques that are applicable here.

Cheers,
/dev