Location stamp — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by lakshmikeerthimeesala.

Components

Sketch code

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

// Create GPS object
TinyGPSPlus gps;

// GPS pins for ESP32 UART2
#define GPS_RX 16
#define GPS_TX 17

// SOURCE LOCATION
double sourceLatitude = 0.0;
double sourceLongitude = 0.0;
bool sourceSaved = false;

// DESTINATION LOCATION
double destinationLatitude = 0.0;
double destinationLongitude = 0.0;
bool destinationSaved = false;

// DISTANCE
double distanceKm = 0.0;

// FARE
double fare = 0.0;
const double FARE_PER_KM = 10.0;

const double EARTH_RADIUS_KM = 6371.0;

// HAVERSINE DISTANCE CALCULATION
double calculateDistance(double lat1, double lon1,
                         double lat2, double lon2) {

  double dLat = radians(lat2 - lat1);
  double dLon = radians(lon2 - lon1);

  double a = sin(dLat / 2) * sin(dLat / 2) +
             cos(radians(lat1)) *
             cos(radians(lat2)) *
             sin(dLon / 2) * sin(dLon / 2);

  double c = 2 * atan2(sqrt(a), sqrt(1 - a));

  return EARTH_RADIUS_KM * c;
}

void setup() {

  Serial.begin(115200);

  Serial2.begin(9600, SERIAL_8N1, GPS_RX, GPS_TX);

  Serial.println();
  Serial.println("========================================");
  Serial.println("       LOCATION STAMP - STEP 6");
  Serial.println("========================================");
  Serial.println("Waiting for GPS fix...");
  Serial.println();
}

void loop() {

  // Read GPS data
  while (Serial2.available()) {
    gps.encode(Serial2.read());
  }

  // Check GPS validity
  if (gps.location.isValid()) {

    Serial.println("GPS FIX AVAILABLE");

    Serial.print("Current Latitude  : ");
    Serial.println(gps.location.lat(), 6);

    Serial.print("Current Longitude : ");
    Serial.println(gps.location.lng(), 6);

    Serial.print("Satellites        : ");
    Serial.println(gps.satellites.value());

    Serial.println();
    Serial.println("S = Save SOURCE");
    Serial.println("D = Save DESTINATION");
    Serial.println("----------------------------------------");

    // Read command
    if (Serial.available()) {

      char command = Serial.read();

      // SAVE SOURCE
      if (command == 'S' || command == 's') {

        sourceLatitude = gps.location.lat();
        sourceLongitude = gps.location.lng();

        sourceSaved = true;

        Serial.println();
        Serial.println("************************************");
        Serial.println("       SOURCE LOCATION SAVED");
        Serial.println("************************************");

        Serial.print("Source Latitude  : ");
        Serial.println(sourceLatitude, 6);

        Serial.print("Source Longitude : ");
        Serial.println(sourceLongitude, 6);

        Serial.println("************************************");
        Serial.println();
      }

      // SAVE DESTINATION
      if (command == 'D' || command == 'd') {

        if (sourceSaved) {

          destinationLatitude = gps.location.lat();
          destinationLongitude = gps.location.lng();

          destinationSaved = true;

          // Calculate distance
   

More projects by lakshmikeerthimeesala