Facing problem on sending SMS on different condition Using GSM module and Arduino mega

I have made a program for sending the condition based sms.But he problem is that the GSM Module send the sms only after first condition become true. Module not sending the sms on other condition even after that's were become true.

Here is my code:-

#include <SoftwareSerial.h>

SoftwareSerial GPRS(11, 10);
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define deviceAddress 0b1101000

#include <EEPROM.h>
#include <Wire.h>
const byte interruptPin = 2;
const uint8_t EngineCommand = 38;
int EventState = 0;
int EventState1 = 0;
int TempState = 0;
int TempState2 = 0;
int rpmState = 0;
int MtsState = 0;
int MtsState1 = 0;
int MtsState2 = 0;
int MtsState3 = 0;
int MtsState4 = 0;
int MtsState5 = 0;
int MtsState6 = 0;
int addr_secnd = 1;
int addr_Minute = 3;
int addr_hrs2 = 4;
int Hrs1;
int Hours1;
long lastTime = 1;


int NoDecHrs;
float Hmread;
float Hrsread;
int diffrE;
int diffrP;
int diffrT;
int diffrH;
int diffrF;
int addr_HMR = 30;
volatile byte state1 = HIGH;
volatile byte seconds = EEPROM.read(addr_secnd);
volatile byte minutes = EEPROM.read(addr_Minute);
volatile int hours = EEPROM.get(addr_hrs2, hours); //Hours1);
//int hours;
void setup() {
  Serial.begin(9600);
  Wire.begin();
  GPRS.begin(9600);
  rtc.begin();
  GPRS.println("AT+CMGF=1");
  delay(500);

  GPRS.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  pinMode(EngineCommand, INPUT_PULLUP);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);

  setSQW(0x00);
}
String time()
{
  String dataString = "";
  DateTime now = rtc.now();
  dataString += String(now.hour(), DEC);
  dataString += String(':');
  dataString += String(now.minute(), DEC);
  dataString += String(':');
  dataString += String(now.second(), DEC);

  return dataString;
}
void loop()
{
  DateTime now = rtc.now();
  //Serial.println(diffrP);
  //Serial.println(time());
  //Serial.print("timeTxt.txt=");
  String Hours = String(hours) + (".") + String(minutes);
  Hmread = Hours.toFloat();
  diffrP = (NoDecHrs - 60);
  diffrT = (NoDecHrs - 40);
  diffrH = (NoDecHrs - 20);
  diffrF = (NoDecHrs - 10);
  if (digitalRead(EngineCommand) == HIGH)
  {
    while (EventState < 1)
    {

      EEPROM.update(addr_secnd, seconds);
      EEPROM.update(addr_Minute, minutes);
      EEPROM.put(addr_hrs2, hours);
      EEPROM.put(addr_HMR, Hmread);
      EEPROM.get(addr_HMR, Hrsread);
      Serial.println(Hrsread);
      NoDecHrs = Hrsread;
      EventState++;
      ///Serial.println (NoDecHrs);
    }

    if (diffrP > 10)
    {
      Serial.println ("diffrP");
      while (MtsState2 < 1)
      {
        String message = String (" Please ready for PTO maints.: ") + (String) diffrP;

        GPRS.println("AT+CMGS=\"+917xxxxxxxx\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character

        delay(200);
        MtsState2++;
      }
    }
    if (diffrT > 10)
    {
      Serial.println ("diffrT");
      while (MtsState3 < 1)
      {
        String message = String (" Please ready for Tm maints.: ") + (String) diffrT;

        GPRS.println("AT+CMGS=\"+917xxxxxxxx\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(200);
        MtsState3++;
      }
    }
    if (diffrE > 10)
    {
      while (MtsState4 < 1)
      {
        Serial.println ("diffrE");
        String message = String (" Please ready for Eng maints.: ") + (String) diffrT;

        GPRS.println("AT+CMGS=\"+917xxxxxxxx\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState4++;
      }
    }
    if (diffrH > 10)
    {
      while (MtsState5 < 1)
      {
        Serial.println ("diffrH");
        String message = String (" Please ready for HYD maints.: ") + (String) diffrH;
        GPRS.println("AT+CMGS=\"+917xxxxxxxx\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState5++;
      }
    }
    if (diffrF > 10)
    {
      Serial.println ("diffrF");
      while (MtsState6 < 1)
      {
        String message = String (" Please ready for FD maints.: ") + (String) diffrF;

        GPRS.println("AT+CMGS=\"+917xxxxxxxx\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState6++;
      }

    }
  }
  // Serial.println(Hours);
  if (digitalRead(EngineCommand) == LOW)
  {
    Serial.println(Hours);
    MtsState = 0;
    MtsState1 = 0;
    MtsState2 = 0;
    MtsState3 = 0;
    MtsState4 = 0;
    MtsState5 = 0;
    MtsState6 = 0;

  }

}
void blink()
{
  if (digitalRead(EngineCommand) == LOW)
  {

    EventState = 0;

    if (lastTime > 0)
    {
      seconds++;
    }
    if (seconds == 4)
    {
      minutes++;
      seconds = 0;
    }

    if (minutes == 2)
    {
      hours++;
      minutes = 0;
    }
    state1 = !state1;

  }
}

void setSQW(uint8_t value) {
  Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
  Wire.write(0x0E); //Control Register Address is queued
  Wire.write(0x00); //Data for Control Register is queued
  Wire.endTransmission(); //queued information are transferred under ACK; STOP
}//+CMT: "+919xxxxxx

Kindly help me to solved the problem.

Exactly which test is failing ?
Have you tried printing the variables that the test uses to see whether they are the values that you expect ?

Sorry for the late reply

Only first condition sms is sending , other conditions o/p is printing sms is not sending.

Here is my Serial O/P

Hours:126.1
Hours:126.1
Hours:126.1
126.10
diffrP:66
diffrT:86
diffrH:106
diffrF:116

Serial print happened in each condition, but other Sms function not taken place

Try adding this text mode setting to each block where a message is to be sent. I'm not certain this can only be done only once in setup. I think the default may be the pdu mode and it may revert with each message.

Please explain about this ..

there is tons of information about text mode or pdu mode ➜ SMS Tutorial: Operating Mode: SMS Text Mode and SMS PDU Mode

Your AT+CMGF=1 command sets the text mode which is what you want (to Send SMS Command in Text Mode).

I think it's sticky but often you'll see

AT+CMGF=1
AT+CMGS="+917xxxxxxxx"
> This is the SMS message
<ctrl+Z >

to ensure the modem is indeed in the right mode to send

Now its sending the sms only in two condition that is first and last condition . Its did not send the SMS in remaining condition.

here is my code

#include <SoftwareSerial.h>

SoftwareSerial GPRS(11, 10);
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define deviceAddress 0b1101000

#include <EEPROM.h>
#include <Wire.h>
const byte interruptPin = 2;
const uint8_t EngineCommand = 38;
int EventState = 0;
int EventState1 = 0;
int TempState = 0;
int TempState2 = 0;
int rpmState = 0;
int MtsState = 0;
int MtsState1 = 0;
int MtsState2 = 0;
int MtsState3 = 0;
int MtsState4 = 0;
int MtsState5 = 0;
int MtsState6 = 0;
int addr_secnd = 1;
int addr_Minute = 3;
int addr_hrs2 = 4;
int Hrs1;
int Hours1;
long lastTime = 1;


int NoDecHrs;
float Hmread;
float Hrsread;
int diffrE;
int diffrP;
int diffrT;
int diffrH;
int diffrF;
int addr_HMR = 30;
volatile byte state1 = HIGH;
volatile byte seconds = EEPROM.read(addr_secnd);
volatile byte minutes = EEPROM.read(addr_Minute);
volatile int hours = EEPROM.get(addr_hrs2, hours); //Hours1);
//int hours;
void setup() {
  Serial.begin(9600);
  Wire.begin();
  GPRS.begin(9600);
  rtc.begin();
  GPRS.println("AT+CMGF=1");
  delay(500);

  GPRS.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  pinMode(EngineCommand, INPUT_PULLUP);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);

  setSQW(0x00);
}
String time()
{
  String dataString = "";
  DateTime now = rtc.now();
  dataString += String(now.hour(), DEC);
  dataString += String(':');
  dataString += String(now.minute(), DEC);
  dataString += String(':');
  dataString += String(now.second(), DEC);

  return dataString;
}
void loop()
{

  DateTime now = rtc.now();
  //Serial.println(diffrP);
  //Serial.println(time());
  //Serial.print("timeTxt.txt=");
  String Hours = String(hours) + (".") + String(minutes);
  Hmread = Hours.toFloat();
  diffrP = (NoDecHrs - 60);
  diffrT = (NoDecHrs - 40);
  diffrH = (NoDecHrs - 20);
  diffrF = (NoDecHrs - 10);
  if (digitalRead(EngineCommand) == HIGH)
  {
    while (EventState < 1)
    {

      EEPROM.update(addr_secnd, seconds);
      EEPROM.update(addr_Minute, minutes);
      EEPROM.put(addr_hrs2, hours);
      EEPROM.put(addr_HMR, Hmread);
      EEPROM.get(addr_HMR, Hrsread);
      Serial.println(Hrsread);
      NoDecHrs = Hrsread;
      EventState++;
      ///Serial.println (NoDecHrs);
    }

    if (diffrP > 10)
    {

      if (MtsState2 < 1)
      {
        Serial.print("diffrP:");
        Serial.println(diffrP);
        String message = String (" Please ready for PTO maints.: ") + (String) diffrP;
        GPRS.println("AT+CMGF=1");
        delay(500);
        GPRS.println("AT+CMGS=\"+917987553768\"");
        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character

        delay(200);

        MtsState2++;
      }
    }
    if (diffrT > 10)
    {

      if (MtsState3 < 1)
      {
        Serial.print ("diffrT:");
        Serial.println(diffrT);

        String message = String (" Please ready for Tm maints.: ") + (String) diffrT;
        GPRS.println("AT+CMGF=1");
        delay(500);
        GPRS.println("AT+CMGS=\"+917987553768\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(200);

        MtsState3++;
      }
    }
    if (diffrE > 10)
    {
      if (MtsState4 < 1)
      {
        Serial.print("diffrE:");
        Serial.println (diffrE);
        String message = String (" Please ready for Eng maints.: ") + (String) diffrT;
        GPRS.println("AT+CMGF=1");
        delay(500);
        GPRS.println("AT+CMGS=\"+917987553768\"");

        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState4++;
      }
    }
    if (diffrH > 10)
    {
      if (MtsState5 < 1)
      {
        Serial.print ("diffrH:");
        Serial.println (diffrH);
        String message = String (" Please ready for HYD maints.: ") + (String) diffrH;

        GPRS.println("AT+CMGF=1");
        delay(500);
        GPRS.println("AT+CMGS=\"+917987553768\"");
        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState5++;
      }
    }
    if (diffrF > 10)
    {

      if (MtsState6 < 1)
      {
        Serial.print ("diffrF:");
        Serial.println (diffrF);
        String message = String (" Please ready for FD maints.: ") + (String) diffrF;
        GPRS.println("AT+CMGF=1");
        delay(500);
        GPRS.println("AT+CMGS=\"+917987553768\"");
        delay(500);
        GPRS.println (message);
        GPRS.write( 0x1a ); // ctrl+Z character
        delay(500);
        MtsState6++;
      }

    }
  }
  // Serial.println(Hours);
  if (digitalRead(EngineCommand) == LOW)
  {
    Serial.print("Hours:");
    Serial.println(Hours);
    MtsState = 0;
    MtsState1 = 0;
    MtsState2 = 0;
    MtsState3 = 0;
    MtsState4 = 0;
    MtsState5 = 0;
    MtsState6 = 0;

  }

}
void blink()
{
  if (digitalRead(EngineCommand) == LOW)
  {

    EventState = 0;

    if (lastTime > 0)
    {
      seconds++;
    }
    if (seconds == 4)
    {
      minutes++;
      seconds = 0;
    }

    if (minutes == 2)
    {
      hours++;
      minutes = 0;
    }
    state1 = !state1;

  }
}

void setSQW(uint8_t value) {
  Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
  Wire.write(0x0E); //Control Register Address is queued
  Wire.write(0x00); //Data for Control Register is queued
  Wire.endTransmission(); //queued information are transferred under ACK; STOP
}//+CMT: "+9195756

Serial monitor o/p

130.10
diffrP:70
diffrT:90
diffrH:110
diffrF:120

Expecting favorable advice

After adding the lineAT+CMGF=1 in the each condition, module sending sms in only first and second condition....Why?

you might have timing issues and you try to send new commands to your module whilst it is still busy sending the SMS.

➜ after sending an SMS, increase the delay(200) to something longer

GPRS.write( 0x1a ); // ctrl+Z character
delay(3000); // for example try with 3 seconds 

ideally you would listen back every time you send a command and see if the module said OK etc before you take the next step - without trying to second guess the timing with the fixed delays

yes ...after adjusting delay between the each conditions, its working good.
thanks

good

so next step = listen to the module after each command and wait for the OK (and handle errors)

:wink:

Yes ..... I checked with different delayS, in 3sec its sending all sms and i fixed that delay :slightly_smiling_face: :smiley:

that's not the point. One day 3s won't be enough and you'll fail again (or it's way too long and your code is slow / blocked for no reason and you might miss stuff)

the best way to handle this is to send the command and then listen to what the module says back to you. once you get the 'OK' (or whatever) you know the command was processed and you can continue.

second guessing the timing on something asynchronous is error prone

How can achieve that i had insert this line for getting get Module returns "OK"

if (GPRS.available())
  {
    String MntIntr=GPRS.readStringUntil('\n');
    Serial.println(MntIntr);
  }

its not working!!

what's not working?
sometimes the answer has multiple lines

its working

here is serial o/p

132.10
diffrP:72
AT+CMGF=1
diffrT:92
diffrH:112
diffrF:122

i think i have to compare this string with preetrmined string and if condition is true ( AT+CMGF=1) module will send next sms...am i right?

yes.

I've a small class StreamDevice that does deal with it.
I've a small explanation in the French forum ➜ Gestion de commandes sur port Série
if you don't read French, google translate can help out
the code is commented in English and there is a simple example

for your case you would declare

#include <StreamDevice.h>
#include <SoftwareSerial.h>
SoftwareSerial GPRS_Serial(11, 10);
StreamDevice GPRS(GPRS_Serial);

do a GPRS_Serial.begin(9600); in the setup

and then when you want to send a command you can do as before

GPRS.println("AT+CMGF=1");

and then await for the OK

  GPRS.endCommand("OK", 5000ul);      // Once done, tell the StreamDevice that the command is done and we expect to receive an "OK" as part of the answer  (wait default to 1s, you can add the timeout as a parameter - herer 5s)

  if (GPRS.awaitKeyword() == KEYWORD_OK)  // this is blocking until timeout or correct answer whichever comes first
    Serial.println(F("all good"));
  else
    Serial.println(F("error"));

there is also an API to capture the full answer if you need to

Tomorrow i will try and get back you..

When @sreekanthmp sorts out all his timing issues and gets reliable code, he can go back and test this.