Code to print all raw data from MKR GPS shield

Hello,

Would you please tell me how I can print all raw data that MKR gps receives ? There is sample code available using Arduino_MKRGPS.h to print few values but that is not in depth.

Best
Sam

Post that sample code and we can suggest how to change it to do what you want.

Please read the forum guide before posting code.

Hello Paul,

thanks for the reply. Below is the sample code :

/*
  GPS Location

  This sketch uses the GPS to determine the location of the board
  and prints it to the Serial monitor.

  Circuit:
   - MKR board
   - MKR GPS attached via I2C cable

  This example code is in the public domain.
*/

#include <Arduino_MKRGPS.h>

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // If you are using the MKR GPS as shield, change the next line to pass
  // the GPS_MODE_SHIELD parameter to the GPS.begin(...)
  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }
}

void loop() {
  // check if there is new GPS data available
  if (GPS.available()) {
    // read GPS values
    float latitude   = GPS.latitude();
    float longitude  = GPS.longitude();
    float altitude   = GPS.altitude();
    float speed      = GPS.speed();
    int   satellites = GPS.satellites();

    // print GPS values
    Serial.print("Location: ");
    Serial.print(latitude, 7);
    Serial.print(", ");
    Serial.println(longitude, 7);

    Serial.print("Altitude: ");
    Serial.print(altitude);
    Serial.println("m");

    Serial.print("Ground speed: ");
    Serial.print(speed);
    Serial.println(" km/h");

    Serial.print("Number of satellites: ");
    Serial.println(satellites);

    Serial.println();
  }
}

I tried making below changes but it did not work.

/*
  GPS Location

  This sketch uses the GPS to determine the location of the board
  and prints it to the Serial monitor.

  Circuit:
   - MKR board
   - MKR GPS attached via I2C cable

  This example code is in the public domain.
*/

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



TinyGPSPlus gpsTiny;
//SoftwareSerial ss(13, 14); // RX TX 
int incomingByte = 0; // for incoming serial data

void setup() {  
  // initialize serial communications and wait for port to open:
  Serial.println(" Hello  i am setup function");
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // If you are using the MKR GPS as shield, change the next line to pass
  // the GPS_MODE_SHIELD parameter to the GPS.begin(...)
  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }
}

void loop() {

  // reply only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
   
  
  
  /*
  // check if there is new GPS data available
  if (GPS.available()) {
    Serial.print("gps is available sam ");
    // read GPS values
    float latitude   = GPS.latitude();
    float longitude  = GPS.longitude();
    float altitude   = GPS.altitude();
    float speed      = GPS.speed();
    int   satellites = GPS.satellites();

    
        // print GPS values
    Serial.print("Location: ");
    Serial.print(latitude, 7);
    Serial.print(", ");
    Serial.println(longitude, 7);

    Serial.print("Altitude: ");
    Serial.print(altitude);
    Serial.println("m");

    Serial.print("Ground speed: ");
    Serial.print(speed);
    Serial.println(" km/h");

    Serial.print("Number of satellites: ");
    Serial.println(satellites);

    Serial.println();

    while (Serial.available() > 0)
    {
      byte b  = Serial.read();
      Serial.println("in while loop ");

      Serial.println( gpsTiny.encode(b));
 


    }


 
    //Serial.println(GPS.);

   
    //Serial.println("Standby mode");
    //GPS.standby();
    //delay(10000);
    //Serial.println("Wakeup");
    //GPS.wakeup();

  }*/
}

The documentation for the library you are using is here

The functions (methods) it has are

begin() *
end() ~
available() *
latitude() *
longitude() *
speed() *
course() +
variation() +
altitude() *
satellites() *
satellites() ?
getTime() +
standby() ~
wakeup() ~

I marked a * against the ones your code is already using. I marked a ~ against methods that won't give you more data. I marked a + against the ones that will give you more data.

Your seem to have changed the code to read input from serial monitor. Try sending some data from serial monitor.

How is your GPS module connected?

@PaulRB thank you Paul for the reply. I referred the docs earlier of Arduino_MKRGPS.h library.

But how I can print every data that GPS module see ? For eg. print all NMEA sentences without using any filter ? I did not find any function to print GPS data without applying any filter.

Ah, ok. Well, that is all that library will give you. If you want more, you will have to stop using it and read the data directly. To do that, you will need to know which UART (Serial) port on the microcontroller that the GPS chip is connected. Check the schematic for the MKR GPS to find out.

A program to read the output of a GPS, lets assume it is on Serial1 and copy the output to Serial so you can see it in the Arduino IDE is;

void loop()
{
  while (Serial1.available())
  {
    Serial.write(Serial1.read());
  }
}

void setup()
{
  Serial1.begin(9600);
  Serial.begin(115200);
  Serial.println();
  Serial.println("GPS_Echo_Hardware_Serial Starting");
}

@PaulRB @srnet Thank you so much for your time. I know that GPS on the MKR GPS Shield can be managed using the Serial1 interface at 9600 8n1. I tried below code but for serial1 it does not print anything. How can I use Serial1 to print raw GPS data ? Serial1 is not available because it does not enter while loop. Just for your information, I have connect MKR GPS shield to Mkr wifi 1010 using I2C cable.


#include <Arduino_MKRGPS.h>

//#include <HardwareSerial.h>

void setup() {
  
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial1.begin(9600);
  while (!Serial1) {
    ; 
  }

  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }
}

void loop() {

    while (Serial1.available())
  {
    Serial.println("SERIAL ONE available");
    Serial.write(Serial1.read());
  }

   if (Serial1.available()) {
    Serial.println("SERIAL ONE available");
    char c = Serial1.read();
    Serial.print(c);
  }
  
  if (GPS.available()) {

    float latitude   = GPS.latitude();
    float longitude  = GPS.longitude();
    // print GPS values
    Serial.print("Location: ");
    Serial.print(latitude, 7);
    Serial.print(", ");
    Serial.println(longitude, 7);
    Serial.println();
    Serial1.println("I am serial ONE ");
  }
  
}

Remember this? It was only a couple of posts ago....

I thought you said the GPS shield was connected to the MKR with UART. Are you now saying it is i2c?

Yes its via I2c cable

If the GPS connects to the MKR with i2c bus, the above code will not work. Are you sure it uses i2c?

Yes 100 % sure. I am using I2C cable to connect Mkr gps to Mkr wifi 1010. In this case , what I should do ? connect both via UART ?

Serial part of the code works but Serial1 does not.

I checked, and what you said in post #10 is correct.

According to this page:

The GPS on the MKR GPS Shield can be managed using the Serial1 interface at 9600 8n1.

It definitely connects via UART, not i2c.

What is this i2c cable you are using and how are you connecting it? Can you share some pictures? This might explain why your current code isn't getting any data. However, is this the same cable you were using with the sample code you mentioned in post #1? You were getting some data with that, so you must have connected it correctly at that point.

@PaulRB Would you please refer link ?

Here its mentioned about I2C cable.
Later on i will also share picture.

To read a Ublox GPS over I2C I have used a program like this, but I dont have a GPS with I2C connections handy at the moment to test it;


#include <Wire.h>

void loop()
{
  const int GPSI2CAddress = 0x42;

  byte i;

  Wire.beginTransmission(GPSI2CAddress);
  Wire.write(0xFF);

  while (1)
  {
    Wire.requestFrom(GPSI2CAddress, 1);
    i = Wire.read();
    if (i != 0xFF)
    {
      Serial.write(i);
    }
  }
}

void setup()
{
 Serial.begin(115200);                      //connect at 115200 so we can read the GPS fast enough and also spit it out
 Serial.println(F("Starting GPS Read"));
}

Ok then, definitely i2c.

Any code using Serial1 is useless.

Over i2c, the device doesn't output NMEA sentences, I suspect. It probably holds various parameters like longitude in internal registers and the controller can read them on request, as and when needed.

So if there are other parameters that the library doesn't give access to, you will need to scrutinise the data sheet for the GPS module to find their register addresses and use the Wire library to request them.