#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"
  #include "freertos/timers.h"
}
#include <AsyncMQTT_ESP32.h>

//#include <MFRC522.h>
#include <Arduino.h>
//=========================
//  RFID-RC522 wire pin
//  SDA = 5   SCK =18
//  MOSI=23   MISO=19
//  RST = 4  
//  GND , VCC
//=========================
#define LEDPIN    27
#define BuzzerPIN 14

//#define MQTT_HOST     "broker.mqtt-dashboard.com"
//#define MQTT_HOST     "broker.hivemq.com"
#define MQTT_HOST       "test.mosquitto.org"  
//#define MQTT_HOST     "broker.mqttgo.io"
#define MQTT_PORT       1883

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;


const char *SubTopic1  = "alex9ufo/esp32/led"; 
const char *PubTopic2  = "alex9ufo/esp32/uid"; 
//const char *PubTopic3  = "alex9ufo/esp32/buzzer";  
//================================================================
//================================================================
void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
//================================================================
void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}
//================================================================
void WiFiEvent(WiFiEvent_t event)
{
  switch (event)
  {
#if USING_CORE_ESP32_CORE_V200_PLUS

    case ARDUINO_EVENT_WIFI_READY:
      Serial.println("WiFi ready");
      break;

    case ARDUINO_EVENT_WIFI_STA_START:
      Serial.println("WiFi STA starting");
      break;

    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.println("WiFi STA connected");
      break;

    case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;

    case ARDUINO_EVENT_WIFI_STA_LOST_IP:
      Serial.println("WiFi lost IP");
      break;

    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
#else

    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;

    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
#endif

    default:
      break;
  }
}
//================================================================
void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);

  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);

  uint16_t packetIdSub1 = mqttClient.subscribe(SubTopic1 , 2);
  Serial.print("Subscribing at QoS 2, packetId: ");
  Serial.println(packetIdSub1);

  //uint16_t packetIdSub2 = mqttClient.subscribe(SubTopic2, 2);
  //Serial.print("Subscribing at QoS 2, packetId: ");
  //Serial.println(packetIdSub2);

}
//================================================================

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}
//================================================================
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  Serial.print("  qos: ");
  Serial.println(qos);
}
//================================================================
void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}
//================================================================
void onMqttPublish(uint16_t packetId) {
  Serial.print("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}
//================================================================
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  
  String messageTemp;
  for (int i = 0; i < len; i++) {
    Serial.print((char)payload[i]);
    messageTemp += (char)payload[i];
  }

  if (strcmp(topic, SubTopic1) == 0) {
    // If the relay is on turn it off (and vice-versa)
    Serial.println();

    Serial.println(messageTemp);  
    if (messageTemp == "on") {
      digitalWrite(LEDPIN,HIGH);
      Serial.println("LED ON");     
    }
    if (messageTemp == "off") {
      digitalWrite(LEDPIN,LOW);
      Serial.println("LED OFF");     
    }
     
  }
}
//================================================================
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Hello, ESP32!");
  pinMode(LEDPIN, OUTPUT);
  pinMode(BuzzerPIN, OUTPUT);


  mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

  WiFi.onEvent(WiFiEvent);

  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onSubscribe(onMqttSubscribe);
  mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onMessage(onMqttMessage); //callback
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  // If your broker requires authentication (username and password), set them below
  //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
  connectToWifi();
}
//================================================================
void loop() {

  if (Serial.available()) {
    Serial.println("Enter MFRC522 UID (format: XX XX XX XX):");
    String input = Serial.readStringUntil('\n');
    input.trim(); // Remove any leading or trailing whitespace
    if (isValidFormat(input)) {
      Serial.println("Valid format.");
      String temp1=input;
      uint16_t packetIdPub1 = mqttClient.publish(PubTopic2, 1, true, temp1.c_str());                            
      Serial.printf("RFID UID Number Published", PubTopic2, packetIdPub1);                            
      digitalWrite(BuzzerPIN, HIGH);
      tone(BuzzerPIN, 750);
      delay(1000);
      digitalWrite(BuzzerPIN, LOW);
      noTone(BuzzerPIN);

    } else {
      Serial.println("Invalid format. Enter Enter MFRC522 UID (format: XX XX XX XX):");
    }
  }
}
//================================================================
bool isValidFormat(String input) {
  // Check if the input matches the format "XX XX XX XX"
  if (input.length() == 11 && input.charAt(2) == ' ' && input.charAt(5) == ' ' && input.charAt(8) == ' ') {
    for (int i = 0; i < input.length(); i++) {
      if (i != 2 && i != 5 && i != 8) {
        if (!isDigit(input.charAt(i))) {
          return false;
        }
      }
    }
    return true;
  }
  return false;
}
//================================================================
NOCOMNCVCCGNDINLED1PWRRelay Module
RFID-RC522Breakout