SMS using RTC module

Good day everyone, I am new to ardunio projects. I need some help regarding my project that needs to send SMS real time,. so in my project i used RTC module for real time clock, yet once the GSM starts to send SMS the entire system hangs up stop at the time I set the GSM to send SMS

  1. you got your post onto a wrong sub-forum

@UKHeliBob or any mod could move it to a right sub-forum

  1. where is your code ? wiring ?

My apology for not posting first my code. I just tried to post my concern if there is anyone willing to help since this is my first here in the forum.

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <PZEM004Tv30.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include <string.h>

PZEM004Tv30 pzem(5, 6);
SoftwareSerial mySerial(3, 2);
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int chipSelect = 10;
File dataFile;

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};


byte rowPins[ROWS] = {A0, A1, A2, A3}; //R1,R2,R3,R4
byte colPins[COLS] = {4, 7, 8};//C1,C2,C3,C4

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;
int num;

String inputString;
long inputInt;
tmElements_t tm;
const int relay = 9;
int key1 = 0;
void setup() {

  pinMode (9, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000);
  while (!Serial) ;
  setSyncProvider(RTC.get);
  lcd.begin();
  lcd.backlight();
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("card initialized.");
  inputString.reserve(10); //
  lcd.print ("Enter kWh:");

  mySerial.begin(9600);
  delay(1000);
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print(key);
    lcd.print (key);
  }
  if (key >= '0' && key <= '9') {
    inputString += key;
  } else if (key == '#') {
    if (inputString.length() > 0) {
      inputInt = inputString.toInt();
      inputString = "";
      Serial.println (inputInt);
      clearLCDLine(3);
      clearLCDLine(0);
    }
  } else if (key == '*') {
    inputString = "";
  }
  while (inputInt != 0) {
    readdata();
  }
}

void readdata() {

  RTC.read(tm);
  float voltage1 = pzem.voltage();
  float current1 = pzem.current();
  float power1 = pzem.power();
  float energy1 = pzem.energy();
  float pf = pzem.pf();
  float price = ((energy1 * 2) * inputInt);

  lcd.setCursor(0, 0);                       //LCD Display
  print2digits(tm.Hour);
  lcd.print (tm.Hour);
  lcd.print (":");

  print2digits(tm.Minute);
  lcd.print (tm.Minute);
  lcd.print (":");

  print2digits(tm.Second);
  lcd.print (tm.Second);

  lcd.setCursor(10, 0);                      //LCD Display
  lcd.print (tm.Day);
  lcd.print ("/");
  lcd.print (tm.Month);
  lcd.print ("/");
  lcd.print (tmYearToCalendar(tm.Year));

  lcd.setCursor(0, 1);
  lcd.print("V=");
  lcd.print(voltage1, 1);
  lcd.setCursor(10, 1);
  lcd.print("E=");
  lcd.print((energy1 * 2), 2);

  lcd.setCursor(0, 2);
  lcd.print("A=");
  lcd.print(current1 * 2);
  lcd.setCursor(10, 2);
  lcd.print("W=");
  lcd.print((power1 * 2), 1);
  lcd.setCursor(0, 3);
  lcd.print("$=");
  lcd.print(price, 2);

  lcd.setCursor(10, 3);
  lcd.print("D=");
  lcd.setCursor(12, 3);
  lcd.print(inputInt);

  int time1 = tm.Hour;
  int time2 = tm.Minute;
  int time3 = tm.Second;
  
  if (time3 == 0) { //(time1 == 8 && time2 == 00 ) && 
      char charVal[8];               //temporarily holds data from vals
      String stringVal = "";
      dtostrf(price, 4, 4, charVal);
      char message[15] = "Your bill is: ";
      strncat(message, charVal, sizeof(charVal));

      mySerial.println("AT");
      updateSerial();
      mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
      updateSerial();
      mySerial.println("AT+CMGS=\"+639059256120\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
      updateSerial();
      mySerial.print(message); //text content
      updateSerial();
      mySerial.write((char)26);
      delay (1000);

      dataFile = SD.open("datalog.txt", FILE_WRITE);
      dataFile.print("Date (D/M/Y) = ");         //SD Card
      dataFile.print(tm.Day);
      dataFile.print("/");
      dataFile.print(tm.Month);
      dataFile.print("/");
      dataFile.println (tmYearToCalendar(tm.Year));

      dataFile.print("Voltage: ");                   //SD Card
      dataFile.print(voltage1);
      dataFile.print("Energy: ");
      dataFile.print(energy1 * 2);
      dataFile.print("Dist.Cost:    ");
      dataFile.println();

      dataFile.print("Current: ");                   //SD Card
      dataFile.print(current1 * 2);
      dataFile.print("Power:      ");
      dataFile.print(power1 * 2);
      dataFile.print("Bill: ");
      dataFile.println(price);
      dataFile.println("\n");
      dataFile.close();
    }

  if (pzem.voltage() >= 270 || pzem.voltage() <= 190) {
    digitalWrite (9, HIGH);
    delay (9000);
    
    dataFile = SD.open("datalog.txt", FILE_WRITE);
    dataFile.println("----------OVERLOAD-----------"); 
    print2digits(tm.Hour);
    dataFile.print("Time = ");                 //SD Card
    dataFile.print (tm.Hour);
    dataFile.print(":");
    print2digits(tm.Minute);
    dataFile.print (tm.Minute);
    dataFile.print(":");
    print2digits(tm.Second);
    dataFile.print (tm.Second);
    dataFile.println("\n");
    dataFile.close();
  }
  else {
    digitalWrite (9, LOW);
  }
  delay(1000);
}

void updateSerial() {
  delay(1000);
  while (Serial.available()) {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.print ('0');
    dataFile.print('0');
  }
}

void clearLCDLine(int line) {
  lcd.setCursor(0, line);
  for (int n = 0; n < 20; n++)         {
    lcd.print(" ");
  }
}

image

I set the code to send SMS on the data recorder by the sensor every minute. yet the code will function only up to 3 to 4 SMS sent, for the 5th sent the system will hang up at exactly a minute. my phone will receive the SMS yet the display of my arduino will hang up and stop everything including the sending of SMS

perhaps one of the array here overflows ?

try replacing that whole code block with this

String message = "Your bill is: " + String(price,2);

Thanks for your suggestion Kassim. I will try this modification and feedback later

hello kassim, the code now works well but the text message has no content. And I set the GSM to send SMS at exactly 00 seconds, yet there are times the clock skips this

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Thank you sterretje,.

have you posted that code?

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <PZEM004Tv30.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include <string.h>

PZEM004Tv30 pzem(5, 6);
SoftwareSerial mySerial(3, 2);
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int chipSelect = 10;
File dataFile;

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};


byte rowPins[ROWS] = {A0, A1, A2, A3}; //R1,R2,R3,R4
byte colPins[COLS] = {4, 7, 8};//C1,C2,C3,C4

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;
int num;

String inputString;
long inputInt;
tmElements_t tm;
const int relay = 9;
int key1 = 0;
void setup() {

  pinMode (9, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000);
  while (!Serial) ;
  setSyncProvider(RTC.get);
  lcd.begin();
  lcd.backlight();
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("card initialized.");
  inputString.reserve(10); //
  lcd.print ("Enter kWh:");

  mySerial.begin(9600);
  delay(1000);
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print(key);
    lcd.print (key);
  }
  if (key >= '0' && key <= '9') {
    inputString += key;
  } else if (key == '#') {
    if (inputString.length() > 0) {
      inputInt = inputString.toInt();
      inputString = "";
      Serial.println (inputInt);
      clearLCDLine(3);
      clearLCDLine(0);
    }
  } else if (key == '*') {
    inputString = "";
  }
  while (inputInt != 0) {
    readdata();
  }
}

void readdata() {

  RTC.read(tm);
  float voltage1 = pzem.voltage();
  float current1 = pzem.current();
  float power1 = pzem.power();
  float energy1 = pzem.energy();
  float pf = pzem.pf();
  float price = ((energy1 * 2) * inputInt);

  lcd.setCursor(0, 0);                       //LCD Display
  print2digits(tm.Hour);
  lcd.print (tm.Hour);
  lcd.print (":");

  print2digits(tm.Minute);
  lcd.print (tm.Minute);
  lcd.print (":");

  print2digits(tm.Second);
  lcd.print (tm.Second);

  lcd.setCursor(10, 0);                      //LCD Display
  lcd.print (tm.Day);
  lcd.print ("/");
  lcd.print (tm.Month);
  lcd.print ("/");
  lcd.print (tmYearToCalendar(tm.Year));

  lcd.setCursor(0, 1);
  lcd.print("V=");
  lcd.print(voltage1, 1);
  lcd.setCursor(10, 1);
  lcd.print("E=");
  lcd.print((energy1 * 2), 2);

  lcd.setCursor(0, 2);
  lcd.print("A=");
  lcd.print(current1 * 2);
  lcd.setCursor(10, 2);
  lcd.print("W=");
  lcd.print((power1 * 2), 1);
  lcd.setCursor(0, 3);
  lcd.print("$=");
  lcd.print(price, 2);

  lcd.setCursor(10, 3);
  lcd.print("D=");
  lcd.setCursor(12, 3);
  lcd.print(inputInt);

  int time1 = tm.Hour;
  int time2 = tm.Minute;
  int time3 = tm.Second;
  
  if (time3 == 0) { //(time1 == 8 && time2 == 00 ) && 
//      char charVal[8];               //temporarily holds data from vals
//      String stringVal = "";
//      dtostrf(price, 4, 4, charVal);
//      char message[15] = "Your bill is: ";
//      strncat(message, charVal, sizeof(charVal));
      String message = "Your bill is: " + String(price,2);

      mySerial.println("AT");
      updateSerial();
      mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
      updateSerial();
      mySerial.println("AT+CMGS=\"+639059256120\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
      updateSerial();
      mySerial.print(message); //text content
      updateSerial();
      mySerial.write((char)26);
      delay (1000);

      dataFile = SD.open("datalog.txt", FILE_WRITE);
      dataFile.print("Date (D/M/Y) = ");         //SD Card
      dataFile.print(tm.Day);
      dataFile.print("/");
      dataFile.print(tm.Month);
      dataFile.print("/");
      dataFile.println (tmYearToCalendar(tm.Year));

      dataFile.print("Voltage: ");                   //SD Card
      dataFile.print(voltage1);
      dataFile.print("Energy: ");
      dataFile.print(energy1 * 2);
      dataFile.print("Dist.Cost:    ");
      dataFile.println();

      dataFile.print("Current: ");                   //SD Card
      dataFile.print(current1 * 2);
      dataFile.print("Power:      ");
      dataFile.print(power1 * 2);
      dataFile.print("Bill: ");
      dataFile.println(price);
      dataFile.println("\n");
      dataFile.close();
    }

  if (pzem.voltage() >= 270 || pzem.voltage() <= 190) {
    digitalWrite (9, HIGH);
    delay (9000);
    
    dataFile = SD.open("datalog.txt", FILE_WRITE);
    dataFile.println("----------OVERLOAD-----------"); 
    print2digits(tm.Hour);
    dataFile.print("Time = ");                 //SD Card
    dataFile.print (tm.Hour);
    dataFile.print(":");
    print2digits(tm.Minute);
    dataFile.print (tm.Minute);
    dataFile.print(":");
    print2digits(tm.Second);
    dataFile.print (tm.Second);
    dataFile.println("\n");
    dataFile.close();
  }
  else {
    digitalWrite (9, LOW);
  }
}

void updateSerial() {
  delay(1000);
  while (Serial.available()) {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.print ('0');
    dataFile.print('0');
  }
}

void clearLCDLine(int line) {
  lcd.setCursor(0, line);
  for (int n = 0; n < 20; n++)         {
    lcd.print(" ");
  }
}

the sim 800L cannot send SMS using variable. I received empty message
that is why from my previous code i used array

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.