Last active
January 2, 2025 16:58
-
-
Save easterncoder/3a23f75684e5e3f447fd08973d2ec7bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TinyGPS++.h> | |
#include <SoftwareSerial.h> | |
/** | |
* counterACT GPS Tracking System | |
* | |
* This program uses the TinyGPS++ library to track the location of a device | |
* and send an SMS message to a recipient when the location changes significantly. | |
* | |
* Team Members: | |
* - Jae Lopez | |
* - Maverick Bayten | |
* - Kurt Tumagan | |
* - Riesse Ramos | |
* - Mika Valera | |
* | |
* GPS to Arduino Pin Connections: | |
* - Power to 3V3 (Red) | |
* - Ground to Ground (Black) | |
* - Rx to D6 (Blue) | |
* - Tx to D7 (White) | |
* | |
* SIM to Arduino Pin Connections | |
* - Power to 5V (Red) | |
* - Ground to Ground (Black) | |
* - Rx to D2 (Green) | |
* - Tx to D3 (Orange) | |
* - Ground to Ground (Black) | |
*/ | |
// Recipient phone number - change as needed | |
const String recipient = "+639000000000"; | |
// SIM800L serial object | |
SoftwareSerial sim(3, 2); // SIM800L Tx & Rx | |
// GPS serial object | |
SoftwareSerial gps(7, 6); // GPS Tx & Rx | |
// TinyGPS object | |
TinyGPSPlus tinygps; | |
// Constants | |
const int baudRate = 9600; | |
const double locationThreshold = 0.0005; // Threshold for location change, approximately 50m +/- 5m | |
// Previous GPS coordinates | |
double prevLatitude = 0.0; | |
double prevLongitude = 0.0; | |
/** | |
* Send a command to the SIM800L and print the response. | |
* | |
* @param cmd Command to send | |
* @param eol End-of-line character | |
*/ | |
void simCmd(const String &cmd, int eol = '\n') { | |
Serial.println("Sending to SIM: " + cmd); | |
sim.print(cmd); | |
if (eol) sim.write(eol); // Optional end-of-line character | |
delay(500); | |
Serial.println("Response:"); | |
while (sim.available() > 0) { | |
Serial.write(sim.read()); // Forward response to Serial object | |
} | |
Serial.println(""); | |
} | |
/** | |
* Send an SMS message to the recipient. | |
* | |
* @param msg Message to send | |
*/ | |
void sendMessage(const String &msg) { | |
simCmd("AT"); | |
simCmd("AT+CMGF=1"); // Configuring TEXT mode | |
simCmd("AT+CMGS=\"" + recipient + "\""); | |
delay(500); | |
sim.print(msg); | |
sim.write(26); // End SMS with Ctrl+Z | |
delay(5000); // Wait for message sending to complete | |
} | |
/** | |
* Check if the location has changed significantly and send an SMS message if it has. | |
*/ | |
void getCoordinates() { | |
while (gps.available() > 0) { | |
tinygps.encode(gps.read()); | |
if (tinygps.location.isUpdated()) { | |
double currentLatitude = tinygps.location.lat(); | |
double currentLongitude = tinygps.location.lng(); | |
// Check if the change in latitude or longitude exceeds the threshold | |
if (abs(currentLatitude - prevLatitude) >= locationThreshold || | |
abs(currentLongitude - prevLongitude) >= locationThreshold) { | |
String msg = "Lat: " + String(currentLatitude, 6) + | |
", Lon: " + String(currentLongitude, 6) + | |
", Alt: " + String(tinygps.altitude.meters(), 2); | |
Serial.println("Significant location change detected: " + msg); | |
sendMessage(msg); | |
// Update previous coordinates | |
prevLatitude = currentLatitude; | |
prevLongitude = currentLongitude; | |
} | |
} | |
} | |
} | |
/** | |
* Setup function | |
*/ | |
void setup() { | |
Serial.begin(baudRate); | |
sim.begin(baudRate); | |
gps.begin(baudRate); | |
// Initialize SIM800L | |
Serial.println("Initializing SIM800L..."); | |
simCmd("AT"); // SIM handshake | |
simCmd("AT+CSQ"); // Signal quality test | |
simCmd("AT+CCID"); // Check SIM card | |
simCmd("AT+CREG?"); // Check network registration | |
Serial.println("SIM800L initialization complete."); | |
Serial.println("Initializing GPS..."); | |
delay(1000); // Allow GPS to stabilize | |
Serial.println("GPS initialization complete."); | |
} | |
/** | |
* Main loop */ | |
void loop() { | |
getCoordinates(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment