Advertisement
pleasedontcode

"SMS Automation" rev_02

Jul 12th, 2024
184
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 Automation"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-13 01:35:00
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connecting and disconnecting the power supply, */
  21.     /* when the "SIMcard" module receives the message */
  22.     /* "Soldier: connected", command the "Soldier" module */
  23.     /* to turn on the power, and if the "SIMcard" module */
  24.     /* receives the "Soldier off" message, to the */
  25.     /* "Soldier" */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SoftwareSerial.h>
  31. #include <Sim800L.h> // https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  32. #include <Relay.h>   // https://github.com/rafaelnsantos/Relay
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38. void handleSms(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t simcard_SIM800L_RING_PIN_D3 = 3;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t simcard_SIM800L_RST_PIN_D2 = 2;
  45. const uint8_t simcard_SIM800L_DTR_PIN_D4 = 4;
  46. const uint8_t soldier_RelayModule_Signal_PIN_D5 = 5;
  47.  
  48. /***** DEFINITION OF Software Serial *****/
  49. const uint8_t simcard_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
  50. const uint8_t simcard_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
  51. SoftwareSerial simcard_SIM800L_Serial(simcard_SIM800L_Serial_PIN_SERIAL_RX_A1, simcard_SIM800L_Serial_PIN_SERIAL_TX_A0);
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. /***** used to store raw data *****/
  55. bool simcard_SIM800L_RST_PIN_D2_rawData = 0;
  56. bool simcard_SIM800L_DTR_PIN_D4_rawData = 0;
  57. bool soldier_RelayModule_Signal_PIN_D5_rawData = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. /***** used to store data after characteristic curve transformation *****/
  61. float simcard_SIM800L_RST_PIN_D2_phyData = 0.0;
  62. float simcard_SIM800L_DTR_PIN_D4_phyData = 0.0;
  63. float soldier_RelayModule_Signal_PIN_D5_phyData = 0.0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. Sim800L GSM(simcard_SIM800L_Serial_PIN_SERIAL_RX_A1, simcard_SIM800L_Serial_PIN_SERIAL_TX_A0, simcard_SIM800L_RST_PIN_D2); // Initialize SIM800L object
  67. Relay light(soldier_RelayModule_Signal_PIN_D5, true); // Initialize relay on pin 5, Normally Open
  68.  
  69. void setup(void)
  70. {
  71.     // put your setup code here, to run once:
  72.     pinMode(simcard_SIM800L_RING_PIN_D3, INPUT_PULLUP);
  73.  
  74.     pinMode(simcard_SIM800L_RST_PIN_D2, OUTPUT);
  75.     pinMode(simcard_SIM800L_DTR_PIN_D4, OUTPUT);
  76.     pinMode(soldier_RelayModule_Signal_PIN_D5, OUTPUT);
  77.  
  78.     simcard_SIM800L_Serial.begin(9600);
  79.     GSM.begin(9600); // Initialize the GSM module with baud rate 9600
  80.  
  81.     light.begin(); // Initialize the relay pin
  82. }
  83.  
  84. void loop(void)
  85. {
  86.     // put your main code here, to run repeatedly:
  87.     handleSms(); // Check and handle incoming SMS
  88.     updateOutputs(); // Refresh output data
  89. }
  90.  
  91. void updateOutputs()
  92. {
  93.     digitalWrite(simcard_SIM800L_RST_PIN_D2, simcard_SIM800L_RST_PIN_D2_rawData);
  94.     digitalWrite(simcard_SIM800L_DTR_PIN_D4, simcard_SIM800L_DTR_PIN_D4_rawData);
  95.     digitalWrite(soldier_RelayModule_Signal_PIN_D5, soldier_RelayModule_Signal_PIN_D5_rawData);
  96. }
  97.  
  98. void handleSms()
  99. {
  100.     String textSms = GSM.readSms(1); // Read the first SMS
  101.  
  102.     if (textSms.indexOf("OK") != -1 && textSms.length() > 7) { // Check if the message is correct and not empty
  103.         textSms.toUpperCase(); // Convert message to uppercase
  104.  
  105.         if (textSms.indexOf("SOLDIER: CONNECTED") != -1) {
  106.             light.turnOn(); // Turn relay on
  107.             soldier_RelayModule_Signal_PIN_D5_rawData = true;
  108.         } else if (textSms.indexOf("SOLDIER OFF") != -1) {
  109.             light.turnOff(); // Turn relay off
  110.             soldier_RelayModule_Signal_PIN_D5_rawData = false;
  111.         }
  112.  
  113.         GSM.delAllSms(); // Delete all SMS
  114.     }
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement