SAFE — ESP32 simulation

sih

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

Components

Sketch code

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TinyGPS++.h>
#include <math.h>

// =====================================================
// VEHICLE
// =====================================================

#define VEHICLE_ID "DUMPER_B"

// =====================================================
// GPS
// =====================================================

#define RXD2 16
#define TXD2 17

bool SIMULATION_MODE = true;

// =====================================================
// WIFI
// =====================================================

const char* WIFI_SSID = "Velxio-GUEST";
const char* WIFI_PASS = "";

// =====================================================
// MQTT
// =====================================================

const char* MQTT_SERVER = "broker.hivemq.com";
const int MQTT_PORT = 1883;

const char* MY_TOPIC = "safehaul/vehicle2";
const char* OTHER_TOPIC = "safehaul/vehicle1";

// =====================================================
// OBJECTS
// =====================================================

WiFiClient espClient;
PubSubClient mqtt(espClient);

TinyGPSPlus gps;
HardwareSerial GPS(2);

LiquidCrystal_I2C lcd(0x27, 16, 2);

// =====================================================
// DUMPER B VIRTUAL DATA
// =====================================================

double myLat = 17.386800;
double myLon = 78.486700;

double mySpeed = 20.0;
double myHeading = 180.0;

// =====================================================
// DUMPER A DATA
// =====================================================

double otherLat = 17.385000;
double otherLon = 78.486700;

double otherSpeed = 0.0;
double otherHeading = 0.0;

unsigned long lastReceive = 0;
unsigned long lastMovement = 0;
unsigned long lastSend = 0;

// =====================================================
// DISTANCE
// =====================================================

double distanceMeters(
  double lat1,
  double lon1,
  double lat2,
  double lon2
) {

  const double R = 6371000.0;

  double p1 =
    lat1 * PI / 180.0;

  double p2 =
    lat2 * PI / 180.0;

  double dp =
    (lat2 - lat1) * PI / 180.0;

  double dl =
    (lon2 - lon1) * PI / 180.0;

  double a =
    sin(dp / 2) * sin(dp / 2) +
    cos(p1) * cos(p2) *
    sin(dl / 2) * sin(dl / 2);

  return R * 2.0 *
         atan2(
           sqrt(a),
           sqrt(1.0 - a)
         );
}

// =====================================================
// CLOSING SPEED
// =====================================================

double calculateClosingSpeed(
  double ownLat,
  double ownLon,
  double ownSpeed,
  double ownHeading,
  double targetLat,
  double targetLon,
  double targetSpeed,
  double targetHeading
) {

  double latRad =
    ownLat * PI / 180.0;

  double dx =
    (targetLon - ownLon) *
    111320.0 *
    cos(latRad);

  double dy =
    (targetLat - ownLat) *
    111320.0;

  double distance =
    sqrt(dx * dx + dy * dy);

  if (distance < 0.1)
    return 0;

  doub

More projects by ramcharantejajakkam