今天是个好日子,
我有一个项目,其中每个事件都有时间戳,时间戳被添加到 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);
}
请注意,我已经广泛搜索并尝试了许多代码示例,我真的被卡住了。
提前感谢您,非常感谢您的帮助