1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM900(3, 4); // Pins 3, 4 are used as used as software serial pins
//#define SIM900 Serial
String incomingData; // for storing incoming serial data
String message = ""; // A String for storing the message
int relay_pin = 2 ; // Initialized a pin for relay module
int relay_pin2 = 5; // Initialized a pin for relay module
String rtl; // to start from pump 1 or pump 2
String noMessage; // if sending confirmation SMS
String noMessage2; // if sending confirmation SMS
unsigned long previousMillis = 0;
long duration; // for storing pump working duration
void setup()
{
delay(10000); //Wait for SIM800 initialisation
Serial.begin(115200); // baudrate for serial monitor
SIM900.begin(19200); // baudrate for GSM shield
pinMode(relay_pin, OUTPUT); // Setting relay pin as output pin
digitalWrite(relay_pin, HIGH); // Making relay pin initailly low
pinMode(relay_pin2, OUTPUT); // Setting relay pin as output pin
digitalWrite(relay_pin2, HIGH); // Making relay pin initailly low
// set SMS mode to text mode
SIM900.println("AT+CMGF=1\r");
delay(100);
// set gsm module to tp show the output on serial out
SIM900.println("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop()
{
//Function for receiving sms
receive_message();
rtl = incomingData.substring(9,8); //Check if "R" in SMS
// if received command is to turn on relay
if(incomingData.indexOf("W_on")>-1)
{
//if reverse command received
if(rtl == "R"){
relay_pin = 5;
relay_pin2 = 2;
}
digitalWrite(relay_pin, LOW); //relay ON
//Duration setup according SMS
if(stringToLong(incomingData.substring(5))!= 0){
duration = stringToLong(incomingData.substring(5));
}
else {
duration = 7;
}
message = "Arrosage ON " + String(duration) + " minutes" ;
// Send a sms back to confirm that the relay is turned on
noMessage = incomingData.substring(11,10); //find "N" in SMS to send message or not
noMessage2 = incomingData.substring(9,8);
if(noMessage != "N" && noMessage2 != "N" ){
// send_message(message);
}
previousMillis = millis();//reset counter
incomingData = "";//empty message reception
}
// if received command is to turn off relay
if(incomingData.indexOf("W_off")>-1)
{
digitalWrite(relay_pin, HIGH);
digitalWrite(relay_pin2, HIGH);
message = "Arrosage OFF";
// Send a sms back to confirm that the relay is turned off
send_message(message);
incomingData = "";
}
if ((digitalRead(relay_pin) == LOW) && (millis() - previousMillis >= (duration * 60000))){
digitalWrite(relay_pin, HIGH);
delay(500);
digitalWrite(relay_pin2, LOW);
previousMillis = millis();
}
if ((digitalRead(relay_pin2) == LOW) && (millis() - previousMillis >= (duration * 60000))){
digitalWrite(relay_pin2, HIGH);
message = "Arrosage OFF";
// Send a sms back to confirm that the relay is turned off
if(noMessage != "N" && noMessage2 != "N" ){
send_message(message);
}
}
}
long stringToLong(String s)
{
char arr[12];
s.toCharArray(arr, sizeof(arr));
return atol(arr);
}
void receive_message()
{
if (SIM900.available() > 0)
{
incomingData = SIM900.readString(); // Get the data from the serial port.
Serial.println(incomingData);
Serial.println(incomingData.substring(5));
//Serial.println(stringToLong(incomingData.substring(4)));
//Serial.println(incomingData.substring(9,8));
//Serial.println(incomingData.substring(11,10));
delay(10);
}
}
void send_message(String message)
{
SIM900.println("AT+CMGF=1"); //Set the GSM Module in Text Mode
delay(100);
SIM900.println("AT+CMGS=\"+123456\"");
delay(100);
SIM900.println(message); // The SMS text you want to send
delay(100);
SIM900.println((char)26); // ASCII code of CTRL+Z
delay(100);
SIM900.println();
delay(1000);
} |
Partager