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
|
#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM900(7, 8); // Pins 7, 8 are used as used as software serial pins
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
unsigned long previousMillis = 0;
const long duration = 7000; // pump working duration
void setup()
{
Serial.begin(115200); // baudrate for serial monitor
SIM900.begin(19200); // baudrate for GSM shield
pinMode(relay_pin, OUTPUT); // Setting erlay pin as output pin
digitalWrite(relay_pin, HIGH); // Making relay pin initailly low
// set SMS mode to text mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// set gsm module to tp show the output on serial out
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop()
{
//Function for receiving sms
receive_message();
// if received command is to turn on relay
if(incomingData.indexOf("Vanne_on")>-1)
{
digitalWrite(relay_pin, LOW);
message = "Vanne ON";
// Send a sms back to confirm that the relay is turned on
send_message(message);
previousMillis = millis();
incomingData = "";
}
// if received command is to turn off relay
if(incomingData.indexOf("Vanne_off")>-1)
{
digitalWrite(relay_pin, HIGH);
message = "Vanne 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)){
digitalWrite(relay_pin, HIGH);
message = "Vanne OFF";
// Send a sms back to confirm that the relay is turned off
send_message(message);
}
}
void receive_message()
{
if (SIM900.available() > 0)
{
incomingData = SIM900.readString(); // Get the data from the serial port.
Serial.print(incomingData);
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=\"+12345678\"");
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);
} |