0

今天是个好日子,

我有一个项目,其中每个事件都有时间戳,时间戳被添加到 SMS 并发送以进行记录。我已经能够从 SIM800L 调制解调器中检索时间,并且当我执行以下操作时:

int time = sim800l.print("AT+CCLK?\r"); Serial.println(时间);

然后时间以检索到的格式打印并正确显示在串行监视器中。但是,当我尝试强制转换/解析时,检索到的只是个位数 9。基本上,目标是得到这个“20/11/02,05:58:59+08”,或者它的一个子集到字符串中,这将允许我将其添加到短信中。

我的完整代码如下:

#include <SoftwareSerial.h>

SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted

#define button1 7 //Button pin, on the other pin it's wired with GND

bool button_State; //Button state

String String1 = "Waypoint 1:";
String String2 = "YYMMDD";
void setup()
{
 
  pinMode(button1, INPUT_PULLUP); //The button is always on HIGH level, when pressed it goes LOW
  sim800l.begin(9600);   //Module baude rate, this is on max, it depends on the version
  Serial.begin(9600);   
  delay(1000);
}
 
void loop()
{
 

  button_State = digitalRead(button1);   //We are constantly reading the button State
 
  if (button_State == LOW) {            //And if it's pressed
    Serial.println("Button pressed");   //Shows this message on the serial monitor
    delay(200);                         //Small delay to avoid detecting the button press many times
   
    SendSMS();                          //And this function is called

 }
 
  if (sim800l.available()){            //Displays on the serial monitor if there's a communication from the module
    Serial.write(sim800l.read());
  }
}
 
void SendSMS()
{
  Serial.println("Sending SMS...");               //Show this message on serial monitor

  int time = sim800l.print("AT+CCLK?\r");
  Serial.println(time);
  String tym = String(time); // <- This just rint a 9 to the serial monitor
 
  sim800l.print("AT+CMGF=1\r");                   //Set the module to SMS mode
  delay(100);
  sim800l.print("AT+CMGS=\"+xxxxxxxxxxx\"\r");  //Your phone number don't forget to include your country code, example +212123456789"
  delay(500);
  sim800l.print(String1 + String2 + Tym);       //This is the text to send to the phone number, don't make it too long or you have to modify the SoftwareSerial buffer
  delay(500);
  sim800l.print((char)26);// (required according to the datasheet)
  delay(500);
  sim800l.println();
  Serial.println("Text Sent.");
  delay(500);

}

请注意,我已经广泛搜索并尝试了许多代码示例,我真的被卡住了。

提前感谢您,非常感谢您的帮助

4

1 回答 1

0

如果您想要其他格式的日期和时间,请更改 GMTTime 函数中的第 65 行代码

#include <SoftwareSerial.h>

const int SimBaudRate = 9600;
const int SerialBaudRate = 9600;
const String String1 = "Waypoint 1: ";
static const uint8_t monthDays[]={31,31,28,31,30,31,30,31,31,30,31,30,31};

SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted

#define button1 7 //Button pin, on the other pin it's wired with GND

bool button_State; //Button state
String GMTDateTime = "";
String localdateTime = "";
String response = "";
String command  = "";
int i;

uint8_t leapYear(uint8_t year) {
  return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0) ? 1 : 0;
}

String GMTTime(String dateTime, float fromGMT) {
  uint8_t dt[6];
  for (uint8_t i = 0; i < 6; i++) {
    dt[i] = dateTime.substring(i * 3, i * 3 + 2).toInt();
  }
  if (fromGMT > 0) {
    uint16_t t = dt[4] + fromGMT * 60;
    if (t > 60) {
      dt[3] += t / 60;
      dt[4] = t % 60;
      if (dt[3] > 24) {
        dt[2] += 1;
        dt[3] -= 24;
        uint8_t maxDay = (dt[1] == 2) ? 28 + leapYear(dt[0]) : monthDays[dt[1]];
        if (dt[2] > maxDay) {
          dt[1] += 1;
          dt[2] = 1;
          if (dt[1] > 12) {
            dt[0] += 1;
            dt[1] = 1;
          }
        }
      }
    } else dt[4] = t;
  } else {
    int t = dt[3] * 60 + dt[4] + fromGMT * 60;
    if (t < 0) {
      dt[2] -= 1;
      t += 24 * 60;        
      if (dt[2] <= 0) {
        dt[1] -= 1;
        uint8_t maxDay = (dt[1] == 2) ? 28 + leapYear(dt[0]) : monthDays[dt[1]];
        dt[2] = maxDay;
        if (dt[1] <= 0) {
          dt[0] -= 1;
          dt[1] = 12;
        }
      }
    }
    dt[3] = t / 60;
    dt[4] = t % 60;
  }
  char buffer[17];
  sprintf(buffer, "%02d/%02d/%02d,%02d:%02d:%02d", dt[0], dt[1], dt[2], dt[3], dt[4], dt[5]); //<<<<<<< change this line for other datetime format
  return String(buffer);
}

void getTime()
{
  sim800l.println("AT+CCLK?");
}

void setSMSParam()
{
  Serial.println("Set sms parameters");               //Show this message on serial monitor
  sim800l.println("AT+CSMP=17,167,0,0");
  delay(100);
  Serial.println("Set text sms mode");               //Show this message on serial monitor
  sim800l.println("AT+CMGF=1");                      //Set the module to SMS mode  
}
void SendSMS()
{
  setSMSParam();
  Serial.println("Sending sms ...");               //Show this message on serial monitor
  sim800l.println("AT+CMGS=\"+xxxxxxxxxxxx\"\r");  //+xxxxxxxxxxxx is Your phone number don't forget to include your country code, example +212123456789"
  delay(500);
  sim800l.print(String1 + GMTDateTime);       //This is the text to send to the phone number, don't make it too long or you have to modify the SoftwareSerial buffer
  delay(500);
  sim800l.write(26);// (required according to the datasheet)
  delay(500);
  sim800l.println();
}

void setup()
{ 
  pinMode(button1, INPUT_PULLUP); //The button is always on HIGH level, when pressed it goes LOW
  sim800l.begin(SimBaudRate);   //Module baude rate, this is on max, it depends on the version
  Serial.begin(SerialBaudRate);   
  delay(1000);
}
 
void loop()
{
  button_State = digitalRead(button1);   //We are constantly reading the button State
 
  if (button_State == LOW) {            //And if it's pressed
    Serial.println("Button pressed");   //Shows this message on the serial monitor
    delay(200);                         //Small delay to avoid detecting the button press many times
    getTime();                          //And this function is called
 }
 
  response = "";
  command  = "";

  while (sim800l.available()) {
    char ch = sim800l.read();
    response += ch;
  }
  if ((response != "") && (response != "⸮&quot;)) {
    //remove \r\n from first and last of 
    if (response.startsWith("\r\n") && response.endsWith("\r\n")) {
      response.remove(0, 2);
      response.remove(response.length() - 2, 2);
    }
    if (response.startsWith("+")) {
      i = response.indexOf(':');
      command = response.substring(1, i);
      response.remove(0, i + 2);    
      if (command == "CCLK") {
        i = response.indexOf('+');
        uint8_t dt[6];
        localdateTime = response.substring(1,i);
        double fromGMT  = -response.substring(i, response.lastIndexOf('"')).toInt() / 4.0;
        GMTDateTime = GMTTime(localdateTime, fromGMT);
        Serial.print("GMT time = ");               //Show this message on serial monitor
        Serial.println(GMTDateTime);
        Serial.print("local time = ");               //Show this message on serial monitor
        Serial.println(localdateTime);
        SendSMS();
      } else if (command == "CMGS") {
        i = response.substring(0, response.indexOf("\r")).toInt();
        Serial.printf("SMS sent with code %d", i);
      }
    } else if (response == "ERROR") {
      Serial.println("Error");
    }
  }
}
于 2021-08-18T09:38:22.563 回答