Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 Notifications"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-10-31 07:42:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The system shall utilize the Sim800L module for */
- /* GSM communication, integrating with a DHT22 sensor */
- /* to monitor temperature and humidity, while */
- /* ensuring reliable data transmission through */
- /* SoftwareSerial. */
- /****** 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
- #include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t SIM800L_RING_PIN_D16 = 16;
- const uint8_t DHT22_DOUT_PIN_D18 = 18;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t SIM800L_RST_PIN_D14 = 14;
- const uint8_t SIM800L_DTR_PIN_D17 = 17;
- /***** DEFINITION OF Software Serial *****/
- const uint8_t SIM800L_Serial_PIN_SERIAL_TX_D4 = 4;
- const uint8_t SIM800L_Serial_PIN_SERIAL_RX_D13 = 13;
- EspSoftwareSerial::UART SIM800L_Serial;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool SIM800L_RST_PIN_D14_rawData = 0;
- bool SIM800L_DTR_PIN_D17_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float SIM800L_RST_PIN_D14_phyData = 0.0;
- float SIM800L_DTR_PIN_D17_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- 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
- // Initialize DHT sensor
- #define DHTPIN DHT22_DOUT_PIN_D18 // Define the pin where the DHT22 is connected
- #define DHTTYPE DHT22 // Define the type of DHT sensor
- DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
- void setup(void)
- {
- // Initialize Serial Monitor for debugging
- Serial.begin(9600);
- // Set pin modes for inputs and outputs
- pinMode(SIM800L_RING_PIN_D16, INPUT_PULLUP);
- pinMode(DHT22_DOUT_PIN_D18, INPUT_PULLUP);
- pinMode(SIM800L_RST_PIN_D14, OUTPUT);
- pinMode(SIM800L_DTR_PIN_D17, OUTPUT);
- // Initialize Software Serial for SIM800L
- SIM800L_Serial.begin(9600, SWSERIAL_8N1, SIM800L_Serial_PIN_SERIAL_RX_D13, SIM800L_Serial_PIN_SERIAL_TX_D4, false);
- GSM.begin(9600); // Initialize the GSM module with a baud rate of 9600
- dht.begin(); // Initialize the DHT sensor
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- // Read data from DHT sensor
- readDHTSensor();
- // Send data via GSM
- sendDataViaGSM();
- }
- void updateOutputs()
- {
- // Update the state of the SIM800L reset and DTR pins
- digitalWrite(SIM800L_RST_PIN_D14, SIM800L_RST_PIN_D14_rawData);
- digitalWrite(SIM800L_DTR_PIN_D17, SIM800L_DTR_PIN_D17_rawData);
- }
- void readDHTSensor()
- {
- // Read humidity and temperature from DHT sensor
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- return;
- }
- // Print the results to the Serial Monitor
- Serial.print(F("Humidity: "));
- Serial.print(h);
- Serial.print(F("% Temperature: "));
- Serial.print(t);
- Serial.println(F("°C"));
- }
- void sendDataViaGSM()
- {
- // Prepare the message to send
- char message[100];
- snprintf(message, sizeof(message), "Humidity: %.2f%%, Temperature: %.2f°C", dht.readHumidity(), dht.readTemperature());
- // Send the SMS
- char* number = "2926451386"; // Replace with the recipient's phone number
- bool error = GSM.sendSms(number, message);
- // Check if SMS was sent successfully
- if (error) {
- Serial.println(F("SMS sent successfully!"));
- } else {
- Serial.println(F("Failed to send SMS!"));
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement