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
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.
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
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
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
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