#include <WiFi.h>
#include <PubSubClient.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";

const char *mqttBroker = "test.mosquitto.org";
const int mqttPort = 1883;
const char *led1Topic = "device/data/led1";
const char *led2Topic = "device/data/led2";
const char *led3Topic = "device/data/led3";
const char *resetTopic = "device/data";  // MQTT topic for resetting ESP32
unsigned long lastMsg = 0;
WiFiClient espClient;
PubSubClient client(espClient);
int led1Pin = 21;
int led2Pin = 5;
int led3Pin = 4;

void setup() {
  Serial.begin(115200);
  // Set LED pins as OUTPUT
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  digitalWrite(led1Pin, HIGH);  // Turn on LED 1 briefly
  delay(500);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);  // Turn on LED 2 briefly
  delay(500);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);  // Turn on LED 3 briefly
  delay(500);
  digitalWrite(led3Pin, LOW);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Connect to MQTT broker
  client.setServer(mqttBroker, mqttPort);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("MPU6050_Client")) {
      Serial.println("Connected to MQTT");

      // Subscribe to topics
      client.subscribe(led1Topic);
      client.subscribe(led2Topic);
      client.subscribe(led3Topic);
      client.subscribe(resetTopic);
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" Retry in 2 seconds...");
      delay(2000);
    }
  }
  client.setCallback(callback);
}

void loop() {
  ///////////////
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("MPU6050_Client")) {
      Serial.println("Connected to MQTT");

      // Subscribe to topics
      client.subscribe(led1Topic);
      client.subscribe(led2Topic);
      client.subscribe(led3Topic);
      client.subscribe(resetTopic);
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" Retry in 2 seconds...");
      delay(2000);
    }
  }


  // Handle MQTT messages for LED control and reset
  client.loop();

  // Wait for a moment before the next iteration
  // delay(2000);
}

// Callback function for MQTT messages
void callback(char *topic, byte *payload, unsigned int length) {
  Serial.println("Inside callback...");
  Serial.print("Message arrived on topic [");
  Serial.print(topic);
  Serial.print("] Payload: ");

  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String message;
  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }

  // Handle LED control messages
  if (String(topic) == led1Topic) {
    if (message == "on") {
      digitalWrite(led1Pin, HIGH);
    } else if (message == "off") {
      digitalWrite(led1Pin, LOW);
    }
  } else if (String(topic) == led2Topic) {
    if (message == "on") {
      digitalWrite(led2Pin, HIGH);
    } else if (message == "off") {
      digitalWrite(led2Pin, LOW);
    }
  } else if (String(topic) == led3Topic) {
    if (message == "on") {
      digitalWrite(led3Pin, HIGH);
    } else if (message == "off") {
      digitalWrite(led3Pin, LOW);
    }
  } else if (String(topic) == resetTopic) {
    if (message == "reset") {
      Serial.println("Resetting ESP32...");
      delay(1000);
         ESP.restart();  // Perform a software reset
    }
  }
}