How to: GPS Module

GPS Module can be used for getting position, time, date and altitude.

How It Works

Thanks to the SDK, setting up and using the GPS Module is very simple. You have to do only two things:

  1. Initialize the GPS Module

  2. Program the event handler (what should happen when GPS Module gets update)

 1#include <application.h>
 2
 3twr_led_t led;
 4twr_led_t gps_led_r;
 5twr_led_t gps_led_g;
 6
 7void gps_module_event_handler(twr_module_gps_event_t event, void *event_param)
 8{
 9    if (event == TWR_MODULE_GPS_EVENT_START)
10    {
11        twr_log_info("APP: Event TWR_MODULE_GPS_EVENT_START");
12
13        twr_led_set_mode(&gps_led_g, TWR_LED_MODE_ON);
14    }
15
16    else if (event == TWR_MODULE_GPS_EVENT_STOP)
17    {
18        twr_log_info("APP: Event TWR_MODULE_GPS_EVENT_STOP");
19
20        twr_led_set_mode(&gps_led_g, TWR_LED_MODE_OFF);
21    }
22
23    else if (event == TWR_MODULE_GPS_EVENT_UPDATE)
24    {
25        twr_led_pulse(&gps_led_r, 50);
26
27        twr_module_gps_time_t time;
28
29        if (twr_module_gps_get_time(&time))
30        {
31            twr_log_info("APP: Date: %04d-%02d-%02d",
32                        time.year, time.month, time.day);
33
34            twr_log_info("APP: Time: %02d:%02d:%02d",
35                        time.hours, time.minutes, time.seconds);
36        }
37
38        twr_module_gps_position_t position;
39
40        if (twr_module_gps_get_position(&position))
41        {
42            twr_log_info("APP: Lat: %03.5f", position.latitude);
43            twr_log_info("APP: Lon: %03.5f", position.longitude);
44        }
45
46        twr_module_gps_altitude_t altitude;
47
48        if (twr_module_gps_get_altitude(&altitude))
49        {
50            twr_log_info("APP: Altitude: %.1f %c", altitude.altitude, tolower(altitude.units));
51        }
52
53        twr_module_gps_quality_t quality;
54
55        if (twr_module_gps_get_quality(&quality))
56        {
57            twr_log_info("APP: Fix quality: %d", quality.fix_quality);
58            twr_log_info("APP: Satellites: %d", quality.satellites_tracked);
59        }
60
61        twr_module_gps_invalidate();
62    }
63
64    else if (event == TWR_MODULE_GPS_EVENT_ERROR)
65    {
66        twr_log_info("APP: Event TWR_MODULE_GPS_EVENT_ERROR");
67    }
68}
69
70void application_init(void)
71{
72    twr_log_init(TWR_LOG_LEVEL_DUMP, TWR_LOG_TIMESTAMP_ABS);
73
74    twr_led_init(&led, TWR_GPIO_LED, false, false);
75    twr_led_set_mode(&led, TWR_LED_MODE_ON);
76
77    if (!twr_module_gps_init())
78    {
79        twr_log_error("APP: GPS Module initialization failed");
80    }
81
82    else
83    {
84        twr_module_gps_set_event_handler(gps_module_event_handler, NULL);
85        twr_module_gps_start();
86    }
87
88    twr_led_init_virtual(&gps_led_r, TWR_MODULE_GPS_LED_RED, twr_module_gps_get_led_driver(), 0);
89    twr_led_init_virtual(&gps_led_g, TWR_MODULE_GPS_LED_GREEN, twr_module_gps_get_led_driver(), 0);
90}

This code will send via twr_log date, time, position, number of satellites module sees and fix quality. PlatformIO serial monitor.