#include <SoftwareSerial.
h>
// Define RX and TX pins for SoftwareSerial communication with GSM
module
// Connect GSM Module TX to Arduino RX_PIN (e.g., D10)
// Connect GSM Module RX to Arduino TX_PIN (e.g., D11)
const int GSM_RX_PIN = 10;
const int GSM_TX_PIN = 11;
// Create a SoftwareSerial object for the GSM module
SoftwareSerial gsmSerial(GSM_RX_PIN, GSM_TX_PIN);
void setup() {
// Initialize hardware serial for debugging (and potential communication
with a host)
Serial.begin(9600);
Serial.println("Arduino GSM SMS Sender");
Serial.println("Initializing GSM module...");
// Initialize software serial for GSM module
// Common baud rates for GSM modules are 9600, 19200, 115200.
// Check your module's datasheet for the correct baud rate.
gsmSerial.begin(9600);
delay(1000); // Give the module time to power up and stabilize
// Check if the GSM module is ready
sendATCommand("AT", 1000); // Simple AT command to check if module
responds
sendATCommand("AT+CSQ", 1000); // Check signal quality
sendATCommand("AT+CREG?", 1000); // Check network registration
status
delay(2000); // Wait for network registration
void loop() {
// Example: Send an SMS every 30 seconds (for demonstration)
// In a real application, you might trigger this based on a sensor, button,
// or command from a host computer.
sendSMS("+639123456789", "Hello from Arduino GSM!"); // Replace with
your phone number
// Wait for 30 seconds before sending another SMS
delay(30000);
// You can also listen for incoming data from the GSM module (e.g.,
incoming SMS)
// by continuously reading from gsmSerial.available()
while (gsmSerial.available()) {
Serial.write(gsmSerial.read()); // Forward anything from GSM to Serial
Monitor
// Function to send AT commands to the GSM module and print its
response
void sendATCommand(String command, long timeout) {
Serial.print("Sending: ");
Serial.println(command);
gsmSerial.println(command);
long startTime = millis();
while (millis() - startTime < timeout) {
if (gsmSerial.available()) {
Serial.write(gsmSerial.read());
Serial.println(); // New line after response
// Function to send an SMS
void sendSMS(String phoneNumber, String message) {
Serial.println("Attempting to send SMS...");
// Set SMS text mode
sendATCommand("AT+CMGF=1", 1000); // Text mode
delay(100);
// Send recipient phone number
gsmSerial.print("AT+CMGS=\"");
gsmSerial.print(phoneNumber);
gsmSerial.println("\"");
delay(100);
// Wait for '>' prompt
long startTime = millis();
while (millis() - startTime < 2000) {
if (gsmSerial.available()) {
char c = gsmSerial.read();
Serial.write(c); // Echo to serial monitor
if (c == '>') {
break;
Serial.println(); // New line for clarity
// Send the message content
gsmSerial.print(message);
gsmSerial.write(0x1A); // ASCII code for CTRL+Z (end of message)
delay(3000); // Give time for SMS to be sent
// Read the response from the module
while (gsmSerial.available()) {
Serial.write(gsmSerial.read());
Serial.println(); // New line after response
Serial.println("SMS send attempt complete.");