#define BLYNK_TEMPLATE_ID "TMPL33W0I8vH2"
#define BLYNK_TEMPLATE_NAME "EV monitor"
#define BLYNK_AUTH_TOKEN "1q2qflGwdaHwIghctqcVHXcYlLr9Ui95"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// -------- WiFi --------
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// -------- Pin Definitions --------
#define ONE_WIRE_BUS 4
#define POT1 34
#define POT2 35
#define LED_PIN 2
#define BUZZER_PIN 15
#define RELAY_PIN 5
// -------- LCD --------
LiquidCrystal_I2C lcd(0x27, 16, 2);
// -------- Temperature --------
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// -------- Thresholds --------
float maxVoltage = 3;
float minVoltage = 1;
float maxCurrent = 4;
float minCurrent = 1;
float maxTemp = 35.0;
float minTemp = 15.0;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
sensors.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting...");
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
lcd.clear();
lcd.print("System Ready");
delay(2000);
lcd.clear();
}
void loop() {
Blynk.run(); //
// ---- Temperature ----
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
// ---- Analog ----
int potValue1 = analogRead(POT1);
int potValue2 = analogRead(POT2);
float voltage = (potValue1 * 3.3) / 4095.0;
float sensorVoltage = (potValue2 * 3.3) / 4095.0;
float current = abs((sensorVoltage - 1.65) / 0.185);
// Battery %
float batteryPercent = ((voltage - minVoltage) / (maxVoltage - minVoltage)) * 100;
batteryPercent = constrain(batteryPercent, 0, 100);
// ---- Faults ----
bool overTemp = (temperature > maxTemp);
bool underTemp = (temperature < minTemp);
bool overVoltage = (voltage > maxVoltage);
bool underVoltage = (voltage < minVoltage);
bool overCurrent = (current > maxCurrent);
bool underCurrent = (current < minCurrent);
bool alert = overTemp || underTemp || overVoltage || underVoltage || overCurrent || underCurrent;
String alertMsg = "SAFE";
if (overTemp) alertMsg = "High Temp";
else if (underTemp) alertMsg = "Low Temp";
else if (overVoltage) alertMsg = "Over Volt";
else if (underVoltage) alertMsg = "Low Volt";
else if (overCurrent) alertMsg = "Over Curr";
else if (underCurrent) alertMsg = "Low Curr";
// ---- SERIAL DEBUGGING (ADDED) ----
Serial.print("Temp: "); Serial.print(temperature);
Serial.print(" °C | Volt: "); Serial.print(voltage);
Serial.print(" V | Curr: "); Serial.print(current);
Serial.print(" A | Battery: "); Serial.print(batteryPercent);
Serial.print("% | Status: "); Serial.println(alertMsg);
// ---- LCD (UPDATED TO SHOW ALL VALUES) ----
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperature, 1);
lcd.print(" V:");
lcd.print(voltage, 1);
lcd.setCursor(0, 1);
lcd.print("I:");
lcd.print(current, 1);
lcd.print(" ");
lcd.print(alertMsg);
// ---- Relay + Alert ----
if (alert) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(RELAY_PIN, LOW);
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RELAY_PIN, HIGH);
}
// ---- SEND TO BLYNK ----
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, voltage);
Blynk.virtualWrite(V2, current);
Blynk.virtualWrite(V3, batteryPercent);
// ---- ALERT NOTIFICATION ----
static unsigned long lastAlert = 0;
if (alert && millis() - lastAlert > 10000) {
Blynk.logEvent("battery_alert", alertMsg);
lastAlert = millis();
}
delay(1000);
}