GPS does not restart after being disabled

I'm able to retrieve GPS data and send it using MQTT or HTTP.

Now the problem is that the GPS won't restart (using GPS.begin()) after the GPS being disabled (using GPS.end()).

Below you can find my Arduino sketch. I'm curious to hear if someone manages to get the GPS to begin and end within the void loop, without getting stuck in the process.

Note:
I discovered that it's only possible to send data using MQTT or HTTP when the GSM is disabled. So I'm only able to send GPS coordinates once.

#include <GPS.h>
#include <GSM.h>

#include "arduino_secrets.h"
char pin[]      = SECRET_PIN;
char apn[]      = SECRET_APN;
char username[] = SECRET_USERNAME;
char pass[]     = SECRET_PASSWORD;

// Check for valid fix every checkValidInterval ms
constexpr unsigned long checkValidInterval { 60000 }; // 1 min interval
unsigned long checkValidNow {};

int state = 0;

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

  Serial.println("Starting Carrier Network registration");
  if (!GSM.begin(pin, apn, username, pass, CATM1)) {
    Serial.println("The board was not able to register to the network...");
    // do nothing forevermore:
    while (1);
  }
}

void loop() {
  if (state == 0 && millis() > checkValidNow) {
    checkValidNow = millis() + checkValidInterval;
    state = 1;
    Serial.println("\nEnable GNSS Engine...");
    // GPS.begin() start and eanble the GNSS engine
    GPS.begin();
    Serial.println("\nGNSS Engine enabled...");
  }

  // Print data
  while (GPS.available()) {
    Serial.print((char) GPS.read());
    delay(1);
  }

  if (state == 1 && millis() > checkValidNow ) {
    checkValidNow = millis() + checkValidInterval;
    state = 0;
    //stop and disable the GNSS engine
    Serial.println("GPS going down");
    GPS.end();
    Serial.println("GPS disabled");
  }
}

cc @tymoteusz_tymek

Hi @laurenssl

I will give it a test soon to see if I can reproduce this behavior.

What I found is that GPS library is missing part of AT cmd in GPS.end() method.
C:\Users\tymoteusz.lipinski\Documents\ArduinoData\packages\arduino\hardware\mbed_portenta\3.2.0\libraries\GPS\src\GPS.cpp
line 200

  _serial->write("^SSIO=7,0\r\n", sizeof("^SSIO=7,0\r\n"));

it should be:

  _serial->write("AT^SSIO=7,0\r\n", sizeof("AT^SSIO=7,0\r\n"));

Maybe it's causing some problems.

Could you perform manual "override" and before calling GPS.begin() send:

GSM.sendAT("AT\r\n");
delay(1);
GSM.sendAT("AT\r\n");

This may help flush any probable unhandled ERROR responses from the module.

BR
Tymek

Thanks for the response @tymoteusz_tymek

This will result in:

GNSS_loop_simple:33:9: error: 'class arduino::GSMClass' has no member named 'sendAT'; did you mean 'end'?
     GSM.sendAT("AT\r\n");
         ^~~~~~

If I manually update the GPS.cpp file with the update you point out, it seems like nothing changes as the portenta still gets stuck during the GPS restart.

Addressed at allow proper GPS functionality after disable · arduino/ArduinoCore-mbed@1a3b712 · GitHub

@ laurenssl - Hi, "How" did you get MQTT working via this board?

I can't find any useful documentation or examples.

I did get the example MQTT code running over the H7's WiFi though.