from machine import Pin, I2C, ADC
import dht
import ssd1306
import network
import urequests
import time
# ---------------------------
# Wi-Fi Details
# ---------------------------
SSID = "vivo Y02t"
PASSWORD = "123456780"
# ---------------------------
# Telegram Details
# ---------------------------
BOT_TOKEN = "8842331681:AAFOfLbELz3NIXBBubq_0tHd8nX-GopC_FE"
CHAT_ID = "8217319801"
# ---------------------------
# Connect Wi-Fi
# ---------------------------
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting to WiFi...")
count = 0
while not wlan.isconnected() and count < 10:
time.sleep(1)
print("Trying...", count + 1)
count += 1
if wlan.isconnected():
print("WiFi Connected")
print(wlan.ifconfig())
else:
print("WiFi Failed! Continuing without WiFi...")
# ---------------------------
# Telegram Function
# ---------------------------
def send_telegram(message):
try:
url = "https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}".format(
BOT_TOKEN,
CHAT_ID,
message.replace(" ", "%20").replace("\n", "%0A")
)
print("Sending Telegram...")
response = urequests.get(url)
print("Status:", response.status_code)
print(response.text)
response.close()
except Exception as e:
print("Telegram Error:", e)
# ---------------------------
# Components
# ---------------------------
sensor = dht.DHT22(Pin(16))
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_UP)
buzzer = Pin(17, Pin.OUT) # GP17 (matches your diagram.json)
motor = Pin(18, Pin.OUT)
motor.value(1)
gas = ADC(26)
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = ssd1306.SSD1306_I2C(128,64,i2c)
alert_sent = False
battery_soc = 90.0
battery_efficiency = 100.0
# ---------------------------
# Motor Energy Variables
# ---------------------------
motor_voltage = 5.0 # Simulated motor voltage (Volts)
motor_current = 0.2 # Simulated motor current (Amps)
motor_power = 0.0 # Power (Watts)
motor_energy = 0.0 # Energy (Watt-hours)
while True:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
gas_value = gas.read_u16()
# Battery SOC Simulation
battery_soc -= 0.2
if battery_soc <= 0:
battery_soc = 100.0
# Battery Efficiency Calculation
battery_efficiency = battery_soc * 0.95
emergency = button.value() == 0
# AI Decision
if emergency:
ai_status = "EMERGENCY STOP"
severity = "HIGH"
elif temp > 30 and gas_value > 30000:
ai_status = "MOTOR OVERHEAT + GAS LEAKAGE"
severity = "HIGH"
elif battery_soc <= 10:
ai_status = "CRITICAL BATTERY"
severity = "HIGH"
elif temp > 30:
ai_status = "MOTOR OVERHEAT"
severity = "MEDIUM"
elif gas_value > 30000:
ai_status = "GAS LEAKAGE"
severity = "MEDIUM"
elif battery_soc <= 25:
ai_status = "LOW BATTERY"
severity = "MEDIUM"
else:
ai_status = "SYSTEM HEALTHY"
severity = "LOW"
# LED & Buzzer
if severity == "HIGH":
led.on()
buzzer.on()
motor.off()
motor_power = 0.0
elif severity == "MEDIUM":
led.on()
buzzer.off()
motor.off() # Motor OFF during medium faults
motor_power = 0.0
else:
led.off()
buzzer.off()
motor.on() # Motor ON only when system is healthy
motor_power = motor_voltage * motor_current
motor_energy += motor_power * (2 / 3600)
# OLED Display
oled.fill(0)
oled.text("EV MONITOR", 0, 0)
oled.text("SOC:"+str(round(battery_soc,1))+"%", 0, 10)
oled.text("EFF:"+str(round(battery_efficiency,1))+"%", 0, 20)
oled.text("Temp:"+str(temp), 0, 30)
oled.text("Gas:"+str(gas_value), 0, 40)
oled.text(ai_status, 0, 55)
oled.show()
# Serial Monitor
print("---------------------------")
print("Temperature :", temp)
print("Humidity :", hum)
print("Gas Value :", gas_value)
print("AI Status :", ai_status)
print("Severity :", severity)
print("Battery SOC :", round(battery_soc,1), "%")
print("Battery Efficiency :", round(battery_efficiency,1), "%")
print("Motor Power :", round(motor_power,2), "W")
print("Motor Energy :", round(motor_energy,4), "Wh")
if battery_soc <= 10:
print("Battery Level : CRITICAL")
elif battery_soc <= 25:
print("Battery Level : LOW")
else:
print("Battery Level : NORMAL")
# Send Telegram only once
if severity == "HIGH" and not alert_sent:
message = f"""🚨 EV ALERT 🚨
Critical Fault Detected!
Temperature : {temp} C
Humidity : {hum} %
Gas Value : {gas_value}
Battery SOC : {round(battery_soc,1)} %
Battery Efficiency : {round(battery_efficiency,1)} %
Motor Power : {round(motor_power,2)} W
Motor Energy : {round(motor_energy,4)} Wh
Battery Level : {"CRITICAL" if battery_soc <= 10 else "LOW" if battery_soc <= 25 else "NORMAL"}
AI Status : {ai_status}
Severity : {severity}
Please check the EV immediately.
"""
send_telegram(message)
alert_sent = True
elif severity != "HIGH":
alert_sent = False
time.sleep(2)