/********* 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: GSM Management
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-06-13 11:01:08

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* if it is received an sms from +390123456, so call */
	/* it after 1 minute. */
/****** END SYSTEM REQUIREMENTS *****/

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

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

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t gsm_SIM800L_RST_PIN_D2 = 2;
const uint8_t gsm_SIM800L_DTR_PIN_D4 = 4;

/***** DEFINITION OF Software Serial *****/
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
EspSoftwareSerial::UART gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool gsm_SIM800L_RST_PIN_D2_rawData = 0;
bool gsm_SIM800L_DTR_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float gsm_SIM800L_RST_PIN_D2_phyData = 0.0;
float gsm_SIM800L_DTR_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Sim800L GSM(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0, gsm_SIM800L_RST_PIN_D2);

void setup(void)
{
    // put your setup code here, to run once:

    pinMode(gsm_SIM800L_RING_PIN_D3, INPUT_PULLUP);

    pinMode(gsm_SIM800L_RST_PIN_D2, OUTPUT);
    pinMode(gsm_SIM800L_DTR_PIN_D4, OUTPUT);

    gsm_SIM800L_Serial.begin(9600, SWSERIAL_8N1, gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0, false);

    GSM.begin(); // Initialize the GSM module
    GSM.reset(); // Reset the GSM module
}

void loop(void)
{
    // put your main code here, to run repeatedly:

    updateOutputs(); // Refresh output data
    handleIncomingSms(); // Handle incoming SMS
}

void updateOutputs()
{
    digitalWrite(gsm_SIM800L_RST_PIN_D2, gsm_SIM800L_RST_PIN_D2_rawData);
    digitalWrite(gsm_SIM800L_DTR_PIN_D4, gsm_SIM800L_DTR_PIN_D4_rawData);
}

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

    if (textSms.length() > 0) { // Check if the message is not empty
        String numberSms = GSM.getNumberSms(1); // Get the sender's number
        Serial.println(numberSms); // Debugging

        if (numberSms == "+390123456") {
            delay(60000); // Wait for 1 minute
            GSM.callNumber(numberSms); // Call the number
        }

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

/* END CODE */