The GSM library has a getTime function that should return the number of seconds since 1 January 1970.
MKRGSM - Arduino Reference.
When I run it I get a number such as:
1072916002
Using a unix epoch converter this is Thursday, 1 January 2004 00:13:22. The current unix epoch time is 1573547448.
How can I get the correct time?
#include <MKRGSM.h>
GSM gsmAccess;
GPRS gprs;
#include "secrets.h"
const char pinnumber[] = SECRET_PINNUMBER;
const char gprs_apn[] = SECRET_GPRS_APN;
const char gprs_login[] = SECRET_GPRS_LOGIN;
const char gprs_password[] = SECRET_GPRS_PASSWORD;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
if (gsmAccess.status() != GSM_READY || gprs.status() != GPRS_READY) {
connectGSM();
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(getTime());
delay(1000);
}
unsigned long getTime() {
// get the current time from the cellular module
return gsmAccess.getTime();
}
void connectGSM() {
Serial.println("Attempting to connect to the cellular network");
while ((gsmAccess.begin(pinnumber) != GSM_READY) ||
(gprs.attachGPRS(gprs_apn, gprs_login, gprs_password) != GPRS_READY)) {
// failed, retry
Serial.print(".");
delay(1000);
}
Serial.println("You're connected to the cellular network");
Serial.println();
}