Development

Communication / 4G:

» 4G 18: A-GPS MS-based

This example shows how to use de A-GPS in MS-Based mode

Required Materials

1 x Waspmote
1 x Battery
1 x 4G module
1 x 4G antenna
1 x SIM card

Notes

- If the required satellites visibility is not available, no GPS position is provided by the module
- The battery has to be connected.
- This example can be executed in Waspmote v15

Code

/*
    --------------- 4G_18 - A-GPS (MS-Based GPS)  ---------------

    Explanation: This example shows how to use de A-GPS in MS-Based mode

    Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
    http://www.libelium.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Version:           3.0
    Design:            David Gascón
    Implementation:    Alejandro Gállego
*/

#include <Wasp4G.h>

// APN settings
///////////////////////////////////////
char apn[] = "movistar.es";
char login[] = "movistar";
char password[] = "movistar";
///////////////////////////////////////

// define variables
uint8_t error;
uint8_t gps_status;
float gps_latitude;
float gps_longitude;
uint32_t previous;
bool gps_autonomous_needed = true;


void setup()
{
  USB.ON();
  USB.println("Start program");


  //////////////////////////////////////////////////
  // Set operator parameters
  //////////////////////////////////////////////////
  _4G.set_APN(apn, login, password);

  //////////////////////////////////////////////////
  // Show APN settings via USB port
  //////////////////////////////////////////////////
  _4G.show_APN();


  //////////////////////////////////////////////////
  // 1. Switch on the 4G module
  //////////////////////////////////////////////////
  error = _4G.ON();

  // check answer
  if (error == 0)
  {
    USB.println(F("1. 4G module ready..."));


    ////////////////////////////////////////////////
    // 2. Start GPS feature
    ////////////////////////////////////////////////

    // get current time
    previous = millis();

    gps_status = _4G.gpsStart(Wasp4G::GPS_MS_BASED);

    // check answer
    if (gps_status == 0)
    {
      USB.print(F("2. GPS started in MS-BASED. Time(secs) = "));
      USB.println((millis()-previous)/1000);
    }
    else
    {
      USB.print(F("2. Error calling the 'gpsStart' function. Code: "));
      USB.println(gps_status, DEC);
    }
  }
  else
  {
    // Problem with the communication with the 4G module
    USB.println(F("1. 4G module not started"));
    USB.print(F("Error code: "));
    USB.println(error, DEC);
    USB.println(F("The code stops here."));
    while (1);
  }
}



void loop()
{
  ////////////////////////////////////////////////
  // Wait for satellite signals and get values
  ////////////////////////////////////////////////
  if (gps_status == 0)
  {
    error = _4G.waitForSignal(20000);

    if (error == 0)
    {
      USB.print(F("3. GPS signal received. Time(secs) = "));
      USB.println((millis()-previous)/1000);

      USB.println(F("Acquired position:"));
      USB.println(F("----------------------------"));
      USB.print(F("Latitude: "));
      USB.print(_4G._latitude);
      USB.print(F(","));
      USB.println(_4G._latitudeNS);
      USB.print(F("Longitude: "));
      USB.print(_4G._longitude);
      USB.print(F(","));
      USB.println(_4G._longitudeEW);
      USB.print(F("UTC_time: "));
      USB.println(_4G._time);
      USB.print(F("date: "));
      USB.println(_4G._date);
      USB.print(F("Number of satellites: "));
      USB.println(_4G._numSatellites, DEC);
      USB.print(F("HDOP: "));
      USB.println(_4G._hdop);
      USB.println(F("----------------------------"));

      // get degrees
      gps_latitude  = _4G.convert2Degrees(_4G._latitude, _4G._latitudeNS);
      gps_longitude = _4G.convert2Degrees(_4G._longitude, _4G._longitudeEW);

      USB.println("Conversion to degrees:");
      USB.print(F("Latitude: "));
      USB.println(gps_latitude);
      USB.print(F("Longitude: "));
      USB.println(gps_longitude);
      USB.println();


      ////////////////////////////////////////////////
      // Change to AUTONOMOUS mode if needed
      ////////////////////////////////////////////////

      if (gps_autonomous_needed == true)
      {
        _4G.gpsStop();

        gps_status = _4G.gpsStart(Wasp4G::GPS_AUTONOMOUS);

        // check answer
        if (gps_status == 0)
        {
          USB.println(F("GPS started in AUTONOMOUS mode"));

          // update variable
          gps_autonomous_needed = false;
        }
        else
        {
          USB.print(F("Error calling the 'gpsStart' function. Code: "));
          USB.println(gps_status, DEC);
        }
      }

      delay(10000);
    }
    else
    {
      USB.print("no satellites fixed. Error: ");
      USB.println(error, DEC);
    }
  }
  else
  {
    ////////////////////////////////////////////////
    // Restart GPS feature
    ////////////////////////////////////////////////

    USB.println(F("Restarting the GPS engine"));

    // stop GPS
    _4G.gpsStop();
    delay(1000);

    // start GPS
    gps_status = _4G.gpsStart(Wasp4G::GPS_MS_BASED);

    // check answer
    if (gps_status == 0)
    {
      USB.print(F("GPS started in MS-BASED. Time(ms) = "));
      USB.println(millis() - previous);
    }
    else
    {
      USB.print(F("Error calling the 'gpsStart' function. Code: "));
      USB.println(gps_status, DEC);
    }
  }

}

Output

Start program
*****************************
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
*****************************
1. 4G module ready...
2. GPS started in MS-BASED. Time(secs) = 21
3. GPS signal received. Time(secs) = 35
Acquired position:
----------------------------
Latitude: 4139.9612,N
Longitude: 00051.4686,W
UTC_time: 114504
date: 110816
Number of satellites: 3
HDOP: 500.0000000000
----------------------------
Conversion to degrees:
Latitude: 41.6660194396
Longitude: -0.8578099250

GPS started in AUTONOMOUS mode
3. GPS signal received. Time(secs) = 47
Acquired position:
----------------------------
Latitude: 4139.9248,N
Longitude: 00051.4887,W
UTC_time: 114515
date: 110816
Number of satellites: 3
HDOP: 500.0000000000
----------------------------
Conversion to degrees:
Latitude: 41.6654129028
Longitude: -0.8581449508

3. GPS signal received. Time(secs) = 57
Acquired position:
----------------------------
Latitude: 4140.1565,N
Longitude: 00051.4161,W
UTC_time: 114527
date: 110816
Number of satellites: 4
HDOP: 2.5000000000
----------------------------
Conversion to degrees:
Latitude: 41.6692733764
Longitude: -0.8569349288

3. GPS signal received. Time(secs) = 68
Acquired position:
----------------------------
Latitude: 4140.1399,N
Longitude: 00051.4251,W
UTC_time: 114537
date: 110816
Number of satellites: 4
HDOP: 2.5000000000
----------------------------
Conversion to degrees:
Latitude: 41.6689987182
Longitude: -0.8570850372

3. GPS signal received. Time(secs) = 78
Acquired position:
----------------------------
Latitude: 4140.1530,N
Longitude: 00051.4287,W
UTC_time: 114547
date: 110816
Number of satellites: 5
HDOP: 1.8000000953
----------------------------
Conversion to degrees:
Latitude: 41.6692161560
Longitude: -0.8571450233

3. GPS signal received. Time(secs) = 88
Acquired position:
----------------------------
Latitude: 4140.1466,N
Longitude: 00051.4267,W
UTC_time: 114557
date: 110816
Number of satellites: 5
HDOP: 1.1000000238
----------------------------
Conversion to degrees:
Latitude: 41.6691093444
Longitude: -0.8571117401

3. GPS signal received. Time(secs) = 98
Acquired position:
----------------------------
Latitude: 4140.1465,N
Longitude: 00051.4262,W
UTC_time: 114608
date: 110816
Number of satellites: 5
HDOP: 2.0000000000
----------------------------
Conversion to degrees:
Latitude: 41.6691093444
Longitude: -0.8571032524

3. GPS signal received. Time(secs) = 109
Acquired position:
----------------------------
Latitude: 4140.1469,N
Longitude: 00051.4259,W
UTC_time: 114618
date: 110816
Number of satellites: 5
HDOP: 2.0000000000
----------------------------
Conversion to degrees:
Latitude: 41.6691131591
Longitude: -0.8570983886

Quick Publish: