# Help in writing freertos code for GPS module

**URL:** <https://forums.freertos.org/t/help-in-writing-freertos-code-for-gps-module/17413>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [May 12, 2023, 3:12pm UTC](https://forums.freertos.org/t/help-in-writing-freertos-code-for-gps-module/17413 "2023-05-12T15:12:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atia](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/atia/32/3516_2.png) [@atia](https://forums.freertos.org/u/atia)\
**Post date:** [May 12, 2023, 3:12pm UTC](https://forums.freertos.org/t/help-in-writing-freertos-code-for-gps-module/17413/1 "2023-05-12T15:12:40Z")

</div>

I am fairly new to the using Freertos and rtos in general and would like to build an application that;

1. Creates two global variables name longitude and latitude
2. Blinks two LEDs alternately at a rate of 1 seconds each.
3. Receive GPS location data (longitude and latitude) and store them in the global variables. This will run at a rate of 10seconds
4. To check if longtitude and latitude values have been updated and display their new value in the serial monitor.

2,3,4 and written as separate tasks. I have also attached my code.

```auto
#include <Arduino_FreeRTOS.h>
#include <TinyGPS++.h> 

#define led1 4
#define led2 6
double longitude;
double latitude;

TinyGPSPlus gps;

void GetGPS(void *pvParameters);
void BlinkLed1(void *pvParameters);
void BlinkLed2(void *pvParameters);
void PrintGPS(void *pvParameters);
//void SendData(void *pvParameters);
//void GetWeights(void *pvParameters);

void setup() {
    Serial.begin(115200); // For serial monitor
    Serial1.begin(115200); // For GPS connections
    Serial1.println("AT+GPS=1"); // atmega32u4[TX] -> A9G[RX]

    xTaskCreate(GetGPS,"gps",128,NULL,3,NULL);
    xTaskCreate(BlinkLed1,"led 1",128,NULL,1,NULL);
    xTaskCreate(BlinkLed2,"led 2",128,NULL,1,NULL);
    xTaskCreate(PrintGPS,"print locations",128,NULL,2,NULL);

    vTaskStartScheduler();
}

void loop() {
    //pass
}

void BlinkLed1(void *pvParameters){
    pinMode(led1,OUTPUT);
    while (1) {
        digitalWrite(led1,HIGH);
        vTaskDelay(1000/portTICK_PERIOD_MS);
        digitalWrite(led1,LOW);
        vTaskDelay(1000/portTICK_PERIOD_MS);
    }
}

void BlinkLed2(void *pvParameters){
    pinMode(led2,OUTPUT);
    while (1) {
        digitalWrite(led2,LOW);
        vTaskDelay(1000/portTICK_PERIOD_MS);
        digitalWrite(led2,HIGH);
        vTaskDelay(1000/portTICK_PERIOD_MS);
    }
}

void GetGPS(void *pvparameters) {
  Serial1.println("AT+GPSRD=10");
    while (1) {
      if (Serial1.available() > 0)
        if (gps.encode(Serial1.read())) {
            longitude = gps.location.lng();
            latitude = gps.location.lat();
        }
        vTaskDelay(10000/portTICK_PERIOD_MS);
    }
}

void PrintGPS(void *pvParameters){
    while (1) {
    // Check if there is any data in the variables
    if (longitude != 0.0 && latitude != 0.0) {
      // Display the data on the serial monitor
      Serial.print("Longitude: ");
      Serial.println(longitude);
      Serial.print("Latitude: ");
      Serial.println(latitude);

      // Reset the variables
      longitude = 0.0;
      latitude = 0.0;
    }

    // Wait for 1 second before checking again
    vTaskDelay(10000 / portTICK_PERIOD_MS);
  }

}

```

The LEDs bit work however there is nothing displayed in the serial monitor to indicate if I am a getting anything from the GPS module. However the same code for getting the GPS readings work when running bare-metal. Could someone guide me to the right resources on how to get it done well. Thanks in advance

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [May 12, 2023, 4:06pm UTC](https://forums.freertos.org/t/help-in-writing-freertos-code-for-gps-module/17413/2 "2023-05-12T16:06:47Z")

</div>

The (minium) task stack of `128` seems too small for the tasks using stack hungry `printf` functions. Retry with increased (x 4) stack sizes. Also use `volatile` for shared variables like `longitude, latitude`.

And:

- Check return codes of FreeRTOS API calls like `xTaskCreate`.  
How do you know that it was successful ?
- Make use of the FreeRTOS built-in development support like defining `configASSERT` and [enable stack checking](https://www.freertos.org/Stacks-and-stack-overflow-checking.html?_ga=2.152686073.136317010.1674028012-1295978378.1669191309).

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 15, 2023, 5:49am UTC](https://forums.freertos.org/t/help-in-writing-freertos-code-for-gps-module/17413/3 "2023-05-15T05:49:34Z")

</div>

In addition to the suggestions from hs2@, you can think of merging GetGPS and PrintGPS tasks into one. This way you won’t have to worry about concurrency issues when accessing the global variables `longitude` and `latitude`.
