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");
}
}