#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
MPU6050 mpu;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Wi-Fi Credentials (For Wokwi Simulation)
const char* ssid = "Wokwi-GUEST";
const char* serverURL = "http://your-server.com/alert"; // Replace with actual server
#define POT_PIN 34 // Potentiometer (Alcohol Sensor)
#define BUZZER_PIN 26 // Buzzer
#define LED_PIN 27 // LED
#define BUTTON_PIN 14 // Button for accident simulation
void setup() {
Serial.begin(115200);
WiFi.begin(ssid); // No password needed for Wokwi-GUEST
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWi-Fi Connected!");
Wire.begin();
mpu.initialize();
pinMode(POT_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Helmet");
delay(2000);
lcd.clear();
}
void loop() {
int16_t ax, ay, az;
int alcoholLevel = analogRead(POT_PIN);
// Read MPU6050 accelerometer values
mpu.getAcceleration(&ax, &ay, &az);
float acceleration = sqrt(ax * ax + ay * ay + az * az) / 16384.0; // Normalize
Serial.print("Acceleration: "); Serial.println(acceleration);
Serial.print("Alcohol Level: "); Serial.println(alcoholLevel);
lcd.setCursor(0, 0);
lcd.print("Acc: ");
lcd.print(acceleration);
lcd.print("g ");
lcd.setCursor(0, 1);
lcd.print("Alcohol: ");
lcd.print(alcoholLevel);
// Accident Detection
if (acceleration > 2.5 || digitalRead(BUTTON_PIN) == LOW) {
Serial.println("🚨 Accident Detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accident Alert!");
triggerAlert("Accident detected!");
activateBuzzer();
}
// Alcohol Detection
if (alcoholLevel > 600) {
Serial.println("🍺 Alcohol Detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alcohol Alert!");
triggerAlert("Alcohol detected!");
activateBuzzer();
}
delay(500);
}
// Function to send alert to server
void triggerAlert(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverURL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String postData = "alert=" + message;
int httpResponseCode = http.POST(postData);
Serial.print("📡 Server Response: ");
Serial.println(httpResponseCode);
http.end();
} else {
Serial.println("⚠️ Wi-Fi Not Connected!");
}
}
// Function to activate buzzer and LED
void activateBuzzer() {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(3000);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.clear();
}