0

Good day Everyone,

I have a project where each event is time-stamped, and the timestamp is added to an SMS and sent for recording. I have been able to retrieve the time from the SIM800L Modem, and when I do the following:

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

Then the time prints in the format it was retrieved and displays in the serial monitor correctly. However when I try and cast/parse, then all that is being retrieved is the single digit 9. Basically, the goal is to get this "20/11/02,05:58:59+08", or a subset of it into String which would allow me to add it to the SMS.

My full code below:

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

}

Please note that I have searched extensively and tried many code samples, I am just really stuck.

Thank you in advance, and your help is much appreciated

  • this is helpful : [how-can-i-read-date-and-time-data-from-rtc-of-sim900-module-using-arduino](https://stackoverflow.com/questions/30650732/how-can-i-read-date-and-time-data-from-rtc-of-sim900-module-using-arduino/30671039#30671039) – AliReza Aug 18 '21 at 09:49

1 Answers1

0

if you want other format of date and time, change line 65 of codes in GMTTime function

#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 != "⸮")) {
    //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");
    }
  }
}
AliReza
  • 106
  • 1
  • 7