#include <WiFi.h>
#include <PubSubClient.h>
#include <DHTesp.h>
#include <Adafruit_MPU6050.h>
#include <Wire.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com"; // or use the MQTT broker of your choice
const int DHT_PIN = 22;
DHTesp dht;
Adafruit_MPU6050 mpu;
Servo myservo;
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
const int msgInterval = 10000; // 10 seconds
const int switchPin = 13;
bool servoEnabled = true;
const float accelerationThreshold = 10.0; // Declare the acceleration threshold
int angle = 0; // Declare the angle variable
bool movementDetected = false;
void setup() {
pinMode(22, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
Serial.begin(115200);
dht.setup(DHT_PIN, DHTesp::DHT22);
myservo.attach(18);
setup_wifi();
client.setServer(mqtt_server, 1883);
// Initialize MPU6050 sensor
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float temperature = 0; // Declare temperature variable
float humidity = 0; // Declare humidity variable
unsigned long now = millis();
if (now - lastMsg > msgInterval) {
lastMsg = now;
// Get temperature and humidity data
TempAndHumidity data = dht.getTempAndHumidity();
temperature = data.temperature;
humidity = data.humidity;
String temperatureData = String(temperature, 2);
String humidityData = String(humidity, 1);
// Get accelerometer data
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float accelX = a.acceleration.x;
float accelY = a.acceleration.y;
float accelZ = a.acceleration.z;
// Calculate total acceleration
float totalAccel = sqrt(accelX * accelX + accelY * accelY + accelZ * accelZ);
// Determine if there is movement based on acceleration threshold
if (totalAccel > accelerationThreshold) {
movementDetected = true;
} else {
movementDetected = false;
}
bool switchState = digitalRead(switchPin);
// Publish data to MQTT topics
client.publish("iotgproject/yiweiyanqi/temperature", temperatureData.c_str());
client.publish("iotgproject/yiweiyanqi/humidity", humidityData.c_str());
client.publish("iotgproject/yiweiyanqi/acceleration", String(totalAccel).c_str());
client.publish("iotgproject/yiweiyanqi/switch", switchState ? "ON" : "OFF");
client.publish("iotgproject/yiweiyanqi/movement", movementDetected ? "MOVING" : "STILL");
// Print all data in a single line
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" - Humidity: ");
Serial.print(humidity);
Serial.print(" - Total accel: ");
Serial.print(totalAccel);
Serial.print(" - Switch: ");
Serial.print(switchState ? "ON" : "OFF");
Serial.print(" - Movement: ");
Serial.println(movementDetected ? "MOVING" : "STILL");
}
// Add a delay to control the loop timing
delay(5000);
}
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(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
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");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}