#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial GPRS(7, 8);
unsigned long interval = 120000; // the time we need to wait for switching off motor dry run (100 seconds interval)
unsigned long previousMillis = 0; // millis() returns an unsigned long.
void SendTextMessage(String s, bool b1, bool b2);
void serialMonitorPrint1(String s1, String s2);
void serialMonitorPrint(String s1, unsigned long s2);
void RecieveTextMessage();
void nonBlockingDelay();
void myserialEvent();
void check(char p[]);
int led = 9; // led for indicating that i received sms
int motor1 = 10; // motor pin connection
int dryRun = 11; // sensor for identifying water flow and used for dry run
//int smsPin = 3; // testing the simulation with SMS RECEIVED PIN
int sms_count = 0; //count the SMS sent
int sms_count1 = 0; //count the SMS sent
String EEPROMValue = " ";
bool timerlock = false;
int analogPin0 = A0;
int analogPin1 = A1;
int analogPin2 = A2;
int registration = 0;
char example_string[] = "0000000000";
int temp = 0;
int i = 0;
char inchar; //Will hold the incoming character from the Serial Port.
int analogval0 = 0;
int analogval1 = 0;
int analogval2 = 0;
int dryRunvalue = 0;
int threshold = 1000;
int ALLPHASEOK = 5; //ALL Phase OK LED
int PHASEMISSING = 6; //Phase missing LED
bool motorState = false; // state variable for the motor operation
bool waterflowing = true;
bool smsReceived = false;
bool count = true;
char str[15]; //string that holds final value received through SMS
char number[20];
String stringreceived;
char phonenumber[] = "0000000000"; //string that stores new phone number
String changedphonenumber = "";
const int eeprom_size = 10; // values saved in eeprom should never exceed 500 bytes
char eeprom_buffer[eeprom_size];
void setup() {
delay(10000);
delay(10000);
// clear eeprom on new flash... just for debugging
//for (int i = 0 ; i < EEPROM.length() ; i++) {
// EEPROM.write(i, 0);
// }
if (EEPROM.read(800) == 1)
{
read_string_from_eeprom(eeprom_buffer);
Serial.println("check the value ");
String storednumber(eeprom_buffer);
Serial.println(storednumber);
storednumber.trim();
changedphonenumber = storednumber;
}
else
{
changedphonenumber = "0000000000";
}
sms_count = 0;
sms_count1 = 0;
Serial.begin(9600);
GPRS.begin(9600);
delay(1000);
GPRS.println("AT+IPR=9600");
Serial.println("AT+IPR=9600");
delay(1000);
GPRS.println("AT+CNMI=2,2,0,0,0");
Serial.println("AT+CNMI=2,2,0,0,0");
delay(1000);
GPRS.println("AT+CMGF=1");
Serial.println("AT+CMGF=1");
delay(1000);
//GPRS.println("AT+CFUN=1");
//Serial.println("AT+CFUN=1");
//delay(1000);
GPRS.println("AT+CMGD=1");
Serial.println("AT+CMGD=1");
delay(1000);
//Send new SMS command and message number
//GPRS.println("AT+CMGS=\"+910000000000\"\r");
//Serial.println("AT+CMGS=\"+910000000000\"\r");
//delay(1000);
//Send SMS content
//GPRS.println("TEST");
//Serial.println("TEST");
//delay(1000);
//GPRS.println("\r"); //the content of the message
//delay(1000);
//Send Ctrl+Z / ESC to denote SMS message is complete
//GPRS.println((char)26);
//Serial.println((char)26);
//delay(1000);
//Serial.println("SMS Sent!");
//GPRS.println("ATD0000000000;");//dial the number, must include country code
//Serial.println("CALL!");
//delay(1000);
pinMode(ALLPHASEOK, OUTPUT);
pinMode(PHASEMISSING, OUTPUT);
pinMode(motor1, OUTPUT);
pinMode(dryRun, INPUT);
//pinMode(smsPin, INPUT);
// READ ANALOG PINS...............................................
analogval0 = analogRead(analogPin0);
analogval1 = analogRead(analogPin1);
analogval2 = analogRead(analogPin2);
if (analogval0 >= threshold && analogval1 >= threshold && analogval2 >= threshold)
{
//SendTextMessage("All phase are ok");
Serial.println("All phase are ok : SMS Sent");
SendTextMessage("SYSTEM READY FOR YOU", false, false);
digitalWrite(ALLPHASEOK, HIGH);
digitalWrite(PHASEMISSING, LOW);
}
else
{
SendTextMessage("SYSTEM READY BUT 3PHASE NOT AVAILABLE", false, false);
digitalWrite(ALLPHASEOK, LOW);
digitalWrite(PHASEMISSING, HIGH);
delay(1000);
}
}
void loop() {
if (GPRS.available() > 0)
{
myserialEvent();
}
analogval0 = analogRead(analogPin0);
analogval1 = analogRead(analogPin1);
analogval2 = analogRead(analogPin2);
if (analogval0 < threshold || analogval1 < threshold || analogval2 < threshold)
{
sms_count1 = 0; //reset the counter for else block
digitalWrite(ALLPHASEOK, LOW);
digitalWrite(PHASEMISSING, HIGH);
// sending one time sms
while (sms_count < 1) //Number of SMS Alerts to be sent limited at 3
{
Serial.println("THREE PHASE NOT AVAILABLE & MOTER IS IN OFF");
SendTextMessage("THREE PHASE NOT AVAILABLE & MOTER IS IN OFF MODE", false, true);
}
count = true; // to freshly start the NON BLOCKING COUNTER
motorState = false; // to freshly start 20seconds Delay
delay(200);
digitalWrite(motor1, LOW); // Phase is missing or no power so motor will be switched off
}
else
{
//Serial.println("inside else statement");
sms_count = 0; //reset sms counter of if block
while (sms_count1 < 1) //Number of SMS Alerts to be sent limited at 3
{
SendTextMessage("THREE PHASE AVAILABLE ", true, false);
}
delay(200);
digitalWrite(ALLPHASEOK, HIGH);
digitalWrite(PHASEMISSING, LOW);
if (temp == 1)
{
Serial.println("......................................inside temp condition statement");
check(str);
temp = 0;
i = 0;
delay(1000);
}
if (motorState)
{
dryRunvalue = digitalRead(dryRun);
//serialMonitorPrint("dry run value : ", + dryRunvalue);
if ( dryRunvalue ) //water flow sensor....if pin 11 is active
{
nonBlockingDelay(); // create countdown delay of 10 minutes
}
else
{
count = true; // to freshly start the NON BLOCKING COUNTER
}
}
}
}// end of the loop
void nonBlockingDelay()
{
Serial.println("***********inside non blocking delay*************");
unsigned long currentMillis = millis(); // grab current time
if (count == true )
{
previousMillis = currentMillis;
}
count = false;
serialMonitorPrint("currentMillis : ", currentMillis);
Serial.println();
// check if "interval" time has passed (10,000 milliseconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
if (motorState)
{
Serial.println("*****************************Timing completed motor will off**************************************");
digitalWrite(motor1, LOW); // sets the motor to off state
SendTextMessage("MOTOR TRIP DUE TO DRY RUN ", false, false);
motorState = false;
count = true;
}
}
}
void myserialEvent()
{
Serial.write(GPRS.read());
if (GPRS.find("#F."))
{
while (GPRS.available())
{
char inChar = GPRS.read();
str[i++] = inChar;
if (inChar == '*')
{
temp = 1;
return;
}
}
}
}
void check(char msg[])
{
Serial.println("......................................inside temp condition statement");
//Serial.println("SMS RECEIVED : MOTOR WILL SWITCH ON or OFF BASED ON SMS DATA");
if (!(strncmp(msg, "ON", 2)))
{
motorSmsOn();
delay(200);
}
else if (!(strncmp(msg, "OFF", 3)))
{
motorSmsOff();
delay(200);
}
else if (!(strncmp(msg, "REG", 3)))
{
// Serial.print("Message received is : " + String(msg) );
stringreceived = String(msg);
delay(100);
stringreceived.remove(0, 3);
delay(100);
stringreceived.remove(12);
delay(100);
Serial.println("String received truncated is : " + stringreceived);
delay(100);
numberchangingsms();
str[0] = '\0' ;
delay(100);
char *cstr = &stringreceived[0u];
delay(100);
int p = 800;
EEPROM.write(p, 1); //test bit for confirming eprom write
delay(100);
save_string_to_eeprom(cstr); //EEPROM WRITE OF PHONE NUMBER
delay(1000);
}
}
void SendTextMessage(String SENDSMS, bool incAllphase, bool incNoPhase)
{
String temporary = "AT+CMGS=" +String('"') +"+"+ String(91)+changedphonenumber+"\""+"\r";
Serial.println(temporary);
//GPRS.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
GPRS.println(temporary); // Replace x with mobile number
delay(1000);
GPRS.println(SENDSMS);// The SMS text you want to send
delay(100);
GPRS.println((char)26);// ASCII code of CTRL+Z
delay(1000);
if (incAllphase)
{
sms_count1++;
}
if (incNoPhase)
{
sms_count++;
}
}
void motorSmsOn()
{
Serial.println("SMS RECEIVED : MOTOR SWITCHED ON");
digitalWrite(motor1, HIGH); // sets the motor to on state
SendTextMessage("MOTOR is SWITCHED ON", false, false);
motorState = true;
delay(2000);
GPRS.println("AT+CMGD=1");
delay(1000);
}
void motorSmsOff()
{
Serial.println("MOTOR SWITCHED OFF");
digitalWrite(motor1, LOW); // sets the motor to on state
SendTextMessage("MOTOR is SWITCHED OFF", false, false);
motorState = false;
delay(2000);
GPRS.println("AT+CMGD=1");
delay(1000);
}
void numberchangingsms()
{
Serial.println("NUMBER TO BE CHANGED");
SendTextMessage("NUMBER TO BE CHANGED PLEASE RESTART THE SYSTEM", false, false);
delay(2000);
GPRS.println("AT+CMGD=1");
delay(1000);
}
void serialMonitorPrint(String s1, unsigned long s2)
{
String s3 = s1 + s2;
Serial.println(s3);
}
void serialMonitorPrint1(String s1, String s2)
{
String s3 = s1 + s2;
Serial.println(s3);
}
void save_string_to_eeprom(char *stringIn) {
Serial.print(strlen(stringIn));
for (int i = 0; i < strlen(stringIn); i++) {
//Serial.print(stringIn[i]);
EEPROM.write(i, stringIn[i]);
}
}
//-------------------------- EEPROM READ--------------------------
void read_string_from_eeprom(char *bufferIn) {
for (int i = 0; i < eeprom_size; i++) {
bufferIn[i] = EEPROM.read(i);
}
}