CCCPaste

"MQTT Potentiometer" rev_03

/********* 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: "MQTT Potentiometer"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-09-14 18:21:30

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Connect the ESP32 DEv board to an Home Assistant */
	/* MQTT Broker. The Broker need a username and */
	/* Password for authentifikation. Makte the */
	/* Potentiometer available for the MQTT Server to */
	/* control it from Home Assitant */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include                 // Include the WiFi library for ESP32
#include         // Include the PubSubClient library for MQTT

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

// Define the MQTT callback function prototype
void mqttCallback(char* topic, byte* payload, unsigned int length);
void reconnect(void);

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4; // Define the pin for the potentiometer

// WiFi and MQTT configuration
const char* ssid = "Your_SSID";               // Your WiFi SSID
const char* password = "Your_PASSWORD";       // Your WiFi Password
const char* mqtt_server = "Your_MQTT_Broker"; // Your MQTT Broker address
const char* mqtt_user = "Your_MQTT_Username"; // MQTT Username
const char* mqtt_password = "Your_MQTT_Password"; // MQTT Password
const char* mqtt_topic = "home/potentiometer"; // MQTT topic to publish potentiometer value

WiFiClient espClient;                         // Create a WiFi client
PubSubClient client(espClient);                // Create a PubSubClient object

void setup(void)
{
    Serial.begin(115200);                      // Start the Serial communication
    pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT); // Set the potentiometer pin as input

    // Connect to WiFi
    WiFi.begin(ssid, password);                 // Start WiFi connection
    while (WiFi.status() != WL_CONNECTED) {    // Wait for connection
        delay(1000);                            // Wait 1 second
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");

    // Set up MQTT server
    client.setServer(mqtt_server, 1883);       // Set the MQTT server and port
    client.setCallback(mqttCallback);           // Set the callback function for incoming messages
}

void loop(void)
{
    // Ensure the client is connected to the MQTT broker
    if (!client.connected()) {
        reconnect();                            // Reconnect if not connected
    }
    client.loop();                              // Handle MQTT communication

    // Read the potentiometer value
    int potValue = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4); // Read the potentiometer value
    char potValueStr[4];                        // Buffer to hold the potentiometer value as a string
    itoa(potValue, potValueStr, 10);            // Convert the integer value to a string

    // Publish the potentiometer value to the MQTT topic
    client.publish(mqtt_topic, potValueStr);   // Publish the value
    delay(2000);                                // Wait for 2 seconds before the next reading
}

// Function to handle reconnection to the MQTT broker
void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
            Serial.println("connected");
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            delay(5000); // Wait 5 seconds before retrying
        }
    }
}

// Callback function for incoming messages
void mqttCallback(char* topic, byte* payload, unsigned int length) {
    // Handle incoming messages if needed
}