/******************************************************
* COURSE CODE: DFN40312 - Embedded Internet of Things
* PRACTICAL EXERCISE: 2 (Manufacturing Industry)
* PROJECT TITLE: Smart Machine Vibration Monitoring System
* STUDENT NAME: NURKHAIRUNASS BINTI ABDULLAH & JESSYCIA CANKEH
* STUDENT ID: 20DDT23F1065 & 20DDT22F2009
* CLASS: DDT5G & DDT6F
* LECTURER: SIR TS. FAIZ BIN TONY
* OBJECTIVE:
* To design an IoT system that detects abnormal machine vibration
* in manufacturing environments to reduce downtime and improve efficiency.
******************************************************/
#include "DHTesp.h"
#define DHT_PIN 4
#define VIBRATION_PIN 34
#define ALERT_LED 13
DHTesp dhtSensor;
// ===== Simple function (1 function only) =====
void readDHT(float &temp, float &hum) {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("DHT22 read error!");
temp = 0;
hum = 0;
} else {
temp = data.temperature;
hum = data.humidity;
}
}
void setup() {
Serial.begin(115200);
pinMode(VIBRATION_PIN, INPUT);
pinMode(ALERT_LED, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("=======================================");
Serial.println("IoT Predictive Maintenance System (Hybrid Version)");
Serial.println("=======================================");
delay(2000);
}
void loop() {
float temperature, humidity;
// ===== Read sensors =====
readDHT(temperature, humidity); // simple function
int vibration = analogRead(VIBRATION_PIN); // student style
// ===== Display data =====
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.print(" % | Vibration: ");
Serial.println(vibration);
// ===== Simple alert logic =====
if (temperature > 40 || vibration > 2500) {
digitalWrite(ALERT_LED, HIGH);
Serial.println("⚠️ WARNING: Machine at risk!");
} else {
digitalWrite(ALERT_LED, LOW);
Serial.println("✅ Machine operating normally.");
}
Serial.println("---------------------------------------");
delay(2000);
}