Arduino Code:
  1. #include "SoftwareSerial.h"
  2. #include "String.h"
  3. SoftwareSerial mySerial(7, 8);
  4.  
  5. const int L1 = 5; //Low level sensor connected to pin 2
  6.  
  7. const int L4 = 2; //High Level Sensor connected to pin 5
  8.  
  9. const int pump = 11; //The pin to which the pump is connected
  10. const int buzzer = 10; //Th epin to which the buzzer is connected
  11. const int LED = 13;
  12.  
  13. int L1Status;
  14. int lastL1Status;
  15.  
  16. int L4Status;
  17. int lastL4Status = LOW;
  18.  
  19. int iTemp = 0; //Temp int var to be used in timer counter
  20.  
  21. void setup()
  22. {
  23. Serial.begin(9600); //initialize the serial communication for debugging
  24. delay(500);
  25. Serial.println("Ready");
  26. mySerial.begin(19200); // the GSM Shield baud rate
  27. delay(500);
  28.  
  29. pinMode(L1, INPUT);
  30. pinMode(L4, INPUT);
  31.  
  32. pinMode(pump, OUTPUT);
  33. pinMode(buzzer, OUTPUT);
  34.  
  35. pinMode(LED, OUTPUT);
  36. }
  37. void loop()
  38. {
  39. L1Status = digitalRead(L1); //get the Low level sensor state
  40. L4Status = digitalRead(L4); //Get the High Level Sensor State
  41.  
  42.  
  43. if (L1Status == LOW) { //if the Tank is empty
  44. digitalWrite(pump, HIGH); //Turn on the Pump
  45. digitalWrite(LED, HIGH); //Turn on LED, indicating the Pump is ON
  46. Serial.println("Pump is ON");
  47. iTemp = 0;
  48. }
  49. if (L4Status == HIGH) {
  50. digitalWrite(pump, LOW); //Turn off the Pump
  51. digitalWrite(LED, LOW); //Turn Off the Indicating LED, to show the pump is OFF
  52. Serial.println("Pump is OFF");
  53. }
  54.  
  55. if ( L4Status != lastL4Status) { //if the state has changed
  56. if (L4Status == HIGH) {
  57. //if the current state is HIGH then HIgh Level Sensor has gone from Off to On
  58. //The code for this condition //The tank is Full
  59. //Dial a Missed Call, to indicate the Tank has been filled
  60. Serial.println("The High Level Sensor Has Toggled from OFF to ON");
  61. digitalWrite(buzzer, HIGH);
  62. powerUp();
  63. delay(100);
  64. DialMissCall();
  65. delay(100);
  66. powerDown();
  67. digitalWrite(buzzer, LOW);
  68. }
  69. lastL4Status = L4Status;
  70. }
  71. check_timer();
  72. }
  73. void check_timer() {
  74. iTemp++;//Incremented at an interval of 1 sec
  75. delay(1000);
  76. if (iTemp == 30) //Upper limit of delay to Stop Motor (in secs)
  77. {
  78. if (L1Status == LOW) //If the water level has still not increased
  79. Serial.println("The Water Level is Not Increasing, is the Ground Resovoir Tank empty?");
  80. digitalWrite(pump, LOW); //Turn off the pump
  81. digitalWrite(LED, LOW); //Turn Off the Indicating LED, to show the pump is OFF
  82. Serial.println("The Pump Has Been Turned OFF to Protect it");
  83.  
  84. Serial.println("Sending a Message to the Owner about the problem");
  85. powerUp();
  86. delay(100);
  87. SendTextMessage();//Send a warning Test Message
  88. delay(100);
  89. powerDown();
  90. Serial.println("Waiting till a Manual Reset");
  91. wait();
  92. }
  93. }
  94.  
  95. void wait() {
  96. while (1) {
  97. delay(1000);
  98. Serial.println("Is everything All Right !");
  99. Serial.println("Check the Ground Reservoir, and Reset Manually");
  100. }
  101. }
  102.  
  103. void powerUp()//The following code is power up subroutine for Arduino if using software triger:
  104. {
  105. pinMode(9, OUTPUT);
  106. digitalWrite(9, LOW);
  107. delay(1000);
  108. digitalWrite(9, HIGH);
  109. delay(2000);
  110. digitalWrite(9, LOW);
  111. delay(3000);
  112. }
  113.  
  114. void powerDown()//The following code is power down subroutine for Arduino if using software triger:
  115. {
  116. pinMode(9, OUTPUT);
  117. digitalWrite(9, LOW);
  118. delay(1000);
  119. digitalWrite(9, HIGH);
  120. delay(2000);
  121. digitalWrite(9, LOW);
  122. delay(3000);
  123. }
  124.  
  125. void SendTextMessage()//this function is to send a sms message
  126. {
  127. mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
  128. delay(100);
  129. mySerial.println("AT + CMGS = \"+919938040894\"");//send sms message, be careful need to add a country code before the cellphone number
  130. delay(100);
  131. mySerial.println("Something is Wrong with your Automatic Level Controller! Please Check it Manually.");//the content of the message
  132. delay(100);
  133. mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
  134. delay(100);
  135. mySerial.println();
  136. }
  137.  
  138. //this function is to dial a voice call and Dissconnect it after 10 Seconds
  139.  
  140. void DialMissCall()
  141. {
  142. mySerial.println("ATD + +919938040894;");//dial the number
  143. delay(100);
  144. delay(10000);
  145. mySerial.println("ATH + 0");
  146. delay(100);
  147. mySerial.println();
  148. }