You are on page 1of 2

#include <SoftwareSerial.h> // Library for the software serial monitor.

#include <TinyGPS.h> // Library to access the GPS device.

SoftwareSerial gpsSerial(9, 8); //Initializing the software serial, RX at pin 8


and TX at pin 9
TinyGPS gps; //Initializing the GPS device.
long lat, lon; //declaring Variables as long integer to store the latitude and
longitude.
unsigned long interval = 30000; // the time system has to wait ton send the
message.
unsigned long previousMillis = 0; // Assigning previousMillis value with value 0

void setup() // Code that only runs once


{
gpsSerial.begin(9600); // Begin the software serial communication.
Serial.begin(9600); //Begin the hardware serial communication.
Serial.println("Initializing please wait..."); // Print Initializing please
wait... on the serial monitor.
}
void SendMessage()// Function to send Message
{
Serial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial.println("AT+CMGS=\"+917411058542\"\r"); // Replace x with mobile number
delay(1000);// delay so that the GSM module receives the command.
Serial.print("Position: "); // Print position in message body.
delay(500);// delay so that the GSM module receives the command.
Serial.print("latitude: "); //Print latitude in message body.
delay(500);// delay so that the GSM module receives the command.
Serial.print(lat); //Print the latitude value in message body.
delay(500);// delay so that the GSM module receives the command.
Serial.print(" ");
delay(500);// delay so that the GSM module receives the command.
Serial.print("longitude: "); //Print longitude in message body.
delay(500);// delay so that the GSM module receives the command.
Serial.println(lon); // Print the latitude value in message body.
delay(500);// delay so that the GSM module receives the command.
Serial.println((char)26);// ASCII code of CTRL+Z i.e exit and to send the
message.
delay(500);// delay so that the GSM module receives the command.
}

void loop() //Code that runs continusely


{
unsigned long currentMillis = millis(); // declaring and assigning the current
machine time to currentmillis
if ((unsigned long)(currentMillis - previousMillis) >= interval) // check if the
difference between the previous time and current time is greater than the interval
set.
{
if (gpsSerial.available()) // check for GPS data
{
if (gps.encode(gpsSerial.read()))// encode GPS data
{
gps.get_position(&lat, &lon); // get latitude and longitude from the GPS
module
Serial.print("Position: "); // Print the
Serial.print("Latitude: ");
Serial.print(lat);
Serial.print(" "); // print latitude
Serial.print("Longitude: ");
Serial.println(lon); // print longitude
delay (1000);
SendMessage(); // run the function of sending the message
delay(1000); //
previousMillis = millis(); // assign machine time to previous millis
}
}
}
}

You might also like