Pages: 1 [2]   Go Down
Author Topic: Timestamp from GSM Network  (Read 7645 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, the shield just returns nothing...

Dear MathiasVDA in order to see a reply from the GSM modem forget Software Serial! smiley-wink
First of all write: GSM gsmAccess(true);
Now you will be able to see all the AT Commands exchanged from the ATMega and GSM Module.
If you want to send your own AT Command please use this format: theGSM3ShieldV1ModemCore.println("AT+CCLK?");

It's for a different gsm shield but the principle is the same I think. I guess all the elements are there to cover the UDP part, except I still have that problem with recieving the answer to my AT commands...

The concept is correct, only the AT Commands are different: the right ones to be used for the Quectel GSM module are in its reference manual: http://arduino.cc/en/uploads/Main/Quectel_M10_AT_commands.pdf
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear MathiasVDA in order to see a reply from the GSM modem forget Software Serial! smiley-wink
First of all write: GSM gsmAccess(true);
Now you will be able to see all the AT Commands exchanged from the ATMega and GSM Module.
If you want to send your own AT Command please use this format: theGSM3ShieldV1ModemCore.println("AT+CCLK?");
Unfortunately, I know all that and still have the problem:
Code:
#include <GSM.h>
GSM gsmAccess(true);

char answer[100];

void setup()
{
  Serial.begin(9600);
  
  boolean notConnected = true;
  
  Serial.println("Connecting to the GSM network");
  
  while(notConnected){
    if(gsmAccess.begin("xxxx") == GSM_READY)
      notConnected = false;
    else {
      Serial.println("Not connected, trying again");
      delay(1000);
    }
  }
  
  Serial.println("Connected. Sending AT Command.");
  theGSM3ShieldV1ModemCore.println("AT+CNUM?");
  theGSM3ShieldV1ModemCore.println("AT+GSN?");
  theGSM3ShieldV1ModemCore.println("AT+CCLK?");
}

void loop()
{

}

And this is what I get in return:
Code:
Connecting to the GSM network
AT%13%
0 9>AT%13%%13%%10%OK%13%%10%
AT+CPIN=xxxx%13%
9 44>AT+CPIN=xxxx%13%%13%%10%+CPIN: READY%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
44 75>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
75 120>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%%13%%10%Call Ready%13%%10%
AT+CGREG?%13%
120 23>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
23 54>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
54 85>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
85 116>AT+CGREG?%13%%13%%10%+CGREG: 0,1%13%%10%%13%%10%OK%13%%10%
AT+IFC=1,1%13%
116 5>AT+IFC=1,1%13%%13%%10%OK%13%%10%
AT+CMGF=1%13%
5 21>AT+CMGF=1%13%%13%%10%OK%13%%10%
AT+CLIP=1%13%
21 37>AT+CLIP=1%13%%13%%10%OK%13%%10%
ATE0%13%
37 48>ATE0%13%%13%%10%OK%13%%10%
AT+COLP=1%13%
48 54>%13%%10%OK%13%%10%
Connected. Sending AT Command.
AT+CNUM?%13%%10%AT+GSN?%13%%10%AT+CCLK?%13%%10%
« Last Edit: July 08, 2013, 04:42:10 pm by MathiasVDA » Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 9
byte me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all,
do you know if is it possible to know the current time (hh/mm/ss) from the GSM network with the GSM Shield?

I wrote something similar for the SIM900 using AT commands.  Depending on your GPRS chip, your AT command may be differnt, but the network response should be the same.

Code:
//Serial Relay - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART
 
#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(2, 3);
 
void setup()
{
  mySerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);                 // the GPRS baud rate   
}
 
void loop()
{
/* if (mySerial.available())     Serial.write(mySerial.read());
 if (Serial.available())       mySerial.write(Serial.read()); 
*/
  String mystr="";
  int i = 0;
  mySerial.println("AT+CCLK?");
  delay (500);
  while (mySerial.available()>0) {
    mystr += char(mySerial.read());
  }
  Serial.println("My String |"+mystr+"|");
  String clockString = "";
  int x = mystr.indexOf(String('"'))+1;   // Find the first occurance of an open quotation.  This is where we begin to read from
  int y = mystr.lastIndexOf(String('"')); // Find the last occurance of an open quotation. This is where we end.
  Serial.println(mystr.substring(x,y));   // This is the time string yy/mm/dd,hh:mm:ss-tz (tz in qtrs)

  while (Serial.read()<1); // Sit and do nothing
 
}



Logged

Offline Offline
God Member
*****
Karma: 8
Posts: 550
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

another way:
when you call a special number in order to know the phone credit, lot of carrier send you a message(free) with money and the TIMESTAMP..you can parse it..obviously this method it's not perfect..you could have an error of 30-60 seconds
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

another way:
when you call a special number in order to know the phone credit, lot of carrier send you a message(free) with money and the TIMESTAMP..you can parse it..obviously this method it's not perfect..you could have an error of 30-60 seconds

The idea is good, but how can you know the GSM operator of the SIM that will be used by another person? smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a way to synchronize the time on the modem with the GSM network automatically. The according command is given in the datasheet "AT+QNITZ".
I tried to play with this command by activating it using "AT+QNITZ=1" but I can't figure out how to get the time stamp then. Asking with the "AT+CCLK?" command gives the usual bad result.

However I found out somthing interesting by accident: Sometimes when starting my GSM shield it gets stuck after the line "AT+CMGF=1%13%" and doesn't continue. In this case however I find some lines above the output "+QNITZ: "13/11/20,22:37:01+04", which is actually the right date and time (up to one hour due to +1 time zone).
The strange thing is that whenever my GSM shield doesn't get stuck on the "AT+CMGF=1%13%" line when booting , the "+QNITZ: "13/11/20,22:37:01+04" line is not present!!!

If it gets stuck during booting or not seems to happen at a random basis.

here the total output when it gets stuck:
Code:
SMS Messages Receiver
AT%13%
0 9>AT%13%%13%%10%OK%13%%10%
AT+CPIN=1234%13%
9 44>AT+CPIN=1234%13%%13%%10%+CPIN: READY%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
58 89>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
89 120>AT+CGREG?%13%%13%%10%+CGREG: 0,2%13%%10%%13%%10%OK%13%%10%
AT+CGREG?%13%
120 59>AT+CGREG?%13%%13%%10%+CGREG: 0,5%13%%10%%13%%10%OK%13%%10%%13%%10%+QNITZ: "13/11/20,22:37:01+04,0"%13%%10%
AT+IFC=1,1%13%
59 76>AT+IFC=1,1%13%%13%%10%OK%13%%10%
AT+CMGF=1%13%

In both cases I use the exact same code which is just the "ReceiveSMS" example.

Anyone with ideas or hints?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all,

I finally figured this out. Thanks to a training power point presentation form Quectel that I found on the web:
http://www.soyter.info/udostepnione_pliki/Quectel/training/training_Quectel.ppt

Please also see this post in order to know how to send AT commands and receive a response from the GSM shield:
http://forum.arduino.cc/index.php?topic=206159.0

For more info about the AT commands used in this post I refer you to the datasheet from the manufacturer:
http://www.sigmaelectronica.net/docs/M95_ATC_V1.2.pdf

There are 3 methods to sync the internal RTC from the GSM shield:
  • 1. Sync clock with internet time (Command AT+QNTP=”TIME SERVER ADDRESS”,port)
  • 2. Sync clock with GSM network time (Command = AT+QNITZ=1)
  • 3. Get location and time from GSM antenna (Command = AT+QGSMLOC=4)

Notes:
  • For option 1, I think you first need to write the server to the shield with the command above and then execute a synchronisation using this command: AT+QNTP.
  • for 1 and 3 you need a GPRS connection.
  • for 2 you need to have a carrier that supports this function (that didn't seem to work with me).
  • A time server address can be an IP or a DNS name. For example a google time server: time1.google.com.
  • Time is in GMT.


To get the RTC timestamp you need this command: AT+CCLK?

And now for a script that allows you to use the commands...

Code:
#include <GSM.h>

// initialize the library instance
GPRS gprsAccess;  // GPRS access
GSM gsmAccess;    // GSM access: include a 'true' parameter for debug enabled
GSMClient client;  // Client service for TCP connection

GSM3ShieldV1DirectModemProvider modemAccess;

// PIN Number
#define PINNUMBER "xxxx"

void setup()
{
  Serial.begin(9600);
  
  boolean notConnected = true;
  
  Serial.println("Connecting to the GSM network");
  
  while(notConnected){
    if(gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else {
      Serial.println("Not connected, trying again");
      delay(1000);
    }
  }
  
  Serial.println("Connected.");

}

void loop()
{
  
  Serial.println("Sending AT Commands.");
  
  send_command("AT+CCLK?",1000);
  send_command("AT+QNTP=\"time1.google.com\"",2000);  
  send_command("AT+QNTP?",1000);
  send_command("AT+QNTP",1000);
  send_command("AT+CCLK?",1000);    
  
  Serial.println("Trying 3rd method");  
  send_command("AT+QGSMLOC=4",1000);
  
  while(true);
}
void send_command(String AT_COMMAND, int tDelay){
  Serial.print("COMMAND: "+AT_COMMAND);
  Serial.println(modemAccess.writeModemCommand(AT_COMMAND,tDelay));
  Serial.println("-----------------------------------------------");
}

In order to succeed you need a response like this:
Code:
-----------------------------------------------
COMMAND: AT+QNTP="time1.google.com"
OK

-----------------------------------------------
COMMAND: AT+QNTP?
+QNTP:"time1.google.com",123

OK

+QNTP: 0

-----------------------------------------------
COMMAND: AT+QNTP
OK

+QNTP: 0

-----------------------------------------------
COMMAND: AT+CCLK?
+CCLK: "13/12/22,18:48:21+00"

OK

-----------------------------------------------

Important is the +QNTP: 0. If you don't get a 0 then there is some kind of error. I first got a 3 but after a while that changed and my time got updated while I was not playing around with the shield. I guess that is due to the shield starting up or something. It needs some time in order to get the time?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When the GSM shield has been out of power for a while, it will lose all settings, also the NTP server address. It will default to the manufacturers NTP server (which in my case doesn't seem to work).

I think it is safest to synchronize by specifying the NTP server each time instead of sending the command AT+QNTP.

When sending the address, the RTC will also sync. That takes less than 3 seconds (I haven't been able to measure it exactly but less than 3s is enough for me).
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a sketch that syncs an RTC from the RTC_DS1307 lib with the GSM module:

Main code:
Code:
#include <GSM.h>
#include <Wire.h>
#include <RTClib.h>

// initialize the library instance
GPRS gprsAccess;  // GPRS access
GSM gsmAccess;    // GSM access: include a 'true' parameter for debug enabled
GSMClient client;  // Client service for TCP connection

GSM3ShieldV1DirectModemProvider modemAccess;

RTC_DS1307 RTC;

// PIN Number
#define PINNUMBER "xxxx"

// APN info
char apn[] = {"xxx"};
char login[] = {""};
char password[] = {""};

void setup(){
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
 
  connectGSM();
 
  //show old datetime from RTC
  showDate("Old datetime: ",RTC.now());
 
  syncRTCwithGSM(getGSMTimestamp(),RTC);
 
  //show new datetime from RTC, synced with GSM module
  showDate("New datetime: ",RTC.now());
}
void loop(){ 
}

GSM functions:
Code:
void connectGSM(){
  boolean notConnected=true;
  Serial.println("Connecting to GSM network...");
  while(notConnected){
    if(gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else delay(1000);
  }
  Serial.println("Connected to GSM, now GPRS...");
 
  // 1000ms bug
  delay(1000);
 
  while(gprsAccess.attachGPRS(apn, login, password)!=GPRS_READY){
    delay(1000);
  }
  Serial.println("GSM shield fully connected.");
}

String getGSMTimestamp(){
  boolean notConnected = true;
 
  return send_AT_Command("AT+CCLK?",1000);
}

void setGSMRTCSyncServer(){
  // set server
  send_AT_Command("AT+QNTP=\"time1.google.com\"",2000);

  // execute sync
  send_AT_Command("AT+QNTP",1000); 
}

String getGSMRTCSyncServer(){
  // get server
  return send_AT_Command("AT+QNTP?",1000);
}

void GSMRTCSync(){
  send_AT_Command("AT+QNTP",1000);   
}

String send_AT_Command(String AT_COMMAND, int tDelay){
  /*Serial.print("COMMAND: "+AT_COMMAND);
  Serial.println(modemAccess.writeModemCommand(AT_COMMAND,tDelay));
  Serial.println("-----------------------------------------------");*/
 
  return modemAccess.writeModemCommand(AT_COMMAND,tDelay);
}

RTC functions:
Code:
void syncRTCwithGSM(String CCLK_Return, RTC_DS1307 myRTC){
  DateTime datetime;
 
  char datetime_year[3];
  char datetime_month[3];
  char datetime_day[3];
  char datetime_hour[3];
  char datetime_minute[3];
  char datetime_second[3];
 
  String datetimeGSM = CCLK_Return.substring(CCLK_Return.indexOf("\"")+1,CCLK_Return.lastIndexOf("\"")-3);
 
  datetimeGSM.substring(0,2).toCharArray(datetime_year,sizeof(datetime_year));
  datetimeGSM.substring(3,5).toCharArray(datetime_month,sizeof(datetime_month));
  datetimeGSM.substring(6,8).toCharArray(datetime_day,sizeof(datetime_day));
  datetimeGSM.substring(9,11).toCharArray(datetime_hour,sizeof(datetime_hour));
  datetimeGSM.substring(12,14).toCharArray(datetime_minute,sizeof(datetime_minute));
  datetimeGSM.substring(15,17).toCharArray(datetime_second,sizeof(datetime_second));
 
  uint8_t datetime_year_int = atoi(datetime_year);
  uint8_t datetime_month_int = atoi(datetime_month);
  uint8_t datetime_day_int = atoi(datetime_day);
  uint8_t datetime_hour_int = atoi(datetime_hour);
  uint8_t datetime_minute_int = atoi(datetime_minute);
  uint8_t datetime_second_int = atoi(datetime_second);
 
  // substract one second in order to take into account the 1s delay from the GSM module
  myRTC.adjust(DateTime((2000+(uint16_t)datetime_year_int),datetime_month_int,datetime_day_int,datetime_hour_int,datetime_minute_int,((int)datetime_second_int-1)));
 
  datetimeGSM="";
}

void showDate(const char* txt, const DateTime& dt) {
    Serial.print(txt);
    Serial.print(' ');
    Serial.print(dt.year(), DEC);
    Serial.print('/');
    Serial.print(dt.month(), DEC);
    Serial.print('/');
    Serial.print(dt.day(), DEC);
    Serial.print(' ');
    Serial.print(dt.hour(), DEC);
    Serial.print(':');
    Serial.print(dt.minute(), DEC);
    Serial.print(':');
    Serial.print(dt.second(), DEC);
    Serial.println();
}
Logged

Pages: 1 [2]   Go Up
Jump to: