code:-
#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define Fan A0
#define Light A1
#define TV A2
int temp=0,i=0;
int led=13;
char str[15];
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Light, OUTPUT);
pinMode(TV, OUTPUT);
digitalWrite(Light, LOW);
digitalWrite(Fan, LOW);
digitalWrite(TV, LOW);
lcd.setCursor(0,0);
lcd.print("GSM Control Home");
lcd.setCursor(0,1);
lcd.print(" Automaton ");
delay(2000);
lcd.clear();
gsm_init();
lcd.setCursor(0,1);
lcd.print("System Ready");
Serial.println("AT+CNMI=2,2,0,0,0");
delay(500);
Serial.println("AT+CMGF=1");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Fan Light TV ");
lcd.setCursor(0,1);
lcd.print("OFF OFF OFF ");
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Fan Light TV");
if(temp==1)
{
check();
temp=0;
i=0;
delay(1000);
}
}
void serialEvent()
{
while(Serial.available())
{
if(Serial.find("#"))
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
while (Serial.available())
{
char inChar=Serial.read();
str[i++]=inChar;
if(inChar=='*')
{
temp=1;
return;
}
}
}
}
}
void check()
{
if(!(strncmp(str,"tv on",5)))
{
digitalWrite(TV, HIGH);
lcd.setCursor(13,1);
lcd.print("ON ");
delay(200);
}
else if(!(strncmp(str,"tv off",6)))
{
digitalWrite(TV, LOW);
lcd.setCursor(13,1);
lcd.print("OFF ");
delay(200);
}
else if(!(strncmp(str,"fan on",5)))
{
digitalWrite(Fan, HIGH);
lcd.setCursor(0,1);
lcd.print("ON ");
delay(200);
}
else if(!(strncmp(str,"fans off",7)))
{
digitalWrite(Fan, LOW);
lcd.setCursor(0,1);
lcd.print("OFF ");
delay(200);
}
else if(!(strncmp(str,"light on",8)))
{
digitalWrite(Light, HIGH);
lcd.setCursor(7,1);
lcd.print("ON ");
delay(200);
}
else if(!(strncmp(str,"light off",9)))
{
digitalWrite(Light, LOW);
lcd.setCursor(7,1);
lcd.print("OFF ");
delay(200);
}
else if(!(strncmp(str,"all on",6)))
{
digitalWrite(Light, HIGH);
digitalWrite(Fan, HIGH);
digitalWrite(TV, HIGH);
lcd.setCursor(0,1);
lcd.print("ON ON ON ");
delay(200);
}
else if(!(strncmp(str,"all off",7)))
{
digitalWrite(Light, LOW);
digitalWrite(Fan, LOW);
digitalWrite(TV, LOW);
lcd.setCursor(0,1);
lcd.print("OFF OFF OFF ");
delay(200);
}
delete_sms();
}
void gsm_init()
{
lcd.clear();
lcd.print("Finding Module..");
boolean at_flag=1;
while(at_flag)
{
Serial.println("AT");
delay(1);
while(Serial.available()>0)
{
if(Serial.find("OK"))
at_flag=0;
}
delay(1000);
}
boolean mg_flag=1;
while(mg_flag)
{
Serial.println("AT+CMGD=1");
delay(1);
while(Serial.available()>0)
{
if(Serial.find("OK"))
mg_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Module Connected..");
delay(1000);
lcd.clear();
lcd.print("Disabling ECHO");
boolean echo_flag=1;
while(echo_flag)
{
Serial.println("ATE0");
while(Serial.available()>0)
{
if(Serial.find("OK"))
echo_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Echo OFF");
delay(1000);
lcd.clear();
lcd.print("Finding Network..");
boolean net_flag=1;
while(net_flag)
{
Serial.println("AT+CPIN?");
while(Serial.available()>0)
{
if(Serial.find("+CPIN: READY"))
net_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Network Found..");
delay(1000);
lcd.clear();
}
void delete_sms()
{
boolean mg_flag=1;
while(mg_flag)
{
Serial.println("AT+CMGD=1");
delay(1);
while(Serial.available()>0)
{
if(Serial.find("OK"))
mg_flag=0;
}
delay(1000);
}
}
Comments
Post a Comment