How to get live GPS Sim800 Module RTC time on Arduino

1k views Asked by At

Hello I wanted the live GPS Sim800 Module RTC time on Arduino without the use of library, I am able to achieve it using library but without library the correct data comes in only 1 minute and other comes the garbage.

Please tell me the code that can help me achieve it

Using code I did the following thing

#include <SoftwareSerial.h>
SoftwareSerial gsm(10, 11);
String Buffer;
String inData;

int i;
int *year, *month, *day, *hour, *minute, *second;

void setup() {
  Serial.begin(9600);
  gsm.begin(9600);
  delay(5000);
}

void loop() {
Ansh:
  delay(500);
  gsm.println("AT+CCLK?\r\n");
  while (gsm.available()) {
    char recieved = gsm.read();
    inData += recieved;
    delay(100);

    // Process message when new line character is recieved
    if (recieved == '\n')
    {
      Serial.print("Arduino Received: ");
      Serial.println(inData);

      if (inData.substring(1, 5) == "+CCLK") {
        Serial.println("Hello");
      }

      // You can put some if and else here to process the message juste like that:

      if (inData == "+++\n") { // DON'T forget to add "\n" at the end of the string.
        Serial.println("OK. Press h for help.");
      }


      inData = ""; // Clear recieved buffer
      goto Ansh;
    }

    /* Buffer = Buffer.substring(Buffer.indexOf("\"") + 1, Buffer.lastIndexOf("\"") - 1);
      year = Buffer.substring(0, 2).toInt();
      month = Buffer.substring(3, 5).toInt();
      day = Buffer.substring(6, 8).toInt();
      hour = Buffer.substring(9, 11).toInt();
      minute = Buffer.substring(15, 17).toInt();
      second = Buffer.substring(18, 20).toInt();
    */
  }/*
  Serial.println(*hour);
  delay(100);
  Serial.println(*minute);
  delay(100);
  Serial.println(*second);
  delay(100);
*/
}

But I don't get live output but after 1 correct output it returns garbage as follows

12:22:38.960 -> Arduino Received: +CCLK: "20/10/04,12:22:31+22" 12:22:38.960 -> 12:22:39.672 -> Arduino Received: 12:22:39.672 -> 12:22:40.596 -> Arduino Received: OK 12:22:40.596 -> 12:22:42.339 -> Arduino Received: AT+CCLK?

12:22:42.339 -> 12:22:44.362 -> Arduino Received: +CCAT+CCLK?

12:22:44.362 -> 12:22:46.067 -> Arduino Received: AT+CCLK?

12:22:46.067 -> 12:22:50.344 -> Arduino Received: +CCLK: "20/10/04,12ATAT+CAT+CCLK?

12:22:50.344 -> 12:22:52.043 -> Arduino Received: AT+CCLK?

12:22:52.043 -> 12:22:54.082 -> Arduino Received: +CCAT+CCLK?

12:22:54.082 -> 12:22:55.822 -> Arduino Received: AT+CCLK?

12:22:55.822 -> 12:23:00.064 -> Arduino Received: +CCLK: "20/10/04,12:22:49AT+CCLK?

12:23:00.098 -> 12:23:01.791 -> Arduino Received: AT+CCLK?

12:23:01.791 -> 12:23:03.810 -> Arduino Received: +CCAT+CCLK?

12:23:03.845 -> 12:23:05.552 -> Arduino Received: AT+CCLK?

12:23:05.552 -> 12:23:09.803 -> Arduino Received: +CCLK: "20/10/04,12:22:58AT+CCLK?

12:23:09.838 -> 12:23:11.544 -> Arduino Received: AT+CCLK?

12:23:11.544 -> 12:23:13.563 -> Arduino Received: +CCAT+CCLK?

12:23:13.563 -> 12:23:15.290 -> Arduino Received: AT+CCLK?

12:23:15.290 -> 12:23:19.557 -> Arduino Received: +CCLK: "20/10/04,12:23:08AT+CCLK?

12:23:19.557 -> 12:23:21.278 -> Arduino Received: AT+CCLK?

if I remove the delays from the code then I receive the following output

12:34:56.406 -> Arduino Received: (⸮⸮SCC⸮⸮CL?⸮

1-⸮

AT+CCLK?

AT+CCL~Q⸮ -j 12:34:56.473 -> 12:34:56.541 -> Arduino Received: ⸮5⸮⸮AT+CCLK?

⸮⸮⸮~⸮⸮ 1⸮j⸮b-⸮

AT+CCLK?

AT+C⸮~⸮⸮ 1-j⸮5⸮⸮(⸮i 1-⸮

AV⸮1-j 12:34:56.607 -> 12:34:56.913 -> Arduino Received: ⸮5⸮⸮J⸮jj⸮j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CK5Q⸮ 1⸮j⸮5⸮⸮AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK⸮Q⸮ Zj⸮5⸮⸮(⸮⸮1-⸮

1⸮j⸮5j⸮ը(⸮CCP⸮i 1-⸮5Q⸮⸮j⸮jj⸮ը(j⸮jQb⸮⸮E⸮jj⸮(⸮⸮1-⸮5Q⸮ Zj⸮5⸮⸮(⸮⸮(j⸮jQ⸮Zj⸮5j⸮ը(⸮j⸮⸮ 1⸮j⸮⸮5j⸮(⸮⸮(⸮j⸮V 1-j 12:34:57.184 -> 12:34:57.252 -> Arduino Received: ⸮5⸮⸮D⸮j1-⸮

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

A 1-⸮

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCL~Q⸮ -j 12:34:57.354 -> 12:34:57.561 -> Arduino Received: ⸮5⸮⸮AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK? j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+C⸮⸮5Q⸮⸮⸮Ԋi⸮j⸮(Q⸮⸮j⸮jj⸮ը(⸮CCP⸮i⸮j⸮ը(⸮CC⸮⸮⸮K? ⸮+CCLK?

AT+C⸮Q⸮Zj⸮5⸮⸮(Q⸮ -j 12:34:57.763 -> 12:34:57.763 -> Arduino Received: ⸮5⸮⸮E⸮jj⸮j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT⸮⸮z⸮⸮z⸮⸮b⸮⸮Қ⸮Ҫ⸮Z⸮⸮j 12:34:57.866 -> 12:34:57.866 -> Arduino Received: 12:34:57.905 -> 12:34:57.905 -> Arduino Received: OK 12:34:57.968 -> 12:34:57.968 -> Arduino Received: AT+CCLK?

12:34:57.968 -> 12:34:57.968 -> Arduino Received: +CCLK: "20/10/04,12:34:54+22" 12:34:58.037 -> 12:34:58.037 -> Arduino Received: 12:34:58.070 -> 12:34:58.070 -> Arduino Received: OK 12:34:58.070 -> 12:34:58.070 -> Arduino Received: AT+CCK ⸮⸮'I⸮)j⸮H(⸮5⸮ꨨH⸮)⸮Қ⸮Ҫ⸮Z⸮⸮j 12:34:58.138 -> 12:34:58.138 -> Arduino Received: 12:34:58.172 -> 12:34:58.172 -> Arduino Received: OK A+⸮1-⸮jRb҂⸮jjZ⸮⸮Ҫ⸮Z⸮⸮j 12:34:58.207 -> 12:34:58.207 -> Arduino Received:

2

There are 2 answers

1
Danny_ds On BEST ANSWER

Try this:

#include <SoftwareSerial.h>

SoftwareSerial gsm(10, 11);
String inData;

void setup() {
    Serial.begin(9600);
    gsm.begin(9600);
    delay(5000);
}

void loop() {

    inData = "";

    gsm.println("AT+CCLK?");
    delay(500);

    while (gsm.available()) {
        inData += char(gsm.read());
    }

    Serial.print("Arduino Received: ");
    Serial.println(inData);

    delay(500);
}
2
A No nymous On

@Danny_ds

doing while (gsm.available()) { inData += char(gsm.read()); }

will empty the incoming buffer quicker than it is filled in if the GSM did not get a chance to fill up the buffer during the 500ms pause you provided before.

The buffer being also only 64 bytes deep, at 9600 bauds you can receive 480 characters in those 500ms.. it means that if the answer is longer than 64 bytes, you'll loose everything else.

so in a nutshell, you are not guaranteed to see the full answer.

Proper way of doing this is to await for the full answer as described in the documentation (usually you get an "OK\r\n" at the end of the answer) or a timeout whatever comes first.