#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* api_key = "HRHV0SG4TPEAE6LE";
#define LED_PIN 13
#define BUZZER_PIN 12
#define BUTTON_PIN 14
#define DHT_PIN 15
#define LDR_PIN 34
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
dht.begin();
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);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.println("Crash detected!");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(1000);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
sendAlert("Crash detected");
}
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) {
Serial.println("Crash detected by accelerometer!");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(1000);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
sendAlert("Crash detected by accelerometer");
}
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(" %");
}
int lightLevel = analogRead(LDR_PIN);
Serial.print("Light level: ");
Serial.println(lightLevel);
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime > 20000) {
lastSendTime = millis();
sendDataToThingSpeak(temperature, humidity, lightLevel);
}
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();
} else {
Serial.println("WiFi Disconnected");
}
}
void sendDataToThingSpeak(float temperature, float humidity, int lightLevel) {
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);
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();
} else {
Serial.println("WiFi Disconnected");
}
}