#include <SoftwareSerial.h>
#include <String.h>
#define NO_OF_DEV 4
int devices[4] = {2 , 3 , 4 , 5}; // Device pins
int switches[4] = {6 , 7 , 8 , 9}; // Switch pins
int d_status[4]; // Device status
int s_status[4]; // Status of switches
int toggle, stat;
String phone_num , buffer;
String id[4] = {"LED 1", "LED 2", "LED 3", "LED 4"};
SoftwareSerial SIM800(10 , 11);
void setup()
{
Serial.begin(4800); // Serial Monitor baud rate
SIM800.begin(4800); // GSM module baud rate
delay(40000);// 40s delay for the GSM module to lock on network (had issues with the GSM module)
SIM800.print("AT+CNMI=2,2,0,0,0\r"); // Blurt out contents of new SMS upon receipt
delay(100);
SIM800.print("AT+CMGF=1\r"); // Set SMS mode to text
delay(100);
for(int i = 0;i < NO_OF_DEV;i++)
{
pinMode(switches[i] , INPUT_PULLUP);
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] = 1; // All devices are OFF by default
for(int i = 0;i <= NO_OF_DEV;i++)
s_status[i] = digitalRead(switches[i]); // Read initial status of switches
toggle = 0;
stat = 0;
buffer.remove(0);
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
Serial.println("Device is ready.");
}
void loop()
{
for(int i = 0;i < NO_OF_DEV;i++)
if(s_status[i] != digitalRead(switches[i])) // Control from switch
{
s_status[i] = !s_status[i];
d_status[i] = !d_status[i]; // Toggle switch and device status
toggle = 1;
stat = 1;
phone_num = "+XXXXXXXXXXX"; //Default recipient of status on toggling of switch
}
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
int len = buffer.length();
buffer.remove(len - 2); // Remove \r\n from tail
Serial.println(buffer);
Serial.println("*** END SMS ***");
if(buffer.startsWith("l") || buffer.startsWith("L"))
{
buffer.remove(0,1);
int i = int(buffer[0]) - int('0') - 1;
buffer.remove(0,2);
d_status[i] = toggle_command(buffer);
toggle = 1;
stat = 1;
}
else if((buffer == "s") || (buffer == "S"))
{
stat = 1;
}
}
if(toggle)
{
for(int i = 0;i < NO_OF_DEV;i++)
{
if(d_status[i])
digitalWrite(devices[i] , HIGH);
else
digitalWrite(devices[i] , LOW);
}
toggle = 0;
}
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.");
}
Comments
Please log in or sign up to comment.