#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
#include <WiFi.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // no password
// Buzzer pin
const int buzzerPin = 18;
// Initialize the MPU6050 and SSD1306 (make sure to check your display's resolution)
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
// Initialize Serial communication for debugging
Serial.begin(115200);
// Initialize I2C communication for the MPU6050 and SSD1306
Wire.begin(22, 21); // SDA, SCL
// Initialize the buzzer pin
pinMode(buzzerPin, OUTPUT);
// Initialize the MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Initialize the SSD1306 display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Check the correct address for your display
Serial.println("SSD1306 allocation failed");
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
void loop() {
// Read the sensor
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Display the orientation data on the SSD1306 display
display.clearDisplay();
display.setCursor(0,0);
// Check if the absolute value of any acceleration axis exceeds 1g
if (abs(a.acceleration.x) > 10 || abs(a.acceleration.y) > 10 || abs(a.acceleration.z) > 10) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
display.print("Alarm !!!");
} else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
display.print("Gyro X: "); display.println(g.gyro.x);
display.print("Gyro Y: "); display.println(g.gyro.y);
display.print("Gyro Z: "); display.println(g.gyro.z);
display.print("Temp: "); display.print(temp.temperature); display.println(" C");
display.display(); // Actually display all of the above
delay(100);
}