Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h> //
- #include <SD.h> //
- #include <Wire.h> //
- #include "RTClib.h" //
- #include <SoftwareSerial.h>
- int pumpRelayPin = 9;
- int sensorPin = A0;
- int sensorValue = 0;
- int percent = 0;
- unsigned long wateringDelay = 1000 * 1 ; //Time in Seconds
- unsigned long readingDelay = 1000 * 5 ;
- int pumpStatus = 0;
- //SIM800L TX is connected to Arduino D7
- #define SIM800L_TX_PIN 7
- //SIM800L RX is connected to Arduino D6
- #define SIM800L_RX_PIN 6
- //Create software serial object to communicate with SIM800L
- SoftwareSerial serialSIM800L(SIM800L_TX_PIN, SIM800L_RX_PIN);
- // A simple data logger for the Arduino analog pins
- // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
- #define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
- #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
- uint32_t syncTime = 0; // time of last sync()
- int buttonReading = 1;
- #define ECHO_TO_SERIAL 1 // echo data to serial port
- #define WAIT_TO_START 0 // Wait for serial input in setup()
- RTC_DS1307 RTC; // define the Real Time Clock object
- // for the data logging shield, we use digital pin 10 for the SD cs line
- const int chipSelect = 10;
- // the logging file
- File logfile;
- void error(char *str)
- {
- Serial.print("error: ");
- Serial.println(str);
- while (1);
- }
- void setup() {
- Serial.begin(9600);
- SPI.begin(); // Initiate SPI bus
- //Being serial communication witj Arduino and SIM800L
- serialSIM800L.begin(9600);
- delay(15000);
- Serial.println("Setup Complete!");
- pinMode(pumpRelayPin, OUTPUT);
- pinMode(sensorPin, INPUT);
- digitalWrite(pumpRelayPin, LOW);
- #if WAIT_TO_START
- Serial.println("Type any character to start");
- while (!Serial.available());
- #endif //WAIT_TO_START
- // initialize the SD card
- Serial.print("Initializing SD card...");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(10, OUTPUT);
- // pinMode(buttonPin, INPUT);
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- error("Card failed, or not present");
- }
- Serial.println("card initialized.");
- // create a new file
- char filename[] = "LOGGER00.CSV";
- for (uint8_t i = 0; i < 100; i++) {
- filename[6] = i / 10 + '0';
- filename[7] = i % 10 + '0';
- if (! SD.exists(filename)) {
- // only open a new file if it doesn't exist
- logfile = SD.open(filename, FILE_WRITE);
- break; // leave the loop!
- }
- }
- if (! logfile) {
- error("couldnt create file");
- }
- Serial.print("Logging to: ");
- Serial.println(filename);
- // connect to RTC
- Wire.begin();
- if (!RTC.begin()) {
- logfile.println("RTC failed");
- #if ECHO_TO_SERIAL
- Serial.println("RTC failed");
- #endif //ECHO_TO_SERIAL
- }
- logfile.println("Date-Time,RAW Values,Percent");
- #if ECHO_TO_SERIAL
- Serial.println(" Date-Time \t\t\t RAW Value \t Percent");
- #endif //ECHO_TO_SERIAL
- }
- void loop() {
- DateTime now = RTC.now();
- sensorValue = analogRead(sensorPin);
- percent = convertToPercent(sensorValue);
- Serial.println(pumpStatus);
- if (pumpStatus == 0 && percent < 90) {
- digitalWrite(pumpRelayPin, HIGH);
- sendSMSon();
- pumpStatus = 1;
- Serial.println("Text is Sent");
- }
- else if (pumpStatus == 1 && percent < 90) {
- digitalWrite(pumpRelayPin, HIGH);
- pumpStatus = 1;
- Serial.println(pumpStatus);
- }
- else if (pumpStatus == 1 && percent > 90) {
- digitalWrite(pumpRelayPin, LOW);
- sendSMSoff();
- pumpStatus = 0;
- Serial.println(pumpStatus);
- }
- printValuesToSerial();
- printValuesToSDcard();
- delay(readingDelay);
- }
- void printValuesToSDcard() {
- DateTime now;
- // fetch the time
- now = RTC.now();
- // log time
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- delay(10);
- logfile.print(", ");
- logfile.print(sensorValue);
- delay(10);
- logfile.print(", ");
- logfile.print(percent);
- logfile.println();
- if ((millis() - syncTime) < SYNC_INTERVAL) return;
- syncTime = millis();
- // blink LED to show we are syncing data to the card & updating FAT!
- logfile.flush();
- delay(1000);
- }
- int convertToPercent(int value)
- {
- int percentValue = 0;
- percentValue = map(value, 1023, 465, 0, 100);
- return percentValue;
- }
- void printValuesToSerial()
- {
- DateTime now;
- // fetch the time
- now = RTC.now();
- Serial.print(' ');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print(' ');
- Serial.print("\t\t ");
- Serial.print(sensorValue);
- Serial.print("\t\t ");
- Serial.print(percent);
- Serial.print("%");
- Serial.println();
- }
- void sendSMSon() {
- Serial.println("Sending Text...");
- serialSIM800L.print("AT+CMGF=1\r"); // Set the shield to SMS mode
- delay(100);
- serialSIM800L.print("AT+CMGS=\"0556983765\"\r");
- delay(200);
- serialSIM800L.print("Water Pump is ON ");
- serialSIM800L.print("Sensor Value:");
- serialSIM800L.print(sensorValue);
- serialSIM800L.print(" , Moisture:");
- serialSIM800L.print(percent);
- serialSIM800L.print("%");
- serialSIM800L.print("\r"); //the content of the message
- delay(500);
- serialSIM800L.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
- delay(100);
- serialSIM800L.println();
- Serial.println("Text Sent.");
- delay(500);
- }
- void sendSMSoff() {
- Serial.println("Sending Text...");
- serialSIM800L.print("AT+CMGF=1\r"); // Set the shield to SMS mode
- delay(100);
- serialSIM800L.print("AT+CMGS=\"0556983765\"\r");
- delay(200);
- serialSIM800L.print("Water Pump is OFF ");
- serialSIM800L.print("Sensor Value:");
- serialSIM800L.print(sensorValue);
- serialSIM800L.print(" , Moisture:");
- serialSIM800L.print(percent);
- serialSIM800L.print("%");
- serialSIM800L.print("\r"); //the content of the message
- delay(500);
- serialSIM800L.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
- delay(100);
- serialSIM800L.println();
- Serial.println("Text Sent.");
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement