#define BLYNK_TEMPLATE_ID "TMPL3z-YoNg_A"
#define BLYNK_TEMPLATE_NAME "Annal D"
#define BLYNK_AUTH_TOKEN "J21YQhvW64KFD8883UZqu_BRZ4eZzpPe"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <DHT.h>
#include <Ultrasonic.h>
#include <BlynkSimpleEsp32.h>
// Blynk Auth Token
char auth[] = "J21YQhvW64KFD8883UZqu_BRZ4eZzpPe";
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Create a TinyGPS++ object
TinyGPSPlus gps;
HardwareSerial GPS_Serial(2); // UART2 for GPS
#define OUT_PIN TX
#define IN_PIN RX
gpsmodule ChargingPoint(OUT_PIN,IN_PIN);
// DHT22
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Ultrasonic Sensor
#define TRIG_PIN 5
#define ECHO_PIN 15
Ultrasonic ultrasonic(TRIG_PIN, ECHO_PIN);
// LCD
#define SDA_PIN 21
#define SCL_PIN 22
LiquidCrystal_I2C lcd(0x27, 16, 2,SDA_PIN,SCL_PIN);
// Potentiometer
#define POT_PIN 34
// Buzzer
#define BUZZER_PIN 13
// Virtual pins for Blynk
#define VPIN_LATITUDE V1
#define VPIN_LONGITUDE V2
#define VPIN_NEAREST_CHARGING V3
#define VPIN_BATTERY_LEVEL_SPEED V4
#define VPIN_HUMIDITY_TEMPERATURE V5
// Function to calculate distance using the Haversine formula
double calculateDistance(float lat1, float lon1, float lat2, float lon2) {
const float R = 6371; // Radius of the Earth in km
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; // Distance in km
return distance;
}
// Predefined charging points
struct ChargingPoint {
float latitude;
float longitude;
String name;
};
ChargingPoint chargingPoints[] = {
{37.7749, -122.4194, "Charging Point 1"},
{34.0522, -118.2437, "Charging Point 2"},
{40.7128, -74.0060, "Charging Point 3"}
};
// Find nearest charging point
ChargingPoint findNearestChargingPoint(float currentLat, float currentLon) {
ChargingPoint nearest = chargingPoints[0];
double minDistance = calculateDistance(currentLat, currentLon, nearest.latitude, nearest.longitude);
for (int i = 1; i < sizeof(chargingPoints)/sizeof(chargingPoints[0]); i++) {
double distance = calculateDistance(currentLat, currentLon, chargingPoints[i].latitude, chargingPoints[i].longitude);
if (distance < minDistance) {
minDistance = distance;
nearest = chargingPoints[i];
}
}
return nearest;
}
void setup() {
Serial.begin(115200);
GPS_Serial.begin(9600, SERIAL_8N1, 16, 17); // GPS
dht.begin();
lcd.begin();
lcd.backlight();
pinMode(POT_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
Serial.println("Setup complete");
}
void loop() {
Blynk.run();
while (GPS_Serial.available() > 0) {
gps.encode(GPS_Serial.read());
}
if (gps.location.isUpdated()) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();
float speed = gps.speed.kmph();
Serial.print("Latitude: "); Serial.println(latitude, 6);
Serial.print("Longitude: "); Serial.println(longitude, 6);
Serial.print("Speed: "); Serial.println(speed);
ChargingPoint nearest = findNearestChargingPoint(latitude, longitude);
Serial.print("Nearest Charging Point: ");
Serial.println(nearest.name);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.print(speed);
lcd.print(" km/h");
Blynk.virtualWrite(VPIN_LATITUDE, latitude);
Blynk.virtualWrite(VPIN_LONGITUDE, longitude);
Blynk.virtualWrite(VPIN_NEAREST_CHARGING, nearest.name);
}
int potValue = analogRead(POT_PIN);
float batteryLevel = potValue * (3.3 / 4095.0);
Serial.print("Battery Level: "); Serial.println(batteryLevel);
float distance = ultrasonic.read();
Serial.print("Distance: "); Serial.println(distance);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: "); Serial.println(h);
Serial.print("Temperature: "); Serial.println(t);
if (t > 30.0) {
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
delay(2000);
}