Advertisement
pleasedontcode

WiFi Potentiometer rev_05

Sep 14th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: WiFi Potentiometer
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-14 19:52:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect the ESP32 Dev board to an Home Assistant */
  21.     /* MQTT Broker. The Broker need a username and */
  22.     /* Password for authentifikation. Add the Adafruit */
  23.     /* DS3502 I2C Digital Pot Makte the Potentiometer */
  24.     /* available for the MQTT Server to control it from */
  25.     /* Home Assitant */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Adafruit_DS3502.h>  // https://github.com/adafruit/Adafruit_DS3502
  30. #include <WiFi.h>             // Library for WiFi connectivity
  31. #include <Adafruit_MQTT.h>    // Library for MQTT functionality
  32. #include <Adafruit_MQTT_Client.h> // Library for MQTT client
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /***** DEFINITION OF ANALOG INPUT PINS *****/
  39. const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4;
  40.  
  41. /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  42. Adafruit_DS3502 ds3502; // Create an instance of the Adafruit_DS3502 class
  43.  
  44. // WiFi and MQTT configuration
  45. const char* ssid = "your_SSID";          // Replace with your WiFi SSID
  46. const char* password = "your_PASSWORD";  // Replace with your WiFi Password
  47. const char* mqtt_server = "your_MQTT_BROKER"; // Replace with your MQTT Broker address
  48. const char* mqtt_user = "your_MQTT_USERNAME"; // Replace with your MQTT username
  49. const char* mqtt_password = "your_MQTT_PASSWORD"; // Replace with your MQTT password
  50.  
  51. WiFiClient client; // Create a WiFi client
  52. Adafruit_MQTT_Client mqtt(&client, mqtt_server, 1883, mqtt_user, mqtt_password); // Create an MQTT client
  53.  
  54. // Define the MQTT topic for controlling the wiper
  55. Adafruit_MQTT_Publish wiperControl = Adafruit_MQTT_Publish(&mqtt, "home/assistant/wiper"); // Topic for wiper control
  56.  
  57. void setup(void)
  58. {
  59.     // Initialize serial communication at 115200 baud rate
  60.     Serial.begin(115200);
  61.     // Wait until the serial port is opened
  62.     while (!Serial) { delay(1); }
  63.  
  64.     // Set the analog pin mode for reading the potentiometer voltage
  65.     pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT);
  66.  
  67.     // Initialize the DS3502 potentiometer
  68.     if (!ds3502.begin()) {
  69.         Serial.println("Couldn't find DS3502 chip");
  70.         while (1); // Halt execution if the chip is not found
  71.     }
  72.     Serial.println("Found DS3502 chip");
  73.  
  74.     // Connect to WiFi
  75.     WiFi.begin(ssid, password);
  76.     while (WiFi.status() != WL_CONNECTED) {
  77.         delay(500);
  78.         Serial.print(".");
  79.     }
  80.     Serial.println("Connected to WiFi");
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // Ensure the MQTT client is connected
  86.     if (mqtt.connected()) {
  87.         mqtt.processPackets(10000); // Process incoming packets
  88.     } else {
  89.         // Reconnect to the MQTT broker
  90.         if (mqtt.connect()) {
  91.             Serial.println("Connected to MQTT Broker");
  92.         } else {
  93.             Serial.print("Failed to connect to MQTT Broker, state: ");
  94.             Serial.println(mqtt.connectError());
  95.             delay(2000); // Wait before retrying
  96.         }
  97.     }
  98.  
  99.     // Example of setting wiper values and reading the output voltage
  100.     for (uint8_t wiper_value : {0, 63, 127}) {
  101.         ds3502.setWiper(wiper_value); // Set the wiper to the specified value
  102.         float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4) * (5.0 / 1024); // Read voltage from the potentiometer
  103.         Serial.print("Wiper voltage with wiper set to ");
  104.         Serial.print(wiper_value);
  105.         Serial.print(": ");
  106.         Serial.print(voltage);
  107.         Serial.println(" V");
  108.  
  109.         // Publish the wiper value to the MQTT topic
  110.         wiperControl.publish(wiper_value);
  111.  
  112.         delay(1000); // Wait for a second before the next iteration
  113.     }
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement