Advertisement
pleasedontcode

SMS Alert rev_02

Aug 2nd, 2024
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: SMS Alert
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-02 05:41:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a system that leverages the EasyButton */
  21.     /* library to detect button presses on pin D2, */
  22.     /* triggering the Sim800L module to send alerts or */
  23.     /* messages when the button state changes. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <Sim800L.h>    //https://github.com/SCRN92/Sim800L
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void onButtonPressed(); // Function prototype for button press handler
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t mb1_PushButton_PIN_D2 = 2;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Initialize the EasyButton object for the push button
  40. EasyButton mb1_PushButton(mb1_PushButton_PIN_D2);
  41.  
  42. // Initialize the Sim800L object with RX and TX pins
  43. Sim800L sim800l(10, 11); // Create an instance of the Sim800L class with RX on pin 10 and TX on pin 11
  44.  
  45. /****** FUNCTION DEFINITIONS *****/
  46. void setup(void)
  47. {
  48.     // Start the serial communication for debugging
  49.     Serial.begin(115200);
  50.    
  51.     // Initialize the button with the specified pin
  52.     mb1_PushButton.begin();
  53.    
  54.     // Set the pin mode for the button
  55.     pinMode(mb1_PushButton_PIN_D2, INPUT_PULLUP);
  56.    
  57.     // Register the button press callback function
  58.     mb1_PushButton.onPressed(onButtonPressed);
  59.    
  60.     // Initialize the SIM800L module with a baud rate
  61.     sim800l.begin(9600); // Set the baud rate to 9600
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Read the button state
  67.     mb1_PushButton.read();
  68.    
  69.     // Add any additional code to handle button presses here
  70. }
  71.  
  72. // Function to handle button press
  73. void onButtonPressed() {
  74.     Serial.println("Button pressed! Sending alert..."); // Debug message
  75.  
  76.     // Check if the Sim800L module is ready
  77.     // No need to check for readiness as begin() does not return a value
  78.     // Instead, we can directly send the SMS
  79.     if (sim800l.sendSms("1234567890", "Alert: Button was pressed!")) { // Replace with actual phone number
  80.         Serial.println("SMS sent successfully.");
  81.     } else {
  82.         Serial.println("Failed to send SMS.");
  83.     }
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement