#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Wokwi default
const char* password = ""; // no password
// Telegram Bot credentials
String botToken = "7667366619:AAGmYyKGYI2YxreHN_KwCCl3Vexe4eS2bJI";
String chatID = "5012843683";
// Hardware pins
const int MQ3_PIN = 34;
const int BUZZER_PIN = 25;
const int LED_PIN = 26;
// Thresholds
const int MQ3_THRESHOLD = 800;
const float ACC_THRESHOLD_G = 2.5;
void sendTelegram(String msg) {
if(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + botToken +
"/sendMessage?chat_id=" + chatID + "&text=" + msg;
http.begin(url);
int httpCode = http.GET();
if(httpCode > 0) {
Serial.println("Telegram sent: " + msg);
} else {
Serial.println("Error sending message");
}
http.end();
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nâ
WiFi Connected");
Wire.begin(21, 22);
mpu.initialize();
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println("Helmet system ready!");
}
float getAccelG() {
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
float axg = ax / 16384.0;
float ayg = ay / 16384.0;
float azg = az / 16384.0;
return sqrt(axg*axg + ayg*ayg + azg*azg);
}
void alarmOn(String reason) {
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 2000);
delay(500);
noTone(BUZZER_PIN);
digitalWrite(LED_PIN, LOW);
delay(200);
sendTelegram("đ¨ Smart Helmet Alert: " + reason);
}
void loop() {
// Alcohol detection
int mq = analogRead(MQ3_PIN);
if (mq >= MQ3_THRESHOLD) {
Serial.println("ALCOHOL DETECTED!");
alarmOn("Alcohol detected in helmet!");
}
// Accident detection
float g = getAccelG();
if (g >= ACC_THRESHOLD_G) {
Serial.print("Impact! g="); Serial.println(g);
alarmOn("Accident detected! Immediate help needed!");
}
delay(500);
}