Real time Clock

Hi,

I'm using mkr GSM and is working perfect. My question is, is it possible to have a real time clock? is it possible to "ask" the carrier "what time is it?"

Thank's

Eduard

I just had a look through the u-block AT commands manual and it looks like you can use some of the provided AT commands to either set or query the onboard realtime clock:

5.7.1 Description
Sets and reads the real-time clock of the MT.

5.7.2 Syntax
Type Syntax Response Example

Set: AT+CCLK=
OK
AT+CCLK="14/07/01,15:00:00+01"
OK

Read: AT+CCLK?
+CCLK:
OK
+CCLK: "14/07/01,15:00:00+01"
OK

Test: AT+CCLK=? OK

5.7.3 Defined values
Parameter Type Description
String Format is "yy/MM/dd,hh:mm:ss+TZ". Characters indicate year, month, day, hours, minutes,
seconds, time zone. The factory-programmed value is "04/01/01,00:00:00+00". Values prior to
the factory-programmed value are not allowed.

I'm also pretty sure you can read the time from the Network carrier more information from the AT command manual:
31.19 Ask for time information +UTIME

Set: AT+UTIME=,[,]

Read: AT+UTIME?

Test: AT+UTIME=?

Good Luck, let us know how you go.

Also the full AT command manual from u-blox is here:

https://www.u-blox.com/sites/default/files/u-blox-CEL_ATCommands_(UBX-13002752).pdf

Hi,

I did it in an easy way, I'm using MKRGSM library because I'm using MKRGSM arduino's micro. The library has the getTime() function...the problem is the date comes in timestamp.

Does anybody know how to translate this timestamp into date format?

Thank's

Eduard

Hey Eduard,

Can you send us the code segment you use for the getTime and I'll see what format it is reporting in, if it's something like Unix time format then we can certainly convert it into something more readable.

Does the getTime function work from the GSM network?

Simon

#include "Time.h"

GSM gsmAccess;

loop() {
//

unsigned long unixTime = gsmAccess->getTime();

time_t tm_unixTime(in_ticks);
struct tm* tmUTC;
tmUTC = localtime(&tm_unixTime);

int year = tmUTC->tm_year;
int month = tmUTC->tm_mon + 1; //Month is 0 based.
int day = tmUTC->tm_mday;
int hour = tmUTC->tm_hour;
int minute = tmUTC->tm_min;
int second = tmUTC->tm_sec;

}

I took your code and updated it a bit. I'm now pulling the time down from the GPRS network and using that time to set the real time clock. A bit nicer than manually setting the real time clock based on numbers you enter into a sketch and upload.

/*
 Real Time Clock Example with MKR GSM 1400 board
 This sketch connects to the GSM Network and pulls the time down and sets the local Real Time Clock.
 If the code and the comments disagree, then both are probably wrong. -- Norm Schryer

 This sketch displays the time in Unix time format and long format,
 both times are in GMT time zone:
 "Unix time = 1539311395
  12/10/18 02:29:55"
  
 Note: Add your GPRS Network/Login details onto the "arduino_secrets.h" file
 
 Changes:
 08102018 - Added inital code         
 12102018 - Removed unneeded variables
 
 Circuit:
 * MKR GSM 1400 board
 * Antenna
 * SIM card with a data plan

 created 08 October 2018
 by SMGS
*/
#include "arduino_secrets.h" 
#include <MKRGSM.h>
#include <RTCZero.h>

RTCZero rtc; // initialize the library instance
GSM gsmAccess;
GPRS gprs;

// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = SECRET_PINNUMBER;
// APN data
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:
  // initialize serial communications and wait for port to open:
  Serial.begin(57600); //Using a conservative Baud rate (max is 115200)
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.println("Starting Arduino web client.");
  // connection state
  boolean connected = false;

  // Connect to the cellular Network
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) 
  {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) 
    {
      connected = true;
    }
    else 
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  // Set the time
  rtc.begin(); // initialize RTC
  // rtc.setTime(hours, minutes, seconds);
  // rtc.setDate(day, month, year);
  rtc.setEpoch(gsmAccess.getTime());
}

void loop()
{
  Serial.print("Unix time = ");
  Serial.println(rtc.getEpoch());
  
  // Print date...
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  // ...and time
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  delay(1000);
}



void print2digits(int number) {
  if (number < 10) {
    Serial.print("0"); // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

thats the main code and then the secrets file just looks like this:

#define SECRET_PINNUMBER     ""
#define SECRET_GPRS_APN      "internet" // replace your GPRS APN
#define SECRET_GPRS_LOGIN    ""    // replace with your GPRS login
#define SECRET_GPRS_PASSWORD "" // replace with your GPRS password

Eventually I will be adding this into some other code so that I can log my sensor data with the correct current time and record it somewhere.

out of curiousity does anyone know how to easily change it from displaying in GMT format/time zone and to get it to display in local time?

Have a good one