Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32C3 and SIM900A GPS Tracker with Safe Zone Alerts

View and Edit Project In Cirkit DesignerImage of ESP32C3 and SIM900A GPS Tracker with Safe Zone Alerts

Circuit Documentation

Summary

This circuit is a GPS tracker system that uses an ESP32C3 microcontroller to interface with a GPS NEO 6M module and a SIM900A Mini module. The system is powered by a 3.7V LiPo battery. The ESP32C3 reads GPS data and sends SMS alerts if the tracked object moves outside a predefined safe zone.

Component List

ESP32C3 Supermini

GPS NEO 6M

SIM900A Mini

3.7V LiPo

Wiring Details

ESP32C3 Supermini

GPS NEO 6M

SIM900A Mini

3.7V LiPo

Documented Code

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

TinyGPSPlus gps;  // GPS object to parse data

// Define serial communication pins for SIM900A and GPS
SoftwareSerial sim900Serial(10, 12); // RX, TX for SIM900A Mini
SoftwareSerial gpsSerial(5, 6);    // RX, TX for GPS Neo-6M

// Define the safe zone coordinates (latitude, longitude)
const double safeLatitude = 37.7749;  // Example: San Francisco latitude
const double safeLongitude = -122.4194;  // Example: San Francisco longitude
const float safeRadius = 100.0;  // Safe radius in meters

// Parent phone number to send SMS alert
const char phoneNumber[] = "+917020961222";  // Replace with parent's phone number

void setup() {
  // Start serial communication
  Serial.begin(115200);
  sim900Serial.begin(9600);  // Initialize SIM900A Serial at 9600 baud rate
  gpsSerial.begin(9600);     // Initialize GPS Serial at 9600 baud rate
  
  Serial.println("GPS Tracker System Starting...");
  
  // Initialize SIM900A
  sendSMS("Device started and waiting for GPS signal...");
}

void loop() {
  // Read data from GPS and SIM900A serial
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
    
    // If we have a valid GPS fix
    if (gps.location.isUpdated()) {
      double currentLatitude = gps.location.lat();
      double currentLongitude = gps.location.lng();

      Serial.print("Current Latitude: ");
      Serial.println(currentLatitude, 6);
      Serial.print("Current Longitude: ");
      Serial.println(currentLongitude, 6);
      
      // Check if the child is outside the safe zone
      if (isOutsideSafeZone(currentLatitude, currentLongitude)) {
        sendSMS("ALERT: Child has left the safe zone!");
        delay(60000);  // Wait for 1 minute before checking again to avoid spamming
      }
    }
  }
}

// Function to calculate distance between two GPS coordinates using Haversine formula
float calculateDistance(double lat1, double lon1, double lat2, double lon2) {
  const float R = 6371.0; // Radius of Earth in kilometers
  float dLat = radians(lat2 - lat1);
  float dLon = radians(lon2 - lon1);
  
  float a = sin(dLat / 2) * sin(dLat / 2) +
            cos(radians(lat1)) * cos(radians(lat2)) *
            sin(dLon / 2) * sin(dLon / 2);
  float c = 2 * atan2(sqrt(a), sqrt(1 - a));
  
  float distance = R * c * 1000;  // Distance in meters
  return distance;
}

// Function to check if the current location is outside the safe zone
bool isOutsideSafeZone(double lat, double lon) {
  float distance = calculateDistance(lat, lon, safeLatitude, safeLongitude);
  Serial.print("Distance from safe zone: ");
  Serial.println(distance);
  
  return distance > safeRadius;
}

// Function to send SMS using SIM900A
void sendSMS(const char* message) {
  sim900Serial.println("AT");  // Check if SIM900A is ready
  delay(1000);
  sim900Serial.println("AT+CMGF=1");  // Set SMS format to text mode
  delay(1000);
  sim900Serial.print("AT+CMGS=\"");
  sim900Serial.print(phoneNumber);  // Send SMS to this number
  sim900Serial.println("\"");
  delay(1000);
  sim900Serial.println(message);  // The message content
  delay(1000);
  sim900Serial.write(26);  // CTRL+Z to send SMS
  delay(1000);
}

This code initializes the GPS and SIM900A modules, reads GPS data, and sends an SMS alert if the tracked object moves outside the predefined safe zone. The safe zone is defined by a central latitude and longitude, and a radius in meters.