#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET_PIN 4
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire);
Adafruit_MPU6050 accelerometer;
boolean isFallDetected = false; // Indicates if a fall has occurred
void setup(void) {
Serial.begin(115200);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Display startup message
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE, 0);
display.setCursor(0, 0);
display.println("Accelerometer:");
display.display();
// Initialize the MPU6050
if (!accelerometer.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Configure MPU6050 settings
accelerometer.setAccelerometerRange(MPU6050_RANGE_8_G);
accelerometer.setGyroRange(MPU6050_RANGE_250_DEG);
accelerometer.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");
delay(1000);
}
void loop() {
// Get new sensor events with the readings
sensors_event_t accelEvent, gyroEvent, tempEvent;
accelerometer.getEvent(&accelEvent, &gyroEvent, &tempEvent);
// Update OLED display with accelerometer and gyroscope data
updateDisplay(accelEvent, gyroEvent);
// Check for fall detection
detectFall(accelEvent, gyroEvent);
// Post webhook if fall detected
if (isFallDetected) {
sendWebhookNotification();
isFallDetected = false; // Reset fall detected flag
}
// Delay before next iteration
delay(10);
}
void updateDisplay(sensors_event_t accelEvent, sensors_event_t gyroEvent) {
// Clear OLED display
display.clearDisplay();
// Display accelerometer data
display.setTextSize(1);
display.setTextColor(WHITE, 0);
display.setCursor(0, 16);
display.print("X: "); display.print(accelEvent.acceleration.x);
display.setCursor(0, 32);
display.print("Y: "); display.print(accelEvent.acceleration.y);
display.setCursor(0, 48);
display.print("Z: "); display.print(accelEvent.acceleration.z);
// Display gyroscope data
display.setCursor(64, 16);
display.print("X°: "); display.print(gyroEvent.gyro.x);
display.setCursor(64, 32);
display.print("Y°: "); display.print(gyroEvent.gyro.y);
display.setCursor(64, 48);
display.print("Z°: "); display.print(gyroEvent.gyro.z);
// Update OLED display
display.display();
}
void detectFall(sensors_event_t accelEvent, sensors_event_t gyroEvent) {
// Simulated fall detection logic
if (accelEvent.acceleration.z < 0.6 && gyroEvent.gyro.z < 45) {
isFallDetected = true;
}
// Display "FALL DETECTED" when a fall is detected
if (isFallDetected) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE, 0);
display.setCursor(0, 20);
display.println("FALL DETECTED");
tone(15, 262, 500); // Plays 262Hz tone for 5.00 seconds
display.display();
}
}
void sendWebhookNotification() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://webhookwizard.com/api/webhook/in?key=sk_4738b49d-a08e-433e-bb52-c5673372dd60";
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Prepare JSON payload
String payload = "{\"event\": \"fall_detected\"}";
// Send HTTP POST request
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode); // Print HTTP response code
Serial.println(response); // Print response payload
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
} else {
Serial.println("Error in WiFi connection");
}
}