CCCPaste

SMS Alert rev_02

/********* 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 Alert
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-08-02 05:41:50

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Develop a system that leverages the EasyButton */
	/* library to detect button presses on pin D2, */
	/* triggering the Sim800L module to send alerts or */
	/* messages when the button state changes. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/evert-arias/EasyButton
#include 	//https://github.com/SCRN92/Sim800L

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onButtonPressed(); // Function prototype for button press handler

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t mb1_PushButton_PIN_D2 = 2;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the EasyButton object for the push button
EasyButton mb1_PushButton(mb1_PushButton_PIN_D2);

// Initialize the Sim800L object with RX and TX pins
Sim800L sim800l(10, 11); // Create an instance of the Sim800L class with RX on pin 10 and TX on pin 11

/****** FUNCTION DEFINITIONS *****/
void setup(void)
{
	// Start the serial communication for debugging
	Serial.begin(115200);
	
	// Initialize the button with the specified pin
	mb1_PushButton.begin();
	
	// Set the pin mode for the button
	pinMode(mb1_PushButton_PIN_D2, INPUT_PULLUP);
	
	// Register the button press callback function
	mb1_PushButton.onPressed(onButtonPressed);
	
	// Initialize the SIM800L module with a baud rate
	sim800l.begin(9600); // Set the baud rate to 9600
}

void loop(void)
{
	// Read the button state
	mb1_PushButton.read();
	
	// Add any additional code to handle button presses here
}

// Function to handle button press
void onButtonPressed() {
	Serial.println("Button pressed! Sending alert..."); // Debug message

	// Check if the Sim800L module is ready
	// No need to check for readiness as begin() does not return a value
	// Instead, we can directly send the SMS
	if (sim800l.sendSms("1234567890", "Alert: Button was pressed!")) { // Replace with actual phone number
		Serial.println("SMS sent successfully.");
	} else {
		Serial.println("Failed to send SMS.");
	}
}

/* END CODE */