#define BLYNK_TEMPLATE_ID "TMPL3inNKWAz5"
#define BLYNK_TEMPLATE_NAME "accelerometer"
#define BLYNK_AUTH_TOKEN "HdH3EUrJI2ZfJ0M_836AaETHrydXJqjC"
#include <Wire.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
Adafruit_MPU6050 mpu;
char auth[] = "HdH3EUrJI2ZfJ0M_836AaETHrydXJqjC"; // You'll receive this after creating a Blynk project
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
const float threshold = 30; // Set your desired threshold (in g)
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(115200);
while (!Serial)
delay(10); // Wait for serial monitor to open
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Blynk.begin(auth, ssid, pass);
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
delay(100);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Calculate the magnitude of acceleration
float ax = a.acceleration.x;
float ay = a.acceleration.y;
float az = a.acceleration.z;
float magnitude = sqrt(ax * ax + ay * ay + az * az);
Serial.print( "X:");
Serial.println(ax);
Serial.print( "Y:");
Serial.println(ay);
Serial.print( "Z:");
Serial.println(az);
Serial.print("Acceleration Magnitude: ");
Serial.println(magnitude);
Blynk.virtualWrite(V0, ax); // Virtual Pin V1 for temperature
Blynk.virtualWrite(V1, ay);
Blynk.virtualWrite(V2, az); // Virtual Pin V1 for temperature
Blynk.virtualWrite(V3, magnitude);
lcd.setCursor(0, 0);
lcd.print("X:");
lcd.println(ax);
lcd.setCursor(0, 1);
lcd.print("Y:");
lcd.println(ay);
lcd.setCursor(9, 0);
lcd.print("Z:");
lcd.println(az);
// Virtual Pin V1 for temperature
delay(1000);
// clears the display to print new message
lcd.clear();
delay(1000);
lcd.setCursor(0, 0);
lcd.print("MAGNITUDE:");
lcd.println( magnitude);
// Check if the magnitude exceeds the threshold
if (magnitude > threshold) {
Serial.println("Threshold exceeded!");
Blynk.virtualWrite(V4,"Threshold exceeded!" );
} else {
Serial.println("Within safe range.");
Blynk.virtualWrite(V4,"Within safe range." );
}
Blynk.run();
timer.run();
delay(500); // Adjust for sampling rate
}