#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#define RELAY1 13
#define RELAY2 12
#define RELAY3 11
SoftwareSerial sim900a(2,3);
LiquidCrystal_I2C lcd(0x27 ,2,1,0,4,5,6,7);
String message,phone;
int data;
void setup()
{
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT);
Serial.begin(9600);
sim900a.begin(9600);
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
/**************LCD*************************/
lcd.begin (16,2);
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print("GSM Control Home");
lcd.setCursor(0,1);
lcd.print(" Automation ");
delay(2000);
lcd.clear();
lcd.print("Circuit Digest ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("System Ready ");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" L1 L2 FAN ");
lcd.setCursor(0,1);
if(digitalRead(RELAY1)==LOW) lcd.print("OFF ");
else lcd.print(" ON ");
if(digitalRead(RELAY2)==LOW) lcd.print(" OFF ");
else lcd.print(" ON ");
if(digitalRead(RELAY3)==LOW) lcd.print(" OFF ");
else lcd.print(" ON ");
}
void loop()
{
if(sim900a.available()>0)
{
delay(100);
message="";
while(sim900a.available())
{
message+=(char)sim900a.read();
data=Serial.read();
}
if (data=='1')
{
Serial.print("RELAY 1 ON");
digitalWrite(RELAY1,HIGH);
lcd.setCursor(0,1);
lcd.print("ON ");
delay(200);
}
if (data=='2')
{
Serial.print("RELAY 1 OFF");
digitalWrite(RELAY1,LOW);
lcd.setCursor(0,1);
lcd.print("OFF ");
delay(200);
}
if (data=='3')
{
Serial.print("RELAY 2 ON");
digitalWrite(RELAY2,HIGH);
lcd.setCursor(7,1);
lcd.print(" ON ");
delay(200);
}
if (data=='4')
{
Serial.print("RELAY 2 OFF");
digitalWrite(RELAY2,LOW);
lcd.setCursor(7,1);
lcd.print("OFF ");
delay(200);
}
if (data=='5')
{
Serial.print("RELAY 3 ON");
digitalWrite(RELAY3,HIGH);
lcd.setCursor(13,1);
lcd.print("ON ");
delay(200);
}
if(data=='6')
{
Serial.print("RELAY 3 OFF");
digitalWrite(RELAY3,LOW);
lcd.setCursor(13,1);
lcd.print("OFF ");
delay(200);
}
if(data=='7')
{digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY3,HIGH);
lcd.setCursor(13,1);
lcd.print("ON ");
lcd.setCursor(7,1);
lcd.print("ON ");
lcd.setCursor(0,1);
lcd.print("ON ");
delay(200);
}
if(data=='8')
{digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
lcd.setCursor(0,1);
lcd.print("OFF OFF OFF");
delay(200);
}
}
if(message.indexOf("CMT")>-1)
{
message=message.substring(message.indexOf('"')+1);
phone=message.substring(0,message.indexOf('"'));
message=message.substring(message.indexOf('"')+1);
message=message.substring(message.indexOf('"')+1);
message=message.substring(message.indexOf('"')+1);
message=message.substring(message.indexOf('"')+1);
message=message.substring(message.indexOf('"')+1);
message=message.substring(message.indexOf('\n')+1);
message=message.substring(0,message.indexOf('\n')-1);
Serial.print("Message:" + message);
Serial.println(" From:" + phone);
if(phone.length()>=10)
{
if(message=="L1 ON")
{
digitalWrite(RELAY1,HIGH);
Serial.print("RELAY1 ON");//Text message
lcd.setCursor(0,1);
lcd.print("ON ");
delay(200);
}
else if(message=="L1 OFF")
{
digitalWrite(RELAY1,LOW);
Serial.print("RELAY1 OFF");//Text message
lcd.setCursor(0,1);
lcd.print("OFF ");
delay(200);
}
else if(message=="L2 ON")
{
digitalWrite(RELAY2,HIGH);
Serial.print("RELAY2 ON");//Text message
lcd.setCursor(7,1);
lcd.print(" ON ");
delay(200);
}
else if(message=="L2 OFF")
{
digitalWrite(RELAY2,LOW);
Serial.print("RELAY2 OFF");//Text message
lcd.setCursor(7,1);
lcd.print("OFF ");
delay(200);
}
else if(message=="L3 ON")
{
digitalWrite(RELAY3,HIGH);
Serial.print("RELAY3 ON");//Text message
lcd.setCursor(13,1);
lcd.print("ON ");
delay(200);
}
else if(message=="L3 OFF")
{
digitalWrite(RELAY3,LOW);
Serial.print("RELAY3 OFF");//Text message
digitalWrite(RELAY3,LOW);
lcd.setCursor(13,1);
lcd.print("OFF ");
}
else if(message=="ALL ON")
{
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY3,HIGH);
Serial.print("ALL RELAY ON");//Text message
lcd.setCursor(0,1);
lcd.print(" ON ON ON");
}
else if(message=="ALL OFF")
{
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
Serial.print("ALL RELAY OFF");//Text message
lcd.setCursor(0,1);
lcd.print("OFF OFF OFF");
}
else
{
sim900a.print("AT+CMGF=1\r"); //Set text mode
delay(1000);
sim900a.print("AT+CMGS=\"" + phone + "\"\r"); //Send message
delay(1000);
sim900a.print("Arahan salah");//Text message
sim900a.println((char)0x1A); //Ctrl+Z
}
lcd.setCursor(0,1);
if(digitalRead(RELAY1)==LOW) lcd.print("OFF ");
else lcd.print(" ON ");
if(digitalRead(RELAY2)==LOW) lcd.print(" OFF ");
else lcd.print(" ON ");
if(digitalRead(RELAY3)==LOW) lcd.print(" OFF ");
else lcd.print(" ON ");
delay(5000);
}
}
else
{
Serial.println(message);
}
}