unsigned long pMillis = 0; // will store last time LED was updated long OnTime = 1; // milliseconds of off-time for E ~~ W long OffTime = 1; // milliseconds of on-time for E ~~ W unsigned long pMillisN = 0; // will store last time LED was updated long OnTimeN = 5000; // milliseconds of off-time for W ~~ E long OffTimeN = 10000; // milliseconds of on-time for W ~~ E boolean state1 = LOW; boolean state2 = LOW; ///--------------------------------------------------------------//// const int hot = 28; //set hot parameter const int cold = 25; //set cold parameter #include //addr, en,rw,rs,d4,d5,d6,d7,bl,blpol LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); /////////////////////////////////////////////// int Mode = 1; #define DP_Sensor 2 #define Engoff 3 #define Engon 4 #define Loop 13 #define SMS A0 #define MODE A1 #define ACK A2 #define Shutoff A3 #define RSTshield 9 int MODEState = 0; // variable for reading the pushbutton status int ACKState = 0; // variable for reading the pushbutton status int RunningState = HIGH; long previousMillis = 0; long interval = 2000; boolean DP_reading; int Solenoid = 5; boolean valve = true; boolean Triptrigger = true; #include #include #include SoftwareSerial SIM900(7, 8);//Configuring the serial pins by software String caracter ; // Variable to save the characters incoming messages String State = ""; //-------------------------------------------------------------------// char Reply[200]; // Serial buffer length int number = 43; // msg length int Signal; // Signal strength as reported int temp1; // 4 temporary integers, used to extract the data from the incoming serial. int temp2; int temp3; int temp4; int BER; // Bit error rate int SignaldBm; //Signal in dBm /////////////////////////////////////////////////////////////////////////// #define ONE_WIRE_BUS 10 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); float Celcius = 0; const int numReadingsC = 4; int readingsC[numReadingsC]; // the readings from the analog input int readIndexC = 0; // the index of the current reading int totalC = 0; // the running total int averageC = 0; String TMessage; String LMessage; //-------------------------------------------------------------------// #define triggerPIN 11 #define echoPIN 12 int returnCM; const int numReadingsCM = 4; int readingsCM[numReadingsCM]; // the readings from the analog input int readIndexCM = 0; // the index of the current reading int totalCM = 0; // the running total int averageCM = 0; // the averageCM byte Thermometer[8] = {B00100, B01010, B01010, B01110, B01110, B11111, B11111, B01110}; // Thermometer Symbol 0 byte ring[8] = {B00000, B00100, B01110, B01110, B01110, B11111, B00000, B00100}; // Ring Symbol 1 byte degree[8] = {B01100, B10010, B10010, B01100, B00000, B00000, B00000, B00000}; // degree Symbol 2 byte signalt[8] = {B11111, B01110, B01110, B00100, B00100, B00100, B00100, B00100}; // tower Symbol 3 byte level[8] = {B11111, B10001, B01010, B00100, B00100, B01010, B10101, B11111}; // levelel 4 byte signals1[8] = {B00000, B00000, B00000, B00000, B10000, B10000, B10000, B10000}; // signal1 5 byte signals2[8] = {B00000, B00000, B00100, B00100, B10100, B10100, B10100, B10100}; // signal2 6 byte signals3[8] = {B00001, B00001, B00101, B00101, B10101, B10101, B10101, B10101}; // signal3 7 void setup() { int percentage, heightTank; pinMode(triggerPIN, OUTPUT); // Set the trigPin as an Output (sr04t or v2.0) pinMode(echoPIN, INPUT_PULLUP); // Set the echoPin as an Input with pullup (sr04t or v2.0) for (int thisReadingCM = 0; thisReadingCM < numReadingsCM; thisReadingCM++) { readingsCM[thisReadingCM] = 0; } for (int thisReadingC = 0; thisReadingC < numReadingsC; thisReadingC++) { readingsC[thisReadingC] = 0; } //////////////////////////////////////////////////// Serial.begin(9600); lcd.begin(16, 2); Serial.println(F("Control System Starting")); pinMode(Solenoid, OUTPUT); digitalWrite(Solenoid, LOW); pinMode(DP_Sensor, INPUT); pinMode(MODE, INPUT); pinMode(ACK, INPUT); DP_reading = digitalRead(DP_Sensor); pinMode(Engoff, OUTPUT); digitalWrite(Engoff, LOW); pinMode(Engon, OUTPUT); digitalWrite(Engon, LOW); pinMode(Loop, OUTPUT); digitalWrite(Loop, LOW); pinMode(SMS, OUTPUT); digitalWrite(SMS, LOW); pinMode(Shutoff, OUTPUT); digitalWrite(Shutoff, LOW); // Automatically turn on the shield digitalWrite(RSTshield, HIGH); delay(1000); digitalWrite(RSTshield, LOW); delay(3000); /////////////////////////////////////////////////// SIM900.begin(9600); // pinMode(led, OUTPUT); SIM900.print("AT+CMGF=1\r");//AT command to configure the SIM900 in text mode delay(200); SIM900.print("AT+CNMI=2,2,0,0,0\r");//configure the module to show the SMS through the serial port. delay(200); SIM900.write("AT+CSQ\r"); //Send command for signal report delay (200); sensors.begin(); pinMode(LED_BUILTIN, OUTPUT); //////////////////////////////////////////////////////////////// lcd.clear(); } void loop() { unsigned long currentMillis = millis(); unsigned long currentMillis1 = millis(); unsigned long currentMillisN = millis(); unsigned long currentMillis1N = millis(); //////---------------------------------------------///////////////// sensors.requestTemperatures(); Celcius = sensors.getTempCByIndex(0); int percentage, heightTank, deviation; //////////////////////////////////////////////// totalC = totalC - readingsC[readIndexC]; readingsC[readIndexC] = Celcius; totalC = totalC + readingsC[readIndexC]; readIndexC = readIndexC + 1; if (readIndexC >= numReadingsC) { readIndexC = 0; } averageC = totalC / numReadingsC; // Serial.println(averageC); delay(1); /////////////////////////////////////////////////////////////// heightTank = 110; deviation = 23; int distanceCM = 0; unsigned long durationMS = 0; // Do sounding here distanceCM = 0; durationMS = 0; // Clear the trigger pin digitalWrite(triggerPIN, LOW); delayMicroseconds(1); // Sets the trigger on HIGH state for 20 micro seconds digitalWrite(triggerPIN, HIGH); delayMicroseconds(20); digitalWrite(triggerPIN, LOW); // wait for the echo durationMS = pulseIn(echoPIN, HIGH); // Calculating the distance distanceCM = (((int) durationMS * 0.0357) / 2); totalCM = totalCM - readingsCM[readIndexCM]; readingsCM[readIndexCM] = distanceCM; totalCM = totalCM + readingsCM[readIndexCM]; readIndexCM = readIndexCM + 1; if (readIndexCM >= numReadingsCM) { readIndexCM = 0; } averageCM = totalCM / numReadingsCM; percentage = 100 - (((averageCM - deviation) * 100) / heightTank); if (percentage > 100) { lcd.createChar(4, level); lcd.setCursor(11, 1); lcd.write((uint8_t)4); lcd.print(" "); lcd.print("HH "); } else if (percentage < 10) { lcd.createChar(4, level); lcd.setCursor(11, 1); lcd.write((uint8_t)4); lcd.print(" "); lcd.print("LL "); } else { lcd.createChar(4, level); lcd.setCursor(11, 1); lcd.write((uint8_t)4); lcd.print(" "); lcd.print(percentage); lcd.print("%"); lcd.print(" "); } TMessage = averageC; LMessage = percentage; //////////////////////////////////////////////// MODEState = digitalRead(MODE); ACKState = digitalRead(ACK); if (digitalRead(MODE) == HIGH) //if button is pressed { Mode++; if (Mode > 4) Mode = 1; //button State from 1 to 3 delay(10); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result. } switch (Mode) { case 1: lcd.setCursor(2, 0); lcd.print("OFF "); EngOFF(); // Measurment(); break; case 2: lcd.setCursor(2, 0); lcd.print("ON "); Measurment(); if (averageC > hot || percentage < 20 ) { EngOFF(); } else { EngON(); } if (digitalRead(ACK) == HIGH) { digitalWrite(SMS, LOW); lcd.setCursor(8, 1); lcd.write(" "); Triptrigger = true; EngON(); } break; case 3: lcd.setCursor(2, 0); lcd.print("AUTO"); if (digitalRead(DP_Sensor) != DP_reading ) { DP_reading = !DP_reading; previousMillis = millis(); } if (DP_reading) { if (millis() - previousMillis > interval) { previousMillis = millis(); if (RunningState == HIGH && (Triptrigger)) { EngOFF(); digitalWrite(SMS, HIGH); lcd.createChar(1, ring); lcd.setCursor(8, 1); lcd.write((uint8_t)1); // ring Symbol RunningState = LOW; Triptrigger = !Triptrigger; MSG(); } else { RunningState = HIGH; valve; // Serial.print(F("Engine Stop & Solenoid closed")); EngOFF(); } } } else { // Serial.println(F("Engine Running & Solenoid Open")); if (averageC > hot || percentage < 20 ) { if (valve) { EngOFF(); } } else { if (!valve) { EngON(); } } if (averageC <= cold || percentage > 30) { if (!valve ) { EngON(); } } } if (digitalRead(ACK) == HIGH) { digitalWrite(SMS, LOW); lcd.setCursor(8, 1); lcd.write(" "); Triptrigger = true; !valve; EngON(); } reply(); if (state1 == LOW && (currentMillis - pMillis >= OnTime)) { digitalWrite(Loop, HIGH); state1 = HIGH; pMillis = currentMillis; } else if (currentMillis - pMillis >= OffTime) { digitalWrite(Loop, LOW); state1 = LOW; pMillis = currentMillis; } ///---------------------//////////////////------------/////////////////////////////// //////////////----------------------///////////////////////--------------/////////////// break; case 4: lcd.setCursor(2, 0); lcd.print("Setg"); lcd.createChar(0, Thermometer); lcd.setCursor(0, 1); lcd.write((uint8_t)0); // Temperature Symbol lcd.write("H"); lcd.createChar(0, Thermometer); lcd.setCursor(4, 1); lcd.write((uint8_t)0); // Temperature Symbol lcd.write("C"); lcd.createChar(4, level); lcd.setCursor(11, 1); lcd.write((uint8_t)4); lcd.print(".."); break; } Measurment(); lcd.setCursor(0, 0); lcd.print("M:"); lcd.setCursor(7, 0); lcd.print("S:"); Serial.println(" "); if (state2 == LOW && (currentMillisN - pMillisN >= OnTimeN)) { GetSignal(); pMillisN = currentMillisN; // Remember the time state2 = HIGH; } else if (currentMillisN - pMillisN >= OffTimeN) { // do somthing pMillisN = currentMillisN; // Remember the time state2 = LOW; } } // Loop END /////////////////////////////////////////////////////////////////////// //////////////////////////////// void EngON() { digitalWrite(Solenoid, LOW); digitalWrite(Engon, HIGH); digitalWrite(Engoff, LOW); digitalWrite(Loop, LOW); lcd.setCursor(9, 0); lcd.print("Run "); } void EngOFF() { digitalWrite(Solenoid, HIGH); digitalWrite(Engoff, HIGH); digitalWrite(Engon, LOW); digitalWrite(Loop, LOW); lcd.setCursor(9, 0); lcd.print("Stop"); } void Measurment() { if ( averageC > hot) { lcd.createChar(0, Thermometer); lcd.setCursor(0, 1); lcd.write((uint8_t)0); // Temperature Symbol lcd.print(" "); lcd.print("HH "); lcd.print(" "); } else if (averageC == -127) { lcd.setCursor(2, 1); lcd.print("Er"); } else { lcd.createChar(0, Thermometer); lcd.setCursor(0, 1); lcd.write((uint8_t)0); // Temperature Symbol lcd.print(" "); lcd.print(averageC); lcd.createChar(2, degree); lcd.setCursor(4, 1); lcd.write((uint8_t)2); // Temperature Symbol lcd.print("C"); } } /////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// void GetSignal() { SIM900.write("AT+CMGF=1\r"); //set GSM to text mode delay (150); SIM900.write("AT+CSQ\r"); //Send command for signal report delay (200); if (SIM900.available() > 0 ) { //wait for responce for (int i = 0; i < 44; i++) //read character into array from serial port Reply[i] = SIM900.read(); } Reply[199] = '/0'; temp1 = Reply[31] - '0'; //convert relevant characters from array into integers temp2 = Reply[32] - '0'; temp3 = Reply[33] - '0'; temp4 = Reply[34] - '0'; if (temp1 == -48) { //if temp1 is -48 then do it again as the data is not valid (yet) GetSignal(); } if (temp3 == -4) { // use temp3 to determine where in the array the relevent integers are (-4 is a ",") Signal = temp2 + temp1 * 10; // calculate signal if the first digit is a multiple of 10 BER = temp4; } else { Signal = temp1; //calculate signal if the first digit is not a multiple of 10 BER = temp3; } if ( Signal == 99) { // if our signal is 99, IE no signal condition , the return a signal of -1 Signal = -1; lcd.setCursor(15, 0); lcd.write(" "); } SignaldBm = Signal * 2 - 113; // calculate dBm for geeks like me. Serial.print ("Signal: "); //output stats to serial interface. Serial.print (" "); Serial.print (Signal); Serial.print (" "); Serial.print (SignaldBm); Serial.print (" "); Serial.print ("dBm"); Serial.print (" "); Serial.print ("BER: "); Serial.print (" "); Serial.println (BER); lcd.createChar(3, signalt); lcd.setCursor(14, 0); lcd.write((uint8_t)3); if (SignaldBm >= -73 && SignaldBm <= -53 && BER == 0) { lcd.createChar(7, signals3); lcd.setCursor(15, 0); lcd.write((uint8_t)7); } if (SignaldBm >= -93 && SignaldBm <= -75 && BER == 0) { lcd.createChar(6, signals2); lcd.setCursor(15, 0); lcd.write((uint8_t)6); } if (SignaldBm >= -109 && SignaldBm <= -95 && BER == 0) { lcd.createChar(5, signals1); lcd.setCursor(15, 0); lcd.write((uint8_t)5); } if (SignaldBm >= -113 && SignaldBm <= -111) { lcd.setCursor(15, 0); lcd.write(" "); } } void reply() { if (SIM900.available() > 0) { //We check if there is data available from the SIM900 caracter = SIM900.readString(); // We read the data and the weights in the message variable Serial.print(caracter); //Prints the incoming data one by one on the serial terminal delay(10);} if(caracter.indexOf("OFF")>=0){ Serial.println("-->Sending message"); State = "Engine off"; SendingMessage(State); EngOFF(); !valve; } if(caracter.indexOf("ON")>=0) { Serial.println("-->Sending message"); State = "Engine on"; SendingMessage(State); EngON(); valve; } if(caracter.indexOf("STATE")>=0){ Serial.println("-->Sending message"); SendingMessage(State); Serial.println("Engine state resquest"); } } void SendingMessage(String State) { // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); SIM900.println("AT + CMGS = \"+xxxxxxxxxxxxxxx\"");//replace by the number to send the message delay(100); SIM900.println("State:" + State);// Replace with the text to be sent delay(100); //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS SIM900.println((char)26); delay(100); SIM900.println(); delay(3000); } void MSG() { // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); SIM900.println("AT + CMGS = \"+xxxxxxxxxxxxxxx\"");//replace by the number to send the message delay(100); SIM900.println("T:" + TMessage + "*C " + " L:" + LMessage + "% "+ "State Stop");// Replace with the text to be sent delay(100); //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS SIM900.println((char)26); delay(100); SIM900.println(); delay(3000); }