#define BLYNK_TEMPLATE_ID "TMPL35V6IvdST"
#define BLYNK_TEMPLATE_NAME "Smart Commute"
#define BLYNK_AUTH_TOKEN "ggHup0zaeK7JYcPGOwKXkjR-5MXyJpVP"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Blynk authentication token
char auth[] = BLYNK_AUTH_TOKEN;
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int potentiometerPin = 34; // GPIO34 on the ESP32
// Constants
const float tankCapacity = 45.0; // Fuel tank capacity in liters
const float consumptionRateBelow100 = 0.01; // 1% reduction per 10 minutes
const float consumptionRateBetween100And150 = 0.02; // 2% reduction per 10 minutes
const float consumptionRateBetween150And200 = 0.03; // 3% reduction per 10 minutes
const float consumptionRateAbove200 = 0.05;
const unsigned long alertDuration = 5000;
#define OVERSPEED_VPIN V3
// Variables
float currentFuelLevel = tankCapacity;
unsigned long previousTime = 0;
unsigned long lastAlertTime = 0; // To track the last alert time
bool isAlertActive = false;
int currentSpeed = 0;
const unsigned long updateInterval = 60000; // 1 minute in milliseconds
void setup() {
// Start serial communication at a baud rate of 115200
Serial.begin(115200);
// Connect to Blynk
Blynk.begin(auth, ssid, pass);
// Initialize previousTime to the current time
previousTime = millis();
}
void loop() {
// Run Blynk
Blynk.run();
// Read the analog value from the potentiometer
int potentiometerValue = analogRead(potentiometerPin);
// Map the potentiometer value (0-4095) to the speed (0-200 km/h)
currentSpeed = map(potentiometerValue, 0, 4095, 0, 200);
// Print the speed to the Serial Monitor
Serial.print("Speed: ");
Serial.print(currentSpeed);
Serial.println(" km/h");
// Update Blynk with current speed
Blynk.virtualWrite(V0, currentSpeed);
// if (currentSpeed > 100) {
// Blynk.virtualWrite(OVERSPEED_VPIN, 1); // Turn on LED
// Blynk.logEvent("over_speeding"); // Trigger the over_speeding event
// } else {
// Blynk.virtualWrite(OVERSPEED_VPIN, 0); // Turn off LED
// }
// Check if 1 minute has passed
unsigned long currentTime = millis();
if (currentSpeed > 100) {
if (!isAlertActive) {
// If the alert is not already active, activate it
Blynk.virtualWrite(OVERSPEED_VPIN, 1); // Turn on LED
Blynk.logEvent("over_speeding"); // Trigger the over_speeding event
lastAlertTime = currentTime; // Update the last alert time
isAlertActive = true; // Set the alert as active
}
}
// Check if 5 seconds have passed since the alert was triggered
if (isAlertActive && (currentTime - lastAlertTime >= alertDuration)) {
// Turn off the alert
Blynk.virtualWrite(OVERSPEED_VPIN, 0); // Turn off LED
isAlertActive = false; // Reset the alert state
}
if (currentTime - previousTime >= updateInterval) {
previousTime = currentTime;
// Update fuel level based on speed thresholds
// if (currentSpeed >= 60) {
// currentFuelLevel -= tankCapacity * consumptionRatePer10Min;
// }
float consumptionRate;
if (currentSpeed < 100) {
consumptionRate = consumptionRateBelow100;
} else if (currentSpeed >= 100 && currentSpeed < 150) {
consumptionRate = consumptionRateBetween100And150;
} else if (currentSpeed >= 150 && currentSpeed < 200) {
consumptionRate = consumptionRateBetween150And200;
} else { // currentSpeed >= 200
consumptionRate = consumptionRateAbove200;
}
// Update fuel level
currentFuelLevel -= tankCapacity * consumptionRate;
if (currentFuelLevel < 0) {
currentFuelLevel = 0;
}
// Print the current fuel level to the Serial Monitor
Serial.print("Fuel Level: ");
Serial.print(currentFuelLevel);
Serial.println(" liters");
// Update Blynk with current fuel level
Blynk.virtualWrite(V1, currentFuelLevel);
// Send fuel level to the graph widget (Virtual Pin V2)
Blynk.virtualWrite(V2, currentFuelLevel);
}
if (currentSpeed > 100) {
Blynk.virtualWrite(OVERSPEED_VPIN, 1); // Send a signal indicating overspeeding
} else {
Blynk.virtualWrite(OVERSPEED_VPIN, 0); // Clear the signal
}
// Add a small delay
delay(100); // 100 milliseconds delay
}
// Define the pin connected to the potentiometer
// const int potentiometerPin = 34; // GPIO34 on the ESP32
// void setup() {
// // Start serial communication at a baud rate of 115200
// Serial.begin(115200);
// // Connect to Blynk
// Blynk.begin(auth, ssid, pass);
// }
// void loop() {
// // Run Blynk
// Blynk.run();
// // Read the analog value from the potentiometer
// int potentiometerValue = analogRead(potentiometerPin);
// int speedKmph = map(potentiometerValue, 0, 4095, 0, 200);
// // Print the potentiometer value to the Serial Monitor
// Serial.print("Current Speed: ");
// Serial.println(speedKmph);
// // Send the potentiometer value to the Blynk app (Virtual Pin V0)
// Blynk.virtualWrite(V0, speedKmph);
// // Add a delay to slow down the output rate
// delay(100); // 100 milliseconds delay
// }