#define BLYNK_TEMPLATE_ID "TMPL6MpQJ09P"
#define BLYNK_TEMPLATE_NAME "MPU6050"
#define BLYNK_AUTH_TOKEN "dlPkxB395riQv-1aom30iXv2k8l0d6sP"
#include <Wire.h>
#include <MPU6050.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <BlynkSimpleEsp32.h>
#define SDA_PIN 21
#define SCL_PIN 22
#define LED_PIN 2 // LED indikator getaran abnormal
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* scriptURL = "https://script.google.com/macros/s/AKfycbw_HKRQOymfYbvbI_LmAHF3IiofAq7zFVJgD62tlGBr52cXJWiHAQpZzuICYHN0O3vA/exec";
MPU6050 mpu;
const float ACCEL_SENSITIVITY = 16384.0; // LSB per g (±2g)
const float G_TO_M_S2 = 9.80665;
const float sampleIntervalSec = 0.05; // delay 50 ms = 20 Hz sampling
const float vibrationThreshold_mm_s = 7.5;
float velocity_mm_s = 0; // variabel untuk menyimpan kecepatan getaran
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.begin(ssid, password);
Serial.print("Menghubungkan ke WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Terhubung!");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 terhubung.");
} else {
Serial.println("Gagal terhubung ke MPU6050.");
while (1);
}
}
void loop() {
Blynk.run();
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
float temperature = mpu.getTemperature() / 340.00 + 36.53;
// Konversi akselerasi mentah ke g
float ax_g = ax / ACCEL_SENSITIVITY;
float ay_g = ay / ACCEL_SENSITIVITY;
float az_g = az / ACCEL_SENSITIVITY;
// Hitung magnitudo akselerasi tanpa gravitasi
// Untuk pengukuran getaran, gravitasi (1 g) harus dikurangi,
// tapi sederhana kita anggap az_g sudah termasuk gravitasi, maka kita hitung perubahan total akselerasi dari 1 g di sumbu z:
float accelX_m_s2 = ax_g * G_TO_M_S2;
float accelY_m_s2 = ay_g * G_TO_M_S2;
float accelZ_m_s2 = (az_g - 1.0) * G_TO_M_S2; // kurangi gravitasi bumi pada sumbu Z
// Magnitudo vektor percepatan getaran
float accelMagnitude_m_s2 = sqrt(accelX_m_s2 * accelX_m_s2 +
accelY_m_s2 * accelY_m_s2 +
accelZ_m_s2 * accelZ_m_s2);
// Integrasi percepatan untuk dapatkan kecepatan getaran (mm/s)
velocity_mm_s += accelMagnitude_m_s2 * sampleIntervalSec * 1000.0;
// Reset velocity jika getaran sangat kecil (noise)
if (accelMagnitude_m_s2 < 0.02) {
velocity_mm_s = 0;
}
// Tentukan status getaran
String status;
if (velocity_mm_s <= 5.0) {
status = "NORMAL";
} else if (velocity_mm_s <= vibrationThreshold_mm_s) {
status = "PERLU PERHATIAN";
} else {
status = "ABNORMAL";
}
bool vibrationDetected = (velocity_mm_s > vibrationThreshold_mm_s);
digitalWrite(LED_PIN, vibrationDetected ? HIGH : LOW);
Serial.print("Getaran: ");
Serial.print(velocity_mm_s, 1);
Serial.print(" mm/s - Status: ");
Serial.println(status);
Serial.print("Suhu: ");
Serial.print(temperature, 2);
Serial.println(" °C");
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, velocity_mm_s);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(scriptURL);
http.addHeader("Content-Type", "application/json");
String json = "{\"vibration\":";
json += velocity_mm_s;
json += ",\"temperature\":";
json += temperature;
json += ",\"status\":\"";
json += status;
json += "\"}";
int httpResponseCode = http.POST(json);
if (httpResponseCode > 0) {
Serial.print("Data terkirim (HTTP ");
Serial.println(httpResponseCode);
} else {
Serial.print("Gagal kirim! HTTP code: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(50);
}