Advertisement
pleasedontcode

"SMS Notifications" rev_01

Oct 31st, 2024
82
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 Notifications"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-31 07:42:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize the Sim800L module for */
  21.     /* GSM communication, integrating with a DHT22 sensor */
  22.     /* to monitor temperature and humidity, while */
  23.     /* ensuring reliable data transmission through */
  24.     /* SoftwareSerial. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SoftwareSerial.h> // https://github.com/plerup/espsoftwareserial
  29. #include <Sim800L.h>       // https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  30. #include <DHT.h>          // https://github.com/adafruit/DHT-sensor-library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t SIM800L_RING_PIN_D16       = 16;
  38. const uint8_t DHT22_DOUT_PIN_D18         = 18;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t SIM800L_RST_PIN_D14        = 14;
  42. const uint8_t SIM800L_DTR_PIN_D17        = 17;
  43.  
  44. /***** DEFINITION OF Software Serial *****/
  45. const uint8_t SIM800L_Serial_PIN_SERIAL_TX_D4        = 4;
  46. const uint8_t SIM800L_Serial_PIN_SERIAL_RX_D13       = 13;
  47. EspSoftwareSerial::UART SIM800L_Serial;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool SIM800L_RST_PIN_D14_rawData       = 0;
  52. bool SIM800L_DTR_PIN_D17_rawData       = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float SIM800L_RST_PIN_D14_phyData       = 0.0;
  57. float SIM800L_DTR_PIN_D17_phyData       = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  60. Sim800L GSM(SIM800L_Serial_PIN_SERIAL_RX_D13, SIM800L_Serial_PIN_SERIAL_TX_D4, SIM800L_RST_PIN_D14); // Initialize SIM800L with RX, TX, and RST pins
  61.  
  62. // Initialize DHT sensor
  63. #define DHTPIN DHT22_DOUT_PIN_D18 // Define the pin where the DHT22 is connected
  64. #define DHTTYPE DHT22             // Define the type of DHT sensor
  65. DHT dht(DHTPIN, DHTTYPE);          // Create an instance of the DHT class
  66.  
  67. void setup(void)
  68. {
  69.     // Initialize Serial Monitor for debugging
  70.     Serial.begin(9600);
  71.    
  72.     // Set pin modes for inputs and outputs
  73.     pinMode(SIM800L_RING_PIN_D16, INPUT_PULLUP);
  74.     pinMode(DHT22_DOUT_PIN_D18, INPUT_PULLUP);
  75.     pinMode(SIM800L_RST_PIN_D14, OUTPUT);
  76.     pinMode(SIM800L_DTR_PIN_D17, OUTPUT);
  77.  
  78.     // Initialize Software Serial for SIM800L
  79.     SIM800L_Serial.begin(9600, SWSERIAL_8N1, SIM800L_Serial_PIN_SERIAL_RX_D13, SIM800L_Serial_PIN_SERIAL_TX_D4, false);
  80.     GSM.begin(9600); // Initialize the GSM module with a baud rate of 9600
  81.  
  82.     dht.begin(); // Initialize the DHT sensor
  83. }
  84.  
  85. void loop(void)
  86. {
  87.     // Refresh output data
  88.     updateOutputs();
  89.     // Read data from DHT sensor
  90.     readDHTSensor();
  91.     // Send data via GSM
  92.     sendDataViaGSM();
  93. }
  94.  
  95. void updateOutputs()
  96. {
  97.     // Update the state of the SIM800L reset and DTR pins
  98.     digitalWrite(SIM800L_RST_PIN_D14, SIM800L_RST_PIN_D14_rawData);
  99.     digitalWrite(SIM800L_DTR_PIN_D17, SIM800L_DTR_PIN_D17_rawData);
  100. }
  101.  
  102. void readDHTSensor()
  103. {
  104.     // Read humidity and temperature from DHT sensor
  105.     float h = dht.readHumidity();
  106.     float t = dht.readTemperature();
  107.  
  108.     // Check if any reads failed and exit early (to try again).
  109.     if (isnan(h) || isnan(t)) {
  110.         Serial.println(F("Failed to read from DHT sensor!"));
  111.         return;
  112.     }
  113.  
  114.     // Print the results to the Serial Monitor
  115.     Serial.print(F("Humidity: "));
  116.     Serial.print(h);
  117.     Serial.print(F("%  Temperature: "));
  118.     Serial.print(t);
  119.     Serial.println(F("°C"));
  120. }
  121.  
  122. void sendDataViaGSM()
  123. {
  124.     // Prepare the message to send
  125.     char message[100];
  126.     snprintf(message, sizeof(message), "Humidity: %.2f%%, Temperature: %.2f°C", dht.readHumidity(), dht.readTemperature());
  127.  
  128.     // Send the SMS
  129.     char* number = "2926451386"; // Replace with the recipient's phone number
  130.     bool error = GSM.sendSms(number, message);
  131.    
  132.     // Check if SMS was sent successfully
  133.     if (error) {
  134.         Serial.println(F("SMS sent successfully!"));
  135.     } else {
  136.         Serial.println(F("Failed to send SMS!"));
  137.     }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement