Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Last active December 4, 2018 12:31
Show Gist options
  • Select an option

  • Save arn-ob/ff50c51c1d799a153a444e947d7d529e to your computer and use it in GitHub Desktop.

Select an option

Save arn-ob/ff50c51c1d799a153a444e947d7d529e to your computer and use it in GitHub Desktop.
GPS based tracking and location sending using SMS
// written by stupid@arnob
#include <SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial SIM900(2, 3); // RX, TX
TinyGPS gps;
SoftwareSerial ss(6, 7);// RX, TX
float flat, flon;
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
unsigned long age;
int button = 11;
int activeLED = 12;
void setup() {
Serial.begin(9600);
SIM900.begin(9600);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
ss.begin(9600);
Serial.print("Simple TinyGPS library v. ");
Serial.println(TinyGPS::library_version());
pinMode(button, INPUT);
pinMode(activeLED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String a = "Here Is the Location";
String b = "Lat";
String c = String(flat, 6);
String d = "Long";
String e = String(flon, 6);
String msg = a + b + c + d + e;
if (digitalRead(button)) {
Serial.println("Button Active");
digitalWrite(activeLED, HIGH);
SendMessage(msg);
delay(1000);
} else {
digitalWrite(activeLED, LOW);
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
}
gps.stats(&chars, &sentences, &failed);
Serial.print(" CHARS=");
Serial.print(chars);
Serial.print(" SENTENCES=");
Serial.print(sentences);
Serial.print(" CSUM ERR=");
Serial.println(failed);
if (chars == 0)
Serial.println("** No characters received from GPS: check wiring **");
}
}
// send message
void SendMessage(String msg)
{
SIM900.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
SIM900.println("AT+CMGS=\"+8801748286562\"\r"); // Replace x with mobile number
delay(1000);
SIM900.println(msg);// The SMS text you want to send
delay(100);
SIM900.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment