#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* api_key = "HRHV0SG4TPEAE6LE";
const char* twilio_account_sid = "ACee6a1f4f8f14c14351d5d73952f6ba20";
const char* twilio_auth_token = "9dbf05f7f31ee136ebee0c6a531a9edf";
const char* twilio_phone_number = "+12067390169";
const char* recipient_phone_number = "+919108239071";
#define LCD_RS 16
#define LCD_EN 17
#define LCD_D4 18
#define LCD_D5 19
#define LCD_D6 23
#define LCD_D7 5
#define LED_PIN 13
#define BUZZER_PIN 12
#define BUTTON_PIN 14
#define DHT_PIN 15
#define LDR_PIN 34
// DHT22 settings
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
Adafruit_MPU6050 mpu;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
bool crashDetected = false;
void setup() {
Serial.begin(91500);
// Initialize components
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
dht.begin();
lcd.begin(16, 2);
lcd.clear();
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1);
}
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
void loop() {
// Check for crash simulation
if (digitalRead(BUTTON_PIN) == LOW) {
if (!crashDetected) {
Serial.println("Crash detected!");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(1000);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You have Crashed");
sendAlert("Crash detected");
crashDetected = true;
}
} else {
crashDetected = false;
}
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
if (abs(a.acceleration.x) > 10 || abs(a.acceleration.y) > 10 || abs(a.acceleration.z) > 10) {
if (!crashDetected) {
Serial.println("Crash detected by accelerometer!");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(1000);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You have Crashed");
sendAlert("Crash detected by accelerometer");
crashDetected = true;
}
}
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Display temperature on LCD
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
}
int lightLevel = analogRead(LDR_PIN);
Serial.print("Light level: ");
Serial.println(lightLevel);
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime > 200) {
lastSendTime = millis();
sendDataToThingSpeak(temperature, humidity, lightLevel, a.acceleration.x, a.acceleration.y, a.acceleration.z);
}
delay(100);
}
void sendAlert(const char* message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String("http://") + server + "/update?api_key=" + api_key + "&field1=" + message;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
// Send SMS using Twilio only if a crash is detected
if (crashDetected) {
sendSMS(message);
}
} else {
Serial.println("WiFi Disconnected");
}
}
void sendDataToThingSpeak(float temperature, float humidity, int lightLevel, float ax, float ay, float az) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String("http://") + server + "/update?api_key=" + api_key +
"&field1=" + String(temperature) +
"&field2=" + String(humidity) +
"&field3=" + String(lightLevel) +
"&field4=" + String(ax) +
"&field5=" + String(ay) +
"&field6=" + String(az);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
// Send SMS
if (crashDetected) {
String smsMessage = "Alert! There has been a crash!. Temp: " + String(temperature) + "C, Humidity: " + String(humidity) + "%, Light: " + String(lightLevel) +
", Acceleration - X: " + String(ax) + ", Y: " + String(ay) + ", Z: " + String(az);
sendSMS(smsMessage.c_str());
}
} else {
Serial.println("WiFi Disconnected");
}
}
void sendSMS(const char* message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(twilio_account_sid) + "/Messages.json";
String data = "To=" + String(recipient_phone_number) + "&From=" + String(twilio_phone_number) + "&Body=" + String(message);
http.begin(url);
http.setAuthorization(twilio_account_sid, twilio_auth_token);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0) {
Serial.print("SMS sent. HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error sending SMS. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}