Can anyone help me complete the code and answer my questions? 1. what recipient number should i put (my phone number or the simcard number)

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

// Create SoftwareSerial connections
SoftwareSerial gpsSerial(4, 3); // RX, TX for GPS
SoftwareSerial gsmSerial(7, 8); // RX, TX for GSM

TinyGPSPlus gps;

void setup() {
  Serial.begin(9600);       // Serial monitor for debugging
  gpsSerial.begin(9600);    // GPS module baud rate
  gsmSerial.begin(9600);    // GSM module baud rate

  // Initialize GSM module
  Serial.println("Initializing GSM module...");
  gsmSerial.println("AT+CMGF=1"); // Set SMS text mode
  delay(1000);
  gsmSerial.println("AT+CNMI=2,1,0,0,0"); // Configure GSM to show new SMS on serial
  delay(1000);
  Serial.println("GSM initialization complete.");
}

void loop() {
  // Check if there's an incoming SMS
  if (gsmSerial.available()) {
    String sms = gsmSerial.readString();
    Serial.println("Received SMS:");
    Serial.println(sms);

    if (sms.indexOf("Location") >= 0) { // Replace "Location" with your keyword
      sendLocation();
    }
  }

  // Process GPS data
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
    if (gps.location.isUpdated()) {
      Serial.print("Latitude: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("Longitude: ");
      Serial.println(gps.location.lng(), 6);
    }
  }
}

void sendLocation() {
  String recipientNumber = "+01xxxxxxx"; // Replace with the actual recipient number
  
  if (gps.location.isValid()) {
    float latitude = gps.location.lat();
    float longitude = gps.location.lng();
    
    // Create a Google Maps link
    String mapsLink = "https://www.google.com/maps?q=" + String(latitude, 6) + "," + String(longitude, 6);
    String message = "Current Location: " + mapsLink;
    
    Serial.println("Sending SMS...");
    gsmSerial.println("AT+CMGF=1"); // Set SMS text mode
    delay(1000);
    gsmSerial.println("AT+CMGS=\"" + recipientNumber + "\""); // Set recipient number
    delay(1000);
    gsmSerial.println(message); // Send message
    delay(1000);
    gsmSerial.println((char)26); // ASCII code of CTRL+Z to send SMS
    delay(5000);
    Serial.println("SMS sent.");
  } else {
    Serial.println("Failed to get GPS location.");
  }
}
***strong text***

My component now are
1.arduino Uno
2.GSM SIM900A
3.NEO-6M-0-001
4.LM2596
5.Battery 3.7V

Is there any addition component should i add and can you teach me how to get the message on Google maps live location link?

Welcome to the forum

SoftwareSerial gpsSerial(4, 3); // RX, TX for GPS
SoftwareSerial gsmSerial(7, 8); // RX, TX for GSM

Forget your question in the topic title for now

I can say with some certainty that 2 instances of SoftwareSerial will not work in your project because there is only one receive buffer shared between the two of them

ouh thank u for the reply so how to do it?my pin number is wrong? sorry im the beginner

It does not matter which pins you use for the two instances of SoftwareSerial, it will not work if both can receive data at any time. You can switch between the two instances by using the SoftwareSerial listen() function but I have never seen anyone make that work with anything other than a trivial example sketch

As to what you can do about it, the obvious answer is to use an Arduino with more than one hardware Serial port such as a Mega

ok im done change it to arduino Mega

Just to be clear, when you use the Mega you do not need to use SoftwareSerial, nor should you try

1 Like

so can i use like this ?AltSoftSerial gpsSerial; // Default RX is pin 8, TX is pin 9 AltSoftSerial gsmSerial; // Default RX is pin 10, TX is pin 11

#include <AltSoftSerial.h>
#include <TinyGPS++.h>

// Create AltSoftSerial connections
AltSoftSerial gpsSerial;  // Default RX is pin 8, TX is pin 9
AltSoftSerial gsmSerial;  // Default RX is pin 10, TX is pin 11

TinyGPSPlus gps;

void setup() {
  Serial.begin(9600);       // Serial monitor for debugging
  gpsSerial.begin(9600);    // GPS module baud rate
  gsmSerial.begin(9600);    // GSM module baud rate

  // Initialize GSM module
  Serial.println("Initializing GSM module...");
  gsmSerial.println("AT+CMGF=1"); // Set SMS text mode
  delay(1000);
  gsmSerial.println("AT+CNMI=2,1,0,0,0"); // Configure GSM to show new SMS on serial
  delay(1000);
  Serial.println("GSM initialization complete.");
}

void loop() {
  // Check if there's an incoming SMS
  if (gsmSerial.available()) {
    String sms = gsmSerial.readString();
    Serial.println("Received SMS:");
    Serial.println(sms);

    if (sms.indexOf("Location") >= 0) { // Replace "Location" with your keyword
      sendLocation();
    }
  }

  // Process GPS data
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
    if (gps.location.isUpdated()) {
      Serial.print("Latitude: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("Longitude: ");
      Serial.println(gps.location.lng(), 6);
    }
  }
}

void sendLocation() {
  String recipientNumber = "+01xxxxxxx"; // Replace with the actual recipient number
  
  if (gps.location.isValid()) {
    float latitude = gps.location.lat();
    float longitude = gps.location.lng();
    
    // Create a Google Maps link
    String mapsLink = "https://www.google.com/maps?q=" + String(latitude, 6) + "," + String(longitude, 6);
    String message = "Current Location: " + mapsLink;
    
    Serial.println("Sending SMS...");
    gsmSerial.println("AT+CMGF=1"); // Set SMS text mode
    delay(1000);
    gsmSerial.println("AT+CMGS=\"" + recipientNumber + "\""); // Set recipient number
    delay(1000);
    gsmSerial.println(message); // Send message
    delay(1000);
    gsmSerial.println((char)26); // ASCII code of CTRL+Z to send SMS
    delay(5000);
    Serial.println("SMS sent.");
  } else {
    Serial.println("Failed to get GPS location.");
  }
}

Why are you still trying to use software serial in any form? As @UKHeliBob has already pointed out the Mega has multiple hardware serial ports.

#include <TinyGPS++.h>

TinyGPSPlus gps;

void setup() {
  Serial.begin(9600);       // Serial monitor for debugging
  Serial1.begin(9600);      // GPS module connected to Serial1 (TX1/RX1)
  Serial2.begin(9600);      // GSM module connected to Serial2 (TX2/RX2)

  // Initialize GSM module
  Serial.println("Initializing GSM module...");
  Serial2.println("AT+CMGF=1"); // Set SMS text mode
  delay(1000);
  Serial2.println("AT+CNMI=2,1,0,0,0"); // Configure GSM to show new SMS on serial
  delay(1000);
  Serial.println("GSM initialization complete.");
}

void loop() {
  // Check if there's an incoming SMS from the GSM module
  if (Serial2.available()) {
    String sms = Serial2.readString();
    Serial.println("Received SMS:");
    Serial.println(sms);

    if (sms.indexOf("Location") >= 0) { // Replace "Location" with your keyword
      sendLocation();
    }
  }

  // Process GPS data from the GPS module
  while (Serial1.available() > 0) {
    gps.encode(Serial1.read());
    if (gps.location.isUpdated()) {
      Serial.print("Latitude: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("Longitude: ");
      Serial.println(gps.location.lng(), 6);
    }
  }
}

void sendLocation() {
  String recipientNumber = "+01xxxx"; // Replace with the actual recipient number
  
  if (gps.location.isValid()) {
    float latitude = gps.location.lat();
    float longitude = gps.location.lng();
    
    // Create a Google Maps link
    String mapsLink = "https://www.google.com/maps?q=" + String(latitude, 6) + "," + String(longitude, 6);
    String message = "Current Location: " + mapsLink;
    
    Serial.println("Sending SMS...");
    Serial2.println("AT+CMGF=1"); // Set SMS text mode
    delay(1000);
    Serial2.println("AT+CMGS=\"" + recipientNumber + "\""); // Set recipient number
    delay(1000);
    Serial2.println(message); // Send message
    delay(1000);
    Serial2.println((char)26); // ASCII code of CTRL+Z to send SMS
    delay(5000);
    Serial.println("SMS sent.");
  } else {
    Serial.println("Failed to get GPS location.");
  }
}

ok like this? serial monitor function but how to receive sms?

i sent the sms to the simcard number but it said that the "sms didnt reach as the phone is switched off" how to solve this problem ?

Have the recipient turn on their 'phone

1 Like

yes

My reply was a suggestion that the recipient 'phone needs to be turned on, not a question

Which sketch are you running and are you sure that you are using the correct recipient 'phone number ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.