/*
* Date : 6 March 2021
* Author : Erick H Moshi
* Description: GSM based switch
*/
#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM800(7, 8); // Pins 7, 8 are used as used as software serial pins
String incomingData; // for storing incoming serial data
String message = ""; // A String for storing the message
String lineState = "IDLE"; // To initially set the state to idle once booted or in cases of power cuts
int relay_pin = 13; // Initialized a pin for relay module
void setup()
{
Serial.begin(115200); // baudrate for serial monitor
SIM800.begin(19200); // baudrate for GSM shield
pinMode(relay_pin, OUTPUT); // Setting erlay pin as output pin
digitalWrite(relay_pin, HIGH); // Making relay pin initailly low
delay(30000);
Serial.println("Initializing ...");
delay(1000);
// set SMS mode to text mode
SIM800.print("AT+CMGF=1\r");
delay(5000);
SIM800.print("AT+CMGD=1,4\r"); // delete all sms on sim card
delay(5000);
// set gsm module to show the output on serial out
SIM800.print("AT+CNMI=2,2,0,0,0\r");
delay(5000);
// Retrieve own mobile number from SIM
Serial.println(F("Attempting to retrieve own MSISDN number from sim"));
SIM800.print("AT+CNUM\r");
delay(5000);
// Enable caller ID on incoming calls
Serial.println(F("Enabling Caller ID on incoming calls"));
SIM800.print("AT+CLIP=1\r");
Serial.println("Setup complete!");
delay(5000);
// Send text to alert that system is back online after a power cut/ when switched on
SIM800.println("AT+CMGF=1");
delay(100);
SIM800.println("AT+CMGS=\"+ZZZxxxxxxxxx\""); // Send SMS to the phone number, this will alert you that the system is on
delay(100);
SIM800.println("SYSTEM ONLINE"); // The content of the SMS
delay(100);
SIM800.println((char)26); // ASCII code of CTRL+Z
delay(100);
SIM800.println();
delay(1000);
}
void loop()
{
//Function for receiving sms
receive_message();
// if received command is to turn on relay
if(incomingData.indexOf("LINE_ON")>=0)
{
digitalWrite(relay_pin, HIGH);// set to high since we are using the Normarlly Closed side of the relay
message = "Line is turned ON";
lineState = "ON";
// Send a sms back to confirm that the relay is turned on
send_message(message);
}
// if received command is to turn off relay
if(incomingData.indexOf("LINE_OFF")>=0)
{
digitalWrite(relay_pin, LOW);//Set to low since we are using the Normally Closed side of the relay and Low will activate the relay switching the line off
message = "Line is turned OFF";
lineState = "OFF";
// Send a sms back to confirm that the relay is turned off
send_message(message);
}
if(incomingData.indexOf("STATE")>=0)
{
message = "Line state is " + lineState;
send_message(message);
Serial.println("Line state resquest");
message = "";
}
}
void receive_message()
{
if (SIM800.available() > 0)
{
incomingData = SIM800.readString(); // Get the data from the serial port.
Serial.print(incomingData);
delay(10);
}
}
void send_message(String message)
{
SIM800.println("AT+CMGF=1"); //Set the GSM Module in Text Mode
delay(100);
SIM800.println("AT+CMGS=\"+ZZZxxxxxxxxx\""); // Sends feedback text to the phone number
delay(100);
SIM800.println(message); // The SMS text you want to send
delay(100);
SIM800.println((char)26); // ASCII code of CTRL+Z
delay(100);
SIM800.println();
delay(1000);
}
Comments