/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "WiFi MQTT Setup"
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-03-06 15:45:13

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* code for esp32 to control relay using manual */
	/* switch and cloud usding mqtt */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/

// Include the required libraries
#include <WiFi.h>
#include <PubSubClient.h>

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void initializeWiFi(void);
void connectToMQTTBroker(void);
void mqttCallback(char* topic, byte* payload, unsigned int length);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t relay_PIN_D4 = 4;

/***** WIFI CREDENTIALS *****/
const char* ssid = "Your SSID";
const char* password = "Your Password";

/***** MQTT BROKER DETAILS *****/
const char* mqttBroker = "your-mqtt-broker";
const char* mqttUsername = "mqtt-username";
const char* mqttPassword = "mqtt-password";
const int mqttPort = 1883;

// Create an instance of the WiFiClient class
WiFiClient wifiClient;

// Create an instance of the PubSubClient class
PubSubClient client(wifiClient);

void setup(void)
{
  // put your setup code here, to run once:

  // Initialize serial communication
  Serial.begin(115200);

  // Initialize WiFi connection
  initializeWiFi();

  // Connect to the MQTT broker
  connectToMQTTBroker();

  // Set the relay pin as input
  pinMode(relay_PIN_D4, INPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  // Check for any incoming messages
  client.loop();

  // TODO: Add your relay control logic using manual switch here

  // TODO: Add your relay control logic using cloud (MQTT) here
}

void initializeWiFi(void)
{
  // Connect to WiFi network
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  // Wait until connected to the WiFi network
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void connectToMQTTBroker(void)
{
  // Set the MQTT broker and port
  client.setServer(mqttBroker, mqttPort);

  // Set the MQTT callback function
  client.setCallback(mqttCallback);

  // Connect to the MQTT broker
  while (!client.connected())
  {
    Serial.print("Connecting to MQTT broker...");
    // Connect to the MQTT broker with username and password
    if (client.connect("ESP32Client", mqttUsername, mqttPassword))
    {
      Serial.println("Connected to MQTT broker");
    }
    else
    {
      // Failed to connect to the MQTT broker, retry after 5 seconds
      Serial.print("Failed to connect to MQTT broker, retrying in 5 seconds...");
      delay(5000);
    }
  }
}

void mqttCallback(char* topic, byte* payload, unsigned int length)
{
  // TODO: Add your MQTT callback logic here
}
