/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "SMS Automation"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-07-13 01:35:00

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Connecting and disconnecting the power supply, */
	/* when the "SIMcard" module receives the message */
	/* "Soldier: connected", command the "Soldier" module */
	/* to turn on the power, and if the "SIMcard" module */
	/* receives the "Soldier off" message, to the */
	/* "Soldier" */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include <SoftwareSerial.h>
#include <Sim800L.h> // https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
#include <Relay.h>   // https://github.com/rafaelnsantos/Relay

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void handleSms(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t simcard_SIM800L_RING_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t simcard_SIM800L_RST_PIN_D2 = 2;
const uint8_t simcard_SIM800L_DTR_PIN_D4 = 4;
const uint8_t soldier_RelayModule_Signal_PIN_D5 = 5;

/***** DEFINITION OF Software Serial *****/
const uint8_t simcard_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
const uint8_t simcard_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial simcard_SIM800L_Serial(simcard_SIM800L_Serial_PIN_SERIAL_RX_A1, simcard_SIM800L_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool simcard_SIM800L_RST_PIN_D2_rawData = 0;
bool simcard_SIM800L_DTR_PIN_D4_rawData = 0;
bool soldier_RelayModule_Signal_PIN_D5_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float simcard_SIM800L_RST_PIN_D2_phyData = 0.0;
float simcard_SIM800L_DTR_PIN_D4_phyData = 0.0;
float soldier_RelayModule_Signal_PIN_D5_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Sim800L GSM(simcard_SIM800L_Serial_PIN_SERIAL_RX_A1, simcard_SIM800L_Serial_PIN_SERIAL_TX_A0, simcard_SIM800L_RST_PIN_D2); // Initialize SIM800L object
Relay light(soldier_RelayModule_Signal_PIN_D5, true); // Initialize relay on pin 5, Normally Open

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(simcard_SIM800L_RING_PIN_D3, INPUT_PULLUP);

    pinMode(simcard_SIM800L_RST_PIN_D2, OUTPUT);
    pinMode(simcard_SIM800L_DTR_PIN_D4, OUTPUT);
    pinMode(soldier_RelayModule_Signal_PIN_D5, OUTPUT);

    simcard_SIM800L_Serial.begin(9600);
    GSM.begin(9600); // Initialize the GSM module with baud rate 9600

    light.begin(); // Initialize the relay pin
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    handleSms(); // Check and handle incoming SMS
    updateOutputs(); // Refresh output data
}

void updateOutputs()
{
    digitalWrite(simcard_SIM800L_RST_PIN_D2, simcard_SIM800L_RST_PIN_D2_rawData);
    digitalWrite(simcard_SIM800L_DTR_PIN_D4, simcard_SIM800L_DTR_PIN_D4_rawData);
    digitalWrite(soldier_RelayModule_Signal_PIN_D5, soldier_RelayModule_Signal_PIN_D5_rawData);
}

void handleSms()
{
    String textSms = GSM.readSms(1); // Read the first SMS

    if (textSms.indexOf("OK") != -1 && textSms.length() > 7) { // Check if the message is correct and not empty
        textSms.toUpperCase(); // Convert message to uppercase

        if (textSms.indexOf("SOLDIER: CONNECTED") != -1) {
            light.turnOn(); // Turn relay on
            soldier_RelayModule_Signal_PIN_D5_rawData = true;
        } else if (textSms.indexOf("SOLDIER OFF") != -1) {
            light.turnOff(); // Turn relay off
            soldier_RelayModule_Signal_PIN_D5_rawData = false;
        }

        GSM.delAllSms(); // Delete all SMS
    }
}

/* END CODE */