CCCPaste

ESP32 SMS rev_01

/********* 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: ESP32 SMS
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2025-11-25 03:29:32

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Enable remote communication by sending SMS using */
	/* the SIM800L module, ensuring proper pin */
	/* configurations and serial communication setup with */
	/* the ESP32 DevKit V1. */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

// Full and Final code implementing SMS send functionality for ESP32 with SIM800L as per System Requirement 1

#include  // https://github.com/plerup/espsoftwareserial
#include           // https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
#include        // https://github.com/madhephaestus/ESP32Servo

// Define pins for SoftwareSerial and SIM800L
const uint8_t RX_PIN = 13;     // RX pin for SoftwareSerial
const uint8_t TX_PIN = 4;      // TX pin for SoftwareSerial
const uint8_t RST_PIN = 14;    // Reset pin for SIM800L
const uint8_t RING_PIN = 16;   // Ring Detection pin (if needed)
// Instantiate SoftwareSerial for SIM800L communication
SoftwareSerial sim800Serial(RX_PIN, TX_PIN);
// Instantiate SIM800L object
Sim800L sim800(sim800Serial);

// Function prototypes
void sendSms(const char* number, const char* text);
// Variable to track system state
bool smsSent = false;

void setup() {
  // Initialize serial for debugging
  Serial.begin(115200);
  // Initialize SIM800L serial communication
  sim800Serial.begin(9600); // Baud rate for SIM800L
  // Initialize SIM800L with reset pin
  pinMode(RST_PIN, OUTPUT);
  digitalWrite(RST_PIN, HIGH); // Keep SIM800L out of reset
  delay(1000); // Wait for stabilization
  // Optional: Reset SIM800L
  digitalWrite(RST_PIN, LOW);
  delay(1000);
  digitalWrite(RST_PIN, HIGH);
  delay(2000); // Wait for module to initialize

  Serial.println("Initializing SIM800L...");
  // Basic check
  if (sim800.getProductInfo() != "") {
    Serial.println("SIM800L Initialized.");
  } else {
    Serial.println("Failed to initialize SIM800L.");
  }
  // Send initial SMS to a number
  sendSms("+1234567890", "Hello from ESP32! This is a system-generated message.");
  // Mark that SMS has been sent
  smsSent = true;
}

void loop() {
  // Keep the loop alive or implement additional communication if needed
  // For demonstration, do nothing
  if (smsSent) {
    // Do something or repeat
  }
}

// Function to send SMS
void sendSms(const char* number, const char* text) {
  Serial.print("Sending SMS to: ");
  Serial.println(number);
  bool success = sim800.sendSms(number, text);
  if (success) {
    Serial.println("SMS sent successfully.");
  } else {
    Serial.println("Failed to send SMS.");
  }
}

/* END CODE */