Sunteți pe pagina 1din 2

// Include Software Serial library to communicate with GSM

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// Configure software serial port
SoftwareSerial SIM900(9,10); //9-GSM TX,10-GSM RX
const int rs = 11, en = 10, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Variable to store text message


String textMessage;
char t[15];

// Create a variable to store Lamp state

void setup(){
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(19200);
SIM900.begin(19200);
lcd.begin(16, 2);
lcd.print("MOTOR FIELD");
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}

void loop(){
if(SIM900.available()>0){
textMessage = SIM900.readString();
Serial.print(textMessage);
delay(10);
int i,j=0;
for(i=8;i<=22;i++)
{
t[j]=textMessage[i];
j++;
}
delay(10);
}

if(textMessage.indexOf("a")>=0){
on();
lcd.setCursor(0, 1);
lcd.print("MOTOR IS ON ");
textMessage="MOTOR ON";
sendSMS(textMessage);
}
if(textMessage.indexOf("b")>=0){
// Turn off relay and save current state
off();
lcd.setCursor(0, 1);
lcd.print("MOTOR IS OFF ");
textMessage="MOTOR OFF";
sendSMS(textMessage);
}
}

// Function that sends SMS


void sendSMS(String message){
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print('A');
SIM900.print('T');
SIM900.print('+');
SIM900.print('C');
SIM900.print('M');
SIM900.print('G');
SIM900.print('S');
SIM900.print('=');
for(int i=0;i<=14;i++)
{
SIM900.print(t[i]);
delay(10);
}
SIM900.println("\r");
delay(100);
// Send the SMS
SIM900.println(message);
delay(100);

// End AT command with a ^Z, ASCII code 26


SIM900.println((char)26);
delay(100);
SIM900.println();
// Give module time to send SMS
delay(500);
}

void on()
{
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
}
void off()
{
digitalWrite(13,LOW);
digitalWrite(12,LOW);
}

S-ar putea să vă placă și