如何在 Arduino 上获取实时 GPS Sim800 模块 RTC 时间

How to get live GPS Sim800 Module RTC time on Arduino

你好,我想在不使用库的情况下在 Arduino 上使用实时 GPS Sim800 模块 RTC 时间,我可以使用库实现它,但没有库,正确的数据仅需 1 分钟,而其他数据则为垃圾。

请告诉我可以帮助我实现它的代码

使用代码我做了以下事情

#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);
*/
}

但我没有得到实时输出,但在 1 次正确输出后 returns 垃圾如下

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?

如果我从代码中删除延迟,那么我会收到以下输出

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:

试试这个:

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

@Danny_ds

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

如果 GSM 在您之前提供的 500 毫秒暂停期间没有机会填充缓冲区,则清空传入缓冲区的速度将快于填充缓冲区的速度。

缓冲区也只有 64 字节深,在 9600 波特率下,您可以在 500 毫秒内接收 480 个字符。这意味着如果答案超过 64 字节,您将丢失其他所有内容。

所以简而言之,您不能保证看到完整的答案。

正确的做法是等待文档中描述的完整答案(通常你会在答案末尾得到一个“OK\r\n” ) 或超时先到先得。