#include <Wire.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Servo.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// Blynk Authentication token (generated from the Blynk app)
char auth[] = "Your-Blynk-Auth-Token";
// WiFi credentials
char ssid[] = "Your-WiFi-SSID";
char pass[] = "Your-WiFi-Password";
// Define the pins for sensors
#define DHTPIN 35
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// Servo Pin
#define SERVO_PIN 13 // Pin to control the servo motor
// Ultrasonic sensor pins
#define TRIG_PIN 14 // Trigger pin for Ultrasonic sensor
#define ECHO_PIN 12 // Echo pin for Ultrasonic sensor
// Set up the servo motor
Servo healingServo;
// Set up the Ultrasonic Sensor
long duration;
int distance;
int crackThreshold = 50; // Adjust this threshold value to determine crack size
// Initialize the MPU6050 (Gyroscope + Accelerometer)
Adafruit_MPU6050 mpu;
int xAccel, yAccel, zAccel; // Accelerometer data
int xGyro, yGyro, zGyro; // Gyroscope data
int temperature = 0;
int humidity = 0;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize DHT sensor
dht.begin();
// Initialize servo motor
healingServo.attach(SERVO_PIN);
healingServo.write(0); // Start with the valve closed
// Initialize ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize MPU6050 sensor
if (!mpu.begin()) {
Serial.println("MPU6050 not detected!");
while (1);
}
// Set up MPU6050 sensor range (optional)
mpu.setAccelerometerRange(MPU6050_RANGE_8G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
}
void loop() {
Blynk.run();
// Read temperature and humidity data
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Send temperature and humidity data to Blynk app
Blynk.virtualWrite(V1, temperature); // Temperature in virtual pin V1
Blynk.virtualWrite(V2, humidity); // Humidity in virtual pin V2
// Measure distance using the ultrasonic sensor
distance = measureDistance();
// Send distance data to Blynk app for monitoring
Blynk.virtualWrite(V3, distance); // Distance from ultrasonic sensor (V3)
// Read accelerometer and gyroscope data
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
xAccel = a.acceleration.x;
yAccel = a.acceleration.y;
zAccel = a.acceleration.z;
xGyro = g.gyro.x;
yGyro = g.gyro.y;
zGyro = g.gyro.z;
// Send accelerometer and gyroscope data to Blynk app
Blynk.virtualWrite(V4, xAccel); // Accelerometer X-axis (V4)
Blynk.virtualWrite(V5, yAccel); // Accelerometer Y-axis (V5)
Blynk.virtualWrite(V6, zAccel); // Accelerometer Z-axis (V6)
Blynk.virtualWrite(V7, xGyro); // Gyroscope X-axis (V7)
Blynk.virtualWrite(V8, yGyro); // Gyroscope Y-axis (V8)
Blynk.virtualWrite(V9, zGyro); // Gyroscope Z-axis (V9)
// Check if the distance indicates a crack (change in material structure)
if (distance < crackThreshold) {
// Check for abnormal movement in accelerometer or gyroscope (e.g., tilt or vibrations)
if (abs(xAccel) > 5 || abs(yAccel) > 5 || abs(zAccel) > 5 || abs(xGyro) > 100 || abs(yGyro) > 100 || abs(zGyro) > 100) {
Blynk.notify("Crack detected and abnormal movement/vibration detected! Initiating repair process...");
triggerHealingProcess();
}
}
delay(1000); // Update readings every 1 second
}
long measureDistance() {
// Send pulse to trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm
return duration * 0.034 / 2;
}
void triggerHealingProcess() {
Serial.println("Initiating self-healing process...");
// Open the valve by rotating the servo motor
healingServo.write(90); // Move the servo to open the valve (90 degrees)
// Wait for the healing proce