CCCPaste

WiFi Potentiometer rev_05

/********* 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 Potentiometer
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-09-14 19:52:47

********* 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. Add the Adafruit */
	/* DS3502 I2C Digital Pot Makte the Potentiometer */
	/* available for the MQTT Server to control it from */
	/* Home Assitant */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/adafruit/Adafruit_DS3502
#include              // Library for WiFi connectivity
#include     // Library for MQTT functionality
#include  // Library for MQTT client

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

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4;

/***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
Adafruit_DS3502 ds3502; // Create an instance of the Adafruit_DS3502 class

// WiFi and MQTT configuration
const char* ssid = "your_SSID";          // Replace with your WiFi SSID
const char* password = "your_PASSWORD";  // Replace with your WiFi Password
const char* mqtt_server = "your_MQTT_BROKER"; // Replace with your MQTT Broker address
const char* mqtt_user = "your_MQTT_USERNAME"; // Replace with your MQTT username
const char* mqtt_password = "your_MQTT_PASSWORD"; // Replace with your MQTT password

WiFiClient client; // Create a WiFi client
Adafruit_MQTT_Client mqtt(&client, mqtt_server, 1883, mqtt_user, mqtt_password); // Create an MQTT client

// Define the MQTT topic for controlling the wiper
Adafruit_MQTT_Publish wiperControl = Adafruit_MQTT_Publish(&mqtt, "home/assistant/wiper"); // Topic for wiper control

void setup(void)
{
    // Initialize serial communication at 115200 baud rate
    Serial.begin(115200);
    // Wait until the serial port is opened
    while (!Serial) { delay(1); }

    // Set the analog pin mode for reading the potentiometer voltage
    pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT);

    // Initialize the DS3502 potentiometer
    if (!ds3502.begin()) {
        Serial.println("Couldn't find DS3502 chip");
        while (1); // Halt execution if the chip is not found
    }
    Serial.println("Found DS3502 chip");

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");
}

void loop(void)
{
    // Ensure the MQTT client is connected
    if (mqtt.connected()) {
        mqtt.processPackets(10000); // Process incoming packets
    } else {
        // Reconnect to the MQTT broker
        if (mqtt.connect()) {
            Serial.println("Connected to MQTT Broker");
        } else {
            Serial.print("Failed to connect to MQTT Broker, state: ");
            Serial.println(mqtt.connectError());
            delay(2000); // Wait before retrying
        }
    }

    // Example of setting wiper values and reading the output voltage
    for (uint8_t wiper_value : {0, 63, 127}) {
        ds3502.setWiper(wiper_value); // Set the wiper to the specified value
        float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4) * (5.0 / 1024); // Read voltage from the potentiometer
        Serial.print("Wiper voltage with wiper set to ");
        Serial.print(wiper_value);
        Serial.print(": ");
        Serial.print(voltage);
        Serial.println(" V");

        // Publish the wiper value to the MQTT topic
        wiperControl.publish(wiper_value);

        delay(1000); // Wait for a second before the next iteration
    }
}

/* END CODE */