#include <WiFi.h>
#include "DHT.h"
#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal_I2C.h>
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT22 setup
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// HC-SR04 setup
#define TRIGPIN 17
#define ECHOPIN 16
// MPU6050 setup
MPU6050 mpu;
// I2C LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize HC-SR04 pins
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
// Initialize MPU6050
Wire.begin();
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed");
}
// Initialize LCD
lcd.init();
lcd.backlight();
}
void readDHT22(float &temperature, float &humidity) {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
temperature = -1;
humidity = -1;
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
}
float readUltrasonic() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
long duration = pulseIn(ECHOPIN, HIGH);
float distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
return distance;
}
void readMPU6050(float& accelX, float& accelY, float& accelZ, float& gyroX, float& gyroY, float& gyroZ) {
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
accelX = ax / 16384.0;
accelY = ay / 16384.0;
accelZ = az / 16384.0;
gyroX = gx / 131.0;
gyroY = gy / 131.0;
gyroZ = gz / 131.0;
Serial.print("Accel X: "); Serial.print(accelX);
Serial.print(" Y: "); Serial.print(accelY);
Serial.print(" Z: "); Serial.println(accelZ);
Serial.print("Gyro X: "); Serial.print(gyroX);
Serial.print(" Y: "); Serial.print(gyroY);
Serial.print(" Z: "); Serial.println(gyroZ);
}
void displayData(float temp, float hum, float dist, float accelX, float accelY, float accelZ, float gyroX, float gyroY, float gyroZ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temp, 1) + "C");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hum: " + String(hum, 1) + "%");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dist: " + String(dist, 1) + "cm");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accel X: " + String(accelX, 2));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accel Y: " + String(accelY, 2));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accel Z: " + String(accelZ, 2));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gyro X: " + String(gyroX, 2));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gyro Y: " + String(gyroY, 2));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gyro Z: " + String(gyroZ, 2));
delay(2000);
}
void loop() {
float temp = 0, hum = 0, dist = 0, accelX = 0, accelY = 0, accelZ = 0, gyroX = 0, gyroY = 0, gyroZ = 0;
// Read sensors
readDHT22(temp, hum);
dist = readUltrasonic();
readMPU6050(accelX, accelY, accelZ, gyroX, gyroY, gyroZ);
// Display data on LCD
displayData(temp, hum, dist, accelX, accelY, accelZ, gyroX, gyroY, gyroZ);
delay(2000);
}