#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Define the LDR pin
const int LDRPin = 34; // Analog pin connected to the LDR
// MPU6050 I2C address
const int MPU_addr = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT Broker settings
const char* mqtt_broker = "broker.hivemq.com";
const char* topic = "C16QS_ESP32";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
// Connect to WiFi network
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void reconnect() {
// Loop until we're reconnected to the MQTT broker
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)")) {
Serial.println("connected");
// Once connected, publish an announcement and resubscribe
client.publish(topic, "Connection Successful");
client.subscribe(topic); // Subscribe to the topic after connection
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_broker, mqtt_port);
// Initialize your sensors here as before
pinMode(LDRPin, INPUT);
Wire.begin(21, 22); // SDA, SCL pins for MPU6050
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0); // Exit from sleep mode
Wire.endTransmission(true);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read LDR value
int ldrValue = analogRead(LDRPin);
// Read data from MPU6050
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
// Construct a message string
String message = "LDR Value: " + String(ldrValue) +
" | Accel X: " + String(AcX) +
" | Accel Y: " + String(AcY) +
" | Accel Z: " + String(AcZ) +
" | Temp: " + String(Tmp / 340.00 + 36.53) +
"°C | Gyro X: " + String(GyX) +
" | Gyro Y: " + String(GyY) +
" | Gyro Z: " + String(GyZ);
// Publish the message to the MQTT topic
client.publish(topic, message.c_str());
// Print the read values to Serial for debugging
Serial.println(message);
delay(500); // Delay a second before next read
}