#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// --- WiFi Configuration ---
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// --- 1. Telegram Configuration ---
const char* botToken = "8507895287:AAHg9uNzUMnNEoxQf3Dx1mzOAJYFvWHe_1Y"; // Tumhara Bot Token
const char* chatId = "5531820147"; // Tumhara Chat ID
// --- 2. WhatsApp Configuration ---
String phoneNumber = "+916398417100"; // Tumhara Registered Number
String apiKey = "YOUR_CALLMEBOT_API_KEY"; // Yahan WhatsApp wala API Key daal dena
// --- Pins & Display Setup ---
const int BUZZER_PIN = 15;
const int BUTTON_PIN = 4;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// --- Objects ---
Adafruit_MPU6050 mpu;
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
// --- Logic Variables ---
bool accidentDetected = false;
unsigned long accidentTime = 0;
const unsigned long WINDOW_TIME = 10000; // 10 seconds
// School of ICT, GBU Coordinates
String mockLat = "28.4231";
String mockLng = "77.5181";
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
showNormalStatus();
if (!mpu.begin()) {
Serial.println("MPU6050 not found!");
while (1) yield();
}
// WiFi Setup
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Telegram ke liye secure certificate zaroori hai
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected! Dual-Alert System Ready.");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float totalAccel = sqrt(sq(a.acceleration.x) + sq(a.acceleration.y) + sq(a.acceleration.z));
if (totalAccel > 12.0 && !accidentDetected) {
Serial.println("\n[!] WARNING: Suspected Crash Detected!");
accidentDetected = true;
accidentTime = millis();
digitalWrite(BUZZER_PIN, HIGH);
// OLED par Warning show karo
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,10);
display.println("CRASH?");
display.setTextSize(1);
display.setCursor(0,40);
display.println("Press Button to Stop!");
display.display();
}
if (accidentDetected) {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.println("[*] Alert Cancelled: Driver is safe.");
resetSystem();
delay(1000);
}
else if (millis() - accidentTime > WINDOW_TIME) {
Serial.println("[!] Sending SOS to Telegram AND WhatsApp...");
// Dono functions ko ek ke baad ek call kar diya
sendTelegramAlert();
sendWhatsAppAlert();
showEmergencyDisplay();
accidentDetected = false;
digitalWrite(BUZZER_PIN, LOW);
}
}
}
void sendTelegramAlert() {
String message = "🚨 *EMERGENCY: Accident Detected!*\n\n";
message += "Driver is unresponsive. Dispatch help immediately.\n\n";
message += "📍 *Live Location:* \n";
message += "https://maps.google.com/?q=" + mockLat + "," + mockLng;
if (bot.sendMessage(chatId, message, "Markdown")) {
Serial.println("[✓] Telegram Alert Sent!");
} else {
Serial.println("[X] Telegram Alert Failed.");
}
}
void sendWhatsAppAlert() {
String message = "🚨 *EMERGENCY: Accident Detected!*\n\n";
message += "Sachin's vehicle has been involved in a crash.\n";
message += "📍 *Live Location:* \n";
message += "https://maps.google.com/?q=" + mockLat + "," + mockLng;
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("[✓] WhatsApp Alert Sent Successfully!");
} else {
Serial.print("[X] Error sending WhatsApp. HTTP Code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void showNormalStatus() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 25);
display.println("System Ready!");
display.setCursor(10, 40);
display.println("Drive Safely");
display.display();
}
void showEmergencyDisplay() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 5);
display.println("EMERGENCY!");
display.setTextSize(1);
display.setCursor(0, 25);
display.println("Call Family:");
display.setTextSize(2);
display.setCursor(0, 40);
display.println("6398417100");
display.display();
}
void resetSystem() {
accidentDetected = false;
digitalWrite(BUZZER_PIN, LOW);
showNormalStatus();
}