#include <SoftwareSerial.h>
#include <String.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#define NO_OF_DEV 4
int devices[4] = {2 , 3 , 4 , 5}; // Device pins
int d_status[4]; // Device status
int toggle, stat;
String phone_num , buffer;
String id[4] = {"LED 1", "LED 2", "LED 3", "LED 4"};
SoftwareSerial SIM800(10 , 11);
boolean at_flag;
void setup()
{
Serial.begin(9600); // Serial Monitor baud rate
SIM800.begin(9600); // GSM module baud rate
lcd.begin();
lcd.backlight();
lcd.print(" SMS Based Home ");
lcd.setCursor(0,1);
lcd.print(" Automation ");
delay(4000);
gsm_init();
for(int i = 0;i < NO_OF_DEV;i++)
{
pinMode(devices[i] , OUTPUT);
}
for(int i = 0;i < NO_OF_DEV;i++)
{
digitalWrite(devices[i] , HIGH);
}
for(int i = 0;i < NO_OF_DEV;i++)
d_status[i] = 0; // All devices are OFF by default
toggle = 0;
stat = 0;
buffer.remove(0);
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
Serial.println("Device is ready.");
lcd.clear();
lcd.println("Device Ready");
delay(1500);
lcd.clear();
lcd.print("LD1 LD2 LD3 LD4");
}
void loop()
{
buffer = readSIM800();
if (buffer.startsWith("\r\n+CMT: "))
{
phone_num = get_num(buffer);
Serial.println("*** RECEIVED SMS *** from " + phone_num);
buffer.remove(0, 51); // Remove first 51 characters /// change the value from 51 to other by checking in putty ANTO (if needed)
int len = buffer.length();
buffer.remove(len - 2); // Remove \r\n from tail
Serial.println(buffer);
Serial.println("*** END SMS ***");
if(buffer=="LED 1 ON")
{ d_status[0]=1;
digitalWrite(devices[0] , !HIGH);
lcd.setCursor(0,1);
lcd.print("ON ");
stat=1;
}
if(buffer=="LED 1 OFF")
{
d_status[0]=0;
digitalWrite(devices[0] , !LOW);
lcd.setCursor(0,1);
lcd.print("OFF");
stat=1;
}
if(buffer=="LED 2 ON")
{ d_status[1]=1;
digitalWrite(devices[1] , !HIGH);
lcd.setCursor(4,1);
lcd.print("ON ");
stat=1;
}
if(buffer=="LED 2 OFF")
{
d_status[1]=0;
digitalWrite(devices[1] , !LOW);
lcd.setCursor(4,1);
lcd.print("OFF");
stat=1;
}
if(buffer=="LED 3 ON")
{ d_status[2]=1;
digitalWrite(devices[2] , !HIGH);
lcd.setCursor(8,1);
lcd.print("ON ");
stat=1;
}
if(buffer=="LED 3 OFF")
{
d_status[2]=0;
digitalWrite(devices[2] , !LOW);
lcd.setCursor(8,1);
lcd.print("OFF");
stat=1;
}
if(buffer=="LED 4 ON")
{ d_status[3]=1;
digitalWrite(devices[3] , !HIGH);
lcd.setCursor(12,1);
lcd.print("ON ");
stat=1;
}
if(buffer=="LED 4 OFF")
{
d_status[3]=0;
digitalWrite(devices[3] , !LOW);
lcd.setCursor(12,1);
lcd.print("OFF");
stat=1;
}
}
if(stat)
{
send_status(phone_num);
stat = 0;
}
}
String readSIM800() // Save whole SMS into a string
{
String buffers;
char c;
while (SIM800.available())
{
c = SIM800.read();
buffers.concat(c);
delay(10);
}
return buffers;
}
String get_num(String buffer)
{
String phone_num;
int i = 9; // Starting index of phone number
while(buffer.charAt(i) != '"')
{
phone_num.concat(buffer.charAt(i));
i++;
}
return(phone_num);
}
int toggle_command(String buffers)
{
if((buffer == "OFF") || (buffer == "off"))
return 1;
else if((buffer == "ON") || (buffer == "on"))
return 0;
}
void send_status(String phone_num)
{
SIM800.println("AT+CMGS=\""+ phone_num +"\"\r");
delay(50);
for(int i = 0;i < NO_OF_DEV;i++)
{
if(d_status[i])
SIM800.println(id[i] + " is ON");
else
SIM800.println(id[i] + " is OFF");
delay(100);
}
SIM800.println((char)26);
Serial.println("Status sent.");
}
void gsm_init()
{
lcd.clear();
lcd.print("Finding Module..");
at_flag=1;
while(at_flag)
{
SIM800.println("AT");
delay(1);
while(SIM800.available()>0)
{
if(SIM800.find("OK"))
at_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Module Connected..");
delay(1000);
lcd.clear();
at_flag=1;
while(at_flag) //
{
SIM800.println("AT+CNMI=2,2,0,0,0");
delay(1);
while(SIM800.available()>0)
{
if(SIM800.find("OK"))
at_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Open Network..");
delay(1000);
lcd.clear();
at_flag=1;
while(at_flag)
{
SIM800.println("AT+CMGF=1");
delay(1);
while(SIM800.available()>0)
{
if(SIM800.find("OK"))
at_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("TEXT MODE..");
delay(1000);
lcd.clear();
lcd.print("Disabling ECHO");
boolean echo_flag=1;
while(echo_flag)
{
SIM800.println("ATE0");
while(SIM800.available()>0)
{
if(SIM800.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)
{
SIM800.println("AT+CPIN?");
while(SIM800.available()>0)
{
if(SIM800.find("+CPIN: READY"))
net_flag=0;
}
delay(1000);
}
lcd.clear();
lcd.print("Network Found..");
delay(1000);
lcd.clear();
}
code:-
Comments
Post a Comment