This circuit integrates an Arduino Nano with a GPS module (NEO 6M), a GSM module (SIM900A), a buzzer, and a pushbutton. The primary function of this circuit is to provide location tracking and emergency alert capabilities. When the pushbutton is pressed, the buzzer is activated, and the GPS coordinates are sent via SMS using the GSM module.
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// Pin definitions
const int buttonPin = 2; // Button connected to PIN 2
const int buzzerPin = 9; // Buzzer connected to PIN 9
const int gsmTx = 10; // GSM module TX pin connected to PIN 10
const int gsmRx = 11; // GSM module RX pin connected to PIN 11
const int gpsRx = 6; // GPS module TX pin connected to PIN 6 (SoftwareSerial)
const int gpsTx = 7; // GPS module RX pin connected to PIN 7 (SoftwareSerial)
SoftwareSerial gsmSerial(gsmRx, gsmTx); // RX, TX for GSM
SoftwareSerial gpsSerial(gpsRx, gpsTx); // RX, TX for GPS
TinyGPSPlus gps; // GPS object
float cachedLatitude = 0.0;
float cachedLongitude = 0.0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
// Start serial communication
Serial.begin(9600);
gsmSerial.begin(9600);
gpsSerial.begin(9600);
Serial.println("Device Ready");
// Initialize GSM module and check connection
if (!initGSM()) {
Serial.println("GSM Initialization Failed");
} else {
Serial.println("GSM Initialized");
}
}
void loop() {
// Continuously read GPS data
while (gpsSerial.available()) {
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()) {
cachedLatitude = gps.location.lat();
cachedLongitude = gps.location.lng();
}
}
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Activate the buzzer
digitalWrite(buzzerPin, HIGH);
delay(1000); // Buzzer on for 1 second
digitalWrite(buzzerPin, LOW);
// Send SMS with last known GPS location
sendSMS(cachedLatitude, cachedLongitude);
// Debounce the button
delay(2000); // 2 seconds wait before next press
}
}
// Function to initialize GSM connection
bool initGSM() {
gsmSerial.println("AT"); // Test AT startup
delay(1000);
if (!checkGSMResponse()) return false;
gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
delay(1000);
return checkGSMResponse();
}
// Function to send SMS
void sendSMS(float latitude, float longitude) {
gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
delay(1000);
if (!checkGSMResponse()) {
Serial.println("Failed to set SMS mode.");
return;
}
gsmSerial.print("AT+CMGS=\""); // Begin sending SMS
gsmSerial.print("+917905030839"); // Replace with actual number
gsmSerial.println("\"");
delay(1000);
if (!checkGSMResponse()) {
Serial.println("Failed to send AT+CMGS command.");
return;
}
// Sending the message body
gsmSerial.print("Emergency alert! My location: ");
gsmSerial.print("https://www.google.com/maps?q=");
gsmSerial.print(latitude, 6);
gsmSerial.print(",");
gsmSerial.print(longitude, 6);
gsmSerial.println((char)26); // Send Ctrl+Z to indicate end of message
delay(5000); // Wait for the message to be sent
if (!checkGSMResponse()) {
Serial.println("Message sending failed, retrying...");
sendSMS(latitude, longitude); // Retry sending SMS
} else {
Serial.println("SMS sent successfully!");
}
}
// Function to check GSM module response
bool checkGSMResponse() {
long timeout = millis() + 5000; // 5 seconds timeout
while (millis() < timeout) {
if (gsmSerial.available()) {
String response = gsmSerial.readString();
Serial.println(response); // Print GSM response
if (response.indexOf("OK") != -1) {
return true; // Success if "OK" is found in the response
}
}
}
return false; // No response or unsuccessful
}
Note: The code provided is for the Arduino Nano microcontroller. It includes the initialization of the GSM and GPS modules, reading GPS data, sending SMS with location information, and handling the pushbutton and buzzer functionality. The phone number in the sendSMS
function should be replaced with the actual recipient's number.