Advertisement
safwan092

Fixed Code for Automatic Irrigation with SMS DataLogger RTC

Feb 18th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. #include <SPI.h> //
  2. #include <SD.h> //
  3. #include <Wire.h> //
  4. #include "RTClib.h" //
  5. #include <SoftwareSerial.h>
  6.  
  7. int pumpRelayPin = 9;
  8. int sensorPin = A0;
  9. int sensorValue = 0;
  10. int percent = 0;
  11. unsigned long wateringDelay = 1000 *       1     ; //Time in Seconds
  12. unsigned long readingDelay =  1000 *       5    ;
  13.  
  14. int pumpStatus = 0;
  15.  
  16.  
  17. //SIM800L TX is connected to Arduino D7
  18. #define SIM800L_TX_PIN 7
  19.  
  20. //SIM800L RX is connected to Arduino D6
  21. #define SIM800L_RX_PIN 6
  22.  
  23. //Create software serial object to communicate with SIM800L
  24. SoftwareSerial serialSIM800L(SIM800L_TX_PIN, SIM800L_RX_PIN);
  25.  
  26.  
  27.  
  28.  
  29. // A simple data logger for the Arduino analog pins
  30.  
  31. // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
  32. #define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
  33. #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
  34. uint32_t syncTime = 0; // time of last sync()
  35. int buttonReading = 1;
  36. #define ECHO_TO_SERIAL   1 // echo data to serial port
  37. #define WAIT_TO_START    0 // Wait for serial input in setup()
  38.  
  39.  
  40. RTC_DS1307 RTC; // define the Real Time Clock object
  41.  
  42. // for the data logging shield, we use digital pin 10 for the SD cs line
  43. const int chipSelect = 10;
  44.  
  45. // the logging file
  46. File logfile;
  47.  
  48. void error(char *str)
  49. {
  50.   Serial.print("error: ");
  51.   Serial.println(str);
  52.  
  53.   while (1);
  54. }
  55.  
  56. void setup() {
  57.   Serial.begin(9600);
  58.   SPI.begin();      // Initiate  SPI bus
  59.   //Being serial communication witj Arduino and SIM800L
  60.   serialSIM800L.begin(9600);
  61.   delay(15000);
  62.   Serial.println("Setup Complete!");
  63.  
  64.   pinMode(pumpRelayPin, OUTPUT);
  65.   pinMode(sensorPin, INPUT);
  66.  
  67.   digitalWrite(pumpRelayPin, LOW);
  68.  
  69. #if WAIT_TO_START
  70.   Serial.println("Type any character to start");
  71.   while (!Serial.available());
  72. #endif //WAIT_TO_START
  73.  
  74.   // initialize the SD card
  75.   Serial.print("Initializing SD card...");
  76.   // make sure that the default chip select pin is set to
  77.   // output, even if you don't use it:
  78.   pinMode(10, OUTPUT);
  79.   //  pinMode(buttonPin, INPUT);
  80.   // see if the card is present and can be initialized:
  81.   if (!SD.begin(chipSelect)) {
  82.     error("Card failed, or not present");
  83.   }
  84.   Serial.println("card initialized.");
  85.  
  86.   // create a new file
  87.   char filename[] = "LOGGER00.CSV";
  88.   for (uint8_t i = 0; i < 100; i++) {
  89.     filename[6] = i / 10 + '0';
  90.     filename[7] = i % 10 + '0';
  91.     if (! SD.exists(filename)) {
  92.       // only open a new file if it doesn't exist
  93.       logfile = SD.open(filename, FILE_WRITE);
  94.       break;  // leave the loop!
  95.     }
  96.   }
  97.  
  98.   if (! logfile) {
  99.     error("couldnt create file");
  100.   }
  101.  
  102.   Serial.print("Logging to: ");
  103.   Serial.println(filename);
  104.  
  105.   // connect to RTC
  106.   Wire.begin();
  107.   if (!RTC.begin()) {
  108.     logfile.println("RTC failed");
  109. #if ECHO_TO_SERIAL
  110.     Serial.println("RTC failed");
  111. #endif  //ECHO_TO_SERIAL
  112.   }
  113.  
  114.  
  115.   logfile.println("Date-Time,RAW Values,Percent");
  116. #if ECHO_TO_SERIAL
  117.   Serial.println(" Date-Time \t\t\t RAW Value \t Percent");
  118. #endif //ECHO_TO_SERIAL
  119.  
  120. }
  121.  
  122. void loop() {
  123.   DateTime now = RTC.now();
  124.  
  125.   sensorValue = analogRead(sensorPin);
  126.   percent = convertToPercent(sensorValue);
  127.  
  128.   Serial.println(pumpStatus);
  129.  
  130.   if (pumpStatus == 0 && percent < 90) {
  131.     digitalWrite(pumpRelayPin, HIGH);
  132.     sendSMSon();
  133.     pumpStatus = 1;
  134.     Serial.println("Text is Sent");
  135.   }
  136.   else if (pumpStatus == 1 && percent < 90) {
  137.     digitalWrite(pumpRelayPin, HIGH);
  138.     pumpStatus = 1;
  139.     Serial.println(pumpStatus);
  140.   }
  141.   else if (pumpStatus == 1 && percent > 90) {
  142.     digitalWrite(pumpRelayPin, LOW);
  143.     sendSMSoff();
  144.     pumpStatus = 0;
  145.     Serial.println(pumpStatus);
  146.   }
  147.  
  148.   printValuesToSerial();
  149.  
  150.   printValuesToSDcard();
  151.  
  152.  
  153.  
  154.   delay(readingDelay);
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. void printValuesToSDcard() {
  174.   DateTime now;
  175.   // fetch the time
  176.   now = RTC.now();
  177.   // log time
  178.   logfile.print('"');
  179.   logfile.print(now.year(), DEC);
  180.   logfile.print("/");
  181.   logfile.print(now.month(), DEC);
  182.   logfile.print("/");
  183.   logfile.print(now.day(), DEC);
  184.   logfile.print(" ");
  185.   logfile.print(now.hour(), DEC);
  186.   logfile.print(":");
  187.   logfile.print(now.minute(), DEC);
  188.   logfile.print(":");
  189.   logfile.print(now.second(), DEC);
  190.   logfile.print('"');
  191.   delay(10);
  192.   logfile.print(", ");
  193.   logfile.print(sensorValue);
  194.   delay(10);
  195.   logfile.print(", ");
  196.   logfile.print(percent);
  197.   logfile.println();
  198.   if ((millis() - syncTime) < SYNC_INTERVAL) return;
  199.   syncTime = millis();
  200.   // blink LED to show we are syncing data to the card & updating FAT!
  201.   logfile.flush();
  202.   delay(1000);
  203. }
  204.  
  205.  
  206.  
  207. int convertToPercent(int value)
  208. {
  209.   int percentValue = 0;
  210.   percentValue = map(value, 1023, 465, 0, 100);
  211.   return percentValue;
  212. }
  213.  
  214. void printValuesToSerial()
  215. {
  216.   DateTime now;
  217.   // fetch the time
  218.   now = RTC.now();
  219.   Serial.print(' ');
  220.   Serial.print(now.year(), DEC);
  221.   Serial.print("/");
  222.   Serial.print(now.month(), DEC);
  223.   Serial.print("/");
  224.   Serial.print(now.day(), DEC);
  225.   Serial.print(" ");
  226.   Serial.print(now.hour(), DEC);
  227.   Serial.print(":");
  228.   Serial.print(now.minute(), DEC);
  229.   Serial.print(":");
  230.   Serial.print(now.second(), DEC);
  231.   Serial.print(' ');
  232.  
  233.   Serial.print("\t\t ");
  234.   Serial.print(sensorValue);
  235.   Serial.print("\t\t ");
  236.   Serial.print(percent);
  237.   Serial.print("%");
  238.   Serial.println();
  239. }
  240.  
  241.  
  242.  
  243. void sendSMSon() {
  244.  
  245.   Serial.println("Sending Text...");
  246.   serialSIM800L.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  247.   delay(100);
  248.   serialSIM800L.print("AT+CMGS=\"0556983765\"\r");
  249.   delay(200);
  250.   serialSIM800L.print("Water Pump is ON   ");
  251.   serialSIM800L.print("Sensor Value:");
  252.   serialSIM800L.print(sensorValue);
  253.   serialSIM800L.print(" , Moisture:");
  254.   serialSIM800L.print(percent);
  255.   serialSIM800L.print("%");
  256.   serialSIM800L.print("\r"); //the content of the message
  257.   delay(500);
  258.   serialSIM800L.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  259.   delay(100);
  260.   serialSIM800L.println();
  261.   Serial.println("Text Sent.");
  262.   delay(500);
  263.  
  264. }
  265.  
  266.  
  267. void sendSMSoff() {
  268.  
  269.   Serial.println("Sending Text...");
  270.   serialSIM800L.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  271.   delay(100);
  272.   serialSIM800L.print("AT+CMGS=\"0556983765\"\r");
  273.   delay(200);
  274.   serialSIM800L.print("Water Pump is OFF   ");
  275.   serialSIM800L.print("Sensor Value:");
  276.   serialSIM800L.print(sensorValue);
  277.   serialSIM800L.print(" , Moisture:");
  278.   serialSIM800L.print(percent);
  279.   serialSIM800L.print("%");
  280.   serialSIM800L.print("\r"); //the content of the message
  281.   delay(500);
  282.   serialSIM800L.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  283.   delay(100);
  284.   serialSIM800L.println();
  285.   Serial.println("Text Sent.");
  286.   delay(500);
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement