#define BLYNK_TEMPLATE_ID "TMPL3JrllXvsC"
#define BLYNK_TEMPLATE_NAME "Batterylevel"
#define BLYNK_AUTH_TOKEN "fHnbV9ra1HlQGi3GQtI2zNpfQ0fJ_ZBV"
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <string.h>
char ssid[] = "Wokwi-GUEST"; //CHANGE TO YOUR OWN WIFI NAME
char pass[] = ""; //CHANGE TO YOUR OWN WIFI PASSWORD
#define POT_PIN 35
// Define the LCD address (check your display documentation)
#define LCD_ADDRESS 0x27
#define LCD_COLS 20 // Adjusted columns to match your display
#define LCD_ROWS 4 // Adjusted rows to match your display
// Create a LiquidCrystal_I2C object
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
// Define the DHT sensor type and pin
#define DHT_PIN 27 // GPIO pin number connected to the DHT22 sensor
#define DHT_TYPE DHT22
// Initialize the DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
// Define the battery voltage range (in volts)
const float minVoltage = 3.0; // Minimum voltage of the battery
const float maxVoltage = 4.2; // Maximum voltage of the battery
// Define the maximum range of the EV
const float maxRangeKm = 275.0; // Maximum range in kilometers when the battery is at 100%
// Counter variable to count how many times battery percentage is 100%
int fullChargeCount = 0;
bool isFullyCharged = false; //important
bool showError = false;
char errorText[180] = ""; //important
bool isLifeCycleEnded = false; //important
// Statistics variables for temperature
float totalTemperature = 0;
float averageTemperature = 0;
int temperatureSampleCount = 0;
// Statistics variables for humidity
float totalHumidity = 0;
float averageHumidity = 0;
int humiditySampleCount = 0;
// Statistics variables for battery
float totalBatteryPercentage = 0;
float averageBatteryPercentage = 0;
int batterySampleCount = 0;
int chargeCycles = 0;
// Statistics variables for range
float totalRange = 0;
float averageRange = 0;
int rangeSampleCount = 0;
void setup() {
// Initialize the LCD display
lcd.init();
lcd.backlight();
// Initialize the DHT sensor
dht.begin();
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(POT_PIN, INPUT);
}
void loop() {
Blynk.run();
float temperature = dht.readTemperature(); //important
float humidity = dht.readHumidity(); //important
int potValue = analogRead(POT_PIN);
float voltage = potValue * (3.3 / 4095.0);
float batteryPercentage = (voltage / 3.3) * 100; //important
float remainingKm = (batteryPercentage / 100.0) * maxRangeKm; //important
// Update temperature statistics
totalTemperature += temperature;
temperatureSampleCount++;
averageTemperature = totalTemperature / temperatureSampleCount; //important
// Update humidity statistics
totalHumidity += humidity;
humiditySampleCount++;
averageHumidity = totalHumidity / humiditySampleCount; //important
// Update battery statistics
totalBatteryPercentage += batteryPercentage;
batterySampleCount++;
averageBatteryPercentage = totalBatteryPercentage / batterySampleCount; //important
// Update range statistics
totalRange += remainingKm;
rangeSampleCount++;
averageRange = totalRange / rangeSampleCount; //important
Serial.println(averageRange);
Serial.println(averageBatteryPercentage);
Serial.println(averageTemperature);
Serial.println(averageHumidity);
Blynk.virtualWrite(V0, batteryPercentage);
Blynk.virtualWrite(V3, remainingKm);
Blynk.virtualWrite(V2, humidity);
Blynk.virtualWrite(V1, temperature);
// Handle error messages
showError = false;
if (temperature > 60) {
lcd.clear();
lcd.setCursor(0, 0);
strcpy(errorText, "Temperature too High!"); //important
lcd.print(errorText);
showError = true;
} else if (humidity > 55) {
lcd.clear();
lcd.setCursor(0, 0);
strcpy(errorText, "Humidity too High!");
lcd.print(errorText);
showError = true;
} else if (batteryPercentage == 0) {
lcd.clear();
lcd.setCursor(0, 0);
strcpy(errorText, "No Battery");
lcd.print(errorText);
showError = true;
} else if (batteryPercentage <= 15) {
lcd.clear();
lcd.setCursor(0, 0);
strcpy(errorText, "Low Battery");
lcd.print(errorText);
showError = true;
} else if (batteryPercentage >= 99.5 && batteryPercentage <= 100.5) {
fullChargeCount++;
isFullyCharged = true; //important
strcpy(errorText, "Battery Fully charged!"); //important
showError = false;
} else {
fullChargeCount = 0;
isFullyCharged = false; //important
strcpy(errorText, ""); //important
}
if (showError) {
delay(1000); // Keep the error message visible
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
lcd.setCursor(0, 2);
lcd.print("Battery: ");
lcd.print(batteryPercentage);
lcd.print(" %");
lcd.setCursor(0, 3);
lcd.print("Range: ");
lcd.print(remainingKm);
lcd.print(" km");
}
if (fullChargeCount > 5) {
lcd.clear();
lcd.setCursor(0, 0);
strcpy(errorText, "Life Cycle Ended"); //important
lcd.print("Life Cycle Ended");
isLifeCycleEnded = true;
while (true) {}
}
delay(1500);
}