#include <DHTesp.h>
#include <Wire.h>
#include <MPU6050_tockn.h>
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
const int PIR_PIN = 14;
const int DHT_PIN = 15;
const int LDR_PIN = 32;
const int LED_PIN = 13;
int pirState = LOW;
int val = 0;
int ldrValue = 0; // Variable to store LDR value
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
DHTesp dhtSensor;
MPU6050 mpu6050(Wire);
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected");
client.publish("/ThinkIOT/Publish", "Welcome");
client.subscribe("/ThinkIOT/Subscribe");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}}
}
void setup() {
Serial.begin(4800);
pinMode(PIR_PIN, INPUT);
pinMode(LDR_PIN, INPUT); // Set the LDR pin as an input
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Read PIR sensor
val = digitalRead(PIR_PIN);
if (val == HIGH) {
pirState = HIGH;
digitalWrite(LED_PIN, HIGH); // Turn on LED when motion is detected
} else {
pirState = LOW;
digitalWrite(LED_PIN, LOW); // Turn off LED when no motion
}
// Read LDR sensor
int analogValue = analogRead(LDR_PIN); // Read from the correct analog pin
float voltage = analogValue / 4095.0 * 3.3; // ESP32 ADC is 12-bit (0-4095), and Vref is 3.3V
float resistance = (10000 * voltage) / (3.3 - voltage); // Calculate resistance
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)); // Calculate lux
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.print ("Room: ");
if (lux > 50) {
Serial.println("Light!");
} else {
Serial.println("Dark ");
}
Serial.print("Lux: ");
Serial.println(lux);
if (pirState == HIGH) {
Serial.println("Motion Detected!");
client.publish("/ThinkIOT/Motion", "Motion Detected!");
}
Serial.println("----------------");
// mpu6050 6-Axis Accel & Gyro Sensor
mpu6050.update();
float ax_mps2 = mpu6050.getAccX() / 1000.0 * 9.81;
float ay_mps2 = mpu6050.getAccY() / 1000.0 * 9.81;
float az_mps2 = mpu6050.getAccZ() / 1000.0 * 9.81;
Serial.print("Accelerometer (mg): ");
Serial.print(mpu6050.getAccX());
Serial.print(", ");
Serial.print(mpu6050.getAccY());
Serial.print(", ");
Serial.print(mpu6050.getAccZ());
Serial.print(" | Gyroscope (deg/s): ");
Serial.print(mpu6050.getGyroX());
Serial.print(", ");
Serial.print(mpu6050.getGyroY());
Serial.print(", ");
Serial.println(mpu6050.getGyroZ());
Serial.println("----------------");
String temp = String(data.temperature, 2);
client.publish("/ThinkIOT/temp", temp.c_str());
String hum = String(data.humidity, 1);
client.publish("/ThinkIOT/hum", hum.c_str());
String luxStr = String(lux);
client.publish("/ThinkIOT/lux", luxStr.c_str());
String AccX = String(mpu6050.getAccX());
String AccY = String(mpu6050.getAccY());
String AccZ = String(mpu6050.getAccZ());
client.publish("/ThinkIOT/AccX", AccX.c_str());
client.publish("/ThinkIOT/AccY", AccY.c_str());
client.publish("/ThinkIOT/AccZ", AccZ.c_str());
String GyroX = String(mpu6050.getGyroX());
String GyroY = String(mpu6050.getGyroY());
String GyroZ = String(mpu6050.getGyroZ());
client.publish("/ThinkIOT/GyroX", GyroX.c_str());
client.publish("/ThinkIOT/GyroY", GyroY.c_str());
client.publish("/ThinkIOT/GyroZ", GyroZ.c_str());
}
delay(1000);
}